diff --git a/package.json b/package.json index 1e7ce0fe72ec31..f877a59b859956 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "test": "run-s test-unit test-serve test-build", "test-serve": "vitest run -c vitest.config.e2e.ts", "test-build": "cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts", + "test-build-legacy-cjs": "cross-env VITE_TEST_LEGACY_CJS_PLUGIN=1 pnpm test-build", "test-unit": "vitest run", "test-docs": "pnpm run docs-build", "debug-serve": "cross-env VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts", diff --git a/packages/vite/package.json b/packages/vite/package.json index 1436e3f275c1e5..afc3a952358902 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -71,7 +71,7 @@ "@babel/types": "^7.18.4", "@jridgewell/trace-mapping": "^0.3.13", "@rollup/plugin-alias": "^3.1.9", - "@rollup/plugin-commonjs": "^21.1.0", + "@rollup/plugin-commonjs": "^22.0.1", "@rollup/plugin-dynamic-import-vars": "^1.4.3", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "13.3.0", diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index fc51e94c299e3c..102a1df2ec4207 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -1,6 +1,7 @@ /* eslint-disable no-restricted-globals */ import fs from 'node:fs' import path from 'node:path' +import url from 'node:url' import nodeResolve from '@rollup/plugin-node-resolve' import typescript from '@rollup/plugin-typescript' import commonjs from '@rollup/plugin-commonjs' @@ -134,15 +135,13 @@ function createNodePlugins( }, // postcss-load-config calls require after register ts-node 'postcss-load-config/src/index.js': { - src: `require(configFile)`, - replacement: `__require(configFile)` - }, - // @rollup/plugin-commonjs uses incorrect esm - '@rollup/plugin-commonjs/dist/index.es.js': { - src: `import { sync } from 'resolve';`, - replacement: `import __resolve from 'resolve';const sync = __resolve.sync;` + pattern: /require(?=\((configFile|'ts-node')\))/g, + replacement: `eval('require')` } }), + + buildTimeImportMetaUrl(), + commonjs({ extensions: ['.js'], // Optional peer deps of ws. Native deps that are mostly for performance. @@ -287,6 +286,21 @@ function shimDepsPlugin(deps: Record): Plugin { } } +// The use of `import.meta.url` in source code is not reliable after bundling. +// For example, it is affected by the `isEntry` bug brought in by the Rollup CJS plugin +// https://github.com/rollup/plugins/pull/1180 +// The better way is to resolve it at build time. +function buildTimeImportMetaUrl(): Plugin { + return { + name: 'buildTimeImportMetaUrl', + resolveImportMeta: (property, chunk) => { + if (property === 'url') { + return `'${url.pathToFileURL(chunk.moduleId).href}'` + } + } + } +} + function licensePlugin() { return license({ thirdParty(dependencies) { diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index acda2d3f7d0ff5..fbc5754af892cd 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -594,6 +594,13 @@ export async function resolveConfig( config = mergeConfig(config, externalConfigCompat(config, configEnv)) const optimizeDeps = config.optimizeDeps || {} + if (process.env.VITE_TEST_LEGACY_CJS_PLUGIN) { + config.legacy = { + ...config.legacy, + buildRollupPluginCommonjs: true + } + } + const BASE_URL = resolvedBase const resolved: ResolvedConfig = { diff --git a/playground/external/vite.config.js b/playground/external/vite.config.js index f6126b069cf49d..fde8e763f810e0 100644 --- a/playground/external/vite.config.js +++ b/playground/external/vite.config.js @@ -5,6 +5,9 @@ export default defineConfig({ minify: false, rollupOptions: { external: ['vue'] + }, + commonjsOptions: { + esmExternals: ['vue'] } } }) diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index df1713b3fc76f8..b0af6debca526e 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -103,11 +103,16 @@ test('vue + vuex', async () => { expect(await page.textContent('.vue')).toMatch(`[success]`) }) -test('esbuild-plugin', async () => { - expect(await page.textContent('.esbuild-plugin')).toMatch( - `Hello from an esbuild plugin` - ) -}) +// When we use the Rollup CommonJS plugin instead of esbuild prebundling, +// the esbuild plugins won't apply to dependencies +test.skipIf(isBuild && process.env.VITE_TEST_LEGACY_CJS_PLUGIN)( + 'esbuild-plugin', + async () => { + expect(await page.textContent('.esbuild-plugin')).toMatch( + `Hello from an esbuild plugin` + ) + } +) test('import from hidden dir', async () => { expect(await page.textContent('.hidden-dir')).toBe('hello!') diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js index e268510d5c8d76..133d0da1e32b74 100644 --- a/playground/optimize-deps/vite.config.js +++ b/playground/optimize-deps/vite.config.js @@ -72,7 +72,7 @@ module.exports = { apply: 'build', enforce: 'pre', load(id) { - if (id === '__vite-browser-external:fs') { + if (id === '__vite-browser-external') { return `export default {}; export function readFileSync() {}` } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a036ebe08ce13..c9b8b2cb090716 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -216,7 +216,7 @@ importers: '@babel/types': ^7.18.4 '@jridgewell/trace-mapping': ^0.3.13 '@rollup/plugin-alias': ^3.1.9 - '@rollup/plugin-commonjs': ^21.1.0 + '@rollup/plugin-commonjs': ^22.0.1 '@rollup/plugin-dynamic-import-vars': ^1.4.3 '@rollup/plugin-json': ^4.1.0 '@rollup/plugin-node-resolve': 13.3.0 @@ -284,7 +284,7 @@ importers: '@babel/types': 7.18.4 '@jridgewell/trace-mapping': 0.3.13 '@rollup/plugin-alias': 3.1.9_rollup@2.75.6 - '@rollup/plugin-commonjs': 21.1.0_rollup@2.75.6 + '@rollup/plugin-commonjs': 22.0.1_rollup@2.75.6 '@rollup/plugin-dynamic-import-vars': 1.4.3_rollup@2.75.6 '@rollup/plugin-json': 4.1.0_rollup@2.75.6 '@rollup/plugin-node-resolve': 13.3.0_rollup@2.75.6 @@ -2021,6 +2021,22 @@ packages: rollup: 2.75.6 dev: true + /@rollup/plugin-commonjs/22.0.1_rollup@2.75.6: + resolution: {integrity: sha512-dGfEZvdjDHObBiP5IvwTKMVeq/tBZGMBHZFMdIV1ClMM/YoWS34xrHFGfag9SN2ZtMgNZRFruqvxZQEa70O6nQ==} + engines: {node: '>= 12.0.0'} + peerDependencies: + rollup: ^2.68.0 + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.75.6 + commondir: 1.0.1 + estree-walker: 2.0.2 + glob: 7.2.0 + is-reference: 1.2.1 + magic-string: 0.25.9 + resolve: 1.22.1 + rollup: 2.75.6 + dev: true + /@rollup/plugin-dynamic-import-vars/1.4.3_rollup@2.75.6: resolution: {integrity: sha512-VYP9BBVI0pcYpLp/DkFT8YP+EmqmWFMmWXoTObDH6OouERxJyPsIj0tC3HxhjNBOKgcRc7eV75IQItzELt7QSg==} engines: {node: '>= 10.0.0'} @@ -3256,7 +3272,7 @@ packages: dev: true /commondir/1.0.1: - resolution: {integrity: sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=} + resolution: {integrity: sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==} dev: true /compare-func/2.0.0: