From b19a010a79d2044a7cdf4b46e6a04908a70e7c61 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 9 Mar 2023 11:23:34 +0800 Subject: [PATCH] fix(optimizer): transform css require to import (fix #12304) --- packages/vite/src/node/optimizer/esbuildDepPlugin.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index 0afc54a75586f9..e6378bc4e94930 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -1,6 +1,6 @@ import path from 'node:path' import type { ImportKind, Plugin } from 'esbuild' -import { KNOWN_ASSET_TYPES } from '../constants' +import { CSS_LANGS_RE, KNOWN_ASSET_TYPES } from '../constants' import { getDepOptimizationConfig } from '..' import type { ResolvedConfig } from '..' import { @@ -153,10 +153,12 @@ export function esbuildDepPlugin( { filter: /./, namespace: externalWithConversionNamespace }, (args) => { // import itself with prefix (this is the actual part of require-import conversion) + const modulePath = `"${convertedExternalPrefix}${args.path}"` return { - contents: - `export { default } from "${convertedExternalPrefix}${args.path}";` + - `export * from "${convertedExternalPrefix}${args.path}";`, + contents: CSS_LANGS_RE.test(args.path) + ? `import ${modulePath};` + : `export { default } from ${modulePath};` + + `export * from ${modulePath};`, loader: 'js', } },