@jsenv/plugin-commonjs 2.0.4 → 2.0.6
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 +7 -7
- package/src/cjs_to_esm.js +30 -30
- package/src/cjs_to_esm_raw.js +34 -34
- package/src/compile_cache/compiled_file_cache.js +67 -67
- package/src/compile_cache/file_lock_registry.js +15 -15
- package/src/compile_cache/update_compile_cache.js +31 -31
- package/src/compile_cache/validate_compile_cache.js +111 -111
- package/src/jsenv_plugin_commonjs.js +31 -31
- package/src/main.js +2 -2
- package/src/rollup_plugin_commonjs_named_exports.js +70 -70
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-commonjs",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/jsenv/
|
|
7
|
+
"url": "https://github.com/jsenv/core",
|
|
8
8
|
"directory": "packages/jsenv-plugin-commonjs"
|
|
9
9
|
},
|
|
10
10
|
"publishConfig": {
|
|
@@ -25,20 +25,20 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@jsenv/filesystem": "4.2.3",
|
|
28
|
-
"@jsenv/log": "3.3.
|
|
28
|
+
"@jsenv/log": "3.3.5",
|
|
29
29
|
"@jsenv/url-meta": "8.1.0",
|
|
30
30
|
"@jsenv/urls": "2.0.0",
|
|
31
|
-
"@rollup/plugin-commonjs": "
|
|
31
|
+
"@rollup/plugin-commonjs": "25.0.0",
|
|
32
32
|
"@rollup/plugin-json": "6.0.0",
|
|
33
33
|
"@rollup/plugin-node-resolve": "15.0.2",
|
|
34
34
|
"@rollup/plugin-replace": "5.0.2",
|
|
35
35
|
"cjs-module-lexer": "1.2.2",
|
|
36
36
|
"is-valid-identifier": "2.0.2",
|
|
37
|
-
"resolve": "1.22.
|
|
38
|
-
"rollup": "3.
|
|
37
|
+
"resolve": "1.22.2",
|
|
38
|
+
"rollup": "3.21.8",
|
|
39
39
|
"rollup-plugin-node-globals": "1.4.0",
|
|
40
40
|
"rollup-plugin-polyfill-node": "0.12.0",
|
|
41
|
-
"vm2": "3.9.
|
|
41
|
+
"vm2": "3.9.19"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@jsenv/core": "../../"
|
package/src/cjs_to_esm.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs"
|
|
2
|
-
import { ensureWindowsDriveLetter } from "@jsenv/filesystem"
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { ensureWindowsDriveLetter } from "@jsenv/filesystem";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
setUrlExtension,
|
|
6
6
|
setUrlFilename,
|
|
7
7
|
urlToExtension,
|
|
8
8
|
urlToBasename,
|
|
9
|
-
} from "@jsenv/urls"
|
|
10
|
-
import { commonJsToJsModuleRaw } from "./cjs_to_esm_raw.js"
|
|
11
|
-
import { reuseOrCreateCompiledFile } from "./compile_cache/compiled_file_cache.js"
|
|
9
|
+
} from "@jsenv/urls";
|
|
10
|
+
import { commonJsToJsModuleRaw } from "./cjs_to_esm_raw.js";
|
|
11
|
+
import { reuseOrCreateCompiledFile } from "./compile_cache/compiled_file_cache.js";
|
|
12
12
|
|
|
13
13
|
export const commonJsToJsModule = ({
|
|
14
14
|
logLevel,
|
|
@@ -23,7 +23,7 @@ export const commonJsToJsModule = ({
|
|
|
23
23
|
url: sourceFileUrl,
|
|
24
24
|
compileCacheDirectoryUrl,
|
|
25
25
|
processEnvNodeEnv,
|
|
26
|
-
})
|
|
26
|
+
});
|
|
27
27
|
|
|
28
28
|
return reuseOrCreateCompiledFile({
|
|
29
29
|
logLevel,
|
|
@@ -36,28 +36,28 @@ export const commonJsToJsModule = ({
|
|
|
36
36
|
sourceFileUrl,
|
|
37
37
|
processEnvNodeEnv,
|
|
38
38
|
...rest,
|
|
39
|
-
})
|
|
39
|
+
});
|
|
40
40
|
const assets = extractCompileAssets({
|
|
41
41
|
sourceUrl: sourceFileUrl,
|
|
42
42
|
sourceContent: String(readFileSync(new URL(sourceFileUrl))),
|
|
43
43
|
compiledUrl: compiledFileUrl,
|
|
44
44
|
compiledContent: content,
|
|
45
45
|
sourcemap,
|
|
46
|
-
})
|
|
46
|
+
});
|
|
47
47
|
return {
|
|
48
48
|
content,
|
|
49
49
|
sourcemap,
|
|
50
50
|
assets,
|
|
51
|
-
}
|
|
51
|
+
};
|
|
52
52
|
},
|
|
53
|
-
})
|
|
53
|
+
});
|
|
54
54
|
}
|
|
55
55
|
return commonJsToJsModuleRaw({
|
|
56
56
|
logLevel,
|
|
57
57
|
sourceFileUrl,
|
|
58
58
|
...rest,
|
|
59
|
-
})
|
|
60
|
-
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
61
|
|
|
62
62
|
const extractCompileAssets = ({
|
|
63
63
|
sourceUrl,
|
|
@@ -65,35 +65,35 @@ const extractCompileAssets = ({
|
|
|
65
65
|
compiledUrl,
|
|
66
66
|
sourcemap,
|
|
67
67
|
}) => {
|
|
68
|
-
const compileAssets = {}
|
|
68
|
+
const compileAssets = {};
|
|
69
69
|
|
|
70
70
|
// ensure the source file is part of sources no matter what
|
|
71
71
|
// it covers cases where sourcemap.sources is empty
|
|
72
72
|
// or do not contain the real source file
|
|
73
73
|
// there is a check to prevent duplicate sources in case the sourcemap is correct and contains
|
|
74
74
|
// the source file in sourcemap.sources
|
|
75
|
-
compileAssets[sourceUrl] = { type: "source", content: sourceContent }
|
|
75
|
+
compileAssets[sourceUrl] = { type: "source", content: sourceContent };
|
|
76
76
|
if (sourcemap) {
|
|
77
77
|
sourcemap.sources.forEach((source, index) => {
|
|
78
78
|
if (source.startsWith("file://")) {
|
|
79
79
|
const contentFromSourcemap = sourcemap.sourcesContent
|
|
80
80
|
? sourcemap.sourcesContent[index]
|
|
81
|
-
: null
|
|
81
|
+
: null;
|
|
82
82
|
const content =
|
|
83
|
-
contentFromSourcemap || String(readFileSync(new URL(source)))
|
|
84
|
-
compileAssets[source] = { type: "source", content }
|
|
83
|
+
contentFromSourcemap || String(readFileSync(new URL(source)));
|
|
84
|
+
compileAssets[source] = { type: "source", content };
|
|
85
85
|
}
|
|
86
|
-
})
|
|
87
|
-
const sourcemapUrl = setUrlExtension(compiledUrl, ".map")
|
|
88
|
-
const sourcemapContent = JSON.stringify(sourcemap)
|
|
86
|
+
});
|
|
87
|
+
const sourcemapUrl = setUrlExtension(compiledUrl, ".map");
|
|
88
|
+
const sourcemapContent = JSON.stringify(sourcemap);
|
|
89
89
|
compileAssets[sourcemapUrl] = {
|
|
90
90
|
type: "sourcemap",
|
|
91
91
|
content: sourcemapContent,
|
|
92
|
-
}
|
|
92
|
+
};
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
return compileAssets
|
|
96
|
-
}
|
|
95
|
+
return compileAssets;
|
|
96
|
+
};
|
|
97
97
|
|
|
98
98
|
const determineCompiledFileUrl = ({
|
|
99
99
|
url,
|
|
@@ -101,11 +101,11 @@ const determineCompiledFileUrl = ({
|
|
|
101
101
|
processEnvNodeEnv,
|
|
102
102
|
}) => {
|
|
103
103
|
if (processEnvNodeEnv) {
|
|
104
|
-
const basename = urlToBasename(url)
|
|
105
|
-
const extension = urlToExtension(url)
|
|
106
|
-
url = setUrlFilename(url, `${basename}.${processEnvNodeEnv}${extension}`)
|
|
104
|
+
const basename = urlToBasename(url);
|
|
105
|
+
const extension = urlToExtension(url);
|
|
106
|
+
url = setUrlFilename(url, `${basename}.${processEnvNodeEnv}${extension}`);
|
|
107
107
|
}
|
|
108
|
-
const fsRootUrl = ensureWindowsDriveLetter("file:///", url)
|
|
109
|
-
url = `${compileCacheDirectoryUrl}@fs/${url.slice(fsRootUrl.length)}
|
|
110
|
-
return url
|
|
111
|
-
}
|
|
108
|
+
const fsRootUrl = ensureWindowsDriveLetter("file:///", url);
|
|
109
|
+
url = `${compileCacheDirectoryUrl}@fs/${url.slice(fsRootUrl.length)}`;
|
|
110
|
+
return url;
|
|
111
|
+
};
|
package/src/cjs_to_esm_raw.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url"
|
|
2
|
-
import { createLogger } from "@jsenv/log"
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { createLogger } from "@jsenv/log";
|
|
3
3
|
|
|
4
|
-
import { rollupPluginCommonJsNamedExports } from "./rollup_plugin_commonjs_named_exports.js"
|
|
4
|
+
import { rollupPluginCommonJsNamedExports } from "./rollup_plugin_commonjs_named_exports.js";
|
|
5
5
|
|
|
6
6
|
export const commonJsToJsModuleRaw = async ({
|
|
7
7
|
logLevel,
|
|
@@ -20,17 +20,17 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
20
20
|
external = [],
|
|
21
21
|
sourcemapExcludeSources,
|
|
22
22
|
} = {}) => {
|
|
23
|
-
const logger = createLogger({ logLevel })
|
|
23
|
+
const logger = createLogger({ logLevel });
|
|
24
24
|
if (!sourceFileUrl.startsWith("file:///")) {
|
|
25
25
|
// it's possible to make rollup compatible with http:// for instance
|
|
26
26
|
// however it's an exotic use case for now
|
|
27
27
|
throw new Error(
|
|
28
28
|
`compatible only with file:// protocol, got ${sourceFileUrl}`,
|
|
29
|
-
)
|
|
29
|
+
);
|
|
30
30
|
}
|
|
31
|
-
const sourceFilePath = fileURLToPath(sourceFileUrl)
|
|
31
|
+
const sourceFilePath = fileURLToPath(sourceFileUrl);
|
|
32
32
|
|
|
33
|
-
const { nodeResolve } = await import("@rollup/plugin-node-resolve")
|
|
33
|
+
const { nodeResolve } = await import("@rollup/plugin-node-resolve");
|
|
34
34
|
const nodeResolveRollupPlugin = nodeResolve({
|
|
35
35
|
mainFields: browsers
|
|
36
36
|
? [
|
|
@@ -45,21 +45,21 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
45
45
|
extensions: [".mjs", ".cjs", ".js", ".json"],
|
|
46
46
|
preferBuiltins: false,
|
|
47
47
|
exportConditions: [],
|
|
48
|
-
})
|
|
48
|
+
});
|
|
49
49
|
|
|
50
50
|
const { default: createJSONRollupPlugin } = await import(
|
|
51
51
|
"@rollup/plugin-json"
|
|
52
|
-
)
|
|
52
|
+
);
|
|
53
53
|
const jsonRollupPlugin = createJSONRollupPlugin({
|
|
54
54
|
preferConst: true,
|
|
55
55
|
indent: " ",
|
|
56
56
|
compact: false,
|
|
57
57
|
namedExports: true,
|
|
58
|
-
})
|
|
58
|
+
});
|
|
59
59
|
|
|
60
60
|
const { default: createReplaceRollupPlugin } = await import(
|
|
61
61
|
"@rollup/plugin-replace"
|
|
62
|
-
)
|
|
62
|
+
);
|
|
63
63
|
const replaceRollupPlugin = createReplaceRollupPlugin({
|
|
64
64
|
preventAssignment: true,
|
|
65
65
|
values: {
|
|
@@ -71,9 +71,9 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
71
71
|
...(replaceGlobalDirname ? { __dirname: __dirnameReplacement } : {}),
|
|
72
72
|
...replaceMap,
|
|
73
73
|
},
|
|
74
|
-
})
|
|
74
|
+
});
|
|
75
75
|
|
|
76
|
-
const { default: commonjs } = await import("@rollup/plugin-commonjs")
|
|
76
|
+
const { default: commonjs } = await import("@rollup/plugin-commonjs");
|
|
77
77
|
// https://github.com/rollup/plugins/tree/master/packages/commonjs
|
|
78
78
|
const commonJsRollupPlugin = commonjs({
|
|
79
79
|
extensions: [".js", ".cjs"],
|
|
@@ -81,20 +81,20 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
81
81
|
// defaultIsModuleExports: true,
|
|
82
82
|
// requireReturnsDefault: "namespace",
|
|
83
83
|
requireReturnsDefault: "auto",
|
|
84
|
-
})
|
|
84
|
+
});
|
|
85
85
|
|
|
86
86
|
const { default: createNodeGlobalRollupPlugin } = await import(
|
|
87
87
|
"rollup-plugin-node-globals"
|
|
88
|
-
)
|
|
88
|
+
);
|
|
89
89
|
|
|
90
90
|
const commonJsNamedExportsRollupPlugin = rollupPluginCommonJsNamedExports({
|
|
91
91
|
logger,
|
|
92
|
-
})
|
|
92
|
+
});
|
|
93
93
|
const { default: rollupPluginNodePolyfills } = await import(
|
|
94
94
|
"rollup-plugin-polyfill-node"
|
|
95
|
-
)
|
|
95
|
+
);
|
|
96
96
|
|
|
97
|
-
const { rollup } = await import("rollup")
|
|
97
|
+
const { rollup } = await import("rollup");
|
|
98
98
|
const rollupBuild = await rollup({
|
|
99
99
|
input: sourceFilePath,
|
|
100
100
|
external,
|
|
@@ -128,13 +128,13 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
128
128
|
warning.code === "UNRESOLVED_IMPORT" &&
|
|
129
129
|
warning.id.endsWith("?commonjs-external")
|
|
130
130
|
) {
|
|
131
|
-
return
|
|
131
|
+
return;
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
const { loc, message } = warning
|
|
134
|
+
const { loc, message } = warning;
|
|
135
135
|
const logMessage = loc
|
|
136
136
|
? `${loc.file}:${loc.line}:${loc.column} ${message}`
|
|
137
|
-
: message
|
|
137
|
+
: message;
|
|
138
138
|
|
|
139
139
|
// These warnings are usually harmless in packages, so don't show them by default
|
|
140
140
|
if (
|
|
@@ -144,13 +144,13 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
144
144
|
warning.code === "EMPTY_BUNDLE" ||
|
|
145
145
|
warning.code === "UNUSED_EXTERNAL_IMPORT"
|
|
146
146
|
) {
|
|
147
|
-
logger.debug(logMessage)
|
|
147
|
+
logger.debug(logMessage);
|
|
148
148
|
} else {
|
|
149
|
-
logger.warn(logMessage)
|
|
149
|
+
logger.warn(logMessage);
|
|
150
150
|
}
|
|
151
151
|
},
|
|
152
|
-
})
|
|
153
|
-
const abstractDirUrl = new URL("./dist/", sourceFileUrl) // to help rollup generate property sourcemap paths
|
|
152
|
+
});
|
|
153
|
+
const abstractDirUrl = new URL("./dist/", sourceFileUrl); // to help rollup generate property sourcemap paths
|
|
154
154
|
const generateOptions = {
|
|
155
155
|
inlineDynamicImports: true,
|
|
156
156
|
// https://rollupjs.org/guide/en#output-format
|
|
@@ -162,19 +162,19 @@ export const commonJsToJsModuleRaw = async ({
|
|
|
162
162
|
exports: "named",
|
|
163
163
|
dir: fileURLToPath(abstractDirUrl),
|
|
164
164
|
sourcemapPathTransform: (relativePath) => {
|
|
165
|
-
const sourceUrl = new URL(relativePath, abstractDirUrl).href
|
|
166
|
-
return sourceUrl
|
|
165
|
+
const sourceUrl = new URL(relativePath, abstractDirUrl).href;
|
|
166
|
+
return sourceUrl;
|
|
167
167
|
},
|
|
168
|
-
}
|
|
168
|
+
};
|
|
169
169
|
|
|
170
|
-
const { output } = await rollupBuild.generate(generateOptions)
|
|
171
|
-
const { code, map } = output[0]
|
|
170
|
+
const { output } = await rollupBuild.generate(generateOptions);
|
|
171
|
+
const { code, map } = output[0];
|
|
172
172
|
return {
|
|
173
173
|
content: code,
|
|
174
174
|
sourcemap: map,
|
|
175
|
-
}
|
|
176
|
-
}
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
177
|
|
|
178
|
-
const __filenameReplacement = `import.meta.url.slice('file:///'.length)
|
|
178
|
+
const __filenameReplacement = `import.meta.url.slice('file:///'.length)`;
|
|
179
179
|
|
|
180
|
-
const __dirnameReplacement = `import.meta.url.slice('file:///'.length).replace(/[\\\/\\\\][^\\\/\\\\]*$/, '')
|
|
180
|
+
const __dirnameReplacement = `import.meta.url.slice('file:///'.length).replace(/[\\\/\\\\][^\\\/\\\\]*$/, '')`;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from "node:fs"
|
|
2
|
-
import { createLogger, UNICODE } from "@jsenv/log"
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { createLogger, UNICODE } from "@jsenv/log";
|
|
3
3
|
import {
|
|
4
4
|
assertAndNormalizeFileUrl,
|
|
5
5
|
ensureEmptyDirectory,
|
|
6
|
-
} from "@jsenv/filesystem"
|
|
6
|
+
} from "@jsenv/filesystem";
|
|
7
7
|
|
|
8
|
-
import { validateCompileCache } from "./validate_compile_cache.js"
|
|
9
|
-
import { createLockRegistry } from "./file_lock_registry.js"
|
|
10
|
-
import { updateCompileCache } from "./update_compile_cache.js"
|
|
8
|
+
import { validateCompileCache } from "./validate_compile_cache.js";
|
|
9
|
+
import { createLockRegistry } from "./file_lock_registry.js";
|
|
10
|
+
import { updateCompileCache } from "./update_compile_cache.js";
|
|
11
11
|
|
|
12
|
-
const { lockForResource } = createLockRegistry()
|
|
12
|
+
const { lockForResource } = createLockRegistry();
|
|
13
13
|
|
|
14
14
|
export const reuseOrCreateCompiledFile = async ({
|
|
15
15
|
logLevel,
|
|
@@ -20,11 +20,11 @@ export const reuseOrCreateCompiledFile = async ({
|
|
|
20
20
|
compileCacheAssetsValidation,
|
|
21
21
|
compile,
|
|
22
22
|
}) => {
|
|
23
|
-
const logger = createLogger({ logLevel })
|
|
24
|
-
await initCompileCacheDirectory({ logger, compileCacheDirectoryUrl })
|
|
25
|
-
sourceFileUrl = assertAndNormalizeFileUrl(sourceFileUrl)
|
|
23
|
+
const logger = createLogger({ logLevel });
|
|
24
|
+
await initCompileCacheDirectory({ logger, compileCacheDirectoryUrl });
|
|
25
|
+
sourceFileUrl = assertAndNormalizeFileUrl(sourceFileUrl);
|
|
26
26
|
if (typeof compile !== "function") {
|
|
27
|
-
throw new TypeError(`compile must be a function, got ${compile}`)
|
|
27
|
+
throw new TypeError(`compile must be a function, got ${compile}`);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
const getCacheValidity = () => {
|
|
@@ -33,146 +33,146 @@ export const reuseOrCreateCompiledFile = async ({
|
|
|
33
33
|
compiledFileUrl,
|
|
34
34
|
compileCacheStrategy,
|
|
35
35
|
compileCacheAssetsValidation,
|
|
36
|
-
})
|
|
37
|
-
return cacheValidity
|
|
38
|
-
}
|
|
36
|
+
});
|
|
37
|
+
return cacheValidity;
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
return startAsap(
|
|
41
41
|
async () => {
|
|
42
|
-
logger.debug(`check cache for ${compiledFileUrl}`)
|
|
43
|
-
const cacheValidity = getCacheValidity()
|
|
42
|
+
logger.debug(`check cache for ${compiledFileUrl}`);
|
|
43
|
+
const cacheValidity = getCacheValidity();
|
|
44
44
|
if (cacheValidity.isValid) {
|
|
45
|
-
logger.debug(`${UNICODE.OK} found a valid cache`)
|
|
46
|
-
const compileInfo = cacheValidity.compileInfo.data
|
|
47
|
-
const content = String(cacheValidity.compiledFile.data.buffer)
|
|
48
|
-
const assets = {}
|
|
49
|
-
let sourcemap = null
|
|
45
|
+
logger.debug(`${UNICODE.OK} found a valid cache`);
|
|
46
|
+
const compileInfo = cacheValidity.compileInfo.data;
|
|
47
|
+
const content = String(cacheValidity.compiledFile.data.buffer);
|
|
48
|
+
const assets = {};
|
|
49
|
+
let sourcemap = null;
|
|
50
50
|
Object.keys(compileInfo.assetInfos).forEach((assetRelativeUrl) => {
|
|
51
|
-
const assetUrl = new URL(assetRelativeUrl, compiledFileUrl).href
|
|
52
|
-
const assetValidity = cacheValidity.assets.data[assetUrl]
|
|
51
|
+
const assetUrl = new URL(assetRelativeUrl, compiledFileUrl).href;
|
|
52
|
+
const assetValidity = cacheValidity.assets.data[assetUrl];
|
|
53
53
|
const asset = {
|
|
54
54
|
type: compileInfo.assetInfos[assetRelativeUrl].type,
|
|
55
55
|
etag: compileInfo.assetInfos[assetRelativeUrl].etag,
|
|
56
56
|
content: assetValidity.data.content,
|
|
57
|
-
}
|
|
58
|
-
assets[assetUrl] = asset
|
|
57
|
+
};
|
|
58
|
+
assets[assetUrl] = asset;
|
|
59
59
|
if (asset.type === "sourcemap") {
|
|
60
|
-
sourcemap = assetValidity.data.sourcemap
|
|
60
|
+
sourcemap = assetValidity.data.sourcemap;
|
|
61
61
|
}
|
|
62
|
-
})
|
|
62
|
+
});
|
|
63
63
|
return {
|
|
64
64
|
isValid: () => getCacheValidity().isValid,
|
|
65
65
|
content,
|
|
66
66
|
sourcemap,
|
|
67
|
-
}
|
|
67
|
+
};
|
|
68
68
|
}
|
|
69
69
|
if (cacheValidity.code === "SOURCES_EMPTY") {
|
|
70
70
|
logger.warn(
|
|
71
71
|
`${UNICODE.WARN} meta.sources is empty for ${compiledFileUrl}`,
|
|
72
|
-
)
|
|
72
|
+
);
|
|
73
73
|
}
|
|
74
|
-
logger.debug(`${UNICODE.INFO} cache not found or invalid`)
|
|
74
|
+
logger.debug(`${UNICODE.INFO} cache not found or invalid`);
|
|
75
75
|
const compileInfoIsValid = cacheValidity.compileInfo
|
|
76
76
|
? cacheValidity.compileInfo.isValid
|
|
77
|
-
: false
|
|
78
|
-
const fileContentAsBuffer = readFileSync(new URL(sourceFileUrl))
|
|
79
|
-
const fileContentAsString = String(fileContentAsBuffer)
|
|
77
|
+
: false;
|
|
78
|
+
const fileContentAsBuffer = readFileSync(new URL(sourceFileUrl));
|
|
79
|
+
const fileContentAsString = String(fileContentAsBuffer);
|
|
80
80
|
|
|
81
81
|
const compileResult = await compile({
|
|
82
82
|
content: fileContentAsString,
|
|
83
|
-
})
|
|
83
|
+
});
|
|
84
84
|
if (typeof compileResult !== "object" || compileResult === null) {
|
|
85
85
|
throw new TypeError(
|
|
86
86
|
`compile must return an object, got ${compileResult}`,
|
|
87
|
-
)
|
|
87
|
+
);
|
|
88
88
|
}
|
|
89
|
-
const { content, sourcemap, assets } = compileResult
|
|
89
|
+
const { content, sourcemap, assets } = compileResult;
|
|
90
90
|
updateCompileCache({
|
|
91
91
|
logger,
|
|
92
92
|
compiledFileUrl,
|
|
93
93
|
content,
|
|
94
94
|
assets,
|
|
95
95
|
compileResultStatus: compileInfoIsValid ? "updated" : "created",
|
|
96
|
-
})
|
|
96
|
+
});
|
|
97
97
|
return {
|
|
98
98
|
isValid: () => getCacheValidity().isValid,
|
|
99
99
|
content,
|
|
100
100
|
sourcemap,
|
|
101
|
-
}
|
|
101
|
+
};
|
|
102
102
|
},
|
|
103
103
|
{
|
|
104
104
|
compiledFileUrl,
|
|
105
105
|
},
|
|
106
|
-
)
|
|
107
|
-
}
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
108
|
|
|
109
|
-
const initalized = {}
|
|
109
|
+
const initalized = {};
|
|
110
110
|
const initCompileCacheDirectory = async ({
|
|
111
111
|
logger,
|
|
112
112
|
compileCacheDirectoryUrl,
|
|
113
113
|
}) => {
|
|
114
114
|
if (initalized[compileCacheDirectoryUrl]) {
|
|
115
|
-
return
|
|
115
|
+
return;
|
|
116
116
|
}
|
|
117
|
-
initalized[compileCacheDirectoryUrl] = true
|
|
118
|
-
logger.debug(`check compile directory at ${compileCacheDirectoryUrl}`)
|
|
117
|
+
initalized[compileCacheDirectoryUrl] = true;
|
|
118
|
+
logger.debug(`check compile directory at ${compileCacheDirectoryUrl}`);
|
|
119
119
|
const compileContextJsonFileUrl = new URL(
|
|
120
120
|
"./__compile_context__.json",
|
|
121
121
|
compileCacheDirectoryUrl,
|
|
122
|
-
)
|
|
122
|
+
);
|
|
123
123
|
const version = JSON.parse(
|
|
124
124
|
readFileSync(new URL("../../package.json", import.meta.url)),
|
|
125
|
-
).version
|
|
125
|
+
).version;
|
|
126
126
|
const compileContext = readCompileContextFile({
|
|
127
127
|
logger,
|
|
128
128
|
compileContextJsonFileUrl,
|
|
129
|
-
})
|
|
129
|
+
});
|
|
130
130
|
if (compileContext && compileContext.version === version) {
|
|
131
|
-
logger.debug(`${UNICODE.OK} reuse compile directory`)
|
|
131
|
+
logger.debug(`${UNICODE.OK} reuse compile directory`);
|
|
132
132
|
} else {
|
|
133
133
|
if (compileContext) {
|
|
134
|
-
logger.debug(`${UNICODE.WARN} clean existing directory`)
|
|
134
|
+
logger.debug(`${UNICODE.WARN} clean existing directory`);
|
|
135
135
|
} else {
|
|
136
|
-
logger.debug(`${UNICODE.INFO} create an empty directory`)
|
|
136
|
+
logger.debug(`${UNICODE.INFO} create an empty directory`);
|
|
137
137
|
}
|
|
138
|
-
await ensureEmptyDirectory(compileCacheDirectoryUrl)
|
|
138
|
+
await ensureEmptyDirectory(compileCacheDirectoryUrl);
|
|
139
139
|
writeFileSync(
|
|
140
140
|
compileContextJsonFileUrl,
|
|
141
141
|
JSON.stringify({ version }, null, " "),
|
|
142
|
-
)
|
|
142
|
+
);
|
|
143
143
|
}
|
|
144
|
-
}
|
|
144
|
+
};
|
|
145
145
|
|
|
146
146
|
const readCompileContextFile = ({ logger, compileContextJsonFileUrl }) => {
|
|
147
|
-
let compileContextFileContent
|
|
147
|
+
let compileContextFileContent;
|
|
148
148
|
try {
|
|
149
|
-
compileContextFileContent = readFileSync(compileContextJsonFileUrl)
|
|
149
|
+
compileContextFileContent = readFileSync(compileContextJsonFileUrl);
|
|
150
150
|
} catch (e) {
|
|
151
151
|
logger.debug(
|
|
152
152
|
`${UNICODE.INFO} cannot read compile context at ${compileContextJsonFileUrl}`,
|
|
153
|
-
)
|
|
154
|
-
return null
|
|
153
|
+
);
|
|
154
|
+
return null;
|
|
155
155
|
}
|
|
156
156
|
try {
|
|
157
|
-
const compileContext = JSON.parse(compileContextFileContent)
|
|
158
|
-
return compileContext
|
|
157
|
+
const compileContext = JSON.parse(compileContextFileContent);
|
|
158
|
+
return compileContext;
|
|
159
159
|
} catch (e) {
|
|
160
160
|
if (e.name === "SyntaxError") {
|
|
161
161
|
logger.warn(
|
|
162
162
|
`${UNICODE.WARN} syntax error in ${compileContextJsonFileUrl}`,
|
|
163
|
-
)
|
|
164
|
-
return null
|
|
163
|
+
);
|
|
164
|
+
return null;
|
|
165
165
|
}
|
|
166
|
-
throw e
|
|
166
|
+
throw e;
|
|
167
167
|
}
|
|
168
|
-
}
|
|
168
|
+
};
|
|
169
169
|
|
|
170
170
|
const startAsap = async (fn, { compiledFileUrl }) => {
|
|
171
|
-
const unlockLocal = await lockForResource(compiledFileUrl)
|
|
171
|
+
const unlockLocal = await lockForResource(compiledFileUrl);
|
|
172
172
|
try {
|
|
173
|
-
return await fn()
|
|
173
|
+
return await fn();
|
|
174
174
|
} finally {
|
|
175
175
|
// "finally" we want to unlock in case of error too
|
|
176
|
-
unlockLocal()
|
|
176
|
+
unlockLocal();
|
|
177
177
|
}
|
|
178
|
-
}
|
|
178
|
+
};
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
export const createLockRegistry = () => {
|
|
2
|
-
let lockArray = []
|
|
2
|
+
let lockArray = [];
|
|
3
3
|
const lockForResource = async (resource) => {
|
|
4
|
-
const currentLock = lockArray.find((lock) => lock.resource === resource)
|
|
5
|
-
let unlockResolve
|
|
4
|
+
const currentLock = lockArray.find((lock) => lock.resource === resource);
|
|
5
|
+
let unlockResolve;
|
|
6
6
|
const unlocked = new Promise((resolve) => {
|
|
7
|
-
unlockResolve = resolve
|
|
8
|
-
})
|
|
7
|
+
unlockResolve = resolve;
|
|
8
|
+
});
|
|
9
9
|
const lock = {
|
|
10
10
|
resource,
|
|
11
11
|
unlocked,
|
|
12
|
-
}
|
|
13
|
-
lockArray = [...lockArray, lock]
|
|
12
|
+
};
|
|
13
|
+
lockArray = [...lockArray, lock];
|
|
14
14
|
|
|
15
|
-
if (currentLock) await currentLock.unlocked
|
|
15
|
+
if (currentLock) await currentLock.unlocked;
|
|
16
16
|
|
|
17
17
|
const unlock = () => {
|
|
18
|
-
lockArray = lockArray.filter((lockCandidate) => lockCandidate !== lock)
|
|
19
|
-
unlockResolve()
|
|
20
|
-
}
|
|
21
|
-
return unlock
|
|
22
|
-
}
|
|
23
|
-
return { lockForResource }
|
|
24
|
-
}
|
|
18
|
+
lockArray = lockArray.filter((lockCandidate) => lockCandidate !== lock);
|
|
19
|
+
unlockResolve();
|
|
20
|
+
};
|
|
21
|
+
return unlock;
|
|
22
|
+
};
|
|
23
|
+
return { lockForResource };
|
|
24
|
+
};
|