Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): fix directory index import in sass modern api #17960

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines -1098 to -1103
Copy link
Collaborator Author

@hi-ogawa hi-ogawa Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To explain what was happening, sass modern api calls a custom importer twice for each @import resolution. I made a simple test code here without Vite https://github.com/hi-ogawa/reproductions/tree/main/vite-17960-sass-legacy-directory-import

For the test case @import "./dir", importer's canonicalize is called twice:

// 1st call
url: 'file:///abs-path-to/scss-dir/dir',
importer: undefined

// 2nd call
url: 'dir'
importer: 'file://abs-path-to-/scss-dir/main.scss'

Before file:// check is introduced in #17909, @import "./dir" was working due to 2nd call of canonicalize. After #17909, it's breaking since it's resolving to a directory by the 1st call.

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';