requirejs-esm 1.0.0 → 1.1.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
@@ -10,7 +10,7 @@ The official [RequireJS optimizer] (`r.js`) does not wire up source maps from th
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 6 or newer.
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
@@ -179,7 +188,8 @@ Licensed under the MIT license.
179
188
  [NPM]: https://www.npmjs.com/
180
189
  [PNPM]: https://pnpm.io/
181
190
  [Yarn]: https://yarnpkg.com/
182
- [demo]: https://github.com/prantlf/requirejs-esm/tree/master/demo
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
183
193
  [default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/src/resolve-path.js#L48
184
194
  [resolvePath]: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath
185
195
  [a lot faster]: ./perf/README.md#readme
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`.
@@ -51,21 +53,24 @@
51
53
 
52
54
  // Ensures that every JavaScript dependency will be prefixed by `esm!`.
53
55
  // Relative paths will be converted to be relative to the parent module.
54
- function resolvePath (sourcePath, currentFile, { pluginName }) {
56
+ function resolvePath (sourcePath, currentFile, { pluginName, needsResolve } = {}) {
55
57
  // Ignore paths with other plugins applied and the three built-in
56
58
  // pseudo-modules of RequireJS.
57
- if (!pluginName || sourcePath.includes('!') || sourcePath === 'require' ||
59
+ if (sourcePath.includes('!') || sourcePath === 'require' ||
58
60
  sourcePath === 'module' || sourcePath === 'exports') return
59
61
 
60
62
  // If `sourcePath` is relative to `currentFile` - starts with ./ or ../ -
61
63
  // prepend the parent directory of `currentFile` to it. This was needed
62
64
  // for modules located outside the source root (`baseUrl`), which were
63
65
  // mapped there using the `paths` of `map` configuration properties.
64
- if (sourcePath.charAt(0) === '.' && (sourcePath.charAt(1) === '/' ||
65
- 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))) {
66
69
  sourcePath = joinPath(parentDir(currentFile), sourcePath);
70
+ if (sourcePath.endsWith('.js')) sourcePath = sourcePath.substring(0, sourcePath.length - 3);
67
71
  }
68
- return `${pluginName}!${sourcePath}`
72
+
73
+ return pluginName ? `${pluginName}!${sourcePath}` : sourcePath
69
74
  }
70
75
 
71
76
  const errorMessages = {
@@ -942,9 +947,9 @@
942
947
  mask |= 8;
943
948
  break;
944
949
  case 115:
945
- if (mask & 12)
950
+ if (mask & 32)
946
951
  report(parser, 34, 's');
947
- mask |= 12;
952
+ mask |= 32;
948
953
  break;
949
954
  default:
950
955
  report(parser, 33);
@@ -1857,7 +1862,7 @@
1857
1862
  }
1858
1863
  else if (ch === 61) {
1859
1864
  advanceChar(parser);
1860
- return 8456000;
1865
+ return 8456256;
1861
1866
  }
1862
1867
  if (ch === 33) {
1863
1868
  const index = parser.index + 1;
@@ -2040,7 +2045,7 @@
2040
2045
  const ch = parser.currentChar;
2041
2046
  if (ch === 61) {
2042
2047
  advanceChar(parser);
2043
- return 8456001;
2048
+ return 8456257;
2044
2049
  }
2045
2050
  if (ch !== 62)
2046
2051
  return 8456259;
@@ -5032,7 +5037,7 @@
5032
5037
  nextToken(parser, context | 32768);
5033
5038
  const argument = parser.flags & 1 || parser.token & 1048576
5034
5039
  ? null
5035
- : parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.line, parser.column);
5040
+ : parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.linePos, parser.colPos);
5036
5041
  matchOrInsertSemicolon(parser, context | 32768);
5037
5042
  return finishNode(parser, context, start, line, column, {
5038
5043
  type: 'ReturnStatement',
@@ -6148,7 +6153,7 @@
6148
6153
  if ((context & 524288) < 1)
6149
6154
  report(parser, 26);
6150
6155
  if (context & 16384)
6151
- report(parser, 143);
6156
+ report(parser, 27);
6152
6157
  parser.assignable = 2;
6153
6158
  break;
6154
6159
  }
@@ -6157,7 +6162,7 @@
6157
6162
  if ((context & 262144) < 1)
6158
6163
  report(parser, 27);
6159
6164
  if (context & 16384)
6160
- report(parser, 143);
6165
+ report(parser, 27);
6161
6166
  parser.assignable = 1;
6162
6167
  break;
6163
6168
  }
@@ -6188,10 +6193,10 @@
6188
6193
  expr = parseUpdateExpression(parser, context, expr, start, line, column);
6189
6194
  }
6190
6195
  else if ((parser.token & 67108864) === 67108864) {
6191
- context = (context | 134217728 | 8192) ^ (134217728 | 8192);
6196
+ context = (context | 134217728) ^ 134217728;
6192
6197
  switch (parser.token) {
6193
6198
  case 67108877: {
6194
- nextToken(parser, context | 1073741824);
6199
+ nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
6195
6200
  parser.assignable = 1;
6196
6201
  const property = parsePropertyOrPrivatePropertyName(parser, context);
6197
6202
  expr = finishNode(parser, context, start, line, column, {
@@ -6247,7 +6252,7 @@
6247
6252
  break;
6248
6253
  }
6249
6254
  case 67108991: {
6250
- nextToken(parser, context);
6255
+ nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
6251
6256
  parser.flags |= 2048;
6252
6257
  parser.assignable = 2;
6253
6258
  expr = parseOptionalChain(parser, context, expr, start, line, column);
@@ -8411,12 +8416,10 @@
8411
8416
  }
8412
8417
  else if (context & 1 && parser.token === 131) {
8413
8418
  kind |= 4096;
8414
- key = parsePrivateIdentifier(parser, context, tokenPos, linePos, colPos);
8415
- context = context | 16384;
8419
+ key = parsePrivateIdentifier(parser, context | 16384, tokenPos, linePos, colPos);
8416
8420
  }
8417
8421
  else if (context & 1 && (parser.token & 1073741824) === 1073741824) {
8418
8422
  kind |= 128;
8419
- context = context | 16384;
8420
8423
  }
8421
8424
  else if (token === 122) {
8422
8425
  key = parseIdentifier(parser, context, 0);
@@ -8782,7 +8785,7 @@
8782
8785
  });
8783
8786
  }
8784
8787
  function parseJSXExpressionContainer(parser, context, inJSXChild, isAttr, start, line, column) {
8785
- nextToken(parser, context);
8788
+ nextToken(parser, context | 32768);
8786
8789
  const { tokenPos, linePos, colPos } = parser;
8787
8790
  if (parser.token === 14)
8788
8791
  return parseJSXSpreadChild(parser, context, tokenPos, linePos, colPos);
@@ -81684,6 +81687,9 @@
81684
81687
  });
81685
81688
 
81686
81689
  obj[impl][utils$1.wrapperSymbol] = obj;
81690
+ if (Impl.init) {
81691
+ Impl.init(obj[impl], privateData);
81692
+ }
81687
81693
  return obj;
81688
81694
  },
81689
81695
  interface: URLSearchParams,
@@ -82235,6 +82241,9 @@
82235
82241
  });
82236
82242
 
82237
82243
  obj[impl][utils$1.wrapperSymbol] = obj;
82244
+ if (Impl.init) {
82245
+ Impl.init(obj[impl], privateData);
82246
+ }
82238
82247
  return obj;
82239
82248
  },
82240
82249
  interface: URL,
@@ -85180,9 +85189,6 @@
85180
85189
 
85181
85190
  // Updates dependency paths to be prefixed by `esm!` or otherwise updated.
85182
85191
  function updateAmdDeps(amd, options) {
85183
- const { resolvePath } = options;
85184
- if (!resolvePath) return
85185
-
85186
85192
  const { deps } = amd;
85187
85193
  if (!deps) return
85188
85194
 
@@ -85190,6 +85196,7 @@
85190
85196
  const { elements } = deps;
85191
85197
  if (!elements.length) return
85192
85198
 
85199
+ const { resolvePath } = options;
85193
85200
  let updated;
85194
85201
  for (const element of elements) {
85195
85202
  if (element.type === 'Literal') {
@@ -85568,11 +85575,8 @@
85568
85575
  // Update dependency paths to be prefixed by `esm!` or otherwise updated.
85569
85576
  function prepareImportPaths(importPaths, options ) {
85570
85577
  const { resolvePath } = options;
85571
- if (!resolvePath) return
85572
-
85573
85578
  if (resolvePath) {
85574
85579
  const { sourceFileName: parentName } = options;
85575
-
85576
85580
  for (const importPath of importPaths) {
85577
85581
  if (importPath.type === 'Literal') {
85578
85582
  const moduleName = importPath.value;
@@ -85583,7 +85587,6 @@
85583
85587
  }
85584
85588
  }
85585
85589
  }
85586
-
85587
85590
  return arrayExpression(importPaths)
85588
85591
  }
85589
85592
 
@@ -85592,7 +85595,7 @@
85592
85595
  const { length } = amds;
85593
85596
  if (length) {
85594
85597
  options.amd = true;
85595
- if (options.pluginName) {
85598
+ if (options.resolvePath) {
85596
85599
  for (const amd of amds) {
85597
85600
  options.updated |= updateAmdDeps(amd, options);
85598
85601
  }