Skip to content

Commit

Permalink
feat(css): format lightningcss error (#18818)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 28, 2024
1 parent ec51115 commit dac7992
Showing 1 changed file with 66 additions and 51 deletions.
117 changes: 66 additions & 51 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dac7992

Please sign in to comment.