Skip to content

Commit

Permalink
fix(css): fix directory index import in sass modern api (#17960)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Sep 2, 2024
1 parent 5c5f82c commit 9b001ba
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,12 +1095,8 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
preferRelative: true,
})
sassResolve = async (...args) => {
const id = args[0]
if (id.startsWith('file://')) {
const fileUrl = new URL(id)
if (fs.existsSync(fileUrl)) {
return fileURLToPath(fileUrl)
}
if (args[0].startsWith('file://')) {
args[0] = fileURLToPath(args[0])
}
return resolver(...args)
}
Expand Down Expand Up @@ -2102,6 +2098,7 @@ const makeScssWorker = (
resolvers: CSSAtImportResolvers,
alias: Alias[],
maxWorkers: number | undefined,
packageName: 'sass' | 'sass-embedded',
) => {
const internalImporter = async (
url: string,
Expand All @@ -2119,6 +2116,9 @@ const makeScssWorker = (
'$',
resolvers.sass,
)
if (packageName === 'sass-embedded') {
return data
}
return fixScssBugImportValue(data)
} catch (data) {
return data
Expand Down Expand Up @@ -2411,7 +2411,12 @@ const scssProcessor = (
? makeModernCompilerScssWorker(resolvers, options.alias, maxWorkers)
: api === 'modern'
? makeModernScssWorker(resolvers, options.alias, maxWorkers)
: makeScssWorker(resolvers, options.alias, maxWorkers),
: makeScssWorker(
resolvers,
options.alias,
maxWorkers,
sassPackage.name,
),
)
}
const worker = workerMap.get(options.alias)!
Expand Down
1 change: 1 addition & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ test('sass', async () => {
)
expect(await getColor(partialImport)).toBe('orchid')
expect(await getColor(await page.$('.sass-file-absolute'))).toBe('orange')
expect(await getColor(await page.$('.sass-dir-index'))).toBe('orange')

editFile('sass.scss', (code) =>
code.replace('color: $injectedColor', 'color: red'),
Expand Down
1 change: 1 addition & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h1>CSS</h1>
<p class="sass-file-absolute">
@import "file:///xxx/absolute-path.scss" should be orange
</p>
<p class="sass-dir-index">@import "./dir" should be orange</p>

<p class="less">Less: This should be blue</p>
<p class="less-at-import">
Expand Down
1 change: 1 addition & 0 deletions playground/css/sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@import '=/pkg-dep'; // package w/out sass field
@import '=/weapp.wxss'; // wxss file
@import 'virtual-file-absolute';
@import '=/scss-dir/main.scss'; // "./dir" reference from vite custom importer

.sass {
/* injected via vite.config.js */
Expand Down
3 changes: 3 additions & 0 deletions playground/css/scss-dir/dir/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sass-dir-index {
color: orange;
}
1 change: 1 addition & 0 deletions playground/css/scss-dir/main.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './dir';

0 comments on commit 9b001ba

Please sign in to comment.