@cspell/dynamic-import 7.1.1 → 7.3.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.
- package/dist/esm/dynamicImport.d.mts +7 -0
- package/dist/esm/dynamicImport.mjs +23 -14
- package/dist/esm/index.d.mts +1 -1
- package/dist/esm/index.mjs +1 -1
- package/package.json +4 -3
- package/dist/esm/test.cjs.d.mts +0 -3
- package/dist/esm/test.cjs.mjs +0 -21
|
@@ -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 =
|
|
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.
|
|
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
|
|
25
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -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
|
package/dist/esm/index.mjs
CHANGED
|
@@ -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.
|
|
6
|
+
"version": "7.3.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": "
|
|
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": "
|
|
61
|
+
"gitHead": "4a16b099b64c5b55645529a4ae79bc32aaf0fc8e"
|
|
61
62
|
}
|
package/dist/esm/test.cjs.d.mts
DELETED
package/dist/esm/test.cjs.mjs
DELETED
|
@@ -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
|