From 4b78d33a3f4f1128e6f71da3be256aae05f328a8 Mon Sep 17 00:00:00 2001 From: proc07 <790847400@qq.com> Date: Sat, 23 Mar 2024 09:47:56 +0800 Subject: [PATCH 1/6] fix: externalize the asset js file type, but enter vite:dep-pre-bundle --- packages/vite/src/node/optimizer/esbuildDepPlugin.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index 08b1abb72d48e3..1f4c4dab16748d 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -155,6 +155,14 @@ export function esbuildDepPlugin( const resolved = await resolve(id, importer, kind) if (resolved) { if (kind === 'require-call') { + // #16116 fix: Import the module.scss path, which is actually module.scss.js + if (resolved.endsWith('.js')) { + return { + path: resolved, + external: false, + } + } + // here it is not set to `external: true` to convert `require` to `import` return { path: resolved, From cf0e3f147a60b1f2166dabf1a3f26411c10f5526 Mon Sep 17 00:00:00 2001 From: proc07 <790847400@qq.com> Date: Sun, 24 Mar 2024 17:57:02 +0800 Subject: [PATCH 2/6] test: externalize the asset js file type (#16116) --- .../__tests__/optimize-deps.spec.ts | 15 +++++++++++++++ .../index.js | 6 ++++++ .../package.json | 5 +++++ .../test.astro.js | 5 +++++ .../test.okay.js | 5 +++++ .../test.scss.js | 5 +++++ .../test.tsx.js | 5 +++++ playground/optimize-deps/index.html | 19 +++++++++++++++++++ playground/optimize-deps/package.json | 1 + playground/optimize-deps/vite.config.js | 4 ++++ 10 files changed, 70 insertions(+) create mode 100644 playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/index.js create mode 100644 playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/package.json create mode 100644 playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.astro.js create mode 100644 playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.okay.js create mode 100644 playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.scss.js create mode 100644 playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.tsx.js diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index ee691fddc4c686..d6c328b9216009 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -331,3 +331,18 @@ test.runIf(isServe)('warn on incompatible dependency', () => { ), ) }) + +test('import the CommonJS external package that omits the js suffix', async () => { + await expectWithRetry(() => page.textContent('.external-package-js')).toBe( + 'okay', + ) + await expectWithRetry(() => + page.textContent('.external-package-scss-js'), + ).toBe('scss') + await expectWithRetry(() => + page.textContent('.external-package-astro-js'), + ).toBe('astro') + await expectWithRetry(() => + page.textContent('.external-package-tsx-js'), + ).toBe('tsx') +}) diff --git a/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/index.js b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/index.js new file mode 100644 index 00000000000000..c3bcc276762093 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/index.js @@ -0,0 +1,6 @@ +const { okay } = require('./test.okay') +const { scss } = require('./test.scss') +const { astro } = require('./test.astro') +const { tsx } = require('./test.tsx') + +module.exports = { okay, scss, astro, tsx } diff --git a/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/package.json b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/package.json new file mode 100644 index 00000000000000..22a91fc97ea119 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/package.json @@ -0,0 +1,5 @@ +{ + "name": "@vitejs/test-dep-cjs-external-package-omit-js-suffix", + "version": "0.0.1", + "main": "index.js" +} diff --git a/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.astro.js b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.astro.js new file mode 100644 index 00000000000000..337124ff2857bc --- /dev/null +++ b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.astro.js @@ -0,0 +1,5 @@ +function astro() { + return 'astro' +} + +module.exports = { astro } diff --git a/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.okay.js b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.okay.js new file mode 100644 index 00000000000000..b8913f7c835d9c --- /dev/null +++ b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.okay.js @@ -0,0 +1,5 @@ +function okay() { + return 'okay' +} + +module.exports = { okay } diff --git a/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.scss.js b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.scss.js new file mode 100644 index 00000000000000..507ebe60f429d3 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.scss.js @@ -0,0 +1,5 @@ +function scss() { + return 'scss' +} + +module.exports = { scss } diff --git a/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.tsx.js b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.tsx.js new file mode 100644 index 00000000000000..6edc349a1880c3 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-external-package-omit-js-suffix/test.tsx.js @@ -0,0 +1,5 @@ +function tsx() { + return 'tsx' +} + +module.exports = { tsx } diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 9b992b7dd4892a..304eb0628522b1 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -110,6 +110,25 @@