requirejs-esm 0.0.1 → 1.0.2
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 +61 -7
- package/bin/esm2requirejs.js +15 -6
- package/dist/api.js +40 -53
- package/dist/api.js.map +1 -1
- package/dist/plugin.js +151 -89
- package/dist/plugin.js.map +1 -1
- package/package.json +26 -24
package/README.md
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
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
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
|
-
This module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version
|
|
13
|
+
This module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version 14 or newer.
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
16
|
npm i -D requirejs-esm
|
|
@@ -48,11 +48,20 @@ pragmasOnSave: {
|
|
|
48
48
|
}
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
See also a [demo] project:
|
|
51
|
+
See also a [demo-local] project, which includes sources only from the local `src` directory:
|
|
52
52
|
|
|
53
53
|
```sh
|
|
54
54
|
npm start
|
|
55
|
-
open http://localhost:8967/demo/normal.html
|
|
55
|
+
open http://localhost:8967/demo-local/normal.html
|
|
56
|
+
open http://localhost:8967/demo-local/optimized.html
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
See also a [demo-extern] project, which includes sources from the local `src` directory and from `node_modules` outside of it:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
npm run start
|
|
63
|
+
open http://localhost:8967/demo-extern/normal.html
|
|
64
|
+
open http://localhost:8967/demo-extern/optimized.html
|
|
56
65
|
```
|
|
57
66
|
|
|
58
67
|
## Advanced
|
|
@@ -111,11 +120,54 @@ The `esm` plugin supports configuration with the following defaults:
|
|
|
111
120
|
// Enforce transpiling even if a optimized module has been loaded.
|
|
112
121
|
mixedAmdAndEsm: false,
|
|
113
122
|
// Suppress transpiling even if an optimized module has not been loaded yet.
|
|
114
|
-
onlyAmd: false
|
|
123
|
+
onlyAmd: false,
|
|
124
|
+
// Enable source maps, can be an object with booleans { inline, content }.
|
|
125
|
+
// If set to true, the object will be set to { inline: true, content: true }.
|
|
126
|
+
sourceMap: false,
|
|
127
|
+
// Enable console logging.
|
|
128
|
+
verbose: false,
|
|
129
|
+
// Save a copy of the transformed modules to a directory for debugging purposes.
|
|
130
|
+
debugDir: ''
|
|
115
131
|
}
|
|
116
132
|
}
|
|
117
133
|
```
|
|
118
134
|
|
|
135
|
+
## API
|
|
136
|
+
|
|
137
|
+
The transformation applied by the plugin can be performed programmatically too.
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
const { transform } = require('requirejs-esm/dist/api')
|
|
141
|
+
const { code } = transform('import a from "a"', 'test', { sourceMap: true })
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The `transform` method supports a subset of plugin options:
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
{
|
|
148
|
+
// Update paths of module dependencies.
|
|
149
|
+
resolvePath: func,
|
|
150
|
+
// Allow using a different plugin alias than `esm` in the source code.
|
|
151
|
+
pluginName: 'esm',
|
|
152
|
+
// Enable source maps, can be an object with booleans { inline, content }.
|
|
153
|
+
// If set to true, the object will be set to { inline: true, content: true }.
|
|
154
|
+
sourceMap: false
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The returned object:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
{
|
|
162
|
+
// The transpiled module code.
|
|
163
|
+
code: '...',
|
|
164
|
+
// The source map if sourceMap.inline from the options above is false.
|
|
165
|
+
map: undefined | { ... },
|
|
166
|
+
// If the transpilation modified the original source text.
|
|
167
|
+
updated: false | true
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
119
171
|
## Contributing
|
|
120
172
|
|
|
121
173
|
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
|
|
@@ -136,6 +188,8 @@ Licensed under the MIT license.
|
|
|
136
188
|
[NPM]: https://www.npmjs.com/
|
|
137
189
|
[PNPM]: https://pnpm.io/
|
|
138
190
|
[Yarn]: https://yarnpkg.com/
|
|
139
|
-
[demo]: https://github.com/prantlf/requirejs-esm/tree/master/demo
|
|
140
|
-
[
|
|
191
|
+
[demo-local]: https://github.com/prantlf/requirejs-esm/tree/master/demo-local
|
|
192
|
+
[demo-extern]: https://github.com/prantlf/requirejs-esm/tree/master/demo-extern
|
|
193
|
+
[default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/src/resolve-path.js#L48
|
|
141
194
|
[resolvePath]: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath
|
|
195
|
+
[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
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.requirejsEsmAPI = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
// Returns the child file name by cutting the directoris before the last slash,
|
|
8
|
+
|
|
7
9
|
// Returns the parent directory by cutting the file name after the last slash,
|
|
8
10
|
// leaving the slash in the result. If there is no slash in the path - the path
|
|
9
11
|
// is just a file name, it will return `undefined`.
|
|
@@ -942,9 +944,9 @@
|
|
|
942
944
|
mask |= 8;
|
|
943
945
|
break;
|
|
944
946
|
case 115:
|
|
945
|
-
if (mask &
|
|
947
|
+
if (mask & 32)
|
|
946
948
|
report(parser, 34, 's');
|
|
947
|
-
mask |=
|
|
949
|
+
mask |= 32;
|
|
948
950
|
break;
|
|
949
951
|
default:
|
|
950
952
|
report(parser, 33);
|
|
@@ -1857,7 +1859,7 @@
|
|
|
1857
1859
|
}
|
|
1858
1860
|
else if (ch === 61) {
|
|
1859
1861
|
advanceChar(parser);
|
|
1860
|
-
return
|
|
1862
|
+
return 8456256;
|
|
1861
1863
|
}
|
|
1862
1864
|
if (ch === 33) {
|
|
1863
1865
|
const index = parser.index + 1;
|
|
@@ -2040,7 +2042,7 @@
|
|
|
2040
2042
|
const ch = parser.currentChar;
|
|
2041
2043
|
if (ch === 61) {
|
|
2042
2044
|
advanceChar(parser);
|
|
2043
|
-
return
|
|
2045
|
+
return 8456257;
|
|
2044
2046
|
}
|
|
2045
2047
|
if (ch !== 62)
|
|
2046
2048
|
return 8456259;
|
|
@@ -5032,7 +5034,7 @@
|
|
|
5032
5034
|
nextToken(parser, context | 32768);
|
|
5033
5035
|
const argument = parser.flags & 1 || parser.token & 1048576
|
|
5034
5036
|
? null
|
|
5035
|
-
: parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.
|
|
5037
|
+
: parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.linePos, parser.colPos);
|
|
5036
5038
|
matchOrInsertSemicolon(parser, context | 32768);
|
|
5037
5039
|
return finishNode(parser, context, start, line, column, {
|
|
5038
5040
|
type: 'ReturnStatement',
|
|
@@ -6148,7 +6150,7 @@
|
|
|
6148
6150
|
if ((context & 524288) < 1)
|
|
6149
6151
|
report(parser, 26);
|
|
6150
6152
|
if (context & 16384)
|
|
6151
|
-
report(parser,
|
|
6153
|
+
report(parser, 27);
|
|
6152
6154
|
parser.assignable = 2;
|
|
6153
6155
|
break;
|
|
6154
6156
|
}
|
|
@@ -6157,7 +6159,7 @@
|
|
|
6157
6159
|
if ((context & 262144) < 1)
|
|
6158
6160
|
report(parser, 27);
|
|
6159
6161
|
if (context & 16384)
|
|
6160
|
-
report(parser,
|
|
6162
|
+
report(parser, 27);
|
|
6161
6163
|
parser.assignable = 1;
|
|
6162
6164
|
break;
|
|
6163
6165
|
}
|
|
@@ -6188,10 +6190,10 @@
|
|
|
6188
6190
|
expr = parseUpdateExpression(parser, context, expr, start, line, column);
|
|
6189
6191
|
}
|
|
6190
6192
|
else if ((parser.token & 67108864) === 67108864) {
|
|
6191
|
-
context = (context | 134217728
|
|
6193
|
+
context = (context | 134217728) ^ 134217728;
|
|
6192
6194
|
switch (parser.token) {
|
|
6193
6195
|
case 67108877: {
|
|
6194
|
-
nextToken(parser, context | 1073741824);
|
|
6196
|
+
nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
|
|
6195
6197
|
parser.assignable = 1;
|
|
6196
6198
|
const property = parsePropertyOrPrivatePropertyName(parser, context);
|
|
6197
6199
|
expr = finishNode(parser, context, start, line, column, {
|
|
@@ -6247,7 +6249,7 @@
|
|
|
6247
6249
|
break;
|
|
6248
6250
|
}
|
|
6249
6251
|
case 67108991: {
|
|
6250
|
-
nextToken(parser, context);
|
|
6252
|
+
nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
|
|
6251
6253
|
parser.flags |= 2048;
|
|
6252
6254
|
parser.assignable = 2;
|
|
6253
6255
|
expr = parseOptionalChain(parser, context, expr, start, line, column);
|
|
@@ -8411,12 +8413,10 @@
|
|
|
8411
8413
|
}
|
|
8412
8414
|
else if (context & 1 && parser.token === 131) {
|
|
8413
8415
|
kind |= 4096;
|
|
8414
|
-
key = parsePrivateIdentifier(parser, context, tokenPos, linePos, colPos);
|
|
8415
|
-
context = context | 16384;
|
|
8416
|
+
key = parsePrivateIdentifier(parser, context | 16384, tokenPos, linePos, colPos);
|
|
8416
8417
|
}
|
|
8417
8418
|
else if (context & 1 && (parser.token & 1073741824) === 1073741824) {
|
|
8418
8419
|
kind |= 128;
|
|
8419
|
-
context = context | 16384;
|
|
8420
8420
|
}
|
|
8421
8421
|
else if (token === 122) {
|
|
8422
8422
|
key = parseIdentifier(parser, context, 0);
|
|
@@ -8782,7 +8782,7 @@
|
|
|
8782
8782
|
});
|
|
8783
8783
|
}
|
|
8784
8784
|
function parseJSXExpressionContainer(parser, context, inJSXChild, isAttr, start, line, column) {
|
|
8785
|
-
nextToken(parser, context);
|
|
8785
|
+
nextToken(parser, context | 32768);
|
|
8786
8786
|
const { tokenPos, linePos, colPos } = parser;
|
|
8787
8787
|
if (parser.token === 14)
|
|
8788
8788
|
return parseJSXSpreadChild(parser, context, tokenPos, linePos, colPos);
|
|
@@ -81684,6 +81684,9 @@
|
|
|
81684
81684
|
});
|
|
81685
81685
|
|
|
81686
81686
|
obj[impl][utils$1.wrapperSymbol] = obj;
|
|
81687
|
+
if (Impl.init) {
|
|
81688
|
+
Impl.init(obj[impl], privateData);
|
|
81689
|
+
}
|
|
81687
81690
|
return obj;
|
|
81688
81691
|
},
|
|
81689
81692
|
interface: URLSearchParams,
|
|
@@ -82235,6 +82238,9 @@
|
|
|
82235
82238
|
});
|
|
82236
82239
|
|
|
82237
82240
|
obj[impl][utils$1.wrapperSymbol] = obj;
|
|
82241
|
+
if (Impl.init) {
|
|
82242
|
+
Impl.init(obj[impl], privateData);
|
|
82243
|
+
}
|
|
82238
82244
|
return obj;
|
|
82239
82245
|
},
|
|
82240
82246
|
interface: URL,
|
|
@@ -85190,17 +85196,18 @@
|
|
|
85190
85196
|
const { elements } = deps;
|
|
85191
85197
|
if (!elements.length) return
|
|
85192
85198
|
|
|
85199
|
+
let updated;
|
|
85193
85200
|
for (const element of elements) {
|
|
85194
85201
|
if (element.type === 'Literal') {
|
|
85195
85202
|
const moduleName = element.value;
|
|
85196
85203
|
const newModuleName = resolvePath(moduleName, parentName, options);
|
|
85197
85204
|
if (newModuleName && newModuleName !== moduleName) {
|
|
85198
85205
|
replaceLiteral(element, newModuleName);
|
|
85206
|
+
updated = true;
|
|
85199
85207
|
}
|
|
85200
85208
|
}
|
|
85201
85209
|
}
|
|
85202
|
-
|
|
85203
|
-
return true
|
|
85210
|
+
return updated
|
|
85204
85211
|
}
|
|
85205
85212
|
|
|
85206
85213
|
function _generateUid(name, i) {
|
|
@@ -85609,7 +85616,6 @@
|
|
|
85609
85616
|
// name with `esm!`, above all.
|
|
85610
85617
|
resolvePath: resolvePath$1 = resolvePath,
|
|
85611
85618
|
// ecmaVersion = 2020,
|
|
85612
|
-
// minify,
|
|
85613
85619
|
sourceMap
|
|
85614
85620
|
} = {}) {
|
|
85615
85621
|
// const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
|
|
@@ -85618,48 +85624,29 @@
|
|
|
85618
85624
|
const options = { sourceFileName: file, pluginName, resolvePath: resolvePath$1, originalResolvePath: resolvePath };
|
|
85619
85625
|
transformModules(ast, options);
|
|
85620
85626
|
|
|
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 });
|
|
85627
|
+
const { updated } = options;
|
|
85628
|
+
let code, map;
|
|
85629
|
+
if (updated) {
|
|
85630
|
+
if (sourceMap === true) {
|
|
85631
|
+
sourceMap = { inline: true, content: true };
|
|
85632
|
+
}
|
|
85633
|
+
const mapGenerator = sourceMap ? new SourceMapGenerator({ file }) : undefined;
|
|
85634
|
+
code = generate(ast, { sourceMap: mapGenerator });
|
|
85654
85635
|
if (sourceMap) {
|
|
85655
|
-
|
|
85636
|
+
if (sourceMap.content) {
|
|
85637
|
+
mapGenerator.setSourceContent(file, text);
|
|
85638
|
+
}
|
|
85639
|
+
if (sourceMap.inline) {
|
|
85640
|
+
code += convertSourceMap.fromObject(mapGenerator.toJSON()).toComment();
|
|
85641
|
+
} else {
|
|
85642
|
+
map = mapGenerator.toJSON();
|
|
85643
|
+
}
|
|
85656
85644
|
}
|
|
85657
|
-
// }
|
|
85658
85645
|
} else {
|
|
85659
85646
|
code = text;
|
|
85660
85647
|
}
|
|
85661
85648
|
|
|
85662
|
-
return code
|
|
85649
|
+
return { code, map, updated }
|
|
85663
85650
|
}
|
|
85664
85651
|
|
|
85665
85652
|
exports.resolvePath = resolvePath;
|