@jsenv/core 36.1.2 → 36.2.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 (38) hide show
  1. package/dist/js/{global_this.js → global_this_js_classic.js} +3 -3
  2. package/dist/js/global_this_js_module.js +20 -0
  3. package/dist/js/inline_content.js +10 -0
  4. package/dist/jsenv_core.js +7479 -7675
  5. package/package.json +23 -20
  6. package/src/build/build.js +4 -4
  7. package/src/dev/file_service.js +1 -1
  8. package/src/kitchen/client/inline_content.js +17 -0
  9. package/src/kitchen/kitchen.js +7 -1
  10. package/src/plugins/commonjs_globals/jsenv_plugin_commonjs_globals.js +4 -1
  11. package/src/plugins/import_meta_hot/jsenv_plugin_import_meta_hot.js +4 -1
  12. package/src/plugins/import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js +4 -1
  13. package/src/plugins/plugins.js +1 -1
  14. package/src/kitchen/compat/features_compatibility.js +0 -220
  15. package/src/kitchen/compat/runtime_compat.js +0 -56
  16. package/src/plugins/reference_analysis/inline_content.js +0 -7
  17. package/src/plugins/transpilation/as_js_module/convert_js_classic_to_js_module.js +0 -45
  18. package/src/plugins/transpilation/as_js_module/jsenv_plugin_as_js_module.js +0 -78
  19. package/src/plugins/transpilation/babel/global_this/babel_plugin_global_this_as_jsenv_import.js +0 -34
  20. package/src/plugins/transpilation/babel/global_this/client/global_this.js +0 -25
  21. package/src/plugins/transpilation/babel/helpers/babel_plugin_babel_helpers_as_jsenv_imports.js +0 -52
  22. package/src/plugins/transpilation/babel/helpers/babel_plugin_structure.js +0 -173
  23. package/src/plugins/transpilation/babel/helpers/babel_plugins_compatibility.js +0 -432
  24. package/src/plugins/transpilation/babel/jsenv_plugin_babel.js +0 -100
  25. package/src/plugins/transpilation/babel/new_stylesheet/babel_plugin_new_stylesheet_as_jsenv_import.js +0 -142
  26. package/src/plugins/transpilation/babel/new_stylesheet/client/new_stylesheet.js +0 -381
  27. package/src/plugins/transpilation/babel/regenerator_runtime/babel_plugin_regenerator_runtime_as_jsenv_import.js +0 -33
  28. package/src/plugins/transpilation/babel/regenerator_runtime/client/regenerator_runtime.js +0 -748
  29. package/src/plugins/transpilation/babel/require_babel_plugin.js +0 -8
  30. package/src/plugins/transpilation/css/jsenv_plugin_css_transpilation.js +0 -54
  31. package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +0 -248
  32. package/src/plugins/transpilation/js_module_fallback/jsenv_plugin_js_module_conversion.js +0 -119
  33. package/src/plugins/transpilation/js_module_fallback/jsenv_plugin_js_module_fallback.js +0 -46
  34. package/src/plugins/transpilation/js_module_fallback/jsenv_plugin_js_module_fallback_inside_html.js +0 -240
  35. package/src/plugins/transpilation/js_module_fallback/jsenv_plugin_js_module_fallback_on_workers.js +0 -62
  36. package/src/plugins/transpilation/jsenv_plugin_import_meta_resolve.js +0 -48
  37. package/src/plugins/transpilation/jsenv_plugin_top_level_await.js +0 -87
  38. package/src/plugins/transpilation/jsenv_plugin_transpilation.js +0 -56
