requirejs-esm 1.0.1 → 2.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 CHANGED
@@ -8,9 +8,11 @@ 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
- This module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version 6 or newer.
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.
14
16
 
15
17
  ```sh
16
18
  npm i -D requirejs-esm
@@ -48,11 +50,20 @@ pragmasOnSave: {
48
50
  }
49
51
  ```
50
52
 
51
- See also a [demo] project:
53
+ See also a [demo-local] project, which includes sources only from the local `src` directory:
52
54
 
53
55
  ```sh
54
56
  npm start
55
- open http://localhost:8967/demo/normal.html
57
+ open http://localhost:8967/demo-local/normal.html
58
+ open http://localhost:8967/demo-local/optimized.html
59
+ ```
60
+
61
+ See also a [demo-extern] project, which includes sources from the local `src` directory and from `node_modules` outside of it:
62
+
63
+ ```sh
64
+ npm run start
65
+ open http://localhost:8967/demo-extern/normal.html
66
+ open http://localhost:8967/demo-extern/optimized.html
56
67
  ```
57
68
 
58
69
  ## Advanced
@@ -173,13 +184,16 @@ Licensed under the MIT license.
173
184
  [RequireJS]: http://requirejs.org
174
185
  [RequireJS optimizer]: https://requirejs.org/docs/optimization.html
175
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
176
189
  [`requirejs`]: https://www.npmjs.com/package/requirejs
177
190
  [`@prantlf/requirejs`]: https://www.npmjs.com/package/@prantlf/requirejs
178
191
  [Node.js]: http://nodejs.org/
179
192
  [NPM]: https://www.npmjs.com/
180
193
  [PNPM]: https://pnpm.io/
181
194
  [Yarn]: https://yarnpkg.com/
182
- [demo]: https://github.com/prantlf/requirejs-esm/tree/master/demo
195
+ [demo-local]: https://github.com/prantlf/requirejs-esm/tree/master/demo-local
196
+ [demo-extern]: https://github.com/prantlf/requirejs-esm/tree/master/demo-extern
183
197
  [default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/src/resolve-path.js#L48
184
198
  [resolvePath]: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath
185
199
  [a lot faster]: ./perf/README.md#readme
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 (!pluginName || sourcePath.includes('!') || sourcePath === 'require' ||
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
- sourcePath.charAt(1) === '.' && sourcePath.charAt(2) === '/')) {
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
- return `${pluginName}!${sourcePath}`
72
+
73
+ return pluginName ? `${pluginName}!${sourcePath}` : sourcePath
71
74
  }
72
75
 
73
76
  const errorMessages = {
@@ -944,9 +947,9 @@
944
947
  mask |= 8;
945
948
  break;
946
949
  case 115:
947
- if (mask & 12)
950
+ if (mask & 32)
948
951
  report(parser, 34, 's');
949
- mask |= 12;
952
+ mask |= 32;
950
953
  break;
951
954
  default:
952
955
  report(parser, 33);
@@ -1859,7 +1862,7 @@
1859
1862
  }
1860
1863
  else if (ch === 61) {
1861
1864
  advanceChar(parser);
1862
- return 8456000;
1865
+ return 8456256;
1863
1866
  }
1864
1867
  if (ch === 33) {
1865
1868
  const index = parser.index + 1;
@@ -2042,7 +2045,7 @@
2042
2045
  const ch = parser.currentChar;
2043
2046
  if (ch === 61) {
2044
2047
  advanceChar(parser);
2045
- return 8456001;
2048
+ return 8456257;
2046
2049
  }
2047
2050
  if (ch !== 62)
2048
2051
  return 8456259;
@@ -5034,7 +5037,7 @@
5034
5037
  nextToken(parser, context | 32768);
5035
5038
  const argument = parser.flags & 1 || parser.token & 1048576
5036
5039
  ? null
5037
- : parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.line, parser.column);
5040
+ : parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.linePos, parser.colPos);
5038
5041
  matchOrInsertSemicolon(parser, context | 32768);
5039
5042
  return finishNode(parser, context, start, line, column, {
5040
5043
  type: 'ReturnStatement',
@@ -6150,7 +6153,7 @@
6150
6153
  if ((context & 524288) < 1)
6151
6154
  report(parser, 26);
6152
6155
  if (context & 16384)
6153
- report(parser, 143);
6156
+ report(parser, 27);
6154
6157
  parser.assignable = 2;
6155
6158
  break;
6156
6159
  }
@@ -6159,7 +6162,7 @@
6159
6162
  if ((context & 262144) < 1)
6160
6163
  report(parser, 27);
6161
6164
  if (context & 16384)
6162
- report(parser, 143);
6165
+ report(parser, 27);
6163
6166
  parser.assignable = 1;
6164
6167
  break;
6165
6168
  }
@@ -6190,10 +6193,10 @@
6190
6193
  expr = parseUpdateExpression(parser, context, expr, start, line, column);
