@jsenv/plugin-as-js-classic 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 +4 -4
- package/src/jsenv_plugin_as_js_classic.js +36 -45
- package/src/url_graph_loader.js +0 -79
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-as-js-classic",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"test": "node --conditions=development ./scripts/test.mjs"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@jsenv/urls": "2.
|
|
28
|
-
"@jsenv/js-module-fallback": "1.
|
|
29
|
-
"@jsenv/plugin-bundling": "2.
|
|
27
|
+
"@jsenv/urls": "2.1.0",
|
|
28
|
+
"@jsenv/js-module-fallback": "1.2.0",
|
|
29
|
+
"@jsenv/plugin-bundling": "2.3.0"
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -5,12 +5,12 @@ import {
|
|
|
5
5
|
} from "@jsenv/js-module-fallback";
|
|
6
6
|
import { bundleJsModules } from "@jsenv/plugin-bundling";
|
|
7
7
|
|
|
8
|
-
import { createUrlGraphLoader } from "./url_graph_loader.js";
|
|
9
|
-
|
|
10
8
|
export const jsenvPluginAsJsClassic = () => {
|
|
11
9
|
const markAsJsClassicProxy = (reference) => {
|
|
12
10
|
reference.expectedType = "js_classic";
|
|
13
|
-
|
|
11
|
+
if (!reference.filename) {
|
|
12
|
+
reference.filename = generateJsClassicFilename(reference.url);
|
|
13
|
+
}
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
return {
|
|
@@ -21,36 +21,26 @@ export const jsenvPluginAsJsClassic = () => {
|
|
|
21
21
|
markAsJsClassicProxy(reference);
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
fetchUrlContent: async (urlInfo
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// is a js_module
|
|
33
|
-
expectedType: "js_module",
|
|
34
|
-
});
|
|
35
|
-
if (!jsModuleReference) {
|
|
24
|
+
fetchUrlContent: async (urlInfo) => {
|
|
25
|
+
const jsModuleUrlInfo = urlInfo.getWithoutSearchParam("as_js_classic", {
|
|
26
|
+
// override the expectedType to "js_module"
|
|
27
|
+
// because when there is ?as_js_classic it means the underlying resource
|
|
28
|
+
// is a js_module
|
|
29
|
+
expectedType: "js_module",
|
|
30
|
+
});
|
|
31
|
+
if (!jsModuleUrlInfo) {
|
|
36
32
|
return null;
|
|
37
33
|
}
|
|
38
34
|
// cook it to get content + dependencies
|
|
39
|
-
await
|
|
40
|
-
|
|
41
|
-
loader.loadReferencedUrlInfos(jsModuleUrlInfo, {
|
|
35
|
+
await jsModuleUrlInfo.cook();
|
|
36
|
+
await jsModuleUrlInfo.cookDependencies({
|
|
42
37
|
// we ignore dynamic import to cook lazyly (as browser request the server)
|
|
43
38
|
// these dynamic imports must inherit "?as_js_classic"
|
|
44
39
|
// This is done inside rollup for convenience
|
|
45
40
|
ignoreDynamicImport: true,
|
|
46
41
|
});
|
|
47
|
-
await
|
|
48
|
-
|
|
49
|
-
jsModuleUrlInfos: [jsModuleUrlInfo],
|
|
50
|
-
context: {
|
|
51
|
-
...context,
|
|
52
|
-
buildDirectoryUrl: new URL("./", import.meta.url),
|
|
53
|
-
},
|
|
42
|
+
const bundleUrlInfos = await bundleJsModules([jsModuleUrlInfo], {
|
|
43
|
+
buildDirectoryUrl: new URL("./", import.meta.url),
|
|
54
44
|
preserveDynamicImport: true,
|
|
55
45
|
augmentDynamicImportUrlSearchParams: () => {
|
|
56
46
|
return {
|
|
@@ -64,14 +54,11 @@ export const jsenvPluginAsJsClassic = () => {
|
|
|
64
54
|
let outputFormat;
|
|
65
55
|
// if imported by js, we have to use systemjs
|
|
66
56
|
// or if import things
|
|
67
|
-
if (
|
|
68
|
-
jsModuleBundledUrlInfo.data.usesImport ||
|
|
69
|
-
isImportedByJs(urlInfo, context.urlGraph)
|
|
70
|
-
) {
|
|
57
|
+
if (jsModuleBundledUrlInfo.data.usesImport || isImportedByJs(urlInfo)) {
|
|
71
58
|
// we have to use system when it uses import
|
|
72
59
|
outputFormat = "system";
|
|
73
60
|
urlInfo.type = "js_classic";
|
|
74
|
-
|
|
61
|
+
urlInfo.dependencies.foundSideEffectFile({
|
|
75
62
|
sideEffectFileUrl: systemJsClientFileUrlDefault,
|
|
76
63
|
expectedType: "js_classic",
|
|
77
64
|
line: 0,
|
|
@@ -83,29 +70,33 @@ export const jsenvPluginAsJsClassic = () => {
|
|
|
83
70
|
outputFormat = "umd";
|
|
84
71
|
}
|
|
85
72
|
|
|
86
|
-
if (context.dev) {
|
|
73
|
+
if (urlInfo.context.dev) {
|
|
87
74
|
jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
|
|
88
|
-
|
|
75
|
+
urlInfo.dependencies.inject({
|
|
76
|
+
isImplicit: true,
|
|
89
77
|
type: "js_url",
|
|
90
78
|
specifier: sourceUrl,
|
|
91
|
-
isImplicit: true,
|
|
92
79
|
});
|
|
93
80
|
});
|
|
94
|
-
} else if (context.build) {
|
|
81
|
+
} else if (urlInfo.context.build) {
|
|
82
|
+
jsModuleUrlInfo.firstReference.remove();
|
|
95
83
|
jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
|
|
96
|
-
const sourceUrlInfo =
|
|
97
|
-
if (
|
|
98
|
-
|
|
84
|
+
const sourceUrlInfo = urlInfo.graph.getUrlInfo(sourceUrl);
|
|
85
|
+
if (
|
|
86
|
+
sourceUrlInfo &&
|
|
87
|
+
!sourceUrlInfo.getFirstReferenceFromOther({ ignoreWeak: true })
|
|
88
|
+
) {
|
|
89
|
+
sourceUrlInfo.deleteFromGraph();
|
|
99
90
|
}
|
|
100
91
|
});
|
|
101
92
|
}
|
|
102
93
|
|
|
103
94
|
const { content, sourcemap } = await convertJsModuleToJsClassic({
|
|
104
|
-
rootDirectoryUrl: context.rootDirectoryUrl,
|
|
95
|
+
rootDirectoryUrl: urlInfo.context.rootDirectoryUrl,
|
|
105
96
|
input: jsModuleBundledUrlInfo.content,
|
|
106
97
|
inputSourcemap: jsModuleBundledUrlInfo.sourcemap,
|
|
107
|
-
inputUrl:
|
|
108
|
-
outputUrl: jsModuleBundledUrlInfo.
|
|
98
|
+
inputUrl: urlInfo.url,
|
|
99
|
+
outputUrl: jsModuleBundledUrlInfo.url,
|
|
109
100
|
outputFormat,
|
|
110
101
|
});
|
|
111
102
|
return {
|
|
@@ -121,12 +112,12 @@ export const jsenvPluginAsJsClassic = () => {
|
|
|
121
112
|
};
|
|
122
113
|
};
|
|
123
114
|
|
|
124
|
-
const isImportedByJs = (jsModuleUrlInfo
|
|
125
|
-
for (const
|
|
126
|
-
const
|
|
115
|
+
const isImportedByJs = (jsModuleUrlInfo) => {
|
|
116
|
+
for (const referenceFromOther of jsModuleUrlInfo.referenceFromOthersSet) {
|
|
117
|
+
const urlInfoReferencingJsModule = referenceFromOther.ownerUrlInfo;
|
|
127
118
|
if (
|
|
128
|
-
|
|
129
|
-
|
|
119
|
+
urlInfoReferencingJsModule.type === "js_module" ||
|
|
120
|
+
urlInfoReferencingJsModule.type === "js_classic"
|
|
130
121
|
) {
|
|
131
122
|
return true;
|
|
132
123
|
}
|
package/src/url_graph_loader.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
// copy pasted from @jsenv/core to avoid dependency
|
|
2
|
-
|
|
3
|
-
export const createUrlGraphLoader = (context) => {
|
|
4
|
-
const promises = [];
|
|
5
|
-
const promiseMap = new Map();
|
|
6
|
-
const load = (
|
|
7
|
-
urlInfo,
|
|
8
|
-
dishContext,
|
|
9
|
-
{ ignoreRessourceHint = true, ignoreDynamicImport = false } = {},
|
|
10
|
-
) => {
|
|
11
|
-
const promiseFromData = promiseMap.get(urlInfo);
|
|
12
|
-
if (promiseFromData) return promiseFromData;
|
|
13
|
-
const promise = (async () => {
|
|
14
|
-
await context.cook(urlInfo, {
|
|
15
|
-
cookDuringCook: load,
|
|
16
|
-
...dishContext,
|
|
17
|
-
});
|
|
18
|
-
loadReferencedUrlInfos(urlInfo, {
|
|
19
|
-
ignoreRessourceHint,
|
|
20
|
-
ignoreDynamicImport,
|
|
21
|
-
});
|
|
22
|
-
})();
|
|
23
|
-
promises.push(promise);
|
|
24
|
-
promiseMap.set(urlInfo, promise);
|
|
25
|
-
return promise;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const loadReferencedUrlInfos = (
|
|
29
|
-
urlInfo,
|
|
30
|
-
{ ignoreRessourceHint, ignoreDynamicImport },
|
|
31
|
-
) => {
|
|
32
|
-
const { references } = urlInfo;
|
|
33
|
-
references.forEach((reference) => {
|
|
34
|
-
// we don't cook resource hints
|
|
35
|
-
// because they might refer to resource that will be modified during build
|
|
36
|
-
// It also means something else have to reference that url in order to cook it
|
|
37
|
-
// so that the preload is deleted by "resync_resource_hints.js" otherwise
|
|
38
|
-
if (ignoreRessourceHint && reference.isResourceHint) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
if (ignoreDynamicImport && reference.subtype === "import_dynamic") {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
// we use reference.generatedUrl to mimic what a browser would do:
|
|
45
|
-
// do a fetch to the specifier as found in the file
|
|
46
|
-
const referencedUrlInfo = context.urlGraph.reuseOrCreateUrlInfo(
|
|
47
|
-
reference.generatedUrl,
|
|
48
|
-
);
|
|
49
|
-
load(referencedUrlInfo, {
|
|
50
|
-
reference,
|
|
51
|
-
ignoreRessourceHint,
|
|
52
|
-
ignoreDynamicImport,
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const getAllLoadDonePromise = async (operation) => {
|
|
58
|
-
const waitAll = async () => {
|
|
59
|
-
if (operation) {
|
|
60
|
-
operation.throwIfAborted();
|
|
61
|
-
}
|
|
62
|
-
if (promises.length === 0) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const promisesToWait = promises.slice();
|
|
66
|
-
promises.length = 0;
|
|
67
|
-
await Promise.all(promisesToWait);
|
|
68
|
-
await waitAll();
|
|
69
|
-
};
|
|
70
|
-
await waitAll();
|
|
71
|
-
promiseMap.clear();
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
load,
|
|
76
|
-
loadReferencedUrlInfos,
|
|
77
|
-
getAllLoadDonePromise,
|
|
78
|
-
};
|
|
79
|
-
};
|