@@ -13,6 +13,10 @@ import {
13
13
import { browserExternalId } from '../plugins/resolve'
14
14
import type { ExportsData } from '.'
15
15
16
+ const externalWithConversionNamespace =
17
+ 'vite:dep-pre-bundle:external-conversion'
18
+ const convertedExternalPrefix = 'vite-dep-pre-bundle-external:'
19
+
16
20
const externalTypes = [
17
21
'css' ,
18
22
// supported pre-processor types
@@ -80,20 +84,42 @@ export function esbuildDepPlugin(
80
84
name : 'vite:dep-pre-bundle' ,
81
85
setup ( build ) {
82
86
// externalize assets and commonly known non-js file types
87
+ // See #8459 for more details about this require-import conversion
83
88
build . onResolve (
84
89
{
85
90
filter : new RegExp ( `\\.(` + allExternalTypes . join ( '|' ) + `)(\\?.*)?$` )
86
91
} ,
87
92
async ( { path : id , importer, kind } ) => {
93
+ // if the prefix exist, it is already converted to `import`, so set `external: true`
94
+ if ( id . startsWith ( convertedExternalPrefix ) ) {
95
+ return {
96
+ path : id . slice ( convertedExternalPrefix . length ) ,
97
+ external : true
98
+ }
99
+ }
100
+
88
101
const resolved = await resolve ( id , importer , kind )
89
102
if ( resolved ) {
103
+ // here it is not set to `external: true` to convert `require` to `import`
90
104
return {
91
105
path : resolved ,
92
- external : true
106
+ namespace : externalWithConversionNamespace
93
107
}
94
108
}
95
109
}
96
110
)
111
+ build . onLoad (
112
+ { filter : / ./ , namespace : externalWithConversionNamespace } ,
113
+ ( args ) => {
114
+ // import itself with prefix (this is the actual part of require-import conversion)
115
+ return {
116
+ contents :
117
+ `export { default } from "${ convertedExternalPrefix } ${ args . path } ";` +
118
+ `export * from "${ convertedExternalPrefix } ${ args . path } ";` ,
119
+ loader : 'js'
120
+ }
121
+ }
122
+ )
97
123
98
124
function resolveEntry ( id : string ) {
99
125
const flatId = flattenId ( id )
0 commit comments