@jsenv/plugin-transpilation 1.0.0 → 1.1.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.
Files changed (21) hide show
  1. package/package.json +26 -26
  2. package/src/as_js_module/jsenv_plugin_as_js_module.js +4 -1
  3. package/src/babel/babel_helper_directory/README.md +1 -1
  4. package/src/babel/babel_helper_directory/babel_helpers/applyDecs2301/applyDecs2301.js +547 -528
  5. package/src/babel/babel_helper_directory/babel_helpers/applyDecs2305/applyDecs2305.js +681 -0
  6. package/src/babel/jsenv_plugin_babel.js +89 -17
  7. package/src/babel/new_stylesheet/babel_plugin_new_stylesheet_injector.js +11 -135
  8. package/src/babel/new_stylesheet/constructable_stylesheet_usage.js +114 -0
  9. package/src/babel/new_stylesheet/new_stylesheet_client_file_url.js +4 -0
  10. package/src/babel/regenerator_runtime/babel_plugin_regenerator_runtime_injector.js +11 -35
  11. package/src/babel/regenerator_runtime/regenerator_runtime_client_file_url.js +4 -0
  12. package/src/babel/regenerator_runtime/regenerator_runtime_usage.js +23 -0
  13. package/src/babel/{polyfill_injection_in_babel_ast.js → side_effect_injection_in_babel_ast.js} +8 -8
  14. package/src/import_assertions/jsenv_plugin_import_assertions.js +3 -3
  15. package/src/js_module_fallback/jsenv_plugin_js_module_conversion.js +15 -7
  16. package/src/js_module_fallback/jsenv_plugin_js_module_fallback.js +2 -11
  17. package/src/js_module_fallback/jsenv_plugin_js_module_fallback_inside_html.js +21 -98
  18. package/src/babel/apply_js_transpilation.js +0 -74
  19. package/src/babel/global_this/babel_plugin_global_this_injector.js +0 -56
  20. package/src/babel/global_this/client/global_this_js_classic.js +0 -25
  21. package/src/babel/global_this/client/global_this_js_module.js +0 -21
@@ -5,7 +5,7 @@
5
5
  * - <link rel="modulepreload"> are converted to <link rel="preload">
6
6
  */
7
7
 
