@jsenv/plugin-commonjs 1.2.4 → 2.0.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-commonjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"test": "node --conditions=development ./scripts/test.mjs"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@jsenv/filesystem": "4.2.
|
|
27
|
+
"@jsenv/filesystem": "4.2.2",
|
|
28
28
|
"@jsenv/log": "3.3.4",
|
|
29
|
-
"@jsenv/url-meta": "
|
|
29
|
+
"@jsenv/url-meta": "8.1.0",
|
|
30
30
|
"@jsenv/urls": "1.2.8",
|
|
31
31
|
"@rollup/plugin-commonjs": "24.0.1",
|
|
32
32
|
"@rollup/plugin-json": "6.0.0",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"cjs-module-lexer": "1.2.2",
|
|
36
36
|
"is-valid-identifier": "2.0.2",
|
|
37
37
|
"resolve": "1.22.1",
|
|
38
|
-
"rollup": "3.
|
|
38
|
+
"rollup": "3.20.1",
|
|
39
39
|
"rollup-plugin-node-globals": "1.4.0",
|
|
40
40
|
"rollup-plugin-polyfill-node": "0.12.0",
|
|
41
41
|
"vm2": "3.9.14"
|
package/src/cjs_to_esm.js
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs"
|
|
2
|
-
import {
|
|
3
|
-
assertAndNormalizeDirectoryUrl,
|
|
4
|
-
ensureWindowsDriveLetter,
|
|
5
|
-
} from "@jsenv/filesystem"
|
|
2
|
+
import { ensureWindowsDriveLetter } from "@jsenv/filesystem"
|
|
6
3
|
|
|
7
4
|
import {
|
|
8
5
|
setUrlExtension,
|
|
9
6
|
setUrlFilename,
|
|
10
|
-
urlIsInsideOf,
|
|
11
|
-
moveUrl,
|
|
12
7
|
urlToExtension,
|
|
13
8
|
urlToBasename,
|
|
14
9
|
} from "@jsenv/urls"
|
|
@@ -18,33 +13,26 @@ import { reuseOrCreateCompiledFile } from "./compile_cache/compiled_file_cache.j
|
|
|
18
13
|
export const commonJsToJsModule = ({
|
|
19
14
|
logLevel,
|
|
20
15
|
filesystemCache = true,
|
|
21
|
-
|
|
16
|
+
compileCacheDirectoryUrl,
|
|
22
17
|
sourceFileUrl,
|
|
23
18
|
processEnvNodeEnv,
|
|
24
19
|
...rest
|
|
25
20
|
}) => {
|
|
26
21
|
if (filesystemCache) {
|
|
27
|
-
rootDirectoryUrl = assertAndNormalizeDirectoryUrl(rootDirectoryUrl)
|
|
28
|
-
const compileDirectoryUrl = new URL(
|
|
29
|
-
"./.jsenv/cjs_to_esm/",
|
|
30
|
-
rootDirectoryUrl,
|
|
31
|
-
)
|
|
32
22
|
const compiledFileUrl = determineCompiledFileUrl({
|
|
33
23
|
url: sourceFileUrl,
|
|
34
|
-
|
|
35
|
-
compileDirectoryUrl,
|
|
24
|
+
compileCacheDirectoryUrl,
|
|
36
25
|
processEnvNodeEnv,
|
|
37
26
|
})
|
|
38
27
|
|
|
39
28
|
return reuseOrCreateCompiledFile({
|
|
40
29
|
logLevel,
|
|
41
|
-
|
|
30
|
+
compileCacheDirectoryUrl,
|
|
42
31
|
sourceFileUrl,
|
|
43
32
|
compiledFileUrl,
|
|
44
33
|
compile: async () => {
|
|
45
34
|
const { content, sourcemap } = await commonJsToJsModuleRaw({
|
|
46
35
|
logLevel,
|
|
47
|
-
rootDirectoryUrl,
|
|
48
36
|
sourceFileUrl,
|
|
49
37
|
processEnvNodeEnv,
|
|
50
38
|
...rest,
|
|
@@ -66,7 +54,6 @@ export const commonJsToJsModule = ({
|
|
|
66
54
|
}
|
|
67
55
|
return commonJsToJsModuleRaw({
|
|
68
56
|
logLevel,
|
|
69
|
-
rootDirectoryUrl,
|
|
70
57
|
sourceFileUrl,
|
|
71
58
|
...rest,
|
|
72
59
|
})
|
|
@@ -110,23 +97,15 @@ const extractCompileAssets = ({
|
|
|
110
97
|
|
|
111
98
|
const determineCompiledFileUrl = ({
|
|
112
99
|
url,
|
|
113
|
-
|
|
114
|
-
compileDirectoryUrl,
|
|
100
|
+
compileCacheDirectoryUrl,
|
|
115
101
|
processEnvNodeEnv,
|
|
116
102
|
}) => {
|
|
117
|
-
if (!urlIsInsideOf(url, rootDirectoryUrl)) {
|
|
118
|
-
const fsRootUrl = ensureWindowsDriveLetter("file:///", url)
|
|
119
|
-
url = `${rootDirectoryUrl}@fs/${url.slice(fsRootUrl.length)}`
|
|
120
|
-
}
|
|
121
103
|
if (processEnvNodeEnv) {
|
|
122
104
|
const basename = urlToBasename(url)
|
|
123
105
|
const extension = urlToExtension(url)
|
|
124
106
|
url = setUrlFilename(url, `${basename}.${processEnvNodeEnv}${extension}`)
|
|
125
107
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
to: compileDirectoryUrl,
|
|
130
|
-
preferAbsolute: true,
|
|
131
|
-
})
|
|
108
|
+
const fsRootUrl = ensureWindowsDriveLetter("file:///", url)
|
|
109
|
+
url = `${compileCacheDirectoryUrl}@fs/${url.slice(fsRootUrl.length)}`
|
|
110
|
+
return url
|
|
132
111
|
}
|
|
@@ -13,7 +13,7 @@ const { lockForResource } = createLockRegistry()
|
|
|
13
13
|
|
|
14
14
|
export const reuseOrCreateCompiledFile = async ({
|
|
15
15
|
logLevel,
|
|
16
|
-
|
|
16
|
+
compileCacheDirectoryUrl,
|
|
17
17
|
sourceFileUrl,
|
|
18
18
|
compiledFileUrl,
|
|
19
19
|
compileCacheStrategy,
|
|
@@ -21,7 +21,7 @@ export const reuseOrCreateCompiledFile = async ({
|
|
|
21
21
|
compile,
|
|
22
22
|
}) => {
|
|
23
23
|
const logger = createLogger({ logLevel })
|
|
24
|
-
await
|
|
24
|
+
await initCompileCacheDirectory({ logger, compileCacheDirectoryUrl })
|
|
25
25
|
sourceFileUrl = assertAndNormalizeFileUrl(sourceFileUrl)
|
|
26
26
|
if (typeof compile !== "function") {
|
|
27
27
|
throw new TypeError(`compile must be a function, got ${compile}`)
|
|
@@ -107,15 +107,18 @@ export const reuseOrCreateCompiledFile = async ({
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
const initalized = {}
|
|
110
|
-
const
|
|
111
|
-
|
|
110
|
+
const initCompileCacheDirectory = async ({
|
|
111
|
+
logger,
|
|
112
|
+
compileCacheDirectoryUrl,
|
|
113
|
+
}) => {
|
|
114
|
+
if (initalized[compileCacheDirectoryUrl]) {
|
|
112
115
|
return
|
|
113
116
|
}
|
|
114
|
-
initalized[
|
|
115
|
-
logger.debug(`check compile directory at ${
|
|
117
|
+
initalized[compileCacheDirectoryUrl] = true
|
|
118
|
+
logger.debug(`check compile directory at ${compileCacheDirectoryUrl}`)
|
|
116
119
|
const compileContextJsonFileUrl = new URL(
|
|
117
120
|
"./__compile_context__.json",
|
|
118
|
-
|
|
121
|
+
compileCacheDirectoryUrl,
|
|
119
122
|
)
|
|
120
123
|
const version = JSON.parse(
|
|
121
124
|
readFileSync(new URL("../../package.json", import.meta.url)),
|
|
@@ -132,7 +135,7 @@ const initCompileDirectory = async ({ logger, compileDirectoryUrl }) => {
|
|
|
132
135
|
} else {
|
|
133
136
|
logger.debug(`${UNICODE.INFO} create an empty directory`)
|
|
134
137
|
}
|
|
135
|
-
await ensureEmptyDirectory(
|
|
138
|
+
await ensureEmptyDirectory(compileCacheDirectoryUrl)
|
|
136
139
|
writeFileSync(
|
|
137
140
|
compileContextJsonFileUrl,
|
|
138
141
|
JSON.stringify({ version }, null, " "),
|
|
@@ -4,6 +4,8 @@ import { defaultLookupPackageScope } from "@jsenv/node-esm-resolution"
|
|
|
4
4
|
|
|
5
5
|
import { commonJsToJsModule } from "./cjs_to_esm.js"
|
|
6
6
|
|
|
7
|
+
const compileCacheDirectoryUrl = new URL("../.cache/", import.meta.url)
|
|
8
|
+
|
|
7
9
|
export const jsenvPluginCommonJs = ({
|
|
8
10
|
name = "jsenv:commonjs",
|
|
9
11
|
logLevel,
|
|
@@ -75,7 +77,7 @@ export const jsenvPluginCommonJs = ({
|
|
|
75
77
|
)
|
|
76
78
|
const { content, sourcemap, isValid } = await commonJsToJsModule({
|
|
77
79
|
logLevel,
|
|
78
|
-
|
|
80
|
+
compileCacheDirectoryUrl,
|
|
79
81
|
sourceFileUrl: commonJsUrlInfo.url,
|
|
80
82
|
browsers: !nodeRuntimeEnabled,
|
|
81
83
|
processEnvNodeEnv: context.dev ? "development" : "production",
|