requirejs-esm 0.0.1 → 1.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/dist/plugin.js CHANGED
@@ -49,6 +49,23 @@
49
49
 
50
50
  var fetchText$1 = fetchText;
51
51
 
52
+ /* global require */
53
+
54
+ let writeText;
55
+
56
+ // Initialise the writeText variable with a function to write to a file.
57
+ /* istanbul ignore if */
58
+ if (!(typeof window !== 'undefined' && window.navigator && window.document)) {
59
+ const { writeFileSync, mkdirSync } = require.nodeRequire ? require.nodeRequire('fs') : require('fs');
60
+ const { dirname } = require.nodeRequire ? require.nodeRequire('path') : require('path');
61
+ writeText = (path, content) => {
62
+ mkdirSync(dirname(path), { recursive: true });
63
+ writeFileSync(path, content);
64
+ };
65
+ }
66
+
67
+ var writeText$1 = writeText;
68
+
52
69
  let skipModules$1 = [];
53
70
 
54
71
  function setSkipModules(names) {
@@ -59,8 +76,12 @@
59
76
  // the transformation.
60
77
  function skipModule(name) {
61
78
  for (const skipModule of skipModules$1) {
79
+ // Recognise an asterisk at the beginning
80
+ if (skipModule.startsWith('*')) {
81
+ if (name.indexOf(skipModule.substring(1)) >= 0) return true
82
+ }
62
83
  // Accept a path prefix as well.
63
- if (name.startsWith(skipModule)) return true
84
+ else if (name.startsWith(skipModule)) return true
64
85
  }
65
86
  }
66
87
 
@@ -85250,17 +85271,18 @@
85250
85271
  const { elements } = deps;
85251
85272
  if (!elements.length) return
85252
85273
 
85274
+ let updated;
85253
85275
  for (const element of elements) {
85254
85276
  if (element.type === 'Literal') {
85255
85277
  const moduleName = element.value;
85256
85278
  const newModuleName = resolvePath(moduleName, parentName, options);
85257
85279
  if (newModuleName && newModuleName !== moduleName) {
85258
85280
  replaceLiteral(element, newModuleName);
85281
+ updated = true;
85259
85282
  }
85260
85283
  }
85261
85284
  }
85262
-
85263
- return true
85285
+ return updated
85264
85286
  }
85265
85287
 
85266
85288
  function _generateUid(name, i) {
@@ -85669,7 +85691,6 @@
85669
85691
  // name with `esm!`, above all.
85670
85692
  resolvePath = resolvePath$1,
85671
85693
  // ecmaVersion = 2020,
85672
- // minify,
85673
85694
  sourceMap
85674
85695
  } = {}) {
85675
85696
  // const ast = parse(text, { ecmaVersion, sourceType: 'module', locations: true })
@@ -85678,48 +85699,29 @@
85678
85699
  const options = { sourceFileName: file, pluginName, resolvePath, originalResolvePath: resolvePath$1 };
85679
85700
  transformModules(ast, options);
85680
85701
 
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 });
85702
+ const { updated } = options;
85703
+ let code, map;
85704
+ if (updated) {
85705
+ if (sourceMap === true) {
85706
+ sourceMap = { inline: true, content: true };
85707
+ }
85708
+ const mapGenerator = sourceMap ? new SourceMapGenerator({ file }) : undefined;
85709
+ code = generate(ast, { sourceMap: mapGenerator });
85714
85710
  if (sourceMap) {
85715
- code += convertSourceMap.fromJSON(sourceMap).toComment();
85711
+ if (sourceMap.content) {
85712
+ mapGenerator.setSourceContent(file, text);
85713
+ }
85714
+ if (sourceMap.inline) {
85715
+ code += convertSourceMap.fromObject(mapGenerator.toJSON()).toComment();
85716
+ } else {
85717
+ map = mapGenerator.toJSON();
85718
+ }
85716
85719
  }
85717
- // }
85718
85720
  } else {
85719
85721
  code = text;
85720
85722
  }
85721
85723
 
85722
- return code
85724
+ return { code, map, updated }
85723
85725
  }