8
- import { readFileSync } from "node:fs";
8
+ import { injectQueryParams } from "@jsenv/urls";
9
9
  import {
10
10
  parseHtmlString,
11
11
  visitHtmlNodes,
@@ -13,16 +13,9 @@ import {
13
13
  getHtmlNodeAttribute,
14
14
  setHtmlNodeAttributes,
15
15
  analyzeScriptNode,
16
- injectHtmlNodeAsEarlyAsPossible,
17
- createHtmlNode,
18
16
  } from "@jsenv/ast";
19
- import { injectQueryParams, urlToRelativeUrl } from "@jsenv/urls";
20
- import { SOURCEMAP } from "@jsenv/sourcemap";
21
17
 
22
- export const jsenvPluginJsModuleFallbackInsideHtml = ({
23
- systemJsInjection,
24
- systemJsClientFileUrl,
25
- }) => {
18
+ export const jsenvPluginJsModuleFallbackInsideHtml = () => {
26
19
  const turnIntoJsClassicProxy = (reference) => {
27
20
  return injectQueryParams(reference.url, { js_module_fallback: "" });
28
21
  };
@@ -86,13 +79,10 @@ export const jsenvPluginJsModuleFallbackInsideHtml = ({
86
79
  ref.type === "link_href" &&
87
80
  ref.subtype === rel,
88
81
  );
89
- if (!isOrWasExpectingJsModule(reference)) {
82
+ if (!wasOrWillBeConvertedToJsClassic(reference)) {
90
83
  return;
91
84
  }
92
- if (
93
- rel === "modulepreload" &&
94
- reference.expectedType === "js_classic"
95
- ) {
85
+ if (rel === "modulepreload") {
96
86
  mutations.push(() => {
97
87
  setHtmlNodeAttributes(node, {
98
88
  rel: "preload",
@@ -101,7 +91,7 @@ export const jsenvPluginJsModuleFallbackInsideHtml = ({
101
91
  });
102
92
  });
103
93
  }
104
- if (rel === "preload" && reference.expectedType === "js_classic") {
94
+ if (rel === "preload") {
105
95
  mutations.push(() => {
106
96
  setHtmlNodeAttributes(node, { crossorigin: undefined });
107
97
  });
@@ -135,83 +125,6 @@ export const jsenvPluginJsModuleFallbackInsideHtml = ({
135
125
  }
136
126
  },
137
127
  });
138
- if (systemJsInjection) {
139
- let needsSystemJs = false;
140
- for (const reference of urlInfo.references) {
141
- if (reference.isResourceHint) {
142
- // we don't cook resource hints
143
- // because they might refer to resource that will be modified during build
144
- // It also means something else HAVE to reference that url in order to cook it
145
- // so that the preload is deleted by "resync_resource_hints.js" otherwise
146
- continue;
147
- }
148
- if (isOrWasExpectingJsModule(reference)) {
149
- const dependencyUrlInfo = context.urlGraph.getUrlInfo(
150
- reference.url,
151
- );
152
- try {
153
- await context.cook(dependencyUrlInfo, { reference });
154
- if (dependencyUrlInfo.data.jsClassicFormat === "system") {
155
- needsSystemJs = true;
156
- break;
157
- }
158
- } catch (e) {
159
- if (context.dev && e.code !== "PARSE_ERROR") {
160
- needsSystemJs = true;
161
- // ignore cooking error, the browser will trigger it again on fetch
162
- // + disable cache for this html file because when browser will reload
163
- // the error might be gone and we might need to inject systemjs
164
- urlInfo.headers["cache-control"] = "no-store";
165
- } else {
166
- throw e;
167
- }
168
- }
169
- }
170
- }
171
- if (needsSystemJs) {
172
- mutations.push(async () => {
173
- let systemJsFileContent = readFileSync(
174
- new URL(systemJsClientFileUrl),
175
- { encoding: "utf8" },
176
- );
177
- const sourcemapFound = SOURCEMAP.readComment({
178
- contentType: "text/javascript",
179
- content: systemJsFileContent,
180
- });
181
- if (sourcemapFound) {
182
- const sourcemapFileUrl = new URL(
183
- sourcemapFound.specifier,
184
- systemJsClientFileUrl,
185
- );
186
- systemJsFileContent = SOURCEMAP.writeComment({
187
- contentType: "text/javascript",
188
- content: systemJsFileContent,
189
- specifier: urlToRelativeUrl(sourcemapFileUrl, urlInfo.url),
190
- });
191
- }
192
- const [systemJsReference, systemJsUrlInfo] =
193
- context.referenceUtils.inject({
194
- type: "script",
195
- expectedType: "js_classic",
196
- isInline: true,
197
- contentType: "text/javascript",
198
- content: systemJsFileContent,
199
- specifier: "s.js",
200
- });
201
- await context.cook(systemJsUrlInfo, {
202
- reference: systemJsReference,
203
- });
204
- injectHtmlNodeAsEarlyAsPossible(
205
- htmlAst,
206
- createHtmlNode({
207
- tagName: "script",
208
- textContent: systemJsUrlInfo.content,
209
- }),
210
- "jsenv:js_module_fallback",
211
- );
212
- });
213
- }
214
- }
215
128
  await Promise.all(mutations.map((mutation) => mutation()));
216
129
  return stringifyHtmlAst(htmlAst, {
217
130
  cleanupPositionAttributes: context.dev,
@@ -221,19 +134,29 @@ export const jsenvPluginJsModuleFallbackInsideHtml = ({
221
134
  };
222
135
  };
223
136
 
224
- const isOrWasExpectingJsModule = (reference) => {
225
- if (isExpectingJsModule(reference)) {
226
- return true;
137
+ const wasOrWillBeConvertedToJsClassic = (reference) => {
138
+ if (reference.expectedType !== "js_module") {
139
+ return false;
227
140
  }
228
- if (reference.original && isExpectingJsModule(reference.original)) {
141
+ if (willBeConvertedToJsClassic(reference)) {
229
142
  return true;
230
143
  }
144
+ let prev = reference.prev;
145
+ while (prev) {
146
+ if (prev.expectedType === "js_classic") {
147
+ return true;
148
+ }
149
+ if (willBeConvertedToJsClassic(prev)) {
150
+ return true;
151
+ }
152
+ prev = prev.prev;
153
+ }
154
+
231
155
  return false;
232
156
  };
233
157
 
234
- const isExpectingJsModule = (reference) => {
158
+ const willBeConvertedToJsClassic = (reference) => {
235
159
  return (
236
- reference.expectedType === "js_module" ||
237
160
  reference.searchParams.has("js_module_fallback") ||
238
161
  reference.searchParams.has("as_js_classic")
239
162
  );
@@ -1,74 +0,0 @@
1
- import { applyBabelPlugins } from "@jsenv/ast";
2
- import { RUNTIME_COMPAT } from "@jsenv/runtime-compat";
3
-
4
- import { getBaseBabelPluginStructure } from "./babel_plugin_structure.js";
5
- import { babelPluginBabelHelpersAsJsenvImports } from "./babel_plugin_babel_helpers_as_jsenv_imports.js";
6
- import { babelPluginNewStylesheetInjector } from "./new_stylesheet/babel_plugin_new_stylesheet_injector.js";
7
- import { babelPluginGlobalThisInjector } from "./global_this/babel_plugin_global_this_injector.js";
8
- import { babelPluginRegeneratorRuntimeInjector } from "./regenerator_runtime/babel_plugin_regenerator_runtime_injector.js";
9
-
10
- export const applyJsTranspilation = async ({
11
- input,
12
- inputIsJsModule = false,
13
- inputUrl,
14
- outputUrl,
15
- runtimeCompat,
16
- babelHelpersAsImport = true,
17
- babelOptions,
18
- getImportSpecifier,
19
- }) => {
20
- const isSupported = (feature) => {
21
- return RUNTIME_COMPAT.isSupported(runtimeCompat, feature);
22
- };
23
- const babelPluginStructure = getBaseBabelPluginStructure({
24
- url: inputUrl,
25
- isSupported,
26
- isJsModule: inputIsJsModule,
27
- getImportSpecifier,
28
- });
29
-
30
- if (!isSupported("global_this")) {
31
- babelPluginStructure["global-this-injector"] = [
32
- babelPluginGlobalThisInjector,
33
- { babelHelpersAsImport, getImportSpecifier },
34
- ];
35
- }
36
- if (!isSupported("async_generator_function")) {
37
- babelPluginStructure["regenerator-runtime-injector"] = [
38
- babelPluginRegeneratorRuntimeInjector,
39
- { babelHelpersAsImport, getImportSpecifier },
40
- ];
41
- }
42
- if (!isSupported("new_stylesheet")) {
43
- babelPluginStructure["new-stylesheet-injector"] = [
44
- babelPluginNewStylesheetInjector,
45
- { babelHelpersAsImport, getImportSpecifier },
46
- ];
47
- }
48
- if (
49
- inputIsJsModule &&
50
- babelHelpersAsImport &&
51
- Object.keys(babelPluginStructure).length > 0
52
- ) {
53
- babelPluginStructure["babel-helper-as-jsenv-import"] = [
54
- babelPluginBabelHelpersAsJsenvImports,
55
- { getImportSpecifier },
56
- ];
57
- }
58
-
59
- const babelPlugins = Object.keys(babelPluginStructure).map(
60
- (babelPluginName) => babelPluginStructure[babelPluginName],
61
- );
62
- const { code, map } = await applyBabelPlugins({
63
- babelPlugins,
64
- options: babelOptions,
65
- input,
66
- inputIsJsModule,
67
- inputUrl,
68
- outputUrl,
69
- });
70
- return {
71
- content: code,
72
- sourcemap: map,
73
- };
74
- };
@@ -1,56 +0,0 @@
1
- import { pathToFileURL } from "node:url";
2
- import { injectPolyfillIntoBabelAst } from "../polyfill_injection_in_babel_ast.js";
3
-
4
- const globalThisJsModuleClientFileUrl = new URL(
5
- "./client/global_this_js_module.js",
6
- import.meta.url,
7
- ).href;
8
- const globalThisJsClassicClientFileUrl = new URL(
9
- "./client/global_this_js_classic.js?js_classic",
10
- import.meta.url,
11
- ).href;
12
-
13
- export const babelPluginGlobalThisInjector = (
14
- babel,
15
- { babelHelpersAsImport, getImportSpecifier },
16
- ) => {
17
- return {
18
- name: "global-this-injector",
19
- visitor: {
20
- Program: {
21
- enter: (path, state) => {
22
- let globalThisDetected = false;
23
- const { filename } = state;
24
- const fileUrl = pathToFileURL(filename).href;
25
- if (fileUrl === globalThisJsModuleClientFileUrl) {
26
- return;
27
- }
28
- path.traverse({
29
- Identifier(path) {
30
- const { node } = path;
31
- if (node.name === "globalThis") {
32
- globalThisDetected = true;
33
- path.stop();
34
- }
35
- },
36
- });
37
- state.file.metadata.globalThisDetected = globalThisDetected;
38
- if (globalThisDetected) {
39
- const { sourceType } = state.file.opts.parserOpts;
40
- const isJsModule = sourceType === "module";
41
- injectPolyfillIntoBabelAst({
42
- programPath: path,
43
- isJsModule,
44
- asImport: babelHelpersAsImport,
45
- polyfillFileUrl: isJsModule
46
- ? globalThisJsModuleClientFileUrl
47
- : globalThisJsClassicClientFileUrl,
48
- getPolyfillImportSpecifier: getImportSpecifier,
49
- babel,
50
- });
51
- }
52
- },
53
- },
54
- },
55
- };
56
- };
@@ -1,25 +0,0 @@
1
- // https://mathiasbynens.be/notes/globalthis
2
-
3
- /* eslint-disable no-redeclare */
4
- /* global globalThis */
5
- let globalObject;
6
- if (typeof globalThis === "object") {
7
- globalObject = globalThis;
8
- } else {
9
- if (typeof this === "object") {
10
- globalObject = this;
11
- } else {
12
- // eslint-disable-next-line no-extend-native
13
- Object.defineProperty(Object.prototype, "__global__", {
14
- get() {
15
- return this;
16
- },
17
- configurable: true,
18
- });
19
- // eslint-disable-next-line no-undef
20
- globalObject = __global__;
21
- delete Object.prototype.__global__;
22
- }
23
-
24
- globalObject.globalThis = globalObject;
25
- }
@@ -1,21 +0,0 @@
1
- // https://mathiasbynens.be/notes/globalthis
2
-
3
- /* eslint-disable no-redeclare */
4
- /* global globalThis */
5
- let globalObject;
6
- if (typeof globalThis === "object") {
7
- globalObject = globalThis;
8
- } else {
9
- // eslint-disable-next-line no-extend-native
10
- Object.defineProperty(Object.prototype, "__global__", {
11
- get() {
12
- return this;
13
- },
14
- configurable: true,
15
- });
16
- // eslint-disable-next-line no-undef
17
- globalObject = __global__;
18
- delete Object.prototype.__global__;
19
- }
20
-
21
- globalObject.globalThis = globalObject;