requirejs-esm 4.1.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}`;
@@ -86428,17 +86462,25 @@
86428
86462
 
86429
86463
  // Updates dependency paths to be prefixed by `esm!` or otherwise updated.
86430
86464
  function updateAmdDeps(amd, options) {
86431
- options.onBeforeUpdate?.(amd);
86465
+ let updated = options.onBeforeUpdate?.({
86466
+ ...options,
86467
+ amd
86468
+ });
86432
86469
 
86433
86470
  const { deps } = amd;
86434
- if (!deps) return
86471
+ if (!deps) {
86472
+ afterUpdate();
86473
+ return updated
86474
+ }
86435
86475
 
86436
86476
  const { sourceFileName: parentName } = options;
86437
86477
  const { elements } = deps;
86438
- if (!elements.length) return
86478
+ if (!elements.length) {
86479
+ afterUpdate();
86480
+ return updated
86481
+ }
86439
86482
 
86440
86483
  const { resolvePath } = options;
86441
- let updated;
86442
86484
  for (const element of elements) {
86443
86485
  if (element.type === 'Literal') {
86444
86486
  const moduleName = element.value;
@@ -86450,9 +86492,27 @@
86450
86492
  }
86451
86493
  }
86452
86494
 
86453
- updated ||= options.onAfterUpdate?.(amd);
86495
+ afterUpdate();
86454
86496
 
86455
86497
  return updated
86498
+
86499
+ function afterUpdate() {
86500
+ const updatedNow = options.onAfterUpdate?.({
86501
+ ...options,
86502
+ amd
86503
+ });
86504
+ updated ||= updatedNow;
86505
+ }
86506
+ }
86507
+
86508
+ function callAmdUpdateHooks(amd, options) {
86509
+ options = {
86510
+ ...options,
86511
+ amd
86512
+ };
86513
+ const updatedBefore = options.onBeforeUpdate?.(options);
86514
+ const updatedAfter = options.onAfterUpdate?.(options);
86515
+ return updatedBefore || updatedAfter
86456
86516
  }
86457
86517
 
86458
86518
  function _generateUid(name, i) {
@@ -86711,7 +86771,7 @@
86711
86771
  const importVar = generateUidIdentifier(exportSource.value, program);
86712
86772
  importVars.push(importVar);
86713
86773
 
86714
- for (let specifier of specifiers) {
86774
+ for (const specifier of specifiers) {
86715
86775
  const { exported, local } = specifier;
86716
86776
  const { name } = local;
86717
86777
  let localName;
@@ -86785,6 +86845,17 @@
86785
86845
  }
86786
86846
  }
86787
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
+
86788
86859
  // adding define wrapper
86789
86860
  if (hasExport && needReturnExport) {
86790
86861
  let returnStat;
@@ -86817,10 +86888,13 @@
86817
86888
 
86818
86889
  options.onAfterTransform?.({
86819
86890
  ...options,
86891
+ updated: true,
86820
86892
  program,
86821
86893
  callbackBody: body
86822
86894
  });
86823
86895
 
86896
+ return true
86897
+
86824
86898
  function addExportStatement({ exported, local }) {
86825
86899
  const asName = exported.name;
86826
86900
  if (asName !== 'default') {
@@ -86902,20 +86976,37 @@
86902
86976
  return arrayExpression(importPaths)
86903
86977
  }
86904
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
+
86905
86996
  function transformAst(program, options = {}) {
86906
86997
  const amds = detectDefinesOrRequires(program);
86907
86998
  const { length } = amds;
86908
86999
  const result = {};
86909
87000
  if (length) {
86910
87001
  result.amd = true;
86911
- if (options.resolvePath) {
86912
- for (const amd of amds) {
86913
- result.updated ||= updateAmdDeps(amd, options);
86914
- }
87002
+ for (const amd of amds) {
87003
+ const updated = options.resolvePath
87004
+ ? updateAmdDeps(amd, options)
87005
+ : callAmdUpdateHooks(amd, options);
87006
+ result.updated ||= updated;
86915
87007
  }
86916
87008
  } else {
86917
- transformEsmToAmd(program, options);
86918
- result.updated = true;
87009
+ result.updated = transformEsmToAmd(program, options);
86919
87010
  }
86920
87011
  return result
86921
87012
  }
@@ -86926,6 +87017,8 @@
86926
87017
  // Method to update paths of module dependencies, to prefix JavaScript module
86927
87018
  // name with `esm!`, above all.
86928
87019
  resolvePath = resolvePath$1,
87020
+ // Assume AMD/UMD if there're no import or export statements.
87021
+ skipIfNoImportExport,
86929
87022
  // ecmaVersion = 2020,
86930
87023
  // Do not insert `"use strict"` expression to the AMD modules. You'd set it
86931
87024
  // to `false` if your bundler inserts `"use strict"` to the outer scope.
@@ -86940,8 +87033,15 @@
86940
87033
  onBeforeUpdate,
86941
87034
  onAfterUpdate
86942
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
+
86943
87043
  // const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
86944
- let ast = parseModule(text, { next: true, loc: true });
87044
+ const ast = parseModule(text, { next: true, loc: true });
86945
87045
 
86946
87046
  const options = {
86947
87047
  sourceFileName: file,
@@ -86949,6 +87049,7 @@
86949
87049
  resolvePath,
86950
87050
  originalResolvePath: resolvePath$1,
86951
87051
  useStrict,
87052
+ skipIfNoImportExport,
86952
87053
  onBeforeTransform,
86953
87054
  onAfterTransform,
86954
87055
  onBeforeUpdate,
@@ -86993,6 +87094,8 @@
86993
87094
  onlyAmd,
86994
87095
  // List of module names not to check and transform.
86995
87096
  skipModules = [],
87097
+ // Assume AMD/UMD if there're no import or export statements.
87098
+ skipIfNoImportExport,
86996
87099
  // Method to update paths of module dependencies, to prefix JavaScript module
86997
87100
  // name with `esm!`, above all.
86998
87101
  resolvePath,
@@ -87089,6 +87192,7 @@
87089
87192
  resolvePath,
87090
87193
  /*ecmaVersion,*/
87091
87194
  useStrict,
87195
+ skipIfNoImportExport,
87092
87196
  // Always produce the source maps when transpiling in the browser, otherwise
87093
87197
  // the debugging would me impossible. When building and bundling, check if
87094
87198
  // the source maps were enabled for the output.
@@ -87133,7 +87237,7 @@
87133
87237
  // compiled with esm refer it with that name and this stub will simplify
87134
87238
  // the module loading by skipping the plugin evaluation.
87135
87239
  write.asModule(`${pluginName}!${moduleName}`,
87136
- '\ndefine([\'' + moduleName + '\'], res => res);\n');
87240
+ `\ndefine(['${moduleName}'], res => res);\n`);
87137
87241
  }
87138
87242
  //>>excludeEnd('excludeEsm')
87139
87243
  }