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/dist/plugin.js CHANGED
@@ -1,10 +1,12 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
2
  typeof define === 'function' && define.amd ? define([
4
3
  //>>excludeStart('excludeEsm', pragmas.excludeEsm)
5
4
  'module'
6
5
  //>>excludeEnd('excludeEsm')
7
6
  ], factory) :
7
+ //>>excludeStart('excludeEsm', pragmas.excludeEsm)
8
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
9
+ //>>excludeEnd('excludeEsm')
8
10
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.requirejsEsmPlugin = factory());
9
11
  })(this, (function (
10
12
  //>>excludeStart('excludeEsm', pragmas.excludeEsm)
@@ -49,19 +51,35 @@
49
51
 
50
52
  var fetchText$1 = fetchText;
51
53
 
52
- let skipModules$1 = [];
54
+ /* global require */
53
55
 
54
- function setSkipModules(names) {
55
- skipModules$1 = names;
56
+ let writeText;
57
+
58
+ // Initialise the writeText variable with a function to write to a file.
59
+ /* istanbul ignore if */
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');
63
+ writeText = (path, content) => {
64
+ mkdirSync(dirname(path), { recursive: true });
65
+ writeFileSync(path, content);
66
+ };
56
67
  }
57
68
 
58
- // Checks if a module name is included in the list of modules to skip
59
- // the transformation.
60
- function skipModule(name) {
61
- for (const skipModule of skipModules$1) {
62
- // Accept a path prefix as well.
63
- if (name.startsWith(skipModule)) return true
69
+ var writeText$1 = writeText;
70
+
71
+ // Returns the child file name by cutting the directoris before the last slash,
72
+ // including the slash. If there is no slash in the path - the path is just
73
+ // a file name, it will return the file name. If the path includes a URL
74
+ // query starting with the question mark, it will cut it away including the
75
+ // question mark.
76
+ function childFile (path) {
77
+ const lastSlash = path.lastIndexOf('/');
78
+ if (lastSlash > 0) {
79
+ path = path.substring(lastSlash + 1);
64
80
  }
81
+ const questionMark = path.lastIndexOf('?');
82
+ return questionMark > 0 ? path.substring(0, questionMark) : path
65
83
  }
66
84
 
67
85
  // Returns the parent directory by cutting the file name after the last slash,
@@ -128,6 +146,25 @@
128
146
  return `${pluginName}!${sourcePath}`
129
147
  }
130
148
 
149
+ let skipModules$1 = [];
150
+
151
+ function setSkipModules(names) {
152
+ skipModules$1 = names;
153
+ }
154
+
155
+ // Checks if a module name is included in the list of modules to skip
156
+ // the transformation.
157
+ function skipModule(name) {
158
+ for (const skipModule of skipModules$1) {
159
+ // Recognise an asterisk at the beginning
160
+ if (skipModule.startsWith('*')) {
161
+ if (name.indexOf(skipModule.substring(1)) >= 0) return true
162
+ }
163
+ // Accept a path prefix as well.
164
+ else if (name.startsWith(skipModule)) return true
165
+ }
166
+ }
167
+
131
168
  const errorMessages = {
132
169
  [0]: 'Unexpected token',
133
170
  [28]: "Unexpected token: '%0'",
@@ -1002,9 +1039,9 @@
1002
1039
  mask |= 8;
1003
1040
  break;
1004
1041
  case 115:
1005
- if (mask & 12)
1042
+ if (mask & 32)
1006
1043
  report(parser, 34, 's');
1007
- mask |= 12;
1044
+ mask |= 32;
1008
1045
  break;
1009
1046
  default:
1010
1047
  report(parser, 33);
@@ -1917,7 +1954,7 @@
1917
1954
  }
1918
1955
  else if (ch === 61) {
1919
1956
  advanceChar(parser);
1920
- return 8456000;
1957
+ return 8456256;
1921
1958
  }
1922
1959
  if (ch === 33) {
1923
1960
  const index = parser.index + 1;
@@ -2100,7 +2137,7 @@
2100
2137
  const ch = parser.currentChar;
2101
2138
  if (ch === 61) {
2102
2139
  advanceChar(parser);
2103
- return 8456001;
2140
+ return 8456257;
2104
2141
  }
2105
2142
  if (ch !== 62)
2106
2143
  return 8456259;
@@ -5092,7 +5129,7 @@
5092
5129
  nextToken(parser, context | 32768);
5093
5130
  const argument = parser.flags & 1 || parser.token & 1048576
5094
5131
  ? null
5095
- : parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.line, parser.column);
5132
+ : parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.linePos, parser.colPos);
5096
5133
  matchOrInsertSemicolon(parser, context | 32768);
5097
5134
  return finishNode(parser, context, start, line, column, {
5098
5135
  type: 'ReturnStatement',
@@ -6208,7 +6245,7 @@
6208
6245
  if ((context & 524288) < 1)
6209
6246
  report(parser, 26);
6210
6247
  if (context & 16384)
6211
- report(parser, 143);
6248
+ report(parser, 27);
6212
6249
  parser.assignable = 2;
6213
6250
  break;
6214
6251
  }
@@ -6217,7 +6254,7 @@
6217
6254
  if ((context & 262144) < 1)
6218
6255
  report(parser, 27);
6219
6256
  if (context & 16384)
6220
- report(parser, 143);
6257
+ report(parser, 27);
6221
6258
  parser.assignable = 1;
6222
6259
  break;
6223
6260
  }
@@ -6248,10 +6285,10 @@
6248
6285
  expr = parseUpdateExpression(parser, context, expr, start, line, column);
6249
6286
  }
6250
6287
  else if ((parser.token & 67108864) === 67108864) {
6251
- context = (context | 134217728 | 8192) ^ (134217728 | 8192);
6288
+ context = (context | 134217728) ^ 134217728;
6252
6289
  switch (parser.token) {
6253
6290
  case 67108877: {
6254
- nextToken(parser, context | 1073741824);
6291
+ nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
6255
6292
  parser.assignable = 1;
6256
6293
  const property = parsePropertyOrPrivatePropertyName(parser, context);
6257
6294
  expr = finishNode(parser, context, start, line, column, {
@@ -6307,7 +6344,7 @@
6307
6344
  break;
6308
6345
  }
6309
6346
  case 67108991: {
6310
- nextToken(parser, context);
6347
+ nextToken(parser, (context | 1073741824 | 8192) ^ 8192);
6311
6348
  parser.flags |= 2048;
6312
6349
  parser.assignable = 2;
6313
6350
  expr = parseOptionalChain(parser, context, expr, start, line, column);
@@ -8471,12 +8508,10 @@
8471
8508
  }
8472
8509
  else if (context & 1 && parser.token === 131) {
8473
8510
  kind |= 4096;
8474
- key = parsePrivateIdentifier(parser, context, tokenPos, linePos, colPos);
8475
- context = context | 16384;
8511
+ key = parsePrivateIdentifier(parser, context | 16384, tokenPos, linePos, colPos);
8476
8512
  }
8477
8513
  else if (context & 1 && (parser.token & 1073741824) === 1073741824) {
8478
8514
  kind |= 128;
8479
- context = context | 16384;
8480
8515
  }
8481
8516
  else if (token === 122) {
8482
8517
  key = parseIdentifier(parser, context, 0);
@@ -8842,7 +8877,7 @@
8842
8877
  });
8843
8878
  }
8844
8879
  function parseJSXExpressionContainer(parser, context, inJSXChild, isAttr, start, line, column) {
8845
- nextToken(parser, context);
8880
+ nextToken(parser, context | 32768);
8846
8881
  const { tokenPos, linePos, colPos } = parser;
8847
8882
  if (parser.token === 14)
8848
8883
  return parseJSXSpreadChild(parser, context, tokenPos, linePos, colPos);
@@ -81744,6 +81779,9 @@
81744
81779
  });
81745
81780
 
81746
81781
  obj[impl][utils$1.wrapperSymbol] = obj;
81782
+ if (Impl.init) {
81783
+ Impl.init(obj[impl], privateData);
81784
+ }
81747
81785
  return obj;
81748
81786
  },
81749
81787
  interface: URLSearchParams,
@@ -82295,6 +82333,9 @@
82295
82333
  });