6191
6194
  }
6192
6195
  else if ((parser.token & 67108864) === 67108864) {
6193
- context = (context | 134217728 | 8192) ^ (134217728 | 8192);
6196
+ context = (context | 134217728) ^ 134217728;
6194
6197
  switch (parser.token) {
6195
6198
  case 67108877: {
6196
- nextToken(parser, context | 1073741824);
6199
+ nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
6197
6200
  parser.assignable = 1;
6198
6201
  const property = parsePropertyOrPrivatePropertyName(parser, context);
6199
6202
  expr = finishNode(parser, context, start, line, column, {
@@ -6249,7 +6252,7 @@
6249
6252
  break;
6250
6253
  }
6251
6254
  case 67108991: {
6252
- nextToken(parser, context);
6255
+ nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
6253
6256
  parser.flags |= 2048;
6254
6257
  parser.assignable = 2;
6255
6258
  expr = parseOptionalChain(parser, context, expr, start, line, column);
@@ -8413,12 +8416,10 @@
8413
8416
  }
8414
8417
  else if (context & 1 && parser.token === 131) {
8415
8418
  kind |= 4096;
8416
- key = parsePrivateIdentifier(parser, context, tokenPos, linePos, colPos);
8417
- context = context | 16384;
8419
+ key = parsePrivateIdentifier(parser, context | 16384, tokenPos, linePos, colPos);
8418
8420
  }
8419
8421
  else if (context & 1 && (parser.token & 1073741824) === 1073741824) {
8420
8422
  kind |= 128;
8421
- context = context | 16384;
8422
8423
  }
8423
8424
  else if (token === 122) {
8424
8425
  key = parseIdentifier(parser, context, 0);
@@ -8784,7 +8785,7 @@
8784
8785
  });
8785
8786
  }
8786
8787
  function parseJSXExpressionContainer(parser, context, inJSXChild, isAttr, start, line, column) {
8787
- nextToken(parser, context);
8788
+ nextToken(parser, context | 32768);
8788
8789
  const { tokenPos, linePos, colPos } = parser;
8789
8790
  if (parser.token === 14)
8790
8791
  return parseJSXSpreadChild(parser, context, tokenPos, linePos, colPos);
@@ -81686,6 +81687,9 @@
81686
81687
  });
81687
81688
 
81688
81689
  obj[impl][utils$1.wrapperSymbol] = obj;
81690
+ if (Impl.init) {
81691
+ Impl.init(obj[impl], privateData);
81692
+ }
81689
81693
  return obj;
81690
81694
  },
81691
81695
  interface: URLSearchParams,
@@ -82237,6 +82241,9 @@
82237
82241
  });
82238
82242
 
82239
82243
  obj[impl][utils$1.wrapperSymbol] = obj;
82244
+ if (Impl.init) {
82245
+ Impl.init(obj[impl], privateData);
82246
+ }
82240
82247
  return obj;
82241
82248
  },
82242
82249
  interface: URL,
@@ -85182,9 +85189,6 @@
85182
85189
 
85183
85190
  // Updates dependency paths to be prefixed by `esm!` or otherwise updated.
85184
85191
  function updateAmdDeps(amd, options) {
85185
- const { resolvePath } = options;
85186
- if (!resolvePath) return
85187
-
85188
85192
  const { deps } = amd;
85189
85193
  if (!deps) return
85190
85194
 
@@ -85192,6 +85196,7 @@
85192
85196
  const { elements } = deps;
85193
85197
  if (!elements.length) return
85194
85198
 
85199
+ const { resolvePath } = options;
85195
85200
  let updated;
85196
85201
  for (const element of elements) {
85197
85202
  if (element.type === 'Literal') {
@@ -85570,11 +85575,8 @@
85570
85575
  // Update dependency paths to be prefixed by `esm!` or otherwise updated.
85571
85576
  function prepareImportPaths(importPaths, options ) {
85572
85577
  const { resolvePath } = options;
85573
- if (!resolvePath) return
85574
-
85575
85578
  if (resolvePath) {
85576
85579
  const { sourceFileName: parentName } = options;
85577
-
85578
85580
  for (const importPath of importPaths) {
85579
85581
  if (importPath.type === 'Literal') {
85580
85582
  const moduleName = importPath.value;
@@ -85585,7 +85587,6 @@
85585
85587
  }
85586
85588
  }
85587
85589
  }
85588
-
85589
85590
  return arrayExpression(importPaths)
85590
85591
  }
85591
85592
 
@@ -85594,7 +85595,7 @@
85594
85595
  const { length } = amds;
85595
85596
  if (length) {
85596
85597
  options.amd = true;
85597
- if (options.pluginName) {
85598
+ if (options.resolvePath) {
85598
85599
  for (const amd of amds) {
85599
85600
  options.updated |= updateAmdDeps(amd, options);
85600
85601
  }