@jsenv/plugin-as-js-classic 1.1.3 → 1.2.1

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.1.3",
3
+ "version": "1.2.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@jsenv/urls": "2.0.0",
28
- "@jsenv/js-module-fallback": "1.0.4",
29
- "@jsenv/plugin-bundling": "2.1.11"
28
+ "@jsenv/js-module-fallback": "1.1.0",
29
+ "@jsenv/plugin-bundling": "2.2.1"
30
30
  }
31
31
  }
@@ -1,18 +1,14 @@
1
1
  import { urlToFilename } from "@jsenv/urls";
2
- import { convertJsModuleToJsClassic } from "@jsenv/js-module-fallback";
2
+ import {
3
+ convertJsModuleToJsClassic,
4
+ systemJsClientFileUrlDefault,
5
+ } from "@jsenv/js-module-fallback";
3
6
  import { bundleJsModules } from "@jsenv/plugin-bundling";
4
7
 
5
8
  import { createUrlGraphLoader } from "./url_graph_loader.js";
6
9
 
7
- export const jsenvPluginAsJsClassic = ({
8
- systemJsInjection = true,
9
- systemJsClientFileUrl,
10
- } = {}) => {
10
+ export const jsenvPluginAsJsClassic = () => {
11
11
  const markAsJsClassicProxy = (reference) => {
12
- if (reference.searchParams.has("dynamic_import")) {
13
- } else {
14
- reference.isEntryPoint = true;
15
- }
16
12
  reference.expectedType = "js_classic";
17
13
  reference.filename = generateJsClassicFilename(reference.url);
18
14
  };
@@ -64,6 +60,29 @@ export const jsenvPluginAsJsClassic = ({
64
60
  },
65
61
  });
66
62
  const jsModuleBundledUrlInfo = bundleUrlInfos[jsModuleUrlInfo.url];
63
+
64
+ let outputFormat;
65
+ // if imported by js, we have to use systemjs
66
+ // or if import things
67
+ if (
68
+ jsModuleBundledUrlInfo.data.usesImport ||
69
+ isImportedByJs(urlInfo, context.urlGraph)
70
+ ) {
71
+ // we have to use system when it uses import
72
+ outputFormat = "system";
73
+ urlInfo.type = "js_classic";
74
+ context.referenceUtils.foundSideEffectFile({
75
+ sideEffectFileUrl: systemJsClientFileUrlDefault,
76
+ expectedType: "js_classic",
77
+ line: 0,
78
+ column: 0,
79
+ });
80
+ } else {
81
+ // if there is no dependency (it does not use import)
82
+ // then we can use UMD
83
+ outputFormat = "umd";
84
+ }
85
+
67
86
  if (context.dev) {
68
87
  jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
69
88
  context.referenceUtils.inject({
@@ -75,30 +94,15 @@ export const jsenvPluginAsJsClassic = ({
75
94
  } else if (context.build) {
76
95
  jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
77
96
  const sourceUrlInfo = context.urlGraph.getUrlInfo(sourceUrl);
78
- if (sourceUrlInfo && sourceUrlInfo.dependents.size === 0) {
97
+ if (sourceUrlInfo && !context.urlGraph.hasDependent(sourceUrlInfo)) {
79
98
  context.urlGraph.deleteUrlInfo(sourceUrl);
80
99
  }
81
100
  });
82
101
  }
83
102
 
84
- let outputFormat;
85
- if (urlInfo.isEntryPoint && !jsModuleUrlInfo.data.usesImport) {
86
- // if it's an entry point without dependency (it does not use import)
87
- // then we can use UMD
88
- outputFormat = "umd";
89
- } else {
90
- // otherwise we have to use system in case it's imported
91
- // by an other file (for entry points)
92
- // or to be able to import when it uses import
93
- outputFormat = "system";
94
- }
95
- urlInfo.data.jsClassicFormat = outputFormat;
96
103
  const { content, sourcemap } = await convertJsModuleToJsClassic({
97
104
  rootDirectoryUrl: context.rootDirectoryUrl,
98
- systemJsInjection,
99
- systemJsClientFileUrl,
100
105
  input: jsModuleBundledUrlInfo.content,
101
- inputIsEntryPoint: urlInfo.isEntryPoint,
102
106
  inputSourcemap: jsModuleBundledUrlInfo.sourcemap,
103
107
  inputUrl: jsModuleBundledUrlInfo.url,
104
108
  outputUrl: jsModuleBundledUrlInfo.generatedUrl,
@@ -117,6 +121,19 @@ export const jsenvPluginAsJsClassic = ({
117
121
  };
118
122
  };
119
123
 
124
+ const isImportedByJs = (jsModuleUrlInfo, urlGraph) => {
125
+ for (const dependentUrl of jsModuleUrlInfo.dependents) {
126
+ const dependentUrlInfo = urlGraph.getUrlInfo(dependentUrl);
127
+ if (
128
+ dependentUrlInfo.type === "js_module" ||
129
+ dependentUrlInfo.type === "js_classic"
130
+ ) {
131
+ return true;
132
+ }
133
+ }
134
+ return false;
135
+ };
136
+
120
137
  const generateJsClassicFilename = (url) => {
121
138
  const filename = urlToFilename(url);
122
139
  let [basename, extension] = splitFileExtension(filename);