82296
82334
 
82297
82335
  obj[impl][utils$1.wrapperSymbol] = obj;
82336
+ if (Impl.init) {
82337
+ Impl.init(obj[impl], privateData);
82338
+ }
82298
82339
  return obj;
82299
82340
  },
82300
82341
  interface: URL,
@@ -85250,17 +85291,18 @@
85250
85291
  const { elements } = deps;
85251
85292
  if (!elements.length) return
85252
85293
 
85294
+ let updated;
85253
85295
  for (const element of elements) {
85254
85296
  if (element.type === 'Literal') {
85255
85297
  const moduleName = element.value;
85256
85298
  const newModuleName = resolvePath(moduleName, parentName, options);
85257
85299
  if (newModuleName && newModuleName !== moduleName) {
85258
85300
  replaceLiteral(element, newModuleName);
85301
+ updated = true;
85259
85302
  }
85260
85303
  }
85261
85304
  }
85262
-
85263
- return true
85305
+ return updated
85264
85306
  }
85265
85307
 
85266
85308
  function _generateUid(name, i) {
@@ -85669,7 +85711,6 @@
85669
85711
  // name with `esm!`, above all.
85670
85712
  resolvePath = resolvePath$1,
85671
85713
  // ecmaVersion = 2020,
85672
- // minify,
85673
85714
  sourceMap
85674
85715
  } = {}) {
85675
85716
  // const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
@@ -85678,48 +85719,29 @@
85678
85719
  const options = { sourceFileName: file, pluginName, resolvePath, originalResolvePath: resolvePath$1 };
85679
85720
  transformModules(ast, options);
85680
85721
 
85681
- let code;
85682
- if (options.updated) {
85683
- // if (minify) {
85684
- // const optimized = optimize(ast)
85685
- // ast = mangle(optimized)
85686
- // }
85687
- // if (minify) {
85688
- // ({ code, map: sourceMap } = generateMinified(ast, {
85689
- // format: FORMAT_MINIFY,
85690
- // sourceMap: sourceMap ? file : undefined,
85691
- // sourceMapWithCode: true,
85692
- // sourceContent: text,
85693
- // file
85694
- // }))
85695
- // if (sourceMap) {
85696
- // code += convert.fromJSON(sourceMap).toComment()
85697
- // }
85698
- // }
85699
- // if (minify) {
85700
- // ({ code } = await generateMinified(ast, {
85701
- // // ecma: ecmaVersion,
85702
- // module: true,
85703
- // parse: { spidermonkey: true },
85704
- // format: { ecma: 2015 },
85705
- // sourceMap: sourceMap ? {
85706
- // filename: file,
85707
- // url: 'inline'
85708
- // // content: map
85709
- // } : undefined
85710
- // }))
85711
- // } else {
85712
- sourceMap = sourceMap && new SourceMapGenerator({ file });
85713
- code = generate(ast, { sourceMap });
85722
+ const { updated } = options;
85723
+ let code, map;
85724
+ if (updated) {
85725
+ if (sourceMap === true) {
85726
+ sourceMap = { inline: true, content: true };
85727
+ }
85728
+ const mapGenerator = sourceMap ? new SourceMapGenerator({ file }) : undefined;
85729
+ code = generate(ast, { sourceMap: mapGenerator });
85714
85730
  if (sourceMap) {
85715
- code += convertSourceMap.fromJSON(sourceMap).toComment();
85731
+ if (sourceMap.content) {
85732
+ mapGenerator.setSourceContent(file, text);
85733
+ }
85734
+ if (sourceMap.inline) {
85735
+ code += convertSourceMap.fromObject(mapGenerator.toJSON()).toComment();
85736
+ } else {
85737
+ map = mapGenerator.toJSON();
85738
+ }
85716
85739
  }
85717
- // }
85718
85740
  } else {
85719
85741
  code = text;
85720
85742
  }
85721
85743
 
85722
- return code
85744
+ return { code, map, updated }
85723
85745
  }
85724
85746
 
85725
85747
  /* global module */
@@ -85736,9 +85758,14 @@
85736
85758
  skipModules = [],
85737
85759
  // Method to update paths of module dependencies, to prefix JavaScript module
85738
85760
  // name with `esm!`, above all.
85739
- resolvePath
85740
- // minify
85741
- // ecmaVersion
85761
+ resolvePath,
85762
+ // ecmaVersion,
85763
+ // Boolean or object with booleans { inline, content }.
85764
+ sourceMap,
85765
+ // Enable console logging.
85766
+ verbose,
85767
+ // Directory to save a copy of the transformed modules.
85768
+ debugDir
85742
85769
  } = typeof module !== 'undefined' && module.config && module.config() || {};