85724
85726
 
85725
85727
  /* global module */
@@ -85736,9 +85738,14 @@
85736
85738
  skipModules = [],
85737
85739
  // Method to update paths of module dependencies, to prefix JavaScript module
85738
85740
  // name with `esm!`, above all.
85739
- resolvePath
85740
- // minify
85741
- // ecmaVersion
85741
+ resolvePath,
85742
+ // ecmaVersion,
85743
+ // Boolean or object with booleans { inline, content }.
85744
+ sourceMap,
85745
+ // Enable console logging.
85746
+ verbose,
85747
+ // Directory to save a copy of the transformed modules.
85748
+ debugDir
85742
85749
  } = typeof module !== 'undefined' && module.config && module.config() || {};
85743
85750
 
85744
85751
  const buildMap = {};
@@ -85748,6 +85755,7 @@
85748
85755
  //>>excludeEnd('excludeEsm')
85749
85756
  var plugin = {
85750
85757
  load(name, req, onload, reqConfig) {
85758
+ verbose && console.log('esm: loading', name);
85751
85759
  // If the module has been already defined from a module bundle, it was
85752
85760
  // already transpiled, when the output bundle was written. No need to
85753
85761
  // re-transpile it. This can happen only during the runtime.
@@ -85763,6 +85771,7 @@
85763
85771
  // modules can be loaded just by `require` to get better performance.
85764
85772
  if (!mixedAmdAndEsm && !reqConfig.isBuild && req.specified(name) ||
85765
85773
  onlyAmd || skipModule(name)) {
85774
+ verbose && console.log('esm: delegating', name);
85766
85775
  return req([name], onload, onload.error)
85767
85776
  }
85768
85777
  //>>excludeStart('excludeEsm', pragmas.excludeEsm)
@@ -85776,26 +85785,40 @@
85776
85785
  // are external and will be loaded during the runtime.
85777
85786
  const url = req.toUrl(file);
85778
85787
  if (url.startsWith('empty:')) {
85788
+ verbose && console.log('esm: skipping', name, 'mapped to', url);
85779
85789
  return onload()
85780
85790
  }
85781
85791
 
85782
85792
  // Fetch the text of the source module by AJAX and transpile it.
85793
+ verbose && console.log('esm: fetching', name, 'mapped to', url);
85783
85794
  fetchText$1(url, async (error, text) => {
85784
85795
  if (error) {
85796
+ verbose && console.log('esm: missing', name);
85785
85797
  return onload.error(error)
85786
85798
  }
85787
85799
 
85788
- let code;
85800
+ let code, updated;
85789
85801
  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 });
85802
+ verbose && console.log('esm: transforming', name);
85803
+ ({ code, updated } = transform(text, file, {
85804
+ pluginName,
85805
+ resolvePath,
85806
+ /*ecmaVersion,*/
85807
+ // Always produce the source maps when transpiling in the browser, otherwise
85808
+ // the debugging would me impossible. When building and bundling, check if
85809
+ // the source maps were enabled for the output.
85810
+ sourceMap: sourceMap || !reqConfig.isBuild
85811
+ }));
85812
+ if (!updated) {
85813
+ verbose && console.log('esm: retaining', name);
85814
+ // return req([name], onload, onload.error)
85815
+ } else if (reqConfig.isBuild && debugDir) {
85816
+ writeText$1(`${debugDir}/${file}`, code);
85817
+ }
85795
85818
  } catch (error) {
85796
85819
  // RequireJS did not always log this error.
85797
- console.log(`Transforming "${name}" (resolved to "${url}") failed:`);
85798
- console.log(error);
85820
+ console.error(`Transforming "${name}" (resolved to "${url}") failed:`);
85821
+ console.error(error);
85799
85822
  return onload.error(error)
85800
85823
  }
85801
85824
 
@@ -85804,6 +85827,7 @@
85804
85827
  buildMap[name] = code;
85805
85828
  }
85806
85829
 
85830
+ verbose && console.log('esm: returning', name);
85807
85831
  onload.fromText(code);
85808
85832
  });
85809
85833
  },