@@ -3,9 +3,14 @@ import { minify } from 'terser';
3
3
4
4
const isCompiledStringId = ( id : string ) => / [ ? & ] c o m p i l e d - s t r i n g / . test ( id ) ;
5
5
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
+ */
7
10
export function compiledStringPlugin ( ) : Plugin {
8
11
let devServer : any ;
12
+ const originals = new Map < string , string > ( ) ;
13
+
9
14
return {
10
15
name : 'compiled-string-plugin' ,
11
16
enforce : 'pre' ,
@@ -21,6 +26,25 @@ export function compiledStringPlugin(): Plugin {
21
26
const cleanId = id . replace ( / ( [ ? & ] ) c o m p i l e d - s t r i n g / , '$1' ) . replace ( / [ ? & ] $ / , '' ) ;
22
27
const resolved = await this . resolve ( cleanId , importer , { skipSelf : true } ) ;
23
28
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 ) ;
24
48
return `virtual:compiled-string:${ resolved . id } ` ;
25
49
}
26
50
} else if ( id . startsWith ( 'virtual:compiled-string:' ) ) {
@@ -35,30 +59,13 @@ export function compiledStringPlugin(): Plugin {
35
59
async handler ( id ) {
36
60
if ( id . startsWith ( 'virtual:compiled-string:' ) ) {
37
61
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 } ` ) ;
58
65
}
59
66
const minified = await minify ( code ) ;
60
67
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 } ` ) ;
62
69
}
63
70
const withoutExports = minified . code . replace ( 'export{}' , '' ) . replace ( / ; + $ / g, '' ) ;
64
71
return {
0 commit comments