@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.
- package/package.json +26 -26
- package/src/as_js_module/jsenv_plugin_as_js_module.js +4 -1
- package/src/babel/babel_helper_directory/README.md +1 -1
- package/src/babel/babel_helper_directory/babel_helpers/applyDecs2301/applyDecs2301.js +547 -528
- package/src/babel/babel_helper_directory/babel_helpers/applyDecs2305/applyDecs2305.js +681 -0
- package/src/babel/jsenv_plugin_babel.js +89 -17
- package/src/babel/new_stylesheet/babel_plugin_new_stylesheet_injector.js +11 -135
- package/src/babel/new_stylesheet/constructable_stylesheet_usage.js +114 -0
- package/src/babel/new_stylesheet/new_stylesheet_client_file_url.js +4 -0
- package/src/babel/regenerator_runtime/babel_plugin_regenerator_runtime_injector.js +11 -35
- package/src/babel/regenerator_runtime/regenerator_runtime_client_file_url.js +4 -0
- package/src/babel/regenerator_runtime/regenerator_runtime_usage.js +23 -0
- package/src/babel/{polyfill_injection_in_babel_ast.js → side_effect_injection_in_babel_ast.js} +8 -8
- package/src/import_assertions/jsenv_plugin_import_assertions.js +3 -3
- package/src/js_module_fallback/jsenv_plugin_js_module_conversion.js +15 -7
- package/src/js_module_fallback/jsenv_plugin_js_module_fallback.js +2 -11
- package/src/js_module_fallback/jsenv_plugin_js_module_fallback_inside_html.js +21 -98
- package/src/babel/apply_js_transpilation.js +0 -74
- package/src/babel/global_this/babel_plugin_global_this_injector.js +0 -56
- package/src/babel/global_this/client/global_this_js_classic.js +0 -25
- package/src/babel/global_this/client/global_this_js_module.js +0 -21
|
@@ -1,29 +1,101 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { applyBabelPlugins } from "@jsenv/ast";
|
|
2
|
+
|
|
3
|
+
import { getBaseBabelPluginStructure } from "./babel_plugin_structure.js";
|
|
4
|
+
import { babelPluginBabelHelpersAsJsenvImports } from "./babel_plugin_babel_helpers_as_jsenv_imports.js";
|
|
5
|
+
import { analyzeRegeneratorRuntimeUsage } from "./regenerator_runtime/regenerator_runtime_usage.js";
|
|
6
|
+
import { babelPluginRegeneratorRuntimeInjector } from "./regenerator_runtime/babel_plugin_regenerator_runtime_injector.js";
|
|
7
|
+
import { regeneratorRuntimeClientFileUrl } from "./regenerator_runtime/regenerator_runtime_client_file_url.js";
|
|
8
|
+
import { analyzeConstructableStyleSheetUsage } from "./new_stylesheet/constructable_stylesheet_usage.js";
|
|
9
|
+
import { babelPluginNewStylesheetInjector } from "./new_stylesheet/babel_plugin_new_stylesheet_injector.js";
|
|
2
10
|
|
|
3
11
|
export const jsenvPluginBabel = ({ babelHelpersAsImport = true } = {}) => {
|
|
4
12
|
const transformWithBabel = async (urlInfo, context) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
const isJsModule = urlInfo.type === "js_module";
|
|
14
|
+
const getImportSpecifier = (clientFileUrl) => {
|
|
15
|
+
const [reference] = context.referenceUtils.inject({
|
|
16
|
+
type: "js_import",
|
|
17
|
+
expectedType: "js_module",
|
|
18
|
+
specifier: clientFileUrl,
|
|
19
|
+
});
|
|
20
|
+
return JSON.parse(reference.generatedSpecifier);
|
|
21
|
+
};
|
|
22
|
+
const isSupported = context.isSupportedOnCurrentClients;
|
|
23
|
+
const babelPluginStructure = getBaseBabelPluginStructure({
|
|
24
|
+
url: urlInfo.originalUrl,
|
|
25
|
+
isSupported,
|
|
26
|
+
isJsModule,
|
|
27
|
+
getImportSpecifier,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (!isSupported("async_generator_function")) {
|
|
31
|
+
const regeneratorRuntimeUsage = analyzeRegeneratorRuntimeUsage(urlInfo);
|
|
32
|
+
if (regeneratorRuntimeUsage) {
|
|
33
|
+
if (isJsModule && babelHelpersAsImport) {
|
|
34
|
+
babelPluginStructure["new-stylesheet-injector"] = [
|
|
35
|
+
babelPluginRegeneratorRuntimeInjector,
|
|
36
|
+
{ babelHelpersAsImport, getImportSpecifier },
|
|
37
|
+
];
|
|
38
|
+
} else {
|
|
39
|
+
context.referenceUtils.foundSideEffectFile({
|
|
40
|
+
sideEffectFileUrl: regeneratorRuntimeClientFileUrl,
|
|
41
|
+
expectedType: "js_classic",
|
|
42
|
+
specifierLine: regeneratorRuntimeUsage.line,
|
|
43
|
+
specifierColumn: regeneratorRuntimeUsage.column,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (!isSupported("new_stylesheet")) {
|
|
49
|
+
const constructableStyleSheetUsage =
|
|
50
|
+
analyzeConstructableStyleSheetUsage(urlInfo);
|
|
51
|
+
if (constructableStyleSheetUsage) {
|
|
52
|
+
if (isJsModule && babelHelpersAsImport) {
|
|
53
|
+
babelPluginStructure["new-stylesheet-injector"] = [
|
|
54
|
+
babelPluginNewStylesheetInjector,
|
|
55
|
+
{ babelHelpersAsImport, getImportSpecifier },
|
|
56
|
+
];
|
|
57
|
+
} else {
|
|
58
|
+
context.referenceUtils.foundSideEffectFile({
|
|
59
|
+
sideEffectFileUrl: regeneratorRuntimeClientFileUrl,
|
|
60
|
+
expectedType: "js_classic",
|
|
61
|
+
specifierLine: constructableStyleSheetUsage.line,
|
|
62
|
+
specifierColumn: constructableStyleSheetUsage.column,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (
|
|
68
|
+
isJsModule &&
|
|
69
|
+
babelHelpersAsImport &&
|
|
70
|
+
Object.keys(babelPluginStructure).length > 0
|
|
71
|
+
) {
|
|
72
|
+
babelPluginStructure["babel-helper-as-jsenv-import"] = [
|
|
73
|
+
babelPluginBabelHelpersAsJsenvImports,
|
|
74
|
+
{ getImportSpecifier },
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const babelPlugins = Object.keys(babelPluginStructure).map(
|
|
79
|
+
(babelPluginName) => babelPluginStructure[babelPluginName],
|
|
80
|
+
);
|
|
81
|
+
const { code, map } = await applyBabelPlugins({
|
|
82
|
+
babelPlugins,
|
|
83
|
+
options: {
|
|
12
84
|
generatorOpts: {
|
|
13
85
|
retainLines: context.dev,
|
|
14
86
|
},
|
|
15
87
|
},
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
expectedType: "js_module",
|
|
21
|
-
specifier: clientFileUrl,
|
|
22
|
-
});
|
|
23
|
-
return JSON.parse(reference.generatedSpecifier);
|
|
24
|
-
},
|
|
88
|
+
input: urlInfo.content,
|
|
89
|
+
inputIsJsModule: isJsModule,
|
|
90
|
+
inputUrl: urlInfo.originalUrl,
|
|
91
|
+
outputUrl: urlInfo.generatedUrl,
|
|
25
92
|
});
|
|
93
|
+
return {
|
|
94
|
+
content: code,
|
|
95
|
+
sourcemap: map,
|
|
96
|
+
};
|
|
26
97
|
};
|
|
98
|
+
|
|
27
99
|
return {
|
|
28
100
|
name: "jsenv:babel",
|
|
29
101
|
appliesDuring: "*",
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const newStylesheetClientFileUrl = new URL(
|
|
5
|
-
"./client/new_stylesheet.js",
|
|
6
|
-
import.meta.url,
|
|
7
|
-
).href;
|
|
1
|
+
import { injectSideEffectFileIntoBabelAst } from "../side_effect_injection_in_babel_ast.js";
|
|
2
|
+
import { newStylesheetClientFileUrl } from "./new_stylesheet_client_file_url.js";
|
|
8
3
|
|
|
9
4
|
export const babelPluginNewStylesheetInjector = (
|
|
10
5
|
babel,
|
|
@@ -14,136 +9,17 @@ export const babelPluginNewStylesheetInjector = (
|
|
|
14
9
|
name: "new-stylesheet-injector",
|
|
15
10
|
visitor: {
|
|
16
11
|
Program: (path, state) => {
|
|
17
|
-
const {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
newStyleSheetDetected = true;
|
|
27
|
-
path.stop();
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
MemberExpression: (path) => {
|
|
31
|
-
if (isDocumentAdoptedStyleSheets(path.node)) {
|
|
32
|
-
newStyleSheetDetected = true;
|
|
33
|
-
path.stop();
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
CallExpression: (path) => {
|
|
37
|
-
if (path.node.callee.type !== "Import") {
|
|
38
|
-
// Some other function call, not import();
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
if (path.node.arguments[0].type !== "StringLiteral") {
|
|
42
|
-
// Non-string argument, probably a variable or expression, e.g.
|
|
43
|
-
// import(moduleId)
|
|
44
|
-
// import('./' + moduleName)
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const sourcePath = path.get("arguments")[0];
|
|
48
|
-
if (
|
|
49
|
-
hasCssModuleQueryParam(sourcePath) ||
|
|
50
|
-
hasImportTypeCssAssertion(path)
|
|
51
|
-
) {
|
|
52
|
-
newStyleSheetDetected = true;
|
|
53
|
-
path.stop();
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
ImportDeclaration: (path) => {
|
|
57
|
-
const sourcePath = path.get("source");
|
|
58
|
-
if (
|
|
59
|
-
hasCssModuleQueryParam(sourcePath) ||
|
|
60
|
-
hasImportTypeCssAssertion(path)
|
|
61
|
-
) {
|
|
62
|
-
newStyleSheetDetected = true;
|
|
63
|
-
path.stop();
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
ExportAllDeclaration: (path) => {
|
|
67
|
-
const sourcePath = path.get("source");
|
|
68
|
-
if (hasCssModuleQueryParam(sourcePath)) {
|
|
69
|
-
newStyleSheetDetected = true;
|
|
70
|
-
path.stop();
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
ExportNamedDeclaration: (path) => {
|
|
74
|
-
if (!path.node.source) {
|
|
75
|
-
// This export has no "source", so it's probably
|
|
76
|
-
// a local variable or function, e.g.
|
|
77
|
-
// export { varName }
|
|
78
|
-
// export const constName = ...
|
|
79
|
-
// export function funcName() {}
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
const sourcePath = path.get("source");
|
|
83
|
-
if (hasCssModuleQueryParam(sourcePath)) {
|
|
84
|
-
newStyleSheetDetected = true;
|
|
85
|
-
path.stop();
|
|
86
|
-
}
|
|
87
|
-
},
|
|
12
|
+
const { sourceType } = state.file.opts.parserOpts;
|
|
13
|
+
const isJsModule = sourceType === "module";
|
|
14
|
+
injectSideEffectFileIntoBabelAst({
|
|
15
|
+
programPath: path,
|
|
16
|
+
isJsModule,
|
|
17
|
+
asImport: babelHelpersAsImport,
|
|
18
|
+
sideEffectFileUrl: newStylesheetClientFileUrl,
|
|
19
|
+
getSideEffectFileSpecifier: getImportSpecifier,
|
|
20
|
+
babel,
|
|
88
21
|
});
|
|
89
|
-
state.file.metadata.newStyleSheetDetected = newStyleSheetDetected;
|
|
90
|
-
if (newStyleSheetDetected) {
|
|
91
|
-
const { sourceType } = state.file.opts.parserOpts;
|
|
92
|
-
const isJsModule = sourceType === "module";
|
|
93
|
-
injectPolyfillIntoBabelAst({
|
|
94
|
-
programPath: path,
|
|
95
|
-
isJsModule,
|
|
96
|
-
asImport: babelHelpersAsImport,
|
|
97
|
-
polyfillFileUrl: newStylesheetClientFileUrl,
|
|
98
|
-
getPolyfillImportSpecifier: getImportSpecifier,
|
|
99
|
-
babel,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
22
|
},
|
|
103
23
|
},
|
|
104
24
|
};
|
|
105
25
|
};
|
|
106
|
-
|
|
107
|
-
const isNewCssStyleSheetCall = (node) => {
|
|
108
|
-
return (
|
|
109
|
-
node.type === "NewExpression" &&
|
|
110
|
-
node.callee.type === "Identifier" &&
|
|
111
|
-
node.callee.name === "CSSStyleSheet"
|
|
112
|
-
);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
const isDocumentAdoptedStyleSheets = (node) => {
|
|
116
|
-
return (
|
|
117
|
-
node.type === "MemberExpression" &&
|
|
118
|
-
node.object.type === "Identifier" &&
|
|
119
|
-
node.object.name === "document" &&
|
|
120
|
-
node.property.type === "Identifier" &&
|
|
121
|
-
node.property.name === "adoptedStyleSheets"
|
|
122
|
-
);
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const hasCssModuleQueryParam = (path) => {
|
|
126
|
-
const { node } = path;
|
|
127
|
-
return (
|
|
128
|
-
node.type === "StringLiteral" &&
|
|
129
|
-
new URL(node.value, "https://jsenv.dev").searchParams.has(`css_module`)
|
|
130
|
-
);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const hasImportTypeCssAssertion = (path) => {
|
|
134
|
-
const importAssertionsDescriptor = getImportAssertionsDescriptor(
|
|
135
|
-
path.node.assertions,
|
|
136
|
-
);
|
|
137
|
-
return Boolean(importAssertionsDescriptor.type === "css");
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const getImportAssertionsDescriptor = (importAssertions) => {
|
|
141
|
-
const importAssertionsDescriptor = {};
|
|
142
|
-
if (importAssertions) {
|
|
143
|
-
importAssertions.forEach((importAssertion) => {
|
|
144
|
-
importAssertionsDescriptor[importAssertion.key.name] =
|
|
145
|
-
importAssertion.value.value;
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
return importAssertionsDescriptor;
|
|
149
|
-
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { visitJsAstUntil } from "@jsenv/ast";
|
|
2
|
+
import { newStylesheetClientFileUrl } from "./new_stylesheet_client_file_url.js";
|
|
3
|
+
|
|
4
|
+
export const analyzeConstructableStyleSheetUsage = (urlInfo) => {
|
|
5
|
+
if (urlInfo.url === newStylesheetClientFileUrl) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const node = visitJsAstUntil(urlInfo.contentAst, {
|
|
9
|
+
NewExpression: (node) => {
|
|
10
|
+
return isNewCssStyleSheetCall(node);
|
|
11
|
+
},
|
|
12
|
+
MemberExpression: (node) => {
|
|
13
|
+
return isDocumentAdoptedStyleSheets(node);
|
|
14
|
+
},
|
|
15
|
+
ImportExpression: (node) => {
|
|
16
|
+
const source = node.source;
|
|
17
|
+
if (source.type !== "Literal" || typeof source.value === "string") {
|
|
18
|
+
// Non-string argument, probably a variable or expression, e.g.
|
|
19
|
+
// import(moduleId)
|
|
20
|
+
// import('./' + moduleName)
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (hasImportTypeCssAssertion(node)) {
|
|
24
|
+
return node;
|
|
25
|
+
}
|
|
26
|
+
if (hasCssModuleQueryParam(source)) {
|
|
27
|
+
return source;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
},
|
|
31
|
+
ImportDeclaration: (node) => {
|
|
32
|
+
const { source } = node;
|
|
33
|
+
if (hasCssModuleQueryParam(source)) {
|
|
34
|
+
return source;
|
|
35
|
+
}
|
|
36
|
+
if (hasImportTypeCssAssertion(node)) {
|
|
37
|
+
return node;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
},
|
|
41
|
+
ExportAllDeclaration: (node) => {
|
|
42
|
+
const { source } = node;
|
|
43
|
+
if (hasCssModuleQueryParam(source)) {
|
|
44
|
+
return source;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
},
|
|
48
|
+
ExportNamedDeclaration: (node) => {
|
|
49
|
+
const { source } = node;
|
|
50
|
+
if (!source) {
|
|
51
|
+
// This export has no "source", so it's probably
|
|
52
|
+
// a local variable or function, e.g.
|
|
53
|
+
// export { varName }
|
|
54
|
+
// export const constName = ...
|
|
55
|
+
// export function funcName() {}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (hasCssModuleQueryParam(source)) {
|
|
59
|
+
return source;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
return node
|
|
65
|
+
? {
|
|
66
|
+
line: node.loc.start.line,
|
|
67
|
+
column: node.loc.start.column,
|
|
68
|
+
}
|
|
69
|
+
: null;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const isNewCssStyleSheetCall = (node) => {
|
|
73
|
+
return (
|
|
74
|
+
node.type === "NewExpression" &&
|
|
75
|
+
node.callee.type === "Identifier" &&
|
|
76
|
+
node.callee.name === "CSSStyleSheet"
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const isDocumentAdoptedStyleSheets = (node) => {
|
|
81
|
+
return (
|
|
82
|
+
node.type === "MemberExpression" &&
|
|
83
|
+
node.object.type === "Identifier" &&
|
|
84
|
+
node.object.name === "document" &&
|
|
85
|
+
node.property.type === "Identifier" &&
|
|
86
|
+
node.property.name === "adoptedStyleSheets"
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const hasCssModuleQueryParam = (node) => {
|
|
91
|
+
return (
|
|
92
|
+
node.type === "Literal" &&
|
|
93
|
+
typeof node.value === "string" &&
|
|
94
|
+
new URL(node.value, "https://jsenv.dev").searchParams.has(`css_module`)
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const hasImportTypeCssAssertion = (node) => {
|
|
99
|
+
const importAssertionsDescriptor = getImportAssertionsDescriptor(
|
|
100
|
+
node.assertions,
|
|
101
|
+
);
|
|
102
|
+
return Boolean(importAssertionsDescriptor.type === "css");
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const getImportAssertionsDescriptor = (importAssertions) => {
|
|
106
|
+
const importAssertionsDescriptor = {};
|
|
107
|
+
if (importAssertions) {
|
|
108
|
+
importAssertions.forEach((importAssertion) => {
|
|
109
|
+
importAssertionsDescriptor[importAssertion.key.name] =
|
|
110
|
+
importAssertion.value.value;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return importAssertionsDescriptor;
|
|
114
|
+
};
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const regeneratorRuntimeClientFileUrl = new URL(
|
|
5
|
-
"./client/regenerator_runtime.js",
|
|
6
|
-
import.meta.url,
|
|
7
|
-
).href;
|
|
1
|
+
import { injectSideEffectFileIntoBabelAst } from "../side_effect_injection_in_babel_ast.js";
|
|
2
|
+
import { regeneratorRuntimeClientFileUrl } from "./regenerator_runtime_client_file_url.js";
|
|
8
3
|
|
|
9
4
|
export const babelPluginRegeneratorRuntimeInjector = (
|
|
10
5
|
babel,
|
|
@@ -14,35 +9,16 @@ export const babelPluginRegeneratorRuntimeInjector = (
|
|
|
14
9
|
name: "regenerator-runtime-injector",
|
|
15
10
|
visitor: {
|
|
16
11
|
Program: (path, state) => {
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (node.name === "regeneratorRuntime") {
|
|
27
|
-
regeneratorRuntimeDetected = true;
|
|
28
|
-
path.stop();
|
|
29
|
-
}
|
|
30
|
-
},
|
|
12
|
+
const { sourceType } = state.file.opts.parserOpts;
|
|
13
|
+
const isJsModule = sourceType === "module";
|
|
14
|
+
injectSideEffectFileIntoBabelAst({
|
|
15
|
+
programPath: path,
|
|
16
|
+
isJsModule,
|
|
17
|
+
asImport: babelHelpersAsImport,
|
|
18
|
+
sideEffectFileUrl: regeneratorRuntimeClientFileUrl,
|
|
19
|
+
getSideEffectFileSpecifier: getImportSpecifier,
|
|
20
|
+
babel,
|
|
31
21
|
});
|
|
32
|
-
state.file.metadata.regeneratorRuntimeDetected =
|
|
33
|
-
regeneratorRuntimeDetected;
|
|
34
|
-
if (regeneratorRuntimeDetected) {
|
|
35
|
-
const { sourceType } = state.file.opts.parserOpts;
|
|
36
|
-
const isJsModule = sourceType === "module";
|
|
37
|
-
injectPolyfillIntoBabelAst({
|
|
38
|
-
programPath: path,
|
|
39
|
-
isJsModule,
|
|
40
|
-
asImport: babelHelpersAsImport,
|
|
41
|
-
polyfillFileUrl: regeneratorRuntimeClientFileUrl,
|
|
42
|
-
getPolyfillImportSpecifier: getImportSpecifier,
|
|
43
|
-
babel,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
22
|
},
|
|
47
23
|
},
|
|
48
24
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { visitJsAstUntil } from "@jsenv/ast";
|
|
2
|
+
import { regeneratorRuntimeClientFileUrl } from "./regenerator_runtime_client_file_url.js";
|
|
3
|
+
|
|
4
|
+
export const analyzeRegeneratorRuntimeUsage = (urlInfo) => {
|
|
5
|
+
if (urlInfo.url === regeneratorRuntimeClientFileUrl) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const ast = urlInfo.contentAst;
|
|
9
|
+
const node = visitJsAstUntil(ast, {
|
|
10
|
+
Identifier: (node) => {
|
|
11
|
+
if (node.name === "regeneratorRuntime") {
|
|
12
|
+
return node;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
return node
|
|
18
|
+
? {
|
|
19
|
+
line: node.loc.start.line,
|
|
20
|
+
column: node.loc.start.column,
|
|
21
|
+
}
|
|
22
|
+
: null;
|
|
23
|
+
};
|
package/src/babel/{polyfill_injection_in_babel_ast.js → side_effect_injection_in_babel_ast.js}
RENAMED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { injectJsImport } from "@jsenv/ast";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const injectSideEffectFileIntoBabelAst = ({
|
|
5
5
|
programPath,
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
sideEffectFileUrl,
|
|
7
|
+
getSideEffectFileSpecifier,
|
|
8
8
|
babel,
|
|
9
9
|
isJsModule,
|
|
10
10
|
asImport = true,
|
|
@@ -12,19 +12,19 @@ export const injectPolyfillIntoBabelAst = ({
|
|
|
12
12
|
if (isJsModule && asImport) {
|
|
13
13
|
injectJsImport({
|
|
14
14
|
programPath,
|
|
15
|
-
from:
|
|
15
|
+
from: getSideEffectFileSpecifier(sideEffectFileUrl),
|
|
16
16
|
sideEffect: true,
|
|
17
17
|
});
|
|
18
18
|
return;
|
|
19
19
|
}
|
|
20
|
-
const
|
|
21
|
-
const
|
|
20
|
+
const sidEffectFileContent = readFileSync(new URL(sideEffectFileUrl), "utf8");
|
|
21
|
+
const sideEffectFileContentAst = babel.parse(sidEffectFileContent);
|
|
22
22
|
if (isJsModule) {
|
|
23
|
-
injectAstAfterImport(programPath,
|
|
23
|
+
injectAstAfterImport(programPath, sideEffectFileContentAst);
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
26
|
const bodyNodePaths = programPath.get("body");
|
|
27
|
-
bodyNodePaths[0].insertBefore(
|
|
27
|
+
bodyNodePaths[0].insertBefore(sideEffectFileContentAst.program.body);
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
const injectAstAfterImport = (programPath, ast) => {
|
|
@@ -124,7 +124,7 @@ const jsenvPluginAsModules = () => {
|
|
|
124
124
|
specifier: jsonReference.url,
|
|
125
125
|
expectedType: "js_module",
|
|
126
126
|
});
|
|
127
|
-
} else if (context.build &&
|
|
127
|
+
} else if (context.build && !context.urlGraph.hasDependent(jsonUrlInfo)) {
|
|
128
128
|
context.urlGraph.deleteUrlInfo(jsonUrlInfo.url);
|
|
129
129
|
}
|
|
130
130
|
const jsonText = JSON.stringify(jsonUrlInfo.content.trim());
|
|
@@ -165,7 +165,7 @@ const jsenvPluginAsModules = () => {
|
|
|
165
165
|
specifier: cssReference.url,
|
|
166
166
|
expectedType: "js_module",
|
|
167
167
|
});
|
|
168
|
-
} else if (context.build &&
|
|
168
|
+
} else if (context.build && !context.urlGraph.hasDependent(cssUrlInfo)) {
|
|
169
169
|
context.urlGraph.deleteUrlInfo(cssUrlInfo.url);
|
|
170
170
|
}
|
|
171
171
|
const cssText = JS_QUOTES.escapeSpecialChars(cssUrlInfo.content, {
|
|
@@ -215,7 +215,7 @@ export default stylesheet;`,
|
|
|
215
215
|
specifier: textReference.url,
|
|
216
216
|
expectedType: "js_module",
|
|
217
217
|
});
|
|
218
|
-
} else if (context.build &&
|
|
218
|
+
} else if (context.build && !context.urlGraph.hasDependent(textUrlInfo)) {
|
|
219
219
|
context.urlGraph.deleteUrlInfo(textUrlInfo.url);
|
|
220
220
|
}
|
|
221
221
|
const textPlain = JS_QUOTES.escapeSpecialChars(urlInfo.content, {
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { injectQueryParams } from "@jsenv/urls";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
convertJsModuleToJsClassic,
|
|
9
|
+
systemJsClientFileUrlDefault,
|
|
10
|
+
} from "@jsenv/js-module-fallback";
|
|
8
11
|
|
|
9
12
|
export const jsenvPluginJsModuleConversion = ({
|
|
10
|
-
systemJsInjection,
|
|
11
|
-
systemJsClientFileUrl,
|
|
12
13
|
generateJsClassicFilename,
|
|
13
14
|
}) => {
|
|
14
15
|
const isReferencingJsModule = (reference) => {
|
|
@@ -95,7 +96,10 @@ export const jsenvPluginJsModuleConversion = ({
|
|
|
95
96
|
specifier: jsModuleReference.url,
|
|
96
97
|
expectedType: "js_module",
|
|
97
98
|
});
|
|
98
|
-
} else if (
|
|
99
|
+
} else if (
|
|
100
|
+
context.build &&
|
|
101
|
+
!context.urlGraph.hasDependent(jsModuleUrlInfo)
|
|
102
|
+
) {
|
|
99
103
|
context.urlGraph.deleteUrlInfo(jsModuleUrlInfo.url);
|
|
100
104
|
}
|
|
101
105
|
|
|
@@ -109,12 +113,16 @@ export const jsenvPluginJsModuleConversion = ({
|
|
|
109
113
|
// by an other file (for entry points)
|
|
110
114
|
// or to be able to import when it uses import
|
|
111
115
|
outputFormat = "system";
|
|
116
|
+
urlInfo.type = "js_classic";
|
|
117
|
+
context.referenceUtils.foundSideEffectFile({
|
|
118
|
+
sideEffectFileUrl: systemJsClientFileUrlDefault,
|
|
119
|
+
expectedType: "js_classic",
|
|
120
|
+
line: 0,
|
|
121
|
+
column: 0,
|
|
122
|
+
});
|
|
112
123
|
}
|
|
113
|
-
urlInfo.data.jsClassicFormat = outputFormat;
|
|
114
124
|
const { content, sourcemap } = await convertJsModuleToJsClassic({
|
|
115
125
|
rootDirectoryUrl: context.rootDirectoryUrl,
|
|
116
|
-
systemJsInjection,
|
|
117
|
-
systemJsClientFileUrl,
|
|
118
126
|
input: jsModuleUrlInfo.content,
|
|
119
127
|
inputIsEntryPoint: urlInfo.isEntryPoint,
|
|
120
128
|
inputSourcemap: jsModuleUrlInfo.sourcemap,
|
|
@@ -1,23 +1,14 @@
|
|
|
1
1
|
import { urlToFilename } from "@jsenv/urls";
|
|
2
|
-
import { systemJsClientFileUrlDefault } from "@jsenv/js-module-fallback";
|
|
3
2
|
|
|
4
3
|
import { jsenvPluginJsModuleConversion } from "./jsenv_plugin_js_module_conversion.js";
|
|
5
4
|
import { jsenvPluginJsModuleFallbackInsideHtml } from "./jsenv_plugin_js_module_fallback_inside_html.js";
|
|
6
5
|
import { jsenvPluginJsModuleFallbackOnWorkers } from "./jsenv_plugin_js_module_fallback_on_workers.js";
|
|
7
6
|
|
|
8
|
-
export const jsenvPluginJsModuleFallback = ({
|
|
9
|
-
systemJsInjection = true,
|
|
10
|
-
systemJsClientFileUrl = systemJsClientFileUrlDefault,
|
|
11
|
-
}) => {
|
|
7
|
+
export const jsenvPluginJsModuleFallback = () => {
|
|
12
8
|
return [
|
|
13
|
-
jsenvPluginJsModuleFallbackInsideHtml(
|
|
14
|
-
systemJsInjection,
|
|
15
|
-
systemJsClientFileUrl,
|
|
16
|
-
}),
|
|
9
|
+
jsenvPluginJsModuleFallbackInsideHtml(),
|
|
17
10
|
jsenvPluginJsModuleFallbackOnWorkers(),
|
|
18
11
|
jsenvPluginJsModuleConversion({
|
|
19
|
-
systemJsInjection,
|
|
20
|
-
systemJsClientFileUrl,
|
|
21
12
|
generateJsClassicFilename,
|
|
22
13
|
}),
|
|
23
14
|
];
|