@cspell/dynamic-import 7.0.2 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,4 +5,11 @@
5
5
  * @returns the loaded module.
6
6
  */
7
7
  export declare function dynamicImportFrom<Module>(moduleName: string | URL, paths: string | URL | (string | URL)[] | undefined): Promise<Module>;
8
+ /**
9
+ * Use Import.meta.resolve logic to try and determine possible locations for a module.
10
+ * @param moduleName - name of module, relative path, or absolute path.
11
+ * @param paths - Places to start resolving from.
12
+ * @returns location of module
13
+ */
14
+ export declare function importResolveModuleName(moduleName: string | URL, paths: (string | URL)[]): URL;
8
15
  //# sourceMappingURL=dynamicImport.d.mts.map
@@ -1,3 +1,4 @@
1
+ import { resolve } from 'import-meta-resolve';
1
2
  import { sep as pathSep } from 'path';
2
3
  import { pathToFileURL } from 'url';
3
4
  const isWindowsPath = /^[a-z]:\\/i;
@@ -9,33 +10,38 @@ const isWindowsPath = /^[a-z]:\\/i;
9
10
  */
10
11
  export async function dynamicImportFrom(moduleName, paths) {
11
12
  paths = Array.isArray(paths) ? paths : paths ? [paths] : undefined;
12
- const modulesNameToImport = typeof moduleName === 'string' && isWindowsPath.test(moduleName) ? pathToFileURL(moduleName) : moduleName;
13
+ const modulesNameToImport = normalizeModuleName(moduleName);
13
14
  if (!paths || !paths.length || typeof moduleName !== 'string') {
14
15
  try {
15
16
  return await import(modulesNameToImport.toString());
16
17
  }
17
18
  catch (e) {
18
- // console.log('%o', e);
19
+ // console.warn('Error %o', e);
19
20
  const err = toError(e);
20
21
  // err.code = err.code || 'ERR_MODULE_NOT_FOUND';
21
22
  throw err;
22
23
  }
23
24
  }
24
- const importResolveModule = await import('import-meta-resolve');
25
- const { resolve } = importResolveModule;
25
+ const location = importResolveModuleName(moduleName, paths);
26
+ return await import(location.toString());
27
+ }
28
+ /**
29
+ * Use Import.meta.resolve logic to try and determine possible locations for a module.
30
+ * @param moduleName - name of module, relative path, or absolute path.
31
+ * @param paths - Places to start resolving from.
32
+ * @returns location of module
33
+ */
34
+ export function importResolveModuleName(moduleName, paths) {
35
+ const modulesNameToImport = normalizeModuleName(moduleName);
26
36
  let lastError = undefined;
27
37
  for (const parent of paths) {
28
- const url = typeof parent === 'string'
29
- ? parent.startsWith('file://')
30
- ? new URL(parent)
31
- : pathToFileURL(parent + pathSep)
32
- : parent;
33
- let resolved = '';
34
- let location = '';
35
38
  try {
36
- resolved = resolve(modulesNameToImport.toString(), url.toString());
37
- location = isWindowsPath.test(resolved) ? pathToFileURL(resolved).toString() : resolved;
38
- return await import(location);
39
+ const url = typeof parent === 'string'
40
+ ? parent.startsWith('file://')
41
+ ? new URL(parent)
42
+ : pathToFileURL(parent + pathSep)
43
+ : parent;
44
+ return new URL(resolve(modulesNameToImport.toString(), url.toString()));
39
45
  }
40
46
  catch (err) {
41
47
  // console.warn('%o', { moduleName, modulesNameToImport, paths, parentUrl: url, err, resolved, location });
@@ -44,6 +50,9 @@ export async function dynamicImportFrom(moduleName, paths) {
44
50
  }
45
51
  throw lastError;
46
52
  }
53
+ function normalizeModuleName(moduleName) {
54
+ return typeof moduleName === 'string' && isWindowsPath.test(moduleName) ? pathToFileURL(moduleName) : moduleName;
55
+ }
47
56
  function toError(e) {
48
57
  if (isError(e))
49
58
  return e;
@@ -1,2 +1,2 @@
1
- export { dynamicImportFrom as dynamicImport } from './dynamicImport.mjs';
1
+ export { dynamicImportFrom as dynamicImport, importResolveModuleName } from './dynamicImport.mjs';
2
2
  //# sourceMappingURL=index.d.mts.map
@@ -1,2 +1,2 @@
1
- export { dynamicImportFrom as dynamicImport } from './dynamicImport.mjs';
1
+ export { dynamicImportFrom as dynamicImport, importResolveModuleName } from './dynamicImport.mjs';
2
2
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "7.0.2",
6
+ "version": "7.2.0",
7
7
  "description": "Dynamic Module Loader",
8
8
  "keywords": [
9
9
  "module",
@@ -37,7 +37,8 @@
37
37
  ],
38
38
  "scripts": {
39
39
  "build": "tsc -b .",
40
- "build:clean": "shx rm -rf dist coverage && pnpm build",
40
+ "build:clean": "pnpm clean && pnpm build",
41
+ "clean": "shx rm -rf dist coverage",
41
42
  "clean-build": "pnpm build:clean",
42
43
  "coverage": "vitest run --coverage",
43
44
  "test-watch": "vitest",
@@ -57,5 +58,5 @@
57
58
  "dependencies": {
58
59
  "import-meta-resolve": "^3.0.0"
59
60
  },
60
- "gitHead": "25aa083421fc81e4d5b7cedb4755f9b327120cfc"
61
+ "gitHead": "b08f7ddc3a4aa22cc80c69ca6638b2a943659a31"
61
62
  }
@@ -1,3 +0,0 @@
1
- export declare function test(): Promise<[string, string, string, string, string]>;
2
- export declare function callDynamicImport<Module>(file: string | URL, paths?: string | URL | (string | URL)[]): Promise<Module>;
3
- //# sourceMappingURL=test.cjs.d.mts.map
@@ -1,21 +0,0 @@
1
- import * as path from 'path';
2
- import { fileURLToPath, pathToFileURL } from 'url';
3
- import { dynamicImport } from '../cjs/index.js';
4
- const fixtures = pathToFileURL('./fixtures/');
5
- async function callHello(file, paths) {
6
- // console.log('%o %o', file.toString(), paths?.toString());
7
- return (await dynamicImport(file, paths)).sayHello('Bob.');
8
- }
9
- export function test() {
10
- return Promise.all([
11
- callHello(pathToFileURL('./fixtures/hello_world.mjs')),
12
- callHello(path.resolve('./fixtures/hello_world.mjs')),
13
- callHello('./hello_world.mjs', [fixtures]),
14
- callHello('./hello_world.mjs', [fileURLToPath(fixtures)]),
15
- callHello(pathToFileURL('./fixtures/hello_world.mjs'), [fileURLToPath(fixtures)]),
16
- ]);
17
- }
18
- export async function callDynamicImport(file, paths) {
19
- return await dynamicImport(file, paths);
20
- }
21
- //# sourceMappingURL=test.cjs.mjs.map