requirejs-esm 1.0.2 → 2.0.1
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/README.md +4 -0
- package/bin/esm2requirejs.js +10 -6
- package/dist/api.js +11 -14
- package/dist/api.js.map +1 -1
- package/dist/plugin.js +11 -14
- package/dist/plugin.js.map +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -8,6 +8,8 @@ A [RequireJS] plugin converting JavaScript modules from ESM to AMD. It takes car
|
|
|
8
8
|
|
|
9
9
|
The official [RequireJS optimizer] (`r.js`) does not wire up source maps from the original (not transpiled) sources to the source map of the output bundle. It makes this or similar plugins unfeasible for serious work. If you want the proper support for source maps, replace the official optimizer package ([`requirejs`]) with the forked [`@prantlf/requirejs`], which is fixed.
|
|
10
10
|
|
|
11
|
+
An alternative to this plugin is a preprocessor, which converts the module format before RequireJS consumes it. It is a lot less intrusive solution, but it requires a pluggable development web server, so that a plugin (compatible with [connect middleware]) can be registered in it. See [requirejs-esm-preprocessor] for more information.
|
|
12
|
+
|
|
11
13
|
## Installation
|
|
12
14
|
|
|
13
15
|
This module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version 14 or newer.
|
|
@@ -182,6 +184,8 @@ Licensed under the MIT license.
|
|
|
182
184
|
[RequireJS]: http://requirejs.org
|
|
183
185
|
[RequireJS optimizer]: https://requirejs.org/docs/optimization.html
|
|
184
186
|
[requirejs-babel7]: https://www.npmjs.com/package/requirejs-babel7
|
|
187
|
+
[requirejs-esm-preprocessor]: https://www.npmjs.com/package/requirejs-esm-preprocessor
|
|
188
|
+
[connect middleware]: https://github.com/senchalabs/connect/wiki
|
|
185
189
|
[`requirejs`]: https://www.npmjs.com/package/requirejs
|
|
186
190
|
[`@prantlf/requirejs`]: https://www.npmjs.com/package/@prantlf/requirejs
|
|
187
191
|
[Node.js]: http://nodejs.org/
|
package/bin/esm2requirejs.js
CHANGED
|
@@ -51,18 +51,22 @@ if (!args.length) {
|
|
|
51
51
|
}
|
|
52
52
|
for (const file of files) {
|
|
53
53
|
const text = await readFile(file, 'utf8')
|
|
54
|
-
const code = transform(text, file, {
|
|
54
|
+
const { code, updated } = transform(text, file, {
|
|
55
55
|
/*ecmaVersion,*/
|
|
56
56
|
pluginName,
|
|
57
57
|
sourceMap,
|
|
58
58
|
verbose
|
|
59
59
|
})
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
if (updated) {
|
|
61
|
+
if (outputFile) {
|
|
62
|
+
await writeFile(outputFile, code)
|
|
63
|
+
} else if (rewrite) {
|
|
64
|
+
await writeFile(file, code)
|
|
65
|
+
} else {
|
|
66
|
+
console.log(`// ${file}\n\n${code}`)
|
|
67
|
+
}
|
|
64
68
|
} else {
|
|
65
|
-
console.log(`// ${file}\n\
|
|
69
|
+
console.log(`// ${file}\n\nnot updated`)
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
72
|
} catch (error) {
|
package/dist/api.js
CHANGED
|
@@ -53,21 +53,24 @@
|
|
|
53
53
|
|
|
54
54
|
// Ensures that every JavaScript dependency will be prefixed by `esm!`.
|
|
55
55
|
// Relative paths will be converted to be relative to the parent module.
|
|
56
|
-
function resolvePath (sourcePath, currentFile, { pluginName }) {
|
|
56
|
+
function resolvePath (sourcePath, currentFile, { pluginName, needsResolve } = {}) {
|
|
57
57
|
// Ignore paths with other plugins applied and the three built-in
|
|
58
58
|
// pseudo-modules of RequireJS.
|
|
59
|
-
if (
|
|
59
|
+
if (sourcePath.includes('!') || sourcePath === 'require' ||
|
|
60
60
|
sourcePath === 'module' || sourcePath === 'exports') return
|
|
61
61
|
|
|
62
62
|
// If `sourcePath` is relative to `currentFile` - starts with ./ or ../ -
|
|
63
63
|
// prepend the parent directory of `currentFile` to it. This was needed
|
|
64
64
|
// for modules located outside the source root (`baseUrl`), which were
|
|
65
65
|
// mapped there using the `paths` of `map` configuration properties.
|
|
66
|
-
if (sourcePath.charAt(0) === '.' && (sourcePath.charAt(1) === '/' ||
|
|
67
|
-
|
|
66
|
+
if ((sourcePath.charAt(0) === '.' && (sourcePath.charAt(1) === '/' ||
|
|
67
|
+
sourcePath.charAt(1) === '.' && sourcePath.charAt(2) === '/')) &&
|
|
68
|
+
!(needsResolve && needsResolve(sourcePath, currentFile))) {
|
|
68
69
|
sourcePath = joinPath(parentDir(currentFile), sourcePath);
|
|
70
|
+
if (sourcePath.endsWith('.js')) sourcePath = sourcePath.substring(0, sourcePath.length - 3);
|
|
69
71
|
}
|
|
70
|
-
|
|
72
|
+
|
|
73
|
+
return pluginName ? `${pluginName}!${sourcePath}` : sourcePath
|
|
71
74
|
}
|
|
72
75
|
|
|
73
76
|
const errorMessages = {
|
|
@@ -85186,9 +85189,6 @@
|
|
|
85186
85189
|
|
|
85187
85190
|
// Updates dependency paths to be prefixed by `esm!` or otherwise updated.
|
|
85188
85191
|
function updateAmdDeps(amd, options) {
|
|
85189
|
-
const { resolvePath } = options;
|
|
85190
|
-
if (!resolvePath) return
|
|
85191
|
-
|
|
85192
85192
|
const { deps } = amd;
|
|
85193
85193
|
if (!deps) return
|
|
85194
85194
|
|
|
@@ -85196,6 +85196,7 @@
|
|
|
85196
85196
|
const { elements } = deps;
|
|
85197
85197
|
if (!elements.length) return
|
|
85198
85198
|
|
|
85199
|
+
const { resolvePath } = options;
|
|
85199
85200
|
let updated;
|
|
85200
85201
|
for (const element of elements) {
|
|
85201
85202
|
if (element.type === 'Literal') {
|
|
@@ -85574,11 +85575,8 @@
|
|
|
85574
85575
|
// Update dependency paths to be prefixed by `esm!` or otherwise updated.
|
|
85575
85576
|
function prepareImportPaths(importPaths, options ) {
|
|
85576
85577
|
const { resolvePath } = options;
|
|
85577
|
-
if (!resolvePath) return
|
|
85578
|
-
|
|
85579
85578
|
if (resolvePath) {
|
|
85580
85579
|
const { sourceFileName: parentName } = options;
|
|
85581
|
-
|
|
85582
85580
|
for (const importPath of importPaths) {
|
|
85583
85581
|
if (importPath.type === 'Literal') {
|
|
85584
85582
|
const moduleName = importPath.value;
|
|
@@ -85589,7 +85587,6 @@
|
|
|
85589
85587
|
}
|
|
85590
85588
|
}
|
|
85591
85589
|
}
|
|
85592
|
-
|
|
85593
85590
|
return arrayExpression(importPaths)
|
|
85594
85591
|
}
|
|
85595
85592
|
|
|
@@ -85598,7 +85595,7 @@
|
|
|
85598
85595
|
const { length } = amds;
|
|
85599
85596
|
if (length) {
|
|
85600
85597
|
options.amd = true;
|
|
85601
|
-
if (options.
|
|
85598
|
+
if (options.resolvePath) {
|
|
85602
85599
|
for (const amd of amds) {
|
|
85603
85600
|
options.updated |= updateAmdDeps(amd, options);
|
|
85604
85601
|
}
|
|
@@ -85619,7 +85616,7 @@
|
|
|
85619
85616
|
sourceMap
|
|
85620
85617
|
} = {}) {
|
|
85621
85618
|
// const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
|
|
85622
|
-
let ast = parseModule(text, { loc: true });
|
|
85619
|
+
let ast = parseModule(text, { next: true, loc: true });
|
|
85623
85620
|
|
|
85624
85621
|
const options = { sourceFileName: file, pluginName, resolvePath: resolvePath$1, originalResolvePath: resolvePath };
|
|
85625
85622
|
transformModules(ast, options);
|