Skip to content

Commit afff05c

Browse files
authored
fix(css): resolve style tags in HTML files correctly for lightningcss (#19001)
1 parent 1acea1f commit afff05c

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import {
8383
} from '../utils'
8484
import type { Logger } from '../logger'
8585
import { cleanUrl, isWindows, slash } from '../../shared/utils'
86+
import { NULL_BYTE_PLACEHOLDER } from '../../shared/constants'
8687
import { createBackCompatIdResolver } from '../idResolver'
8788
import type { ResolveIdFn } from '../idResolver'
8889
import { PartialEnvironment } from '../baseEnvironment'
@@ -3160,10 +3161,9 @@ async function compileLightningCSS(
31603161
): ReturnType<typeof compileCSS> {
31613162
const { config } = environment
31623163
const deps = new Set<string>()
3163-
// Relative path is needed to get stable hash when using CSS modules
3164-
const filename = cleanUrl(path.relative(config.root, id))
3165-
const toAbsolute = (filePath: string) =>
3166-
path.isAbsolute(filePath) ? filePath : path.join(config.root, filePath)
3164+
// replace null byte as lightningcss treats that as a string terminator
3165+
// https://github.com/parcel-bundler/lightningcss/issues/874
3166+
const filename = id.replace('\0', NULL_BYTE_PLACEHOLDER)
31673167

31683168
let res: LightningCssTransformAttributeResult | LightningCssTransformResult
31693169
try {
@@ -3180,16 +3180,14 @@ async function compileLightningCSS(
31803180
).bundleAsync({
31813181
...config.css.lightningcss,
31823182
filename,
3183+
// projectRoot is needed to get stable hash when using CSS modules
3184+
projectRoot: config.root,
31833185
resolver: {
31843186
read(filePath) {
31853187
if (filePath === filename) {
31863188
return src
31873189
}
3188-
// This happens with html-proxy (#13776)
3189-
if (!filePath.endsWith('.css')) {
3190-
return src
3191-
}
3192-
return fs.readFileSync(toAbsolute(filePath), 'utf-8')
3190+
return fs.readFileSync(filePath, 'utf-8')
31933191
},
31943192
async resolve(id, from) {
31953193
const publicFile = checkPublicFile(
@@ -3202,7 +3200,7 @@ async function compileLightningCSS(
32023200

32033201
const resolved = await getAtImportResolvers(
32043202
environment.getTopLevelConfig(),
3205-
).css(environment, id, toAbsolute(from))
3203+
).css(environment, id, from)
32063204

32073205
if (resolved) {
32083206
deps.add(resolved)
@@ -3224,7 +3222,7 @@ async function compileLightningCSS(
32243222
} catch (e) {
32253223
e.message = `[lightningcss] ${e.message}`
32263224
e.loc = {
3227-
file: toAbsolute(e.fileName),
3225+
file: e.fileName.replace(NULL_BYTE_PLACEHOLDER, '\0'),
32283226
line: e.loc.line,
32293227
column: e.loc.column - 1, // 1-based
32303228
}
@@ -3242,7 +3240,10 @@ async function compileLightningCSS(
32423240
if (skipUrlReplacer(dep.url)) {
32433241
replaceUrl = dep.url
32443242
} else if (urlReplacer) {
3245-
replaceUrl = await urlReplacer(dep.url, toAbsolute(dep.loc.filePath))
3243+
replaceUrl = await urlReplacer(
3244+
dep.url,
3245+
dep.loc.filePath.replace(NULL_BYTE_PLACEHOLDER, '\0'),
3246+
)
32463247
} else {
32473248
replaceUrl = dep.url
32483249
}

playground/css-lightningcss/__tests__/css-lightningcss.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ test('css modules', async () => {
6161

6262
test('inline css modules', async () => {
6363
const css = await page.textContent('.modules-inline')
64-
expect(css).toMatch(/\.\w{6}_apply-color-inline/)
64+
expect(css).toMatch(/\._?\w{6}_apply-color-inline/)
6565
})
6666

6767
test.runIf(isBuild)('minify css', async () => {

0 commit comments

Comments
 (0)