Skip to content

Commit d32306a

Browse files
committed
update rollup config
1 parent 935fa7f commit d32306a

File tree

1 file changed

+87
-59
lines changed

1 file changed

+87
-59
lines changed

packages/vite/rollup.config.ts

+87-59
Original file line numberDiff line numberDiff line change
@@ -107,39 +107,64 @@ const nodeConfig = defineConfig({
107107
// Shim them with eval() so rollup can skip these calls.
108108
shimDepsPlugin({
109109
// 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+
],
114116
// postcss-import -> sugarss
115-
'process-content.js': {
116-
src: 'require("sugarss")',
117-
replacement: `__require('sugarss')`,
118-
},
119-
'lilconfig/src/index.js': {
120-
pattern: /: require;/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-
/\(await import\('(jiti)'\)\).default|await import\('(tsx\/cjs\/api)'\)/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: /: require;/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+
],
132153
// postcss-import uses the `resolve` dep if the `resolve` option is not passed.
133154
// However, we always pass the `resolve` option. Remove this import to avoid
134155
// 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+
],
143168
}),
144169
...createSharedNodePlugins({}),
145170
licensePlugin(
@@ -203,50 +228,53 @@ interface ShimOptions {
203228
pattern?: RegExp
204229
}
205230

206-
function shimDepsPlugin(deps: Record<string, ShimOptions>): Plugin {
231+
function shimDepsPlugin(deps: Record<string, ShimOptions[]>): Plugin {
207232
const transformed: Record<string, boolean> = {}
208233

209234
return {
210235
name: 'shim-deps',
211236
transform(code, id) {
212237
for (const file in deps) {
213238
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)
215241

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)
223251
}
224-
transformed[file] = true
225-
magicString.overwrite(pos, pos + src.length, replacement)
226-
console.log(`shimmed: ${file}`)
227-
}
228252

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+
)
238269
}
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-
)
245270
}
246-
console.log(`shimmed: ${file}`)
271+
272+
code = magicString.toString()
247273
}
248274

249-
return magicString.toString()
275+
console.log(`shimmed: ${file}`)
276+
277+
return code
250278
}
251279
}
252280
},

0 commit comments

Comments
 (0)