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 @@

SSR Dependencies

+ diff --git a/playground/ssr-deps/package.json b/playground/ssr-deps/package.json index 13e7c627a2139e..ac82dadbcead63 100644 --- a/playground/ssr-deps/package.json +++ b/playground/ssr-deps/package.json @@ -9,6 +9,7 @@ "debug": "node --inspect-brk server" }, "dependencies": { + "@vitejs/css-lib": "file:./css-lib", "bcrypt": "^5.0.1", "define-properties-exports": "file:./define-properties-exports", "define-property-exports": "file:./define-property-exports", diff --git a/playground/ssr-deps/src/app.js b/playground/ssr-deps/src/app.js index 0b0fe2d9b968ec..3afb1b00caf5ab 100644 --- a/playground/ssr-deps/src/app.js +++ b/playground/ssr-deps/src/app.js @@ -13,6 +13,7 @@ import noExternalCjs from 'no-external-cjs' import importBuiltinCjs from 'import-builtin-cjs' import { hello as linkedNoExternal } from 'linked-no-external' import virtualMessage from 'pkg-exports/virtual' +import '@vitejs/css-lib' // This import will set a 'Hello World!" message in the nested-external non-entry dependency import 'non-optimized-with-nested-external' @@ -82,5 +83,7 @@ export async function render(url, rootDir) { html += `\n

message from dep-virtual: ${virtualMessage}

` + html += `\n

I should be blue

` + return html + '\n' } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3b00d19680be01..3874fd80599fd3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -894,6 +894,7 @@ importers: playground/ssr-deps: specifiers: + '@vitejs/css-lib': file:./css-lib bcrypt: ^5.0.1 cross-env: ^7.0.3 define-properties-exports: file:./define-properties-exports @@ -917,6 +918,7 @@ importers: require-absolute: file:./require-absolute ts-transpiled-exports: file:./ts-transpiled-exports dependencies: + '@vitejs/css-lib': file:playground/ssr-deps/css-lib bcrypt: 5.0.1 define-properties-exports: file:playground/ssr-deps/define-properties-exports define-property-exports: file:playground/ssr-deps/define-property-exports @@ -941,6 +943,9 @@ importers: cross-env: 7.0.3 express: 4.18.1 + playground/ssr-deps/css-lib: + specifiers: {} + playground/ssr-deps/define-properties-exports: specifiers: {} @@ -9168,6 +9173,12 @@ packages: version: 0.0.0 dev: false + file:playground/ssr-deps/css-lib: + resolution: {directory: playground/ssr-deps/css-lib, type: directory} + name: '@vitejs/css-lib' + version: 0.0.0 + dev: false + file:playground/ssr-deps/define-properties-exports: resolution: {directory: playground/ssr-deps/define-properties-exports, type: directory} name: define-properties-exports