@cspell/dynamic-import 6.22.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/LICENSE +21 -0
- package/README.md +31 -0
- package/dist/cjs/index.cjs +65 -0
- package/lib/dImport.d.ts +6 -0
- package/lib/dImport.js +8 -0
- package/lib/dynamicImport.d.ts +11 -0
- package/lib/dynamicImport.js +52 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Jason Dent
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# `@cspell/dynamic-import`
|
|
2
|
+
|
|
3
|
+
A small library to assist with dynamically loading CommonJS and ESM Modules from either CommonJS or ESM Modules.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install -S @cspell/dynamic-import
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
**TypeScript**
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { dynamicImport } from '@cspell/dynamic-import';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
<!--- @@inject: ../../static/footer.md --->
|
|
20
|
+
|
|
21
|
+
<br/>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
Brought to you by <a href="https://streetsidesoftware.com" title="Street Side Software">
|
|
27
|
+
<img width="16" alt="Street Side Software Logo" src="https://i.imgur.com/CyduuVY.png" /> Street Side Software
|
|
28
|
+
</a>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
<!--- @@inject-end: ../../static/footer.md --->
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_path = require('node:path');
|
|
4
|
+
var node_url = require('node: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 =
|
|
50
|
+
typeof parent === 'string'
|
|
51
|
+
? parent.startsWith('file://')
|
|
52
|
+
? new URL(parent)
|
|
53
|
+
: node_url.pathToFileURL(parent + node_path.sep)
|
|
54
|
+
: parent;
|
|
55
|
+
const location = await resolve(moduleName, url);
|
|
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
ADDED
package/lib/dImport.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { dImport } from './dImport.js';
|
|
2
|
+
import { sep as pathSep } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node: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
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { dynamicImportFrom as dynamicImport } from './dynamicImport';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { dynamicImportFrom as dynamicImport } from './dynamicImport.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cspell/dynamic-import",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "6.22.0",
|
|
7
|
+
"description": "Dynamic Module Loader",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"module",
|
|
10
|
+
"esm",
|
|
11
|
+
"cjs",
|
|
12
|
+
"import",
|
|
13
|
+
"require"
|
|
14
|
+
],
|
|
15
|
+
"author": "Jason Dent <jason@streetsidesoftware.nl>",
|
|
16
|
+
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/dynamic-import#readme",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "dist/cjs/index.cjs",
|
|
20
|
+
"module": "lib/index.js",
|
|
21
|
+
"types": "lib/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"dist",
|
|
25
|
+
"!**/__mocks__",
|
|
26
|
+
"!**/*.spec.*",
|
|
27
|
+
"!**/*.test.*",
|
|
28
|
+
"!**/test/**",
|
|
29
|
+
"!**/*.map"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "pnpm build:cjs",
|
|
33
|
+
"build:cjs": "rollup -c rollup.config.mjs",
|
|
34
|
+
"coverage:vitest": "vitest run --coverage",
|
|
35
|
+
"coverage:fix": "nyc report --temp-dir \"$(pwd)/coverage\" --reporter lcov --report-dir \"$(pwd)/coverage\" --cwd ../..",
|
|
36
|
+
"test-watch": "vitest",
|
|
37
|
+
"test": "vitest run"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/streetsidesoftware/cspell.git"
|
|
42
|
+
},
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-pipe"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=14"
|
|
48
|
+
},
|
|
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.14.0",
|
|
54
|
+
"vitest": "^0.28.4"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"import-meta-resolve": "^2.2.1"
|
|
58
|
+
},
|
|
59
|
+
"gitHead": "a133874ed7590cbe140f5067cfa80db84b644a5d"
|
|
60
|
+
}
|