Skip to content

Commit e92d025

Browse files
authored
fix(css): handle environment with browser globals (#11079)
1 parent 8b3d656 commit e92d025

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,42 @@ function loadPreprocessor(
14991499
}
15001500
}
15011501

1502+
// in unix, scss might append `location.href` in environments that shim `location`
1503+
// see https://github.com/sass/dart-sass/issues/710
1504+
function cleanScssBugUrl(url: string) {
1505+
if (
1506+
// check bug via `window` and `location` global
1507+
typeof window !== 'undefined' &&
1508+
typeof location !== 'undefined'
1509+
) {
1510+
const prefix = location.href.replace(/\/$/, '')
1511+
return url.replace(prefix, '')
1512+
} else {
1513+
return url
1514+
}
1515+
}
1516+
1517+
function fixScssBugImportValue(
1518+
data: Sass.ImporterReturnType
1519+
): Sass.ImporterReturnType {
1520+
// the scss bug doesn't load files properly so we have to load it ourselves
1521+
// to prevent internal error when it loads itself
1522+
if (
1523+
// check bug via `window` and `location` global
1524+
typeof window !== 'undefined' &&
1525+
typeof location !== 'undefined' &&
1526+
data &&
1527+
// @ts-expect-error
1528+
data.file &&
1529+
// @ts-expect-error
1530+
data.contents == null
1531+
) {
1532+
// @ts-expect-error
1533+
data.contents = fs.readFileSync(data.file, 'utf-8')
1534+
}
1535+
return data
1536+
}
1537+
15021538
// .scss/.sass processor
15031539
const scss: SassStylePreprocessor = async (
15041540
source,
@@ -1507,11 +1543,14 @@ const scss: SassStylePreprocessor = async (
15071543
resolvers
15081544
) => {
15091545
const render = loadPreprocessor(PreprocessLang.sass, root).render
1546+
// NOTE: `sass` always runs it's own importer first, and only falls back to
1547+
// the `importer` option when it can't resolve a path
15101548
const internalImporter: Sass.Importer = (url, importer, done) => {
1549+
importer = cleanScssBugUrl(importer)
15111550
resolvers.sass(url, importer).then((resolved) => {
15121551
if (resolved) {
15131552
rebaseUrls(resolved, options.filename, options.alias, '$')
1514-
.then((data) => done?.(data))
1553+
.then((data) => done?.(fixScssBugImportValue(data)))
15151554
.catch((data) => done?.(data))
15161555
} else {
15171556
done?.(null)
@@ -1556,7 +1595,7 @@ const scss: SassStylePreprocessor = async (
15561595
}
15571596
})
15581597
})
1559-
const deps = result.stats.includedFiles
1598+
const deps = result.stats.includedFiles.map((f) => cleanScssBugUrl(f))
15601599
const map: ExistingRawSourceMap | undefined = result.map
15611600
? JSON.parse(result.map.toString())
15621601
: undefined

playground/css/vite.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const path = require('node:path')
22

3+
// trigger scss bug: https://github.com/sass/dart-sass/issues/710
4+
// make sure Vite handles safely
5+
globalThis.window = {}
6+
globalThis.location = new URL('http://localhost/')
7+
38
/**
49
* @type {import('vite').UserConfig}
510
*/

0 commit comments

Comments
 (0)