requirejs-esm 0.0.1 → 1.0.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/README.md +47 -3
- package/bin/esm2requirejs.js +15 -6
- package/dist/api.js +20 -39
- package/dist/api.js.map +1 -1
- package/dist/plugin.js +75 -51
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|

|
|
5
5
|
](https://www.npmjs.com/package/requirejs-esm)
|
|
6
6
|
|
|
7
|
-
A [RequireJS] plugin converting JavaScript modules from ESM to AMD. It takes care only of the module format; it does not transpile the language and that is why it is a lot faster than plugins using [Babel]. If you need to transpile the code to an earlier ECMAScript version, have a look at [requirejs-babel7].
|
|
7
|
+
A [RequireJS] plugin converting JavaScript modules from ESM to AMD. It takes care only of the module format; it does not transpile the language and that is why it is [a lot faster] than plugins using [Babel]. If you need to transpile the code to an earlier ECMAScript version, have a look at [requirejs-babel7].
|
|
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
|
|
|
@@ -111,11 +111,54 @@ The `esm` plugin supports configuration with the following defaults:
|
|
|
111
111
|
// Enforce transpiling even if a optimized module has been loaded.
|
|
112
112
|
mixedAmdAndEsm: false,
|
|
113
113
|
// Suppress transpiling even if an optimized module has not been loaded yet.
|
|
114
|
-
onlyAmd: false
|
|
114
|
+
onlyAmd: false,
|
|
115
|
+
// Enable source maps, can be an object with booleans { inline, content }.
|
|
116
|
+
// If set to true, the object will be set to { inline: true, content: true }.
|
|
117
|
+
sourceMap: false,
|
|
118
|
+
// Enable console logging.
|
|
119
|
+
verbose: false,
|
|
120
|
+
// Save a copy of the transformed modules to a directory for debugging purposes.
|
|
121
|
+
debugDir: ''
|
|
115
122
|
}
|
|
116
123
|
}
|
|
117
124
|
```
|
|
118
125
|
|
|
126
|
+
## API
|
|
127
|
+
|
|
128
|
+
The transformation applied by the plugin can be performed programmatically too.
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
const { transform } = require('requirejs-esm/dist/api')
|
|
132
|
+
const { code } = transform('import a from "a"', 'test', { sourceMap: true })
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The `transform` method supports a subset of plugin options:
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
{
|
|
139
|
+
// Update paths of module dependencies.
|
|
140
|
+
resolvePath: func,
|
|
141
|
+
// Allow using a different plugin alias than `esm` in the source code.
|
|
142
|
+
pluginName: 'esm',
|
|
143
|
+
// Enable source maps, can be an object with booleans { inline, content }.
|
|
144
|
+
// If set to true, the object will be set to { inline: true, content: true }.
|
|
145
|
+
sourceMap: false
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The returned object:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
{
|
|
153
|
+
// The transpiled module code.
|
|
154
|
+
code: '...',
|
|
155
|
+
// The source map if sourceMap.inline from the options above is false.
|
|
156
|
+
map: undefined | { ... },
|
|
157
|
+
// If the transpilation modified the original source text.
|
|
158
|
+
updated: false | true
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
119
162
|
## Contributing
|
|
120
163
|
|
|
121
164
|
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
|
|
@@ -137,5 +180,6 @@ Licensed under the MIT license.
|
|
|
137
180
|
[PNPM]: https://pnpm.io/
|
|
138
181
|
[Yarn]: https://yarnpkg.com/
|
|
139
182
|
[demo]: https://github.com/prantlf/requirejs-esm/tree/master/demo
|
|
140
|
-
[default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/
|
|
183
|
+
[default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/src/resolve-path.js#L48
|
|
141
184
|
[resolvePath]: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath
|
|
185
|
+
[a lot faster]: ./perf/README.md#readme
|
package/bin/esm2requirejs.js
CHANGED
|
@@ -14,9 +14,8 @@ commander.description('Transforms an ESM module to AMD or adapts an AMD module f
|
|
|
14
14
|
.option('-p, --plugin <name>', 'change the plugin name from "esm" to other name')
|
|
15
15
|
.option('-o, --output <file>', 'write the adapted module source to a file')
|
|
16
16
|
.option('-r, --rewrite', 'rewrite the input files with the adapted output')
|
|
17
|
-
// .option('-m, --minify', 'minify the generated output')
|
|
18
17
|
.option('-s, --source-map', 'write inline source maps to the adapted output')
|
|
19
|
-
.option('-v, --verbose', 'print call stack in case of error')
|
|
18
|
+
.option('-v, --verbose', 'print progress and call stack in case of error')
|
|
20
19
|
.on('--help', function () {
|
|
21
20
|
console.log()
|
|
22
21
|
console.log('You can use one or more file names or one or more glob patterns to specify one')
|
|
@@ -31,9 +30,14 @@ commander.description('Transforms an ESM module to AMD or adapts an AMD module f
|
|
|
31
30
|
.parse(process.argv)
|
|
32
31
|
|
|
33
32
|
const {
|
|
34
|
-
/*ecma: ecmaVersion,*/
|
|
33
|
+
/*ecma: ecmaVersion,*/
|
|
34
|
+
plugin: pluginName,
|
|
35
|
+
sourceMap,
|
|
36
|
+
output: outputFile,
|
|
37
|
+
rewrite,
|
|
38
|
+
verbose
|
|
35
39
|
} = commander.opts()
|
|
36
|
-
const { args } = commander
|
|
40
|
+
const { args } = commander
|
|
37
41
|
if (!args.length) {
|
|
38
42
|
commander.help()
|
|
39
43
|
}
|
|
@@ -47,7 +51,12 @@ if (!args.length) {
|
|
|
47
51
|
}
|
|
48
52
|
for (const file of files) {
|
|
49
53
|
const text = await readFile(file, 'utf8')
|
|
50
|
-
const code = transform(text, file, {
|
|
54
|
+
const code = transform(text, file, {
|
|
55
|
+
/*ecmaVersion,*/
|
|
56
|
+
pluginName,
|
|
57
|
+
sourceMap,
|
|
58
|
+
verbose
|
|
59
|
+
})
|
|
51
60
|
if (outputFile) {
|
|
52
61
|
await writeFile(outputFile, code)
|
|
53
62
|
} else if (rewrite) {
|
|
@@ -60,4 +69,4 @@ if (!args.length) {
|
|
|
60
69
|
console.error((!verbose && error.message) || error)
|
|
61
70
|
process.exitCode = 1
|
|
62
71
|
}
|
|
63
|
-
}())
|
|
72
|
+
}())
|
package/dist/api.js
CHANGED
|
@@ -85190,17 +85190,18 @@
|
|
|
85190
85190
|
const { elements } = deps;
|
|
85191
85191
|
if (!elements.length) return
|
|
85192
85192
|
|
|
85193
|
+
let updated;
|
|
85193
85194
|
for (const element of elements) {
|
|
85194
85195
|
if (element.type === 'Literal') {
|
|
85195
85196
|
const moduleName = element.value;
|
|
85196
85197
|
const newModuleName = resolvePath(moduleName, parentName, options);
|
|
85197
85198
|
if (newModuleName && newModuleName !== moduleName) {
|
|
85198
85199
|
replaceLiteral(element, newModuleName);
|
|
85200
|
+
updated = true;
|
|
85199
85201
|
}
|
|
85200
85202
|
}
|
|
85201
85203
|
}
|
|
85202
|
-
|
|
85203
|
-
return true
|
|
85204
|
+
return updated
|
|
85204
85205
|
}
|
|
85205
85206
|
|
|
85206
85207
|
function _generateUid(name, i) {
|
|
@@ -85609,7 +85610,6 @@
|
|
|
85609
85610
|
// name with `esm!`, above all.
|
|
85610
85611
|
resolvePath: resolvePath$1 = resolvePath,
|
|
85611
85612
|
// ecmaVersion = 2020,
|
|
85612
|
-
// minify,
|
|
85613
85613
|
sourceMap
|
|
85614
85614
|
} = {}) {
|
|
85615
85615
|
// const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
|
|
@@ -85618,48 +85618,29 @@
|
|
|
85618
85618
|
const options = { sourceFileName: file, pluginName, resolvePath: resolvePath$1, originalResolvePath: resolvePath };
|
|
85619
85619
|
transformModules(ast, options);
|
|
85620
85620
|
|
|
85621
|
-
|
|
85622
|
-
|
|
85623
|
-
|
|
85624
|
-
|
|
85625
|
-
|
|
85626
|
-
|
|
85627
|
-
|
|
85628
|
-
|
|
85629
|
-
// format: FORMAT_MINIFY,
|
|
85630
|
-
// sourceMap: sourceMap ? file : undefined,
|
|
85631
|
-
// sourceMapWithCode: true,
|
|
85632
|
-
// sourceContent: text,
|
|
85633
|
-
// file
|
|
85634
|
-
// }))
|
|
85635
|
-
// if (sourceMap) {
|
|
85636
|
-
// code += convert.fromJSON(sourceMap).toComment()
|
|
85637
|
-
// }
|
|
85638
|
-
// }
|
|
85639
|
-
// if (minify) {
|
|
85640
|
-
// ({ code } = await generateMinified(ast, {
|
|
85641
|
-
// // ecma: ecmaVersion,
|
|
85642
|
-
// module: true,
|
|
85643
|
-
// parse: { spidermonkey: true },
|
|
85644
|
-
// format: { ecma: 2015 },
|
|
85645
|
-
// sourceMap: sourceMap ? {
|
|
85646
|
-
// filename: file,
|
|
85647
|
-
// url: 'inline'
|
|
85648
|
-
// // content: map
|
|
85649
|
-
// } : undefined
|
|
85650
|
-
// }))
|
|
85651
|
-
// } else {
|
|
85652
|
-
sourceMap = sourceMap && new SourceMapGenerator({ file });
|
|
85653
|
-
code = generate(ast, { sourceMap });
|
|
85621
|
+
const { updated } = options;
|
|
85622
|
+
let code, map;
|
|
85623
|
+
if (updated) {
|
|
85624
|
+
if (sourceMap === true) {
|
|
85625
|
+
sourceMap = { inline: true, content: true };
|
|
85626
|
+
}
|
|
85627
|
+
const mapGenerator = sourceMap ? new SourceMapGenerator({ file }) : undefined;
|
|
85628
|
+
code = generate(ast, { sourceMap: mapGenerator });
|
|
85654
85629
|
if (sourceMap) {
|
|
85655
|
-
|
|
85630
|
+
if (sourceMap.content) {
|
|
85631
|
+
mapGenerator.setSourceContent(file, text);
|
|
85632
|
+
}
|
|
85633
|
+
if (sourceMap.inline) {
|
|
85634
|
+
code += convertSourceMap.fromObject(mapGenerator.toJSON()).toComment();
|
|
85635
|
+
} else {
|
|
85636
|
+
map = mapGenerator.toJSON();
|
|
85637
|
+
}
|
|
85656
85638
|
}
|
|
85657
|
-
// }
|
|
85658
85639
|
} else {
|
|
85659
85640
|
code = text;
|
|
85660
85641
|
}
|
|
85661
85642
|
|
|
85662
|
-
return code
|
|
85643
|
+
return { code, map, updated }
|
|
85663
85644
|
}
|
|
85664
85645
|
|
|
85665
85646
|
exports.resolvePath = resolvePath;
|