Skip to content

Commit d4dcdd1

Browse files
chore!: update rollup commonjs plugin to v22 (#8743)
Co-authored-by: sapphi-red <[email protected]>
1 parent eac0494 commit d4dcdd1

File tree

8 files changed

+63
-17
lines changed

8 files changed

+63
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"test": "run-s test-unit test-serve test-build",
2121
"test-serve": "vitest run -c vitest.config.e2e.ts",
2222
"test-build": "cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
23+
"test-build-legacy-cjs": "cross-env VITE_TEST_LEGACY_CJS_PLUGIN=1 pnpm test-build",
2324
"test-unit": "vitest run",
2425
"test-docs": "pnpm run docs-build",
2526
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts",

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"@babel/types": "^7.18.4",
7272
"@jridgewell/trace-mapping": "^0.3.13",
7373
"@rollup/plugin-alias": "^3.1.9",
74-
"@rollup/plugin-commonjs": "^21.1.0",
74+
"@rollup/plugin-commonjs": "^22.0.1",
7575
"@rollup/plugin-dynamic-import-vars": "^1.4.3",
7676
"@rollup/plugin-json": "^4.1.0",
7777
"@rollup/plugin-node-resolve": "13.3.0",

packages/vite/rollup.config.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-restricted-globals */
22
import fs from 'node:fs'
33
import path from 'node:path'
4+
import url from 'node:url'
45
import nodeResolve from '@rollup/plugin-node-resolve'
56
import typescript from '@rollup/plugin-typescript'
67
import commonjs from '@rollup/plugin-commonjs'
@@ -134,15 +135,13 @@ function createNodePlugins(
134135
},
135136
// postcss-load-config calls require after register ts-node
136137
'postcss-load-config/src/index.js': {
137-
src: `require(configFile)`,
138-
replacement: `__require(configFile)`
139-
},
140-
// @rollup/plugin-commonjs uses incorrect esm
141-
'@rollup/plugin-commonjs/dist/index.es.js': {
142-
src: `import { sync } from 'resolve';`,
143-
replacement: `import __resolve from 'resolve';const sync = __resolve.sync;`
138+
pattern: /require(?=\((configFile|'ts-node')\))/g,
139+
replacement: `eval('require')`
144140
}
145141
}),
142+
143+
buildTimeImportMetaUrl(),
144+
146145
commonjs({
147146
extensions: ['.js'],
148147
// Optional peer deps of ws. Native deps that are mostly for performance.
@@ -287,6 +286,21 @@ function shimDepsPlugin(deps: Record<string, ShimOptions>): Plugin {
287286
}
288287
}
289288

289+
// The use of `import.meta.url` in source code is not reliable after bundling.
290+
// For example, it is affected by the `isEntry` bug brought in by the Rollup CJS plugin
291+
// https://github.com/rollup/plugins/pull/1180
292+
// The better way is to resolve it at build time.
293+
function buildTimeImportMetaUrl(): Plugin {
294+
return {
295+
name: 'buildTimeImportMetaUrl',
296+
resolveImportMeta: (property, chunk) => {
297+
if (property === 'url') {
298+
return `'${url.pathToFileURL(chunk.moduleId).href}'`
299+
}
300+
}
301+
}
302+
}
303+
290304
function licensePlugin() {
291305
return license({
292306
thirdParty(dependencies) {

packages/vite/src/node/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,13 @@ export async function resolveConfig(
594594
config = mergeConfig(config, externalConfigCompat(config, configEnv))
595595
const optimizeDeps = config.optimizeDeps || {}
596596

597+
if (process.env.VITE_TEST_LEGACY_CJS_PLUGIN) {
598+
config.legacy = {
599+
...config.legacy,
600+
buildRollupPluginCommonjs: true
601+
}
602+
}
603+
597604
const BASE_URL = resolvedBase
598605

599606
const resolved: ResolvedConfig = {

playground/external/vite.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export default defineConfig({
55
minify: false,
66
rollupOptions: {
77
external: ['vue']
8+
},
9+
commonjsOptions: {
10+
esmExternals: ['vue']
811
}
912
}
1013
})

playground/optimize-deps/__tests__/optimize-deps.spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,16 @@ test('vue + vuex', async () => {
103103
expect(await page.textContent('.vue')).toMatch(`[success]`)
104104
})
105105

106-
test('esbuild-plugin', async () => {
107-
expect(await page.textContent('.esbuild-plugin')).toMatch(
108-
`Hello from an esbuild plugin`
109-
)
110-
})
106+
// When we use the Rollup CommonJS plugin instead of esbuild prebundling,
107+
// the esbuild plugins won't apply to dependencies
108+
test.skipIf(isBuild && process.env.VITE_TEST_LEGACY_CJS_PLUGIN)(
109+
'esbuild-plugin',
110+
async () => {
111+
expect(await page.textContent('.esbuild-plugin')).toMatch(
112+
`Hello from an esbuild plugin`
113+
)
114+
}
115+
)
111116

112117
test('import from hidden dir', async () => {
113118
expect(await page.textContent('.hidden-dir')).toBe('hello!')

playground/optimize-deps/vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports = {
7272
apply: 'build',
7373
enforce: 'pre',
7474
load(id) {
75-
if (id === '__vite-browser-external:fs') {
75+
if (id === '__vite-browser-external') {
7676
return `export default {}; export function readFileSync() {}`
7777
}
7878
}

pnpm-lock.yaml

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)