From dac7992e8725234007c7515f86f543992874c7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 28 Nov 2024 21:53:42 +0900 Subject: [PATCH] feat(css): format lightningcss error (#18818) --- packages/vite/src/node/plugins/css.ts | 117 +++++++++++++++----------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 8c484b54e28e4a..43bb7ad20df3de 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -33,6 +33,10 @@ import type { SassModernPreprocessBaseOptions, StylusPreprocessorBaseOptions, } from 'types/internal/cssPreprocessorOptions' +import type { + TransformAttributeResult as LightningCssTransformAttributeResult, + TransformResult as LightningCssTransformResult, +} from 'lightningcss' import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap' import type { EnvironmentModuleNode } from '../server/moduleGraph' import { @@ -3114,60 +3118,71 @@ async function compileLightningCSS( const toAbsolute = (filePath: string) => path.isAbsolute(filePath) ? filePath : path.join(config.root, filePath) - const res = styleAttrRE.test(id) - ? (await importLightningCSS()).transformStyleAttribute({ - filename, - code: Buffer.from(src), - targets: config.css?.lightningcss?.targets, - minify: config.isProduction && !!config.build.cssMinify, - analyzeDependencies: true, - }) - : await ( - await importLightningCSS() - ).bundleAsync({ - ...config.css?.lightningcss, - filename, - resolver: { - read(filePath) { - if (filePath === filename) { - return src - } - // This happens with html-proxy (#13776) - if (!filePath.endsWith('.css')) { - return src - } - return fs.readFileSync(toAbsolute(filePath), 'utf-8') - }, - async resolve(id, from) { - const publicFile = checkPublicFile( - id, - environment.getTopLevelConfig(), - ) - if (publicFile) { - return publicFile - } + let res: LightningCssTransformAttributeResult | LightningCssTransformResult + try { + res = styleAttrRE.test(id) + ? (await importLightningCSS()).transformStyleAttribute({ + filename, + code: Buffer.from(src), + targets: config.css?.lightningcss?.targets, + minify: config.isProduction && !!config.build.cssMinify, + analyzeDependencies: true, + }) + : await ( + await importLightningCSS() + ).bundleAsync({ + ...config.css?.lightningcss, + filename, + resolver: { + read(filePath) { + if (filePath === filename) { + return src + } + // This happens with html-proxy (#13776) + if (!filePath.endsWith('.css')) { + return src + } + return fs.readFileSync(toAbsolute(filePath), 'utf-8') + }, + async resolve(id, from) { + const publicFile = checkPublicFile( + id, + environment.getTopLevelConfig(), + ) + if (publicFile) { + return publicFile + } - const resolved = await getAtImportResolvers( - environment.getTopLevelConfig(), - ).css(environment, id, toAbsolute(from)) + const resolved = await getAtImportResolvers( + environment.getTopLevelConfig(), + ).css(environment, id, toAbsolute(from)) - if (resolved) { - deps.add(resolved) - return resolved - } - return id + if (resolved) { + deps.add(resolved) + return resolved + } + return id + }, }, - }, - minify: config.isProduction && !!config.build.cssMinify, - sourceMap: - config.command === 'build' - ? !!config.build.sourcemap - : config.css?.devSourcemap, - analyzeDependencies: true, - cssModules: cssModuleRE.test(id) - ? (config.css?.lightningcss?.cssModules ?? true) - : undefined, - }) + minify: config.isProduction && !!config.build.cssMinify, + sourceMap: + config.command === 'build' + ? !!config.build.sourcemap + : config.css?.devSourcemap, + analyzeDependencies: true, + cssModules: cssModuleRE.test(id) + ? (config.css?.lightningcss?.cssModules ?? true) + : undefined, + }) + } catch (e) { + e.message = `[lightningcss] ${e.message}` + e.loc = { + file: toAbsolute(e.fileName), + line: e.loc.line, + column: e.loc.column - 1, // 1-based + } + throw e + } // NodeJS res.code = Buffer // Deno res.code = Uint8Array