85743
85770
 
85744
85771
  const buildMap = {};
@@ -85748,6 +85775,35 @@
85748
85775
  //>>excludeEnd('excludeEsm')
85749
85776
  var plugin = {
85750
85777
  load(name, req, onload, reqConfig) {
85778
+ //>>excludeStart('excludeEsm', pragmas.excludeEsm)
85779
+ const { isBuild } = reqConfig;
85780
+ // if (!bundledModules) {
85781
+ // bundledModules = new Set()
85782
+ // const { bundles } = reqConfig
85783
+ // if (bundles) {
85784
+ // verbose && console.log('esm: initialising bundles', name)
85785
+ // for (const bundle in bundles) {
85786
+ // for (const module of bundles[bundle]) {
85787
+ // bundledModules.add(module)
85788
+ // }
85789
+ // }
85790
+ // }
85791
+ // }
85792
+
85793
+ // Paths relative to the current directory include the file extension.
85794
+ // Otherwise the file extension must not be used for JavaScript modules.
85795
+ // The file name should include the orioginal extension in the source maps.
85796
+ const file = name.endsWith(fileExtension) ? name : name + fileExtension;
85797
+ // Compilation and bundling of module sub-trees can be skipped, if those
85798
+ // are mapped to the pseudo-path `empty:`, meaning that those modules
85799
+ // are external and will be loaded during the runtime.
85800
+ const url = req.toUrl(file);
85801
+ if (url.startsWith('empty:')) {
85802
+ verbose && console.log('esm: skipping', name, 'mapped to', url);
85803
+ return onload()
85804
+ }
85805
+
85806
+ verbose && console.log('esm: loading', name);
85751
85807
  // If the module has been already defined from a module bundle, it was
85752
85808
  // already transpiled, when the output bundle was written. No need to
85753
85809
  // re-transpile it. This can happen only during the runtime.
@@ -85761,49 +85817,55 @@
85761
85817
  // If the whole application is transpiled, there is no need to transpile
85762
85818
  // ESM modules or prefix dependencies of AMD modules. Even not yet defined
85763
85819
  // modules can be loaded just by `require` to get better performance.
85764
- if (!mixedAmdAndEsm && !reqConfig.isBuild && req.specified(name) ||
85765
- onlyAmd || skipModule(name)) {
85820
+ if (!mixedAmdAndEsm && !isBuild && req.specified(name) ||
85821
+ /*bundledModules.has(name) ||*/ onlyAmd || skipModule(name) ||
85822
+ // If the module was bundled, it had to be already transpiled.
85823
+ !isBuild && childFile(url) !== childFile(file)) {
85824
+ verbose && console.log('esm: delegating', name);
85825
+ //>>excludeEnd('excludeEsm')
85766
85826
  return req([name], onload, onload.error)
85767
- }
85768
85827
  //>>excludeStart('excludeEsm', pragmas.excludeEsm)
85769
-
85770
- // Paths relative to the current directory include the file extension.
85771
- // Otherwise the file extension must not be used for JavaScript modules.
85772
- // The file name should include the orioginal extension in the source maps.
85773
- const file = name.endsWith(fileExtension) ? name : name + fileExtension;
85774
- // Compilation and bundling of module sub-trees can be skipped, if those
85775
- // are mapped to the pseudo-path `empty:`, meaning that those modules
85776
- // are external and will be loaded during the runtime.
85777
- const url = req.toUrl(file);
85778
- if (url.startsWith('empty:')) {
85779
- return onload()
85780
85828
  }
85781
85829
 
85782
85830
  // Fetch the text of the source module by AJAX and transpile it.
85831
+ verbose && console.log('esm: fetching', name, 'mapped to', url);
85783
85832
  fetchText$1(url, async (error, text) => {
85784
85833
  if (error) {
85834
+ verbose && console.log('esm: missing', name);
85785
85835
  return onload.error(error)
85786
85836
  }
85787
85837
 
85788
- let code;
85838
+ let code, updated;
85789
85839
  try {
85790
- // Always produce the source maps when transpiling in the browser, otherwise
85791
- // the debugging would me impossible. When building and bundling, check if
85792
- // the source maps were enabled for the output.
85793
- const sourceMap = !reqConfig.isBuild || reqConfig.generateSourceMaps;
85794
- code = transform(text, file, { pluginName, resolvePath, /*ecmaVersion, minify,*/ sourceMap });
85840
+ verbose && console.log('esm: transforming', name);
85841
+ ({ code, updated } = transform(text, file, {
85842
+ pluginName,
85843
+ resolvePath,
85844
+ /*ecmaVersion,*/
85845
+ // Always produce the source maps when transpiling in the browser, otherwise
85846
+ // the debugging would me impossible. When building and bundling, check if
85847
+ // the source maps were enabled for the output.
85848
+ sourceMap: sourceMap || !isBuild
85849
+ }));
85850
+ if (!updated) {
85851
+ verbose && console.log('esm: retaining', name);
85852
+ // return req([name], onload, onload.error)
85853
+ } else if (isBuild && debugDir) {
85854
+ writeText$1(`${debugDir}/${file}`, code);
85855
+ }
85795
85856
  } catch (error) {
85796
85857
  // RequireJS did not always log this error.
85797
- console.log(`Transforming "${name}" (resolved to "${url}") failed:`);
85798
- console.log(error);
85858
+ console.error(`Transforming "${name}" (resolved to "${url}") failed:`);
85859
+ console.error(error);
85799
85860
  return onload.error(error)
85800
85861
  }
85801
85862
 
85802
85863
  // Remember the transpiled content for the writing phase during the build.
85803
- if (reqConfig.isBuild) {
85864
+ if (isBuild) {
85804
85865
  buildMap[name] = code;
85805
85866
  }
85806
85867
 
85868
+ verbose && console.log('esm: returning', name);
85807
85869
  onload.fromText(code);
85808
85870
  });
85809
85871
  },