@jsenv/js-module-fallback 1.2.1 → 1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/js-module-fallback",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,9 +31,9 @@
31
31
  "@babel/plugin-transform-modules-umd": "7.22.5",
32
32
  "@babel/plugin-transform-modules-systemjs": "7.22.5",
33
33
  "babel-plugin-transform-async-to-promises": "0.8.18",
34
- "@jsenv/ast": "5.0.1",
35
- "@jsenv/sourcemap": "1.1.0",
36
- "@jsenv/urls": "2.1.0"
34
+ "@jsenv/ast": "5.1.0",
35
+ "@jsenv/sourcemap": "1.2.0",
36
+ "@jsenv/urls": "2.2.0"
37
37
  },
38
38
  "scripts": {
39
39
  "eslint": "npx eslint . --ext=.js,.mjs,.cjs,.html"
@@ -21,7 +21,41 @@ export const convertJsModuleToJsClassic = async ({
21
21
  inputUrl,
22
22
  outputUrl,
23
23
  outputFormat = "system", // "systemjs" or "umd"
24
+ preferAbsoluteSpecifiers,
25
+ remapImportSpecifier = (specifier) => specifier,
24
26
  }) => {
27
+ /*
28
+ * When systemjs or umd format is used by babel, it will generated UID based on
29
+ * the import specifier:
30
+ * https://github.com/babel/babel/blob/97d1967826077f15e766778c0d64711399e9a72a/packages/babel-plugin-transform-modules-systemjs/src/index.ts#L498
31
+ * But at this stage import specifier are absolute file urls
32
+ * This can be mitigated by minification that will rename them.
33
+ * But to fix this issue once and for all there is babelPluginRelativeImports below
34
+ */
35
+ const transformImportSpecifier = (specifier) => {
36
+ specifier = remapImportSpecifier(specifier);
37
+ if (!specifier.startsWith("file://")) {
38
+ return null;
39
+ }
40
+ const specifierUrlObject = new URL(specifier);
41
+ const { searchParams } = specifierUrlObject;
42
+ searchParams.delete("dynamic_import");
43
+ const specifierWithoutDynamicImportParam = specifierUrlObject.href;
44
+ if (preferAbsoluteSpecifiers) {
45
+ return specifierWithoutDynamicImportParam;
46
+ }
47
+ const specifierRelative = urlToRelativeUrl(specifier, outputUrl);
48
+ if (specifierRelative.startsWith("file://")) {
49
+ return specifierRelative;
50
+ }
51
+ if (specifierRelative[0] === ".") {
52
+ return specifierRelative;
53
+ }
54
+ // ensure relative specifier starts with "." so they are not detected as bare specifier
55
+ // that would trigger node module resolution or importmap
56
+ return `./${specifierRelative}`;
57
+ };
58
+
25
59
  const { code, map } = await applyBabelPlugins({
26
60
  babelPlugins: [
27
61
  ...(outputFormat === "system"
@@ -29,7 +63,10 @@ export const convertJsModuleToJsClassic = async ({
29
63
  // proposal-dynamic-import required with systemjs for babel8:
30
64
  // https://github.com/babel/babel/issues/10746
31
65
  require("@babel/plugin-proposal-dynamic-import"),
32
- [babelPluginRelativeImports, { rootUrl: outputUrl }],
66
+ [
67
+ babelPluginTransformImportSpecifiers,
68
+ { transformImportSpecifier },
69
+ ],
33
70
  require("@babel/plugin-transform-modules-systemjs"),
34
71
  [
35
72
  customAsyncToPromises,
@@ -49,7 +86,10 @@ export const convertJsModuleToJsClassic = async ({
49
86
  ],
50
87
  babelPluginTransformImportMetaUrl,
51
88
  babelPluginTransformImportMetaResolve,
52
- [babelPluginRelativeImports, { rootUrl: outputUrl }],
89
+ [
90
+ babelPluginTransformImportSpecifiers,
91
+ { transformImportSpecifier },
92
+ ],
53
93
  require("@babel/plugin-transform-modules-umd"),
54
94
  ]),
55
95
  ],
@@ -65,39 +105,19 @@ export const convertJsModuleToJsClassic = async ({
65
105
  };
66
106
  };
67
107
 
68
- /*
69
- * When systemjs or umd format is used by babel, it will generated UID based on
70
- * the import specifier:
71
- * https://github.com/babel/babel/blob/97d1967826077f15e766778c0d64711399e9a72a/packages/babel-plugin-transform-modules-systemjs/src/index.ts#L498
72
- * But at this stage import specifier are absolute file urls
73
- * This can be mitigated by minification that will rename them.
74
- * But to fix this issue once and for all there is babelPluginRelativeImports below
75
- */
76
- const babelPluginRelativeImports = (babel) => {
108
+ const babelPluginTransformImportSpecifiers = (babel) => {
77
109
  const t = babel.types;
78
110
 
79
111
  const replaceSpecifierAtPath = (path, state) => {
80
- let specifier = path.node.value;
81
- if (specifier.startsWith("file://")) {
82
- const specifierUrlObject = new URL(specifier);
83
- const { searchParams } = specifierUrlObject;
84
- searchParams.delete("dynamic_import");
85
- specifier = specifierUrlObject.href;
86
- const rootUrl = state.opts.rootUrl;
87
- let specifierRelative = urlToRelativeUrl(specifier, rootUrl);
88
- if (
89
- !specifierRelative.startsWith("file://") &&
90
- specifierRelative[0] !== "."
91
- ) {
92
- // so avoid applying node module resolution or importmap
93
- specifierRelative = `./${specifierRelative}`;
94
- }
95
- path.replaceWith(t.stringLiteral(specifierRelative));
112
+ const specifier = path.node.value;
113
+ const specifierTransformed = state.opts.transformImportSpecifier(specifier);
114
+ if (specifierTransformed && specifierTransformed !== specifier) {
115
+ path.replaceWith(t.stringLiteral(specifierTransformed));
96
116
  }
97
117
  };
98
118
 
99
119
  return {
100
- name: "relative-imports",
120
+ name: "transform-import-specifiers",
101
121
  visitor: {
102
122
  CallExpression: (path, state) => {
103
123
  if (path.node.callee.type !== "Import") {