@@ -1,240 +0,0 @@
1
- /*
2
- * when <script type="module"> cannot be used:
3
- * - ?js_module_fallback is injected into the src of <script type="module">
4
- * - js inside <script type="module"> is transformed into classic js
5
- * - <link rel="modulepreload"> are converted to <link rel="preload">
6
- */
7
-
8
- import { readFileSync } from "node:fs";
9
- import {
10
- parseHtmlString,
11
- visitHtmlNodes,
12
- stringifyHtmlAst,
13
- getHtmlNodeAttribute,
14
- setHtmlNodeAttributes,
15
- analyzeScriptNode,
16
- injectHtmlNodeAsEarlyAsPossible,
17
- createHtmlNode,
18
- } from "@jsenv/ast";
19
- import { injectQueryParams, urlToRelativeUrl } from "@jsenv/urls";
20
- import { SOURCEMAP } from "@jsenv/sourcemap";
21
-
22
- export const jsenvPluginJsModuleFallbackInsideHtml = ({
23
- systemJsInjection,
24
- systemJsClientFileUrl,
25
- }) => {
26
- const turnIntoJsClassicProxy = (reference) => {
27
- return injectQueryParams(reference.url, { js_module_fallback: "" });
28
- };
29
-
30
- return {
31
- name: "jsenv:js_module_fallback_inside_html",
32
- appliesDuring: "*",
33
- redirectReference: {
34
- link_href: (reference, context) => {
35
- if (
36
- context.systemJsTranspilation &&
37
- reference.subtype === "modulepreload"
38
- ) {
39
- return turnIntoJsClassicProxy(reference);
40
- }
41
- if (
42
- context.systemJsTranspilation &&
43
- reference.subtype === "preload" &&
44
- reference.expectedType === "js_module"
45
- ) {
46
- return turnIntoJsClassicProxy(reference);
47
- }
48
- return null;
49
- },
50
- script: (reference, context) => {
51
- if (
52
- context.systemJsTranspilation &&
53
- reference.expectedType === "js_module"
54
- ) {
55
- return turnIntoJsClassicProxy(reference);
56
- }
57
- return null;
58
- },
59
- js_url: (reference, context) => {
60
- if (
61
- context.systemJsTranspilation &&
62
- reference.expectedType === "js_module"
63
- ) {
64
- return turnIntoJsClassicProxy(reference);
65
- }
66
- return null;
67
- },
68
- },
69
- finalizeUrlContent: {
70
- html: async (urlInfo, context) => {
71
- const htmlAst = parseHtmlString(urlInfo.content);
72
- const mutations = [];
73
- visitHtmlNodes(htmlAst, {
74
- link: (node) => {
75
- const rel = getHtmlNodeAttribute(node, "rel");
76
- if (rel !== "modulepreload" && rel !== "preload") {
77
- return;
78
- }
79
- const href = getHtmlNodeAttribute(node, "href");
80
- if (!href) {
81
- return;
82
- }
83
- const reference = context.referenceUtils.find(
84
- (ref) =>
85
- ref.generatedSpecifier === href &&
86
- ref.type === "link_href" &&
87
- ref.subtype === rel,
88
- );
89
- if (!isOrWasExpectingJsModule(reference)) {
90
- return;
91
- }
92
- if (
93
- rel === "modulepreload" &&
94
- reference.expectedType === "js_classic"
95
- ) {
96
- mutations.push(() => {
97
- setHtmlNodeAttributes(node, {
98
- rel: "preload",
99
- as: "script",
100
- crossorigin: undefined,
101
- });
102
- });
103
- }
104
- if (rel === "preload" && reference.expectedType === "js_classic") {
105
- mutations.push(() => {
106
- setHtmlNodeAttributes(node, { crossorigin: undefined });
107
- });
108
- }
109
- },
110
- script: (node) => {
111
- const { type } = analyzeScriptNode(node);
112
- if (type !== "js_module") {
113
- return;
114
- }
115
- const src = getHtmlNodeAttribute(node, "src");
116
- if (src) {
117
- const reference = context.referenceUtils.find(
118
- (ref) =>
119
- ref.generatedSpecifier === src &&
120
- ref.type === "script" &&
121
- ref.subtype === "js_module",
122
- );
123
- if (!reference) {
124
- return;
125
- }
126
- if (reference.expectedType === "js_classic") {
127
- mutations.push(() => {
128
- setHtmlNodeAttributes(node, { type: undefined });
129
- });
130
- }
131
- } else if (context.systemJsTranspilation) {
132
- mutations.push(() => {
133
- setHtmlNodeAttributes(node, { type: undefined });
134
- });
135
- }
136
- },
137
- });
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
- await Promise.all(mutations.map((mutation) => mutation()));
216
- return stringifyHtmlAst(htmlAst, {
217
- cleanupPositionAttributes: context.dev,
218
- });
219
- },
220
- },
221
- };
222
- };
223
-
224
- const isOrWasExpectingJsModule = (reference) => {
225
- if (isExpectingJsModule(reference)) {
226
- return true;
227
- }
228
- if (reference.original && isExpectingJsModule(reference.original)) {
229
- return true;
230
- }
231
- return false;
232
- };
233
-
234
- const isExpectingJsModule = (reference) => {
235
- return (
236
- reference.expectedType === "js_module" ||
237
- reference.searchParams.has("js_module_fallback") ||
238
- reference.searchParams.has("as_js_classic")
239
- );
240
- };
@@ -1,62 +0,0 @@
1
- /*
2
- * when {type: "module"} cannot be used on web workers:
3
- * - new Worker("worker.js", { type: "module" })
4
- * transformed into
5
- * new Worker("worker.js?js_module_fallback", { type: " lassic" })
6
- * - navigator.serviceWorker.register("service_worker.js", { type: "module" })
7
- * transformed into
8
- * navigator.serviceWorker.register("service_worker.js?js_module_fallback", { type: "classic" })
9
- * - new SharedWorker("shared_worker.js", { type: "module" })
10
- * transformed into
11
- * new SharedWorker("shared_worker.js?js_module_fallback", { type: "classic" })
12
- */
13
-
14
- import { injectQueryParams } from "@jsenv/urls";
15
-
16
- export const jsenvPluginJsModuleFallbackOnWorkers = () => {
17
- const turnIntoJsClassicProxy = (reference) => {
18
- reference.mutation = (magicSource) => {
19
- magicSource.replace({
20
- start: reference.typePropertyNode.value.start,
21
- end: reference.typePropertyNode.value.end,
22
- replacement: JSON.stringify("classic"),
23
- });
24
- };
25
- return injectQueryParams(reference.url, { js_module_fallback: "" });
26
- };
27
-
28
- return {
29
- name: "jsenv:js_module_fallback_on_workers",
30
- appliesDuring: "*",
31
- redirectReference: {
32
- js_url: (reference, context) => {
33
- if (reference.expectedType !== "js_module") {
34
- return null;
35
- }
36
- if (reference.expectedSubtype === "worker") {
37
- if (context.isSupportedOnCurrentClients("worker_type_module")) {
38
- return null;
39
- }
40
- return turnIntoJsClassicProxy(reference);
41
- }
42
- if (reference.expectedSubtype === "service_worker") {
43
- if (
44
- context.isSupportedOnCurrentClients("service_worker_type_module")
45
- ) {
46
- return null;
47
- }
48
- return turnIntoJsClassicProxy(reference);
49
- }
50
- if (reference.expectedSubtype === "shared_worker") {
51
- if (
52
- context.isSupportedOnCurrentClients("shared_worker_type_module")
53
- ) {
54
- return null;
55
- }
56
- return turnIntoJsClassicProxy(reference);
57
- }
58
- return null;
59
- },
60
- },
61
- };
62
- };
@@ -1,48 +0,0 @@
1
- import { createMagicSource } from "@jsenv/sourcemap";
2
-
3
- export const jsenvPluginImportMetaResolve = () => {
4
- return {
5
- name: "jsenv:import_meta_resolve",
6
- appliesDuring: "*",
7
- init: (context) => {
8
- if (context.isSupportedOnCurrentClients("import_meta_resolve")) {
9
- return false;
10
- }
11
- // keep it untouched, systemjs will handle it
12
- if (context.systemJsTranspilation) {
13
- return false;
14
- }
15
- return true;
16
- },
17
- transformUrlContent: {
18
- js_module: async (urlInfo, context) => {
19
- const magicSource = createMagicSource(urlInfo.content);
20
- context.referenceUtils._references.forEach((ref) => {
21
- if (ref.subtype === "import_meta_resolve") {
22
- const originalSpecifierLength = Buffer.byteLength(ref.specifier);
23
- const specifierLength = Buffer.byteLength(
24
- ref.generatedSpecifier.slice(1, -1), // remove `"` around
25
- );
26
- const specifierLengthDiff =
27
- specifierLength - originalSpecifierLength;
28
- const end = ref.node.end + specifierLengthDiff;
29
- magicSource.replace({
30
- start: ref.node.start,
31
- end,
32
- replacement: `new URL(${ref.generatedSpecifier}, import.meta.url).href`,
33
- });
34
- const currentLengthBeforeSpecifier = "import.meta.resolve(".length;
35
- const newLengthBeforeSpecifier = "new URL(".length;
36
- const lengthDiff =
37
- currentLengthBeforeSpecifier - newLengthBeforeSpecifier;
38
- ref.specifierColumn -= lengthDiff;
39
- ref.specifierStart -= lengthDiff;
40
- ref.specifierEnd =
41
- ref.specifierStart + Buffer.byteLength(ref.generatedSpecifier);
42
- }
43
- });
44
- return magicSource.toContentAndSourcemap();
45
- },
46
- },
47
- };
48
- };
@@ -1,87 +0,0 @@
1
- import { applyBabelPlugins } from "@jsenv/ast";
2
-
3
- import { requireBabelPlugin } from "./babel/require_babel_plugin.js";
4
-
5
- export const jsenvPluginTopLevelAwait = () => {
6
- return {
7
- name: "jsenv:top_level_await",
8
- appliesDuring: "*",
9
- init: (context) => {
10
- if (context.isSupportedOnCurrentClients("top_level_await")) {
11
- return false;
12
- }
13
- // keep it untouched, systemjs will handle it
14
- if (context.systemJsTranspilation) {
15
- return false;
16
- }
17
- return true;
18
- },
19
- transformUrlContent: {
20
- js_module: async (urlInfo) => {
21
- const usesTLA = await usesTopLevelAwait(urlInfo);
22
- if (!usesTLA) {
23
- return null;
24
- }
25
- const { code, map } = await applyBabelPlugins({
26
- urlInfo,
27
- babelPlugins: [
28
- [
29
- requireBabelPlugin("babel-plugin-transform-async-to-promises"),
30
- {
31
- // Maybe we could pass target: "es6" when we support arrow function
32
- // https://github.com/rpetrich/babel-plugin-transform-async-to-promises/blob/92755ff8c943c97596523e586b5fa515c2e99326/async-to-promises.ts#L55
33
- topLevelAwait: "simple",
34
- // enable once https://github.com/rpetrich/babel-plugin-transform-async-to-promises/pull/83
35
- // externalHelpers: true,
36
- // externalHelpersPath: JSON.parse(
37
- // context.referenceUtils.inject({
38
- // type: "js_import",
39
- // expectedType: "js_module",
40
- // specifier:
41
- // "babel-plugin-transform-async-to-promises/helpers.mjs",
42
- // })[0],
43
- // ),
44
- },
45
- ],
46
- ],
47
- });
48
- return {
49
- content: code,
50
- sourcemap: map,
51
- };
52
- },
53
- },
54
- };
55
- };
56
-
57
- const usesTopLevelAwait = async (urlInfo) => {
58
- if (!urlInfo.content.includes("await ")) {
59
- return false;
60
- }
61
- const { metadata } = await applyBabelPlugins({
62
- urlInfo,
63
- babelPlugins: [babelPluginMetadataUsesTopLevelAwait],
64
- });
65
- return metadata.usesTopLevelAwait;
66
- };
67
-
68
- const babelPluginMetadataUsesTopLevelAwait = () => {
69
- return {
70
- name: "metadata-uses-top-level-await",
71
- visitor: {
72
- Program: (programPath, state) => {
73
- let usesTopLevelAwait = false;
74
- programPath.traverse({
75
- AwaitExpression: (path) => {
76
- const closestFunction = path.getFunctionParent();
77
- if (!closestFunction || closestFunction.type === "Program") {
78
- usesTopLevelAwait = true;
79
- path.stop();
80
- }
81
- },
82
- });
83
- state.file.metadata.usesTopLevelAwait = usesTopLevelAwait;
84
- },
85
- },
86
- };
87
- };
@@ -1,56 +0,0 @@
1
- /*
2
- * Transforms code to make it compatible with browser that would not be able to
3
- * run it otherwise. For instance:
4
- * - const -> var
5
- * - async/await -> promises
6
- * Anything that is not standard (import.meta.dev for instance) is outside the scope
7
- * of this plugin
8
- */
9
-
10
- import { jsenvPluginCssTranspilation } from "./css/jsenv_plugin_css_transpilation.js";
11
- import { jsenvPluginImportAssertions } from "./import_assertions/jsenv_plugin_import_assertions.js";
12
- import { jsenvPluginJsModuleFallback } from "./js_module_fallback/jsenv_plugin_js_module_fallback.js";
13
- import { jsenvPluginAsJsModule } from "./as_js_module/jsenv_plugin_as_js_module.js";
14
- import { jsenvPluginBabel } from "./babel/jsenv_plugin_babel.js";
15
- import { jsenvPluginTopLevelAwait } from "./jsenv_plugin_top_level_await.js";
16
- import { jsenvPluginImportMetaResolve } from "./jsenv_plugin_import_meta_resolve.js";
17
-
18
- export const jsenvPluginTranspilation = ({
19
- importAssertions = true,
20
- css = true,
21
- // build sets jsModuleFallbackOnJsClassic: false during first step of the build
22
- // and re-enable it in the second phase (when performing the bundling)
23
- // so that bundling is applied on js modules THEN it is converted to js classic if needed
24
- jsModuleFallbackOnJsClassic = true,
25
- topLevelAwait = true,
26
- importMetaResolve = true,
27
- babelHelpersAsImport = true,
28
- getCustomBabelPlugins,
29
- }) => {
30
- if (importAssertions === true) {
31
- importAssertions = {};
32
- }
33
- if (jsModuleFallbackOnJsClassic === true) {
34
- jsModuleFallbackOnJsClassic = {};
35
- }
36
- return [
37
- ...(importMetaResolve ? [jsenvPluginImportMetaResolve()] : []),
38
- ...(importAssertions
39
- ? [jsenvPluginImportAssertions(importAssertions)]
40
- : []),
41
- // babel also so that rollup can bundle babel helpers for instance
42
- jsenvPluginBabel({
43
- topLevelAwait,
44
- getCustomBabelPlugins,
45
- babelHelpersAsImport,
46
- }),
47
- ...(jsModuleFallbackOnJsClassic
48
- ? [jsenvPluginJsModuleFallback(jsModuleFallbackOnJsClassic)]
49
- : []),
50
- jsenvPluginAsJsModule(),
51
- // topLevelAwait must come after jsModuleFallback because it's related to the module format
52
- // so we want to wait to know the module format before transforming things related to top level await
53
- ...(topLevelAwait ? [jsenvPluginTopLevelAwait(topLevelAwait)] : []),
54
- ...(css ? [jsenvPluginCssTranspilation()] : []),
55
- ];
56
- };