@jsenv/plugin-as-js-classic 1.0.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 ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@jsenv/plugin-as-js-classic",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/jsenv/jsenv-core",
8
+ "directory": "packages/jsenv-plugin-as-js-classic"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./src/main.js"
17
+ },
18
+ "./*": "./*"
19
+ },
20
+ "files": [
21
+ "/src/"
22
+ ],
23
+ "scripts": {
24
+ "test": "node --conditions=development ./scripts/test.mjs"
25
+ },
26
+ "dependencies": {
27
+ "@jsenv/core": "34.2.0",
28
+ "@jsenv/plugin-bundling": "2.1.5"
29
+ },
30
+ "devDependencies": {
31
+ "@jsenv/core": "../../"
32
+ }
33
+ }
@@ -0,0 +1,113 @@
1
+ import { urlToFilename } from "@jsenv/urls"
2
+ import { bundleJsModules } from "@jsenv/plugin-bundling"
3
+
4
+ import { createUrlGraphLoader } from "@jsenv/core/src/kitchen/url_graph/url_graph_loader.js"
5
+ import { convertJsModuleToJsClassic } from "@jsenv/core/src/plugins/transpilation/js_module_fallback/convert_js_module_to_js_classic.js"
6
+
7
+ export const jsenvPluginAsJsClassic = ({
8
+ systemJsInjection = true,
9
+ systemJsClientFileUrl,
10
+ } = {}) => {
11
+ const markAsJsClassicProxy = (reference) => {
12
+ reference.expectedType = "js_classic"
13
+ reference.filename = generateJsClassicFilename(reference.url)
14
+ }
15
+
16
+ return {
17
+ name: "jsenv:as_js_classic",
18
+ appliesDuring: "*",
19
+ redirectUrl: (reference) => {
20
+ if (reference.searchParams.has("as_js_classic")) {
21
+ markAsJsClassicProxy(reference)
22
+ }
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) {
36
+ return null
37
+ }
38
+ // cook it to get content + dependencies
39
+ await context.cook(jsModuleUrlInfo, { reference: jsModuleReference })
40
+ const loader = createUrlGraphLoader(context)
41
+ loader.loadReferencedUrlInfos(jsModuleUrlInfo, {
42
+ // we ignore dynamic import to cook lazyly (as browser request the server)
43
+ // these dynamic imports must inherit "?as_js_classic"
44
+ // This is done inside rollup for convenience
45
+ ignoreDynamicImport: true,
46
+ })
47
+ await loader.getAllLoadDonePromise()
48
+ const bundleUrlInfos = await bundleJsModules({
49
+ jsModuleUrlInfos: [jsModuleUrlInfo],
50
+ context: {
51
+ ...context,
52
+ buildDirectoryUrl: new URL("./", import.meta.url),
53
+ },
54
+ preserveDynamicImport: true,
55
+ })
56
+ const jsModuleBundledUrlInfo = bundleUrlInfos[jsModuleUrlInfo.url]
57
+ if (context.dev) {
58
+ jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
59
+ context.referenceUtils.inject({
60
+ type: "js_url",
61
+ specifier: sourceUrl,
62
+ isImplicit: true,
63
+ })
64
+ })
65
+ } else if (context.build) {
66
+ jsModuleBundledUrlInfo.sourceUrls.forEach((sourceUrl) => {
67
+ const sourceUrlInfo = context.urlGraph.getUrlInfo(sourceUrl)
68
+ if (sourceUrlInfo && sourceUrlInfo.dependents.size === 0) {
69
+ context.urlGraph.deleteUrlInfo(sourceUrl)
70
+ }
71
+ })
72
+ }
73
+ const { content, sourcemap } = await convertJsModuleToJsClassic({
74
+ rootDirectoryUrl: context.rootDirectoryUrl,
75
+ systemJsInjection,
76
+ systemJsClientFileUrl,
77
+ urlInfo,
78
+ jsModuleUrlInfo: jsModuleBundledUrlInfo,
79
+ })
80
+ return {
81
+ content,
82
+ contentType: "text/javascript",
83
+ type: "js_classic",
84
+ originalUrl: urlInfo.originalUrl,
85
+ originalContent: jsModuleUrlInfo.originalContent,
86
+ sourcemap,
87
+ data: jsModuleUrlInfo.data,
88
+ }
89
+ },
90
+ }
91
+ }
92
+
93
+ const generateJsClassicFilename = (url) => {
94
+ const filename = urlToFilename(url)
95
+ let [basename, extension] = splitFileExtension(filename)
96
+ const { searchParams } = new URL(url)
97
+ if (
98
+ searchParams.has("as_json_module") ||
99
+ searchParams.has("as_css_module") ||
100
+ searchParams.has("as_text_module")
101
+ ) {
102
+ extension = ".js"
103
+ }
104
+ return `${basename}.nomodule${extension}`
105
+ }
106
+
107
+ const splitFileExtension = (filename) => {
108
+ const dotLastIndex = filename.lastIndexOf(".")
109
+ if (dotLastIndex === -1) {
110
+ return [filename, ""]
111
+ }
112
+ return [filename.slice(0, dotLastIndex), filename.slice(dotLastIndex)]
113
+ }
package/src/main.js ADDED
@@ -0,0 +1 @@
1
+ export { jsenvPluginAsJsClassic } from "./jsenv_plugin_as_js_classic.js"