requirejs-esm 4.2.0 → 4.3.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/dist/plugin.js CHANGED
@@ -38,7 +38,7 @@
38
38
  xhr.send(null);
39
39
  };
40
40
  } else {
41
- const { readFileSync } = require.nodeRequire ? require.nodeRequire('fs') : require('fs');
41
+ const { readFileSync } = require.nodeRequire ? require.nodeRequire('fs') : require('node:fs');
42
42
  fetchText = (path, callback) => {
43
43
  // Asynchronous reading is not possible during the build in the optimizer.
44
44
  try {
@@ -58,8 +58,8 @@
58
58
  // Initialise the writeText variable with a function to write to a file.
59
59
  /* istanbul ignore if */
60
60
  if (!(typeof window !== 'undefined' && window.navigator && window.document)) {
61
- const { writeFileSync, mkdirSync } = require.nodeRequire ? require.nodeRequire('fs') : require('fs');
62
- const { dirname } = require.nodeRequire ? require.nodeRequire('path') : require('path');
61
+ const { writeFileSync, mkdirSync } = require.nodeRequire ? require.nodeRequire('fs') : require('node:fs');
62
+ const { dirname } = require.nodeRequire ? require.nodeRequire('path') : require('node:path');
63
63
  writeText = (path, content) => {
64
64
  mkdirSync(dirname(path), { recursive: true });
65
65
  writeFileSync(path, content);
@@ -141,7 +141,7 @@
141
141
  // mapped there using the `paths` of `map` configuration properties.
142
142
  if ((sourcePath.charAt(0) === '.' && (sourcePath.charAt(1) === '/' ||
143
143
  sourcePath.charAt(1) === '.' && sourcePath.charAt(2) === '/')) &&
144
- !(needsResolve && needsResolve(sourcePath, currentFile))) {
144
+ !(needsResolve?.(sourcePath, currentFile))) {
145
145
  sourcePath = joinPath(parentDir(currentFile), sourcePath);
146
146
  if (sourcePath.endsWith('.js')) sourcePath = sourcePath.substring(0, sourcePath.length - 3);
147
147
  }
@@ -2171,7 +2171,7 @@
2171
2171
  advanceChar(parser);
2172
2172
  if (parser.currentChar === 61) {
2173
2173
  advanceChar(parser);
2174
- return 4194344;
2174
+ return 4718632;
2175
2175
  }
2176
2176
  return 8913465;
2177
2177
  }
@@ -2214,7 +2214,7 @@
2214
2214
  advanceChar(parser);
2215
2215
  if (parser.currentChar === 61) {
2216
2216
  advanceChar(parser);
2217
- return 4194345;
2217
+ return 4718633;
2218
2218
  }
2219
2219
  return 8913720;
2220
2220
  }
@@ -2230,7 +2230,7 @@
2230
2230
  advanceChar(parser);
2231
2231
  if (parser.currentChar === 61) {
2232
2232
  advanceChar(parser);
2233
- return 4194346;
2233
+ return 4718634;
2234
2234
  }
2235
2235
  return 276824445;
2236
2236
  }
@@ -4896,7 +4896,7 @@
4896
4896
  currentChar = 0;
4897
4897
  exportedNames = new Set();
4898
4898
  exportedBindings = new Set();
4899
- assignable = 1;
4899
+ assignable = 0;
4900
4900
  destructible = 0;
4901
4901
  leadingDecorators = { decorators: [] };
4902
4902
  constructor(source, rawOptions = {}) {
@@ -6185,6 +6185,8 @@
6185
6185
  if ((token & 4194304) === 4194304) {
6186
6186
  if (parser.assignable & 2)
6187
6187
  parser.report(26);
6188
+ if ((token & 524288) === 524288 && parser.assignable & 4)
6189
+ parser.report(26);
6188
6190
  if ((!isPattern && token === 1077936155 && left.type === 'ArrayExpression') ||
6189
6191
  left.type === 'ObjectExpression') {
6190
6192
  reinterpretToPattern(parser, left);
@@ -6550,7 +6552,12 @@
6550
6552
  parser.flags = (parser.flags | 2048) ^ 2048;
6551
6553
  }
6552
6554
  const args = parseArguments(parser, context, privateScope, inGroup);
6553
- parser.assignable = 2;
6555
+ if (!(context & 1) && parser.options.webcompat) {
6556
+ parser.assignable = 4;
6557
+ }
6558
+ else {
6559
+ parser.assignable = 2;
6560
+ }
6554
6561
  expr = parser.finishNode({
6555
6562
  type: 'CallExpression',
6556
6563
  callee: expr,
@@ -6618,7 +6625,12 @@
6618
6625
  }
6619
6626
  else if (parser.getToken() === 67174411) {
6620
6627
  const args = parseArguments(parser, context, privateScope, 0);
6621
- parser.assignable = 2;
6628
+ if (!(context & 1) && parser.options.webcompat) {
6629
+ parser.assignable = 4;
6630
+ }
6631
+ else {
6632
+ parser.assignable = 2;
6633
+ }
6622
6634
  node = parser.finishNode({
6623
6635
  type: 'CallExpression',
6624
6636
  callee: expr,
@@ -7223,9 +7235,9 @@
7223
7235
  }
7224
7236
  else if (parser.getToken() !== 1077936155) {
7225
7237
  destructible |=
7226
- parser.assignable & 2
7227
- ? 16
7228
- : 32;
7238
+ parser.assignable & 1
7239
+ ? 32
7240
+ : 16;
7229
7241
  }
7230
7242
  }
7231
7243
  }
@@ -7255,9 +7267,9 @@
7255
7267
  }
7256
7268
  else if (parser.getToken() !== 1077936155) {
7257
7269
  destructible |=
7258
- parser.assignable & 2
7259
- ? 16
7260
- : 32;
7270
+ parser.assignable & 1
7271
+ ? 32
7272
+ : 16;
7261
7273
  }
7262
7274
  }
7263
7275
  }
@@ -7374,7 +7386,7 @@
7374
7386
  if (parser.destructible & 8)
7375
7387
  parser.report(71);
7376
7388
  argument = parseMemberOrUpdateExpression(parser, context, privateScope, argument, inGroup, 0, tokenStart);
7377
- destructible |= parser.assignable & 2 ? 16 : 0;
7389
+ destructible |= parser.assignable & 1 ? 0 : 16;
7378
7390
  if ((parser.getToken() & 4194304) === 4194304) {
7379
7391
  if (parser.getToken() !== 1077936155)
7380
7392
  destructible |= 16;
@@ -7388,9 +7400,9 @@
7388
7400
  argument = parseConditionalExpression(parser, context, privateScope, argument, tokenStart);
7389
7401
  }
7390
7402
  destructible |=
7391
- parser.assignable & 2
7392
- ? 16
7393
- : 32;
7403
+ parser.assignable & 1
7404
+ ? 32
7405
+ : 16;
7394
7406
  }
7395
7407
  }
