@cspell/dynamic-import 6.26.3 → 6.28.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/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +9 -0
- package/dist/esm/dynamicImport.d.mts +8 -0
- package/dist/esm/dynamicImport.mjs +49 -0
- package/{lib/index.js → dist/esm/index.d.mts} +2 -1
- package/{lib/index.d.ts → dist/esm/index.mjs} +2 -1
- package/dist/esm/test.cjs.d.mts +3 -0
- package/dist/esm/test.cjs.mjs +21 -0
- package/package.json +19 -16
- package/dist/cjs/index.cjs +0 -65
- package/lib/dImport.d.ts +0 -6
- package/lib/dImport.js +0 -8
- package/lib/dynamicImport.d.ts +0 -11
- package/lib/dynamicImport.js +0 -52
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dynamicImport = void 0;
|
|
4
|
+
async function dynamicImport(moduleName, paths) {
|
|
5
|
+
const { dynamicImportFrom } = await import('../esm/dynamicImport.mjs');
|
|
6
|
+
return dynamicImportFrom(moduleName, paths);
|
|
7
|
+
}
|
|
8
|
+
exports.dynamicImport = dynamicImport;
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dynamically import a module using `import`.
|
|
3
|
+
* @param moduleName - name of module, or relative path.
|
|
4
|
+
* @param paths - search paths
|
|
5
|
+
* @returns the loaded module.
|
|
6
|
+
*/
|
|
7
|
+
export declare function dynamicImportFrom<Module>(moduleName: string | URL, paths: string | URL | (string | URL)[] | undefined): Promise<Module>;
|
|
8
|
+
//# sourceMappingURL=dynamicImport.d.mts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { sep as pathSep } from 'path';
|
|
2
|
+
import { pathToFileURL } from 'url';
|
|
3
|
+
/**
|
|
4
|
+
* Dynamically import a module using `import`.
|
|
5
|
+
* @param moduleName - name of module, or relative path.
|
|
6
|
+
* @param paths - search paths
|
|
7
|
+
* @returns the loaded module.
|
|
8
|
+
*/
|
|
9
|
+
export async function dynamicImportFrom(moduleName, paths) {
|
|
10
|
+
paths = Array.isArray(paths) ? paths : paths ? [paths] : undefined;
|
|
11
|
+
if (!paths || !paths.length || typeof moduleName !== 'string') {
|
|
12
|
+
try {
|
|
13
|
+
return await import(moduleName.toString());
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
// console.log('%o', e);
|
|
17
|
+
const err = toError(e);
|
|
18
|
+
// err.code = err.code || 'ERR_MODULE_NOT_FOUND';
|
|
19
|
+
throw err;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const importResolveModule = await import('import-meta-resolve');
|
|
23
|
+
const { resolve } = importResolveModule;
|
|
24
|
+
let lastError = undefined;
|
|
25
|
+
for (const parent of paths) {
|
|
26
|
+
try {
|
|
27
|
+
const url = typeof parent === 'string'
|
|
28
|
+
? parent.startsWith('file://')
|
|
29
|
+
? new URL(parent)
|
|
30
|
+
: pathToFileURL(parent + pathSep)
|
|
31
|
+
: parent;
|
|
32
|
+
const location = await resolve(moduleName, url.toString());
|
|
33
|
+
return await import(location);
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
lastError = err;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw lastError;
|
|
40
|
+
}
|
|
41
|
+
function toError(e) {
|
|
42
|
+
if (isError(e))
|
|
43
|
+
return e;
|
|
44
|
+
return new Error(e?.toString());
|
|
45
|
+
}
|
|
46
|
+
function isError(e) {
|
|
47
|
+
return e instanceof Error;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=dynamicImport.mjs.map
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { dynamicImportFrom as dynamicImport } from './dynamicImport.
|
|
1
|
+
export { dynamicImportFrom as dynamicImport } from './dynamicImport.mjs';
|
|
2
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { dynamicImportFrom as dynamicImport } from './dynamicImport';
|
|
1
|
+
export { dynamicImportFrom as dynamicImport } from './dynamicImport.mjs';
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "6.
|
|
6
|
+
"version": "6.28.0",
|
|
7
7
|
"description": "Dynamic Module Loader",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"module",
|
|
@@ -15,13 +15,20 @@
|
|
|
15
15
|
"author": "Jason Dent <jason@streetsidesoftware.nl>",
|
|
16
16
|
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/dynamic-import#readme",
|
|
17
17
|
"license": "MIT",
|
|
18
|
-
"type": "
|
|
19
|
-
"main": "dist/cjs/index.
|
|
20
|
-
"module": "
|
|
21
|
-
"types": "
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"main": "./dist/cjs/index.js",
|
|
20
|
+
"module": "./dist/esm/index.mjs",
|
|
21
|
+
"types": "./dist/cjs/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"require": "./dist/cjs/index.js",
|
|
25
|
+
"import": "./dist/esm/index.mjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
22
28
|
"files": [
|
|
23
29
|
"lib",
|
|
24
30
|
"dist",
|
|
31
|
+
"!**/*.tsbuildInfo",
|
|
25
32
|
"!**/__mocks__",
|
|
26
33
|
"!**/*.spec.*",
|
|
27
34
|
"!**/*.test.*",
|
|
@@ -29,12 +36,15 @@
|
|
|
29
36
|
"!**/*.map"
|
|
30
37
|
],
|
|
31
38
|
"scripts": {
|
|
32
|
-
"build": "
|
|
33
|
-
"build:
|
|
39
|
+
"build": "tsc -b .",
|
|
40
|
+
"build:clean": "shx rm -rf dist coverage && pnpm build",
|
|
41
|
+
"clean-build": "pnpm build:clean",
|
|
42
|
+
"coverage": "pnpm coverage:vitest && pnpm coverage:fix",
|
|
34
43
|
"coverage:vitest": "vitest run --coverage",
|
|
35
44
|
"coverage:fix": "nyc report --temp-dir \"$(pwd)/coverage\" --reporter lcov --report-dir \"$(pwd)/coverage\" --cwd ../..",
|
|
36
45
|
"test-watch": "vitest",
|
|
37
|
-
"test": "vitest run"
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"watch": "tsc -b . --watch"
|
|
38
48
|
},
|
|
39
49
|
"repository": {
|
|
40
50
|
"type": "git",
|
|
@@ -46,15 +56,8 @@
|
|
|
46
56
|
"engines": {
|
|
47
57
|
"node": ">=14"
|
|
48
58
|
},
|
|
49
|
-
"devDependencies": {
|
|
50
|
-
"@rollup/plugin-commonjs": "^24.0.1",
|
|
51
|
-
"@rollup/plugin-json": "^6.0.0",
|
|
52
|
-
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
53
|
-
"rollup": "^3.15.0",
|
|
54
|
-
"vitest": "^0.28.5"
|
|
55
|
-
},
|
|
56
59
|
"dependencies": {
|
|
57
60
|
"import-meta-resolve": "^2.2.1"
|
|
58
61
|
},
|
|
59
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "1c314413e76908e5fbf61fd2555726112b177c0e"
|
|
60
63
|
}
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var path = require('path');
|
|
4
|
-
var url = require('url');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Thunk Layer for `import` to allow dynamic imports.
|
|
8
|
-
* @param {string} module - name of module to load
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
function dImport(module) {
|
|
12
|
-
return import(module.toString());
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Lazy load the importer so we can use an ESM packages inside a cjs file.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @type {Promise<typeof import('import-meta-resolve')> | undefined}
|
|
21
|
-
*/
|
|
22
|
-
let pImportResolve;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Dynamically import a module using `import`.
|
|
26
|
-
* @param {string} moduleName - name of module, or relative path.
|
|
27
|
-
* @param {(string | URL)[] | string | URL} paths - search paths
|
|
28
|
-
* @returns the loaded module.
|
|
29
|
-
*/
|
|
30
|
-
async function dynamicImportFrom(moduleName, paths) {
|
|
31
|
-
paths = Array.isArray(paths) ? paths : paths ? [paths] : undefined;
|
|
32
|
-
if (!paths || !paths.length || typeof moduleName !== 'string') {
|
|
33
|
-
try {
|
|
34
|
-
return await dImport(moduleName);
|
|
35
|
-
} catch (err) {
|
|
36
|
-
err.code = err.code ?? 'ERR_MODULE_NOT_FOUND';
|
|
37
|
-
throw err;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const importResolveModule = await (pImportResolve || (pImportResolve = dImport('import-meta-resolve')));
|
|
42
|
-
|
|
43
|
-
const { resolve } = importResolveModule;
|
|
44
|
-
|
|
45
|
-
let lastError = undefined;
|
|
46
|
-
|
|
47
|
-
for (const parent of paths) {
|
|
48
|
-
try {
|
|
49
|
-
const url$1 =
|
|
50
|
-
typeof parent === 'string'
|
|
51
|
-
? parent.startsWith('file://')
|
|
52
|
-
? new URL(parent)
|
|
53
|
-
: url.pathToFileURL(parent + path.sep)
|
|
54
|
-
: parent;
|
|
55
|
-
const location = await resolve(moduleName, url$1);
|
|
56
|
-
return await dImport(location);
|
|
57
|
-
} catch (err) {
|
|
58
|
-
lastError = err;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
throw lastError;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
exports.dynamicImport = dynamicImportFrom;
|
|
65
|
-
//# sourceMappingURL=index.cjs.map
|
package/lib/dImport.d.ts
DELETED
package/lib/dImport.js
DELETED
package/lib/dynamicImport.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dynamically import a module using `import`.
|
|
3
|
-
* @param moduleName - name of module, or relative path.
|
|
4
|
-
* @param searchPath - absolute search path, URL, or array of paths. Note: Paths must be absolute.
|
|
5
|
-
* The last path element of the URL will be removed if it does not end in a `/`.
|
|
6
|
-
* @returns Promise resolving to the loaded module.
|
|
7
|
-
*/
|
|
8
|
-
export declare function dynamicImportFrom<T>(
|
|
9
|
-
moduleName: string,
|
|
10
|
-
searchPath: (string | URL)[] | string | URL
|
|
11
|
-
): Promise<T>;
|
package/lib/dynamicImport.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { dImport } from './dImport.js';
|
|
2
|
-
import { sep as pathSep } from 'path';
|
|
3
|
-
import { pathToFileURL } from 'url';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Lazy load the importer so we can use an ESM packages inside a cjs file.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @type {Promise<typeof import('import-meta-resolve')> | undefined}
|
|
11
|
-
*/
|
|
12
|
-
let pImportResolve;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Dynamically import a module using `import`.
|
|
16
|
-
* @param {string} moduleName - name of module, or relative path.
|
|
17
|
-
* @param {(string | URL)[] | string | URL} paths - search paths
|
|
18
|
-
* @returns the loaded module.
|
|
19
|
-
*/
|
|
20
|
-
export async function dynamicImportFrom(moduleName, paths) {
|
|
21
|
-
paths = Array.isArray(paths) ? paths : paths ? [paths] : undefined;
|
|
22
|
-
if (!paths || !paths.length || typeof moduleName !== 'string') {
|
|
23
|
-
try {
|
|
24
|
-
return await dImport(moduleName);
|
|
25
|
-
} catch (err) {
|
|
26
|
-
err.code = err.code ?? 'ERR_MODULE_NOT_FOUND';
|
|
27
|
-
throw err;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const importResolveModule = await (pImportResolve || (pImportResolve = dImport('import-meta-resolve')));
|
|
32
|
-
|
|
33
|
-
const { resolve } = importResolveModule;
|
|
34
|
-
|
|
35
|
-
let lastError = undefined;
|
|
36
|
-
|
|
37
|
-
for (const parent of paths) {
|
|
38
|
-
try {
|
|
39
|
-
const url =
|
|
40
|
-
typeof parent === 'string'
|
|
41
|
-
? parent.startsWith('file://')
|
|
42
|
-
? new URL(parent)
|
|
43
|
-
: pathToFileURL(parent + pathSep)
|
|
44
|
-
: parent;
|
|
45
|
-
const location = await resolve(moduleName, url);
|
|
46
|
-
return await dImport(location);
|
|
47
|
-
} catch (err) {
|
|
48
|
-
lastError = err;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
throw lastError;
|
|
52
|
-
}
|