@jsenv/plugin-as-js-classic 1.2.0 → 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/plugin-as-js-classic",
3
- "version": "1.2.0",
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.0.0",
28
- "@jsenv/js-module-fallback": "1.1.0",
29
- "@jsenv/plugin-bundling": "2.2.0"
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
- reference.filename = generateJsClassicFilename(reference.url);
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, context) => {
25
- const [jsModuleReference, jsModuleUrlInfo] =
26
- context.getWithoutSearchParam({
27
- urlInfo,
28
- context,
29
- searchParam: "as_js_classic",
30
- // override the expectedType to "js_module"
31
- // because when there is ?as_js_classic it means the underlying resource
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 context.cook(jsModuleUrlInfo, { reference: jsModuleReference });
40
- const loader = createUrlGraphLoader(context);
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 loader.getAllLoadDonePromise();
48
- const bundleUrlInfos = await bundleJsModules({
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 {
@@ -60,47 +50,53 @@ export const jsenvPluginAsJsClassic = () => {
60
50
  },
61
51
  });
62
52
  const jsModuleBundledUrlInfo = bundleUrlInfos[jsModuleUrlInfo.url];
63
- if (context.dev) {
64
- jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
65
- context.referenceUtils.inject({
66
- type: "js_url",
67
- specifier: sourceUrl,
68
- isImplicit: true,
69
- });
70
- });
71
- } else if (context.build) {
72
- jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
73
- const sourceUrlInfo = context.urlGraph.getUrlInfo(sourceUrl);
74
- if (sourceUrlInfo && !context.urlGraph.hasDependent(sourceUrlInfo)) {
75
- context.urlGraph.deleteUrlInfo(sourceUrl);
76
- }
77
- });
78
- }
79
53
 
80
54
  let outputFormat;
81
- if (urlInfo.isEntryPoint && !jsModuleBundledUrlInfo.data.usesImport) {
82
- // if it's an entry point without dependency (it does not use import)
83
- // then we can use UMD
84
- outputFormat = "umd";
85
- } else {
86
- // otherwise we have to use system in case it's imported
87
- // by an other file (for entry points)
88
- // or to be able to import when it uses import
55
+ // if imported by js, we have to use systemjs
56
+ // or if import things
57
+ if (jsModuleBundledUrlInfo.data.usesImport || isImportedByJs(urlInfo)) {
58
+ // we have to use system when it uses import
89
59
  outputFormat = "system";
90
60
  urlInfo.type = "js_classic";
91
- context.referenceUtils.foundSideEffectFile({
61
+ urlInfo.dependencies.foundSideEffectFile({
92
62
  sideEffectFileUrl: systemJsClientFileUrlDefault,
93
63
  expectedType: "js_classic",
94
64
  line: 0,
95
65
  column: 0,
96
66
  });
67
+ } else {
68
+ // if there is no dependency (it does not use import)
69
+ // then we can use UMD
70
+ outputFormat = "umd";
97
71
  }
72
+
73
+ if (urlInfo.context.dev) {
74
+ jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
75
+ urlInfo.dependencies.inject({
76
+ isImplicit: true,
77
+ type: "js_url",
78
+ specifier: sourceUrl,
79
+ });
80
+ });
81
+ } else if (urlInfo.context.build) {
82
+ jsModuleUrlInfo.firstReference.remove();
83
+ jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
84
+ const sourceUrlInfo = urlInfo.graph.getUrlInfo(sourceUrl);
85
+ if (
86
+ sourceUrlInfo &&
87
+ !sourceUrlInfo.getFirstReferenceFromOther({ ignoreWeak: true })
88
+ ) {
89
+ sourceUrlInfo.deleteFromGraph();
90
+ }
91
+ });
92
+ }
93
+
98
94
  const { content, sourcemap } = await convertJsModuleToJsClassic({
99
- rootDirectoryUrl: context.rootDirectoryUrl,
95
+ rootDirectoryUrl: urlInfo.context.rootDirectoryUrl,
100
96
  input: jsModuleBundledUrlInfo.content,
101
97
  inputSourcemap: jsModuleBundledUrlInfo.sourcemap,
102
- inputUrl: jsModuleBundledUrlInfo.url,
103
- outputUrl: jsModuleBundledUrlInfo.generatedUrl,
98
+ inputUrl: urlInfo.url,
99
+ outputUrl: jsModuleBundledUrlInfo.url,
104
100
  outputFormat,
105
101
  });
106
102
  return {
@@ -116,6 +112,19 @@ export const jsenvPluginAsJsClassic = () => {
116
112
  };
117
113
  };
118
114
 
115
+ const isImportedByJs = (jsModuleUrlInfo) => {
116
+ for (const referenceFromOther of jsModuleUrlInfo.referenceFromOthersSet) {
117
+ const urlInfoReferencingJsModule = referenceFromOther.ownerUrlInfo;
118
+ if (
119
+ urlInfoReferencingJsModule.type === "js_module" ||
120
+ urlInfoReferencingJsModule.type === "js_classic"
121
+ ) {
122
+ return true;
123
+ }
124
+ }
125
+ return false;
126
+ };
127
+
119
128
  const generateJsClassicFilename = (url) => {
120
129
  const filename = urlToFilename(url);
121
130
  let [basename, extension] = splitFileExtension(filename);
@@ -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
- };