7396
7408
  else {
@@ -7419,7 +7431,9 @@
7419
7431
  argument = parseAssignmentExpression(parser, context, privateScope, inGroup, isPattern, tokenStart, argument);
7420
7432
  }
7421
7433
  destructible |=
7422
- parser.assignable & 1 ? 32 : 16;
7434
+ parser.assignable & 1
7435
+ ? 32
7436
+ : 16;
7423
7437
  }
7424
7438
  parser.destructible = destructible;
7425
7439
  if (parser.getToken() !== closingToken && parser.getToken() !== 18)
@@ -7611,7 +7625,9 @@
7611
7625
  : parseObjectLiteralOrPattern(parser, context, scope, privateScope, 0, inGroup, isPattern, kind, origin);
7612
7626
  destructible = parser.destructible;
7613
7627
  parser.assignable =
7614
- destructible & 16 ? 2 : 1;
7628
+ destructible & 16
7629
+ ? 2
7630
+ : 1;
7615
7631
  if (parser.getToken() === 18 || parser.getToken() === 1074790415) {
7616
7632
  if (parser.assignable & 2)
7617
7633
  destructible |= 16;
@@ -7633,9 +7649,9 @@
7633
7649
  value = parseConditionalExpression(parser, context, privateScope, value, tokenStart);
7634
7650
  }
