@jsenv/plugin-commonjs 1.0.1 → 1.0.2
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 +2 -2
- package/src/jsenv_plugin_commonjs.js +39 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-commonjs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"main": "./main.js",
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@jsenv/filesystem": "4.1.3",
|
|
17
|
-
"@jsenv/log": "3.
|
|
17
|
+
"@jsenv/log": "3.3.0",
|
|
18
18
|
"@jsenv/url-meta": "7.0.0",
|
|
19
19
|
"@jsenv/urls": "1.2.7",
|
|
20
20
|
"@rollup/plugin-commonjs": "22.0.1",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { URL_META } from "@jsenv/url-meta"
|
|
2
2
|
import { injectQueryParams, urlToExtension } from "@jsenv/urls"
|
|
3
|
+
import { defaultLookupPackageScope } from "@jsenv/node-esm-resolution"
|
|
3
4
|
|
|
4
5
|
import { commonJsToJsModule } from "./cjs_to_esm.js"
|
|
5
6
|
|
|
@@ -8,6 +9,23 @@ export const jsenvPluginCommonJs = ({
|
|
|
8
9
|
logLevel,
|
|
9
10
|
include,
|
|
10
11
|
}) => {
|
|
12
|
+
const markAsJsModuleProxy = (reference) => {
|
|
13
|
+
reference.expectedType = "js_module"
|
|
14
|
+
const packageFileUrl = defaultLookupPackageScope(reference.url)
|
|
15
|
+
if (packageFileUrl) {
|
|
16
|
+
reference.filename = `${reference.specifier}${urlToExtension(
|
|
17
|
+
reference.parentUrl,
|
|
18
|
+
)}`
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const turnIntoJsModuleProxy = (reference) => {
|
|
22
|
+
const urlTransformed = injectQueryParams(reference.url, {
|
|
23
|
+
cjs_as_js_module: "",
|
|
24
|
+
})
|
|
25
|
+
markAsJsModuleProxy(reference)
|
|
26
|
+
return urlTransformed
|
|
27
|
+
}
|
|
28
|
+
|
|
11
29
|
let associations
|
|
12
30
|
|
|
13
31
|
return {
|
|
@@ -23,6 +41,10 @@ export const jsenvPluginCommonJs = ({
|
|
|
23
41
|
},
|
|
24
42
|
redirectUrl: {
|
|
25
43
|
js_import_export: (reference) => {
|
|
44
|
+
if (reference.searchParams.has("cjs_as_js_module")) {
|
|
45
|
+
markAsJsModuleProxy(reference)
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
26
48
|
const { commonjs } = URL_META.applyAssociations({
|
|
27
49
|
url: reference.url,
|
|
28
50
|
associations,
|
|
@@ -31,30 +53,32 @@ export const jsenvPluginCommonJs = ({
|
|
|
31
53
|
return null
|
|
32
54
|
}
|
|
33
55
|
reference.data.commonjs = commonjs
|
|
34
|
-
return
|
|
35
|
-
cjs_as_js_module: "",
|
|
36
|
-
})
|
|
56
|
+
return turnIntoJsModuleProxy(reference)
|
|
37
57
|
},
|
|
38
58
|
},
|
|
39
59
|
fetchUrlContent: async (urlInfo, context) => {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
60
|
+
const [commonJsReference, commonJsUrlInfo] =
|
|
61
|
+
context.getWithoutSearchParam({
|
|
62
|
+
urlInfo,
|
|
63
|
+
context,
|
|
64
|
+
searchParam: "cjs_as_js_module",
|
|
65
|
+
// during this fetch we don't want to alter the original file
|
|
66
|
+
// so we consider it as text
|
|
67
|
+
expectedType: "text",
|
|
68
|
+
})
|
|
69
|
+
if (!commonJsReference) {
|
|
49
70
|
return null
|
|
50
71
|
}
|
|
72
|
+
await context.fetchUrlContent(commonJsUrlInfo, {
|
|
73
|
+
reference: commonJsReference,
|
|
74
|
+
})
|
|
51
75
|
const nodeRuntimeEnabled = Object.keys(context.runtimeCompat).includes(
|
|
52
76
|
"node",
|
|
53
77
|
)
|
|
54
78
|
const { content, sourcemap, isValid } = await commonJsToJsModule({
|
|
55
79
|
logLevel,
|
|
56
80
|
rootDirectoryUrl: context.rootDirectoryUrl,
|
|
57
|
-
sourceFileUrl:
|
|
81
|
+
sourceFileUrl: commonJsUrlInfo.url,
|
|
58
82
|
browsers: !nodeRuntimeEnabled,
|
|
59
83
|
processEnvNodeEnv: context.scenarios.dev ? "development" : "production",
|
|
60
84
|
...urlInfo.data.commonjs,
|
|
@@ -62,22 +86,13 @@ export const jsenvPluginCommonJs = ({
|
|
|
62
86
|
if (isValid) {
|
|
63
87
|
urlInfo.isValid = isValid
|
|
64
88
|
}
|
|
65
|
-
const originalReference = context.reference.original
|
|
66
|
-
? context.reference.original
|
|
67
|
-
: context.reference
|
|
68
|
-
const filename = originalReference.dependsOnPackageJson
|
|
69
|
-
? `${originalReference.specifier}${urlToExtension(
|
|
70
|
-
originalReference.parentUrl,
|
|
71
|
-
)}`
|
|
72
|
-
: undefined
|
|
73
89
|
return {
|
|
74
90
|
content,
|
|
75
91
|
contentType: "text/javascript",
|
|
76
92
|
type: "js_module",
|
|
77
|
-
originalUrl:
|
|
78
|
-
originalContent:
|
|
93
|
+
originalUrl: commonJsUrlInfo.originalUrl,
|
|
94
|
+
originalContent: commonJsUrlInfo.originalContent,
|
|
79
95
|
sourcemap,
|
|
80
|
-
filename,
|
|
81
96
|
}
|
|
82
97
|
},
|
|
83
98
|
}
|