diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 5363e6a4c246eb..6cd442ba1d29bd 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -657,18 +657,22 @@ export function tryNodeResolve( if (!externalize) { return resolved } - // dont external symlink packages + // don't external symlink packages if (!allowLinkedExternal && !resolved.id.includes('node_modules')) { return resolved } const resolvedExt = path.extname(resolved.id) + // don't external non-js imports + if ( + resolvedExt && + resolvedExt !== '.js' && + resolvedExt !== '.mjs' && + resolvedExt !== '.cjs' + ) { + return resolved + } let resolvedId = id if (isDeepImport) { - // check ext before externalizing - only externalize - // extension-less imports and explicit .js imports - if (resolvedExt && !resolved.id.match(/(.js|.mjs|.cjs)$/)) { - return resolved - } if (!pkg?.data.exports && path.extname(id) !== resolvedExt) { resolvedId += resolvedExt } diff --git a/playground/ssr-deps/__tests__/ssr-deps.spec.ts b/playground/ssr-deps/__tests__/ssr-deps.spec.ts index f20dca26263f65..26bec1fe2de2c0 100644 --- a/playground/ssr-deps/__tests__/ssr-deps.spec.ts +++ b/playground/ssr-deps/__tests__/ssr-deps.spec.ts @@ -1,5 +1,5 @@ import { port } from './serve' -import { page } from '~utils' +import { getColor, page } from '~utils' const url = `http://localhost:${port}` @@ -108,3 +108,8 @@ test('msg from linked no external', async () => { await page.goto(url) expect(await page.textContent('.dep-virtual')).toMatch('[success]') }) + +test('import css library', async () => { + await page.goto(url) + expect(await getColor('.css-lib')).toBe('blue') +}) diff --git a/playground/ssr-deps/css-lib/index.css b/playground/ssr-deps/css-lib/index.css new file mode 100644 index 00000000000000..d3974e432dc451 --- /dev/null +++ b/playground/ssr-deps/css-lib/index.css @@ -0,0 +1,3 @@ +.css-lib { + color: blue; +} diff --git a/playground/ssr-deps/css-lib/package.json b/playground/ssr-deps/css-lib/package.json new file mode 100644 index 00000000000000..2314aeb4530e3e --- /dev/null +++ b/playground/ssr-deps/css-lib/package.json @@ -0,0 +1,6 @@ +{ + "name": "@vitejs/css-lib", + "private": true, + "version": "0.0.0", + "main": "./index.css" +} diff --git a/playground/ssr-deps/index.html b/playground/ssr-deps/index.html index b1e884efaab01a..bc482a33e118ae 100644 --- a/playground/ssr-deps/index.html +++ b/playground/ssr-deps/index.html @@ -8,5 +8,9 @@