7635
7651
  destructible |=
7636
- parser.assignable & 2
7637
- ? 16
7638
- : 32;
7652
+ parser.assignable & 1
7653
+ ? 32
7654
+ : 16;
7639
7655
  }
7640
7656
  }
7641
7657
  }
@@ -7792,7 +7808,9 @@
7792
7808
  : parseObjectLiteralOrPattern(parser, context, scope, privateScope, 0, inGroup, isPattern, kind, origin);
7793
7809
  destructible = parser.destructible;
7794
7810
  parser.assignable =
7795
- destructible & 16 ? 2 : 1;
7811
+ destructible & 16
7812
+ ? 2
7813
+ : 1;
7796
7814
  if (parser.getToken() === 18 || parser.getToken() === 1074790415) {
7797
7815
  if (parser.assignable & 2) {
7798
7816
  destructible |= 16;
@@ -7812,9 +7830,9 @@
7812
7830
  value = parseConditionalExpression(parser, context, privateScope, value, tokenStart);
7813
7831
  }
7814
7832
  destructible |=
7815
- parser.assignable & 2
7816
- ? 16
7817
- : 32;
7833
+ parser.assignable & 1
7834
+ ? 32
7835
+ : 16;
7818
7836
  }
7819
7837
  }
7820
7838
  }
@@ -7843,7 +7861,7 @@
7843
7861
  else if (parser.getToken() === 67174411) {
7844
7862
  state |= 1;
7845
7863
  value = parseMethodDefinition(parser, context, privateScope, state, inGroup, parser.tokenStart);
7846
- destructible = parser.assignable | 16;
7864
+ destructible = 16;
7847
7865
  }
7848
7866
  else {
7849
7867
  parser.report(134);
@@ -7863,11 +7881,11 @@
7863
7881
  value = parseMemberOrUpdateExpression(parser, context, privateScope, value, inGroup, 0, tokenStart);
7864
7882
  if ((parser.getToken() & 4194304) === 4194304) {
7865
7883
  destructible |=
7866
- parser.assignable & 2
7867
- ? 16
7868
- : token === 1077936155
7884
+ parser.assignable & 1
7885
+ ? token === 1077936155
7869
7886
  ? 0
7870
- : 32;
7887
+ : 32
7888
+ : 16;
7871
7889
  value = parseAssignmentExpressionOrPattern(parser, context, privateScope, inGroup, isPattern, tokenStart, value);
7872
7890
  }
7873
7891
  else if (parser.getToken() === 18 || parser.getToken() === 1074790415) {
@@ -7898,7 +7916,9 @@
7898
7916
  : parseObjectLiteralOrPattern(parser, context, scope, privateScope, 0, inGroup, isPattern, kind, origin);
7899
7917
  destructible = parser.destructible;
7900
7918
  parser.assignable =
7901
- destructible & 16 ? 2 : 1;
7919
+ destructible & 16
7920
+ ? 2
7921
+ : 1;
7902
7922
  if (parser.getToken() === 18 || parser.getToken() === 1074790415) {
7903
7923
  if (parser.assignable & 2)
7904
7924
  destructible |= 16;
@@ -7923,9 +7943,9 @@
7923
7943
  value = parseConditionalExpression(parser, context, privateScope, value, tokenStart);
7924
7944
  }
7925
7945
  destructible |=
7926
- parser.assignable & 2
7927
- ? 16
7928
- : 32;
7946
+ parser.assignable & 1
7947
+ ? 32
7948
+ : 16;
7929
7949
  }
7930
7950
  }
7931
7951
  }
@@ -8530,6 +8550,12 @@
8530
8550
  parser.report(48);
8531
8551
  return parseParenthesizedArrow(parser, context, scope, privateScope, [], canAssign, 1, start);
8532
8552
  }
