Skip to content

Commit 9a010fe

Browse files
committed
fix: compiled-string maxParallelFileOps=1 error
See vitejs/vite#20775
1 parent 605c676 commit 9a010fe

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

scripts/compiled-string-plugin.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { minify } from 'terser';
33

44
const isCompiledStringId = (id: string) => /[?&]compiled-string/.test(id);
55

6-
/** This returns the source code of a module after transforming */
6+
/**
7+
* This returns the source code of a module after transforming. Note that imports aren't
8+
* transformed.
9+
*/
710
export function compiledStringPlugin(): Plugin {
811
let devServer: any;
12+
const originals = new Map<string, string>();
13+
914
return {
1015
name: 'compiled-string-plugin',
1116
enforce: 'pre',
@@ -21,6 +26,25 @@ export function compiledStringPlugin(): Plugin {
2126
const cleanId = id.replace(/([?&])compiled-string/, '$1').replace(/[?&]$/, '');
2227
const resolved = await this.resolve(cleanId, importer, { skipSelf: true });
2328
if (resolved) {
29+
/**
30+
* Note: we load the code here instead of in the load hook to prevent a bug in Vite when
31+
* `rollupOptions.maxParallelFileOps=1`. See
32+
* https://github.com/vitejs/vite/issues/20775
33+
*/
34+
let code: string | null;
35+
if (devServer) {
36+
// in dev mode, you need to use the dev server to transform the request
37+
const transformResult = await devServer.transformRequest(resolved.id);
38+
code = transformResult?.code;
39+
this.addWatchFile(resolved.id);
40+
} else {
41+
const loaded = await this.load({ id: resolved.id });
42+
code = loaded.code;
43+
}
44+
if (!code) {
45+
throw new Error(`compiled-string: Unable to load code for ${resolved.id}`);
46+
}
47+
originals.set(resolved.id, code);
2448
return `virtual:compiled-string:${resolved.id}`;
2549
}
2650
} else if (id.startsWith('virtual:compiled-string:')) {
@@ -35,30 +59,13 @@ export function compiledStringPlugin(): Plugin {
3559
async handler(id) {
3660
if (id.startsWith('virtual:compiled-string:')) {
3761
const originalId = id.slice('virtual:compiled-string:'.length);
38-
39-
const result = await this.load({
40-
id: originalId,
41-
moduleSideEffects: true,
42-
});
43-
44-
let code: string;
45-
if (result && 'code' in result && result.code) {
46-
// If this.load provides code, use it
47-
code = result.code;
48-
} else if (devServer) {
49-
// in dev mode, you need to use the dev server to transform the request
50-
const transformResult = await devServer.transformRequest(originalId);
51-
if (transformResult && transformResult.code) {
52-
code = transformResult.code;
53-
}
54-
this.addWatchFile(originalId);
55-
}
56-
if (!code!) {
57-
throw new Error(`Unable to load code for ${originalId}`);
62+
const code = originals.get(originalId);
63+
if (!code) {
64+
throw new Error(`compiled-string: Unable to retrieve loaded code for ${originalId}`);
5865
}
5966
const minified = await minify(code);
6067
if (!minified.code) {
61-
throw new Error(`Unable to minify code for ${originalId}`);
68+
throw new Error(`compiled-string: Unable to minify code for ${originalId}`);
6269
}
6370
const withoutExports = minified.code.replace('export{}', '').replace(/;+$/g, '');
6471
return {

0 commit comments

Comments
 (0)