@@ -107,39 +107,64 @@ const nodeConfig = defineConfig({
107
107
// Shim them with eval() so rollup can skip these calls.
108
108
shimDepsPlugin ( {
109
109
// chokidar -> fsevents
110
- 'fsevents-handler.js' : {
111
- src : `require('fsevents')` ,
112
- replacement : `__require('fsevents')` ,
113
- } ,
110
+ 'fsevents-handler.js' : [
111
+ {
112
+ src : `require('fsevents')` ,
113
+ replacement : `__require('fsevents')` ,
114
+ } ,
115
+ ] ,
114
116
// postcss-import -> sugarss
115
- 'process-content.js' : {
116
- src : 'require("sugarss")' ,
117
- replacement : `__require('sugarss')` ,
118
- } ,
119
- 'lilconfig/src/index.js' : {
120
- pattern : / : r e q u i r e ; / g,
121
- replacement : `: __require;` ,
122
- } ,
123
- 'postcss-load-config/src/index.js' : {
124
- src : "await import('yaml')" ,
125
- replacement : `__require('yaml')` ,
126
- } ,
127
- 'postcss-load-config/src/req.js' : {
128
- pattern :
129
- / \( a w a i t i m p o r t \( ' ( j i t i ) ' \) \) .d e f a u l t | a w a i t i m p o r t \( ' ( t s x \/ c j s \/ a p i ) ' \) / g,
130
- replacement : `__require('$1$2')` ,
131
- } ,
117
+ 'process-content.js' : [
118
+ {
119
+ src : 'require("sugarss")' ,
120
+ replacement : `__require('sugarss')` ,
121
+ } ,
122
+ ] ,
123
+ 'lilconfig/src/index.js' : [
124
+ {
125
+ pattern : / : r e q u i r e ; / g,
126
+ replacement : ': __require;' ,
127
+ } ,
128
+ ] ,
129
+ 'postcss-load-config/src/index.js' : [
130
+ {
131
+ src : `await import('yaml')` ,
132
+ replacement : `__require('yaml')` ,
133
+ } ,
134
+ ] ,
135
+ 'postcss-load-config/src/req.js' : [
136
+ {
137
+ src : `(await import('jiti')).default` ,
138
+ replacement : `__require('jiti')` ,
139
+ } ,
140
+ {
141
+ src : `await import('tsx/cjs/api')` ,
142
+ replacement : `__require('tsx/cjs/api')` ,
143
+ } ,
144
+ {
145
+ src : "const { pathToFileURL } = require('node:url')" ,
146
+ replacement : `const { fileURLToPath, pathToFileURL } = require('node:url')` ,
147
+ } ,
148
+ {
149
+ src : '__filename' ,
150
+ replacement : 'fileURLToPath(import.meta.url)' ,
151
+ } ,
152
+ ] ,
132
153
// postcss-import uses the `resolve` dep if the `resolve` option is not passed.
133
154
// However, we always pass the `resolve` option. Remove this import to avoid
134
155
// bundling the `resolve` dep.
135
- 'postcss-import/index.js' : {
136
- src : 'const resolveId = require("./lib/resolve-id")' ,
137
- replacement : 'const resolveId = (id) => id' ,
138
- } ,
139
- 'postcss-import/lib/parse-styles.js' : {
140
- src : 'const resolveId = require("./resolve-id")' ,
141
- replacement : 'const resolveId = (id) => id' ,
142
- } ,
156
+ 'postcss-import/index.js' : [
157
+ {
158
+ src : 'const resolveId = require("./lib/resolve-id")' ,
159
+ replacement : 'const resolveId = (id) => id' ,
160
+ } ,
161
+ ] ,
162
+ 'postcss-import/lib/parse-styles.js' : [
163
+ {
164
+ src : 'const resolveId = require("./resolve-id")' ,
165
+ replacement : 'const resolveId = (id) => id' ,
166
+ } ,
167
+ ] ,
143
168
} ) ,
144
169
...createSharedNodePlugins ( { } ) ,
145
170
licensePlugin (
@@ -203,50 +228,53 @@ interface ShimOptions {
203
228
pattern ?: RegExp
204
229
}
205
230
206
- function shimDepsPlugin ( deps : Record < string , ShimOptions > ) : Plugin {
231
+ function shimDepsPlugin ( deps : Record < string , ShimOptions [ ] > ) : Plugin {
207
232
const transformed : Record < string , boolean > = { }
208
233
209
234
return {
210
235
name : 'shim-deps' ,
211
236
transform ( code , id ) {
212
237
for ( const file in deps ) {
213
238
if ( id . replace ( / \\ / g, '/' ) . endsWith ( file ) ) {
214
- const { src, replacement, pattern } = deps [ file ]
239
+ for ( const { src, replacement, pattern } of deps [ file ] ) {
240
+ const magicString = new MagicString ( code )
215
241
216
- const magicString = new MagicString ( code )
217
- if ( src ) {
218
- const pos = code . indexOf ( src )
219
- if ( pos < 0 ) {
220
- this . error (
221
- `Could not find expected src "${ src } " in file "${ file } "` ,
222
- )
242
+ if ( src ) {
243
+ const pos = code . indexOf ( src )
244
+ if ( pos < 0 ) {
245
+ this . error (
246
+ `Could not find expected src "${ src } " in file "${ file } "` ,
247
+ )
248
+ }
249
+ transformed [ file ] = true
250
+ magicString . overwrite ( pos , pos + src . length , replacement )
223
251
}
224
- transformed [ file ] = true
225
- magicString . overwrite ( pos , pos + src . length , replacement )
226
- console . log ( `shimmed: ${ file } ` )
227
- }
228
252
229
- if ( pattern ) {
230
- let match
231
- while ( ( match = pattern . exec ( code ) ) ) {
232
- transformed [ file ] = true
233
- const start = match . index
234
- const end = start + match [ 0 ] . length
235
- let _replacement = replacement
236
- for ( let i = 1 ; i <= match . length ; i ++ ) {
237
- _replacement = _replacement . replace ( `$${ i } ` , match [ i ] || '' )
253
+ if ( pattern ) {
254
+ let match
255
+ while ( ( match = pattern . exec ( code ) ) ) {
256
+ transformed [ file ] = true
257
+ const start = match . index
258
+ const end = start + match [ 0 ] . length
259
+ let _replacement = replacement
260
+ for ( let i = 1 ; i <= match . length ; i ++ ) {
261
+ _replacement = _replacement . replace ( `$${ i } ` , match [ i ] || '' )
262
+ }
263
+ magicString . overwrite ( start , end , _replacement )
264
+ }
265
+ if ( ! transformed [ file ] ) {
266
+ this . error (
267
+ `Could not find expected pattern "${ pattern } " in file "${ file } "` ,
268
+ )
238
269
}
239
- magicString . overwrite ( start , end , _replacement )
240
- }
241
- if ( ! transformed [ file ] ) {
242
- this . error (
243
- `Could not find expected pattern "${ pattern } " in file "${ file } "` ,
244
- )
245
270
}
246
- console . log ( `shimmed: ${ file } ` )
271
+
272
+ code = magicString . toString ( )
247
273
}
248
274
249
- return magicString . toString ( )
275
+ console . log ( `shimmed: ${ file } ` )
276
+
277
+ return code
250
278
}
251
279
}
252
280
} ,
0 commit comments