8553
+ if (!(context & 1) && parser.options.webcompat) {
8554
+ parser.assignable = 4;
8555
+ }
8556
+ else {
8557
+ parser.assignable = 2;
8558
+ }
8533
8559
  return parser.finishNode({
8534
8560
  type: 'CallExpression',
8535
8561
  callee,
@@ -8603,7 +8629,7 @@
8603
8629
  }
8604
8630
  else {
8605
8631
  expr = parseExpression(parser, context, privateScope, 1, 0, tokenStart);
8606
- destructible = parser.assignable;
8632
+ destructible = 0;
8607
8633
  params.push(expr);
8608
8634
  while (consumeOpt(parser, context | 32, 18)) {
8609
8635
  params.push(parseExpression(parser, context, privateScope, 1, 0, tokenStart));
@@ -8611,7 +8637,12 @@
8611
8637
  destructible |= parser.assignable;
8612
8638
  consume(parser, context, 16);
8613
8639
  parser.destructible = destructible | 16;
8614
- parser.assignable = 2;
8640
+ if (!(context & 1) && parser.options.webcompat) {
8641
+ parser.assignable = 4;
8642
+ }
8643
+ else {
8644
+ parser.assignable = 2;
8645
+ }
8615
8646
  return parser.finishNode({
8616
8647
  type: 'CallExpression',
8617
8648
  callee,
@@ -8649,7 +8680,12 @@
8649
8680
  if (destructible & 8) {
8650
8681
  parser.report(62);
8651
8682
  }
8652
- parser.assignable = 2;
8683
+ if (!(context & 1) && parser.options.webcompat) {
8684
+ parser.assignable = 4;
8685
+ }
8686
+ else {
8687
+ parser.assignable = 2;
8688
+ }
8653
8689
  return parser.finishNode({
8654
8690
  type: 'CallExpression',
8655
8691
  callee,
@@ -86181,7 +86217,7 @@
86181
86217
  }
86182
86218
 
86183
86219
  function toIdentifier(input) {
86184
- input = input + '';
86220
+ input = `${input}`;
86185
86221
 
86186
86222
  let name = '';
86187
86223
  for (const c of input) {
@@ -86190,9 +86226,7 @@
86190
86226
 
86191
86227
  name = name.replace(/^[-0-9]+/, '');
86192
86228
 
86193
- name = name.replace(/[-\s]+(.)?/g, function (match, c) {
86194
- return c ? c.toUpperCase() : ''
86195
- });
86229
+ name = name.replace(/[-\s]+(.)?/g, (_match, c) => c ? c.toUpperCase() : '');
86196
86230
 
86197
86231
  if (!isValidIdentifier(name)) {
86198
86232
  name = `_${name}`;
@@ -86737,7 +86771,7 @@
86737
86771
  const importVar = generateUidIdentifier(exportSource.value, program);
86738
86772
  importVars.push(importVar);
86739
86773
 
86740
- for (let specifier of specifiers) {
86774
+ for (const specifier of specifiers) {
86741
86775
  const { exported, local } = specifier;
86742
86776
  const { name } = local;
86743
86777
  let localName;
@@ -86811,6 +86845,17 @@
86811
86845
  }
86812
86846
  }
86813
86847
 
86848
+ // no imports and exports, do not wrap to AMD module, if AMD/UMD should be assumed
86849
+ if (options.skipIfNoImportExport && !(importPaths.length || hasExport)) {
86850
+ options.onAfterTransform?.({
86851
+ ...options,
86852
+ updated: false,
86853
+ program,
86854
+ callbackBody: body
86855
+ });
86856
+ return false
86857
+ }
86858
+
86814
86859
  // adding define wrapper
86815
86860
  if (hasExport && needReturnExport) {
86816
86861
  let returnStat;
@@ -86843,10 +86888,13 @@
86843
86888
 
86844
86889
  options.onAfterTransform?.({
86845
86890
  ...options,
86891
+ updated: true,
86846
86892
  program,
86847
86893
  callbackBody: body
86848
86894
  });
86849
86895
 
86896
+ return true
86897
+
86850
86898
  function addExportStatement({ exported, local }) {
86851
86899
  const asName = exported.name;
86852
86900
  if (asName !== 'default') {
@@ -86928,6 +86976,23 @@
86928
86976
  return arrayExpression(importPaths)
86929
86977
  }
86930
86978
 
86979
+ // Check if one of the first 10 lines shorter than 100 characters contains
86980
+ // "// requirejs-esm-skip-file" or // requirejs-esm-process-file".
86981
+ function processOrSkipByComment(text) {
86982
+ for (let start = 0, i = 0; i < 10; ++i) {
86983
+ const endLine = text.indexOf('\n', start);
86984
+ if (endLine < 0 || endLine > 100) break
86985
+ const line = text.substring(start, endLine);
86986
+ const comment = /^\s*\/\//.test(line);
86987
+ if (!comment) break
86988
+ const directive = /^\s*\/\/\s*requirejs-esm-(skip|process)-file\s*$/.exec(line);
86989
+ if (directive) {
86990
+ return directive[1] === 'process'
86991
+ }
86992
+ start = endLine + 1;
86993
+ }
86994
+ }
86995
+
86931
86996
  function transformAst(program, options = {}) {
86932
86997
  const amds = detectDefinesOrRequires(program);
86933
86998
  const { length } = amds;
@@ -86941,8 +87006,7 @@
86941
87006
  result.updated ||= updated;
86942
87007
  }
86943
87008
  } else {
86944
- transformEsmToAmd(program, options);
86945
- result.updated = true;
87009
+ result.updated = transformEsmToAmd(program, options);
86946
87010
  }
86947
87011
  return result
86948
87012
  }
@@ -86953,6 +87017,8 @@
86953
87017
  // Method to update paths of module dependencies, to prefix JavaScript module
86954
87018
  // name with `esm!`, above all.
86955
87019
  resolvePath = resolvePath$1,
87020
+ // Assume AMD/UMD if there're no import or export statements.
87021
+ skipIfNoImportExport,
86956
87022
  // ecmaVersion = 2020,
86957
87023
  // Do not insert `"use strict"` expression to the AMD modules. You'd set it
86958
87024
  // to `false` if your bundler inserts `"use strict"` to the outer scope.
@@ -86967,8 +87033,15 @@
86967
87033
  onBeforeUpdate,
86968
87034
  onAfterUpdate
86969
87035
  } = {}) {
87036
+ const processOrSkip = processOrSkipByComment(text);
87037
+ if (processOrSkip === false) {
87038
+ return { code: text, map: null, updated: false }
87039
+ } else if (processOrSkip === true) {
87040
+ skipIfNoImportExport = undefined;
87041
+ }
87042
+
86970
87043
  // const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
86971
- let ast = parseModule(text, { next: true, loc: true });
87044
+ const ast = parseModule(text, { next: true, loc: true });
86972
87045
 
86973
87046
  const options = {
86974
87047
  sourceFileName: file,
@@ -86976,6 +87049,7 @@
86976
87049
  resolvePath,
86977
87050
  originalResolvePath: resolvePath$1,
86978
87051
  useStrict,
87052
+ skipIfNoImportExport,
86979
87053
  onBeforeTransform,
86980
87054
  onAfterTransform,
86981
87055
  onBeforeUpdate,
@@ -87020,6 +87094,8 @@
87020
87094
  onlyAmd,
87021
87095
  // List of module names not to check and transform.
87022
87096
  skipModules = [],
87097
+ // Assume AMD/UMD if there're no import or export statements.
87098
+ skipIfNoImportExport,
87023
87099
  // Method to update paths of module dependencies, to prefix JavaScript module
87024
87100
  // name with `esm!`, above all.
87025
87101
  resolvePath,
@@ -87116,6 +87192,7 @@
87116
87192
  resolvePath,
87117
87193
  /*ecmaVersion,*/
87118
87194
  useStrict,
87195
+ skipIfNoImportExport,
87119
87196
  // Always produce the source maps when transpiling in the browser, otherwise
87120
87197
  // the debugging would me impossible. When building and bundling, check if
87121
87198
  // the source maps were enabled for the output.
@@ -87160,7 +87237,7 @@
87160
87237
  // compiled with esm refer it with that name and this stub will simplify
87161
87238
  // the module loading by skipping the plugin evaluation.
87162
87239
  write.asModule(`${pluginName}!${moduleName}`,
87163
- '\ndefine([\'' + moduleName + '\'], res => res);\n');
87240
+ `\ndefine(['${moduleName}'], res => res);\n`);
87164
87241
  }
87165
87242
  //>>excludeEnd('excludeEsm')
87166
87243
  }