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): using background url which match alias in sass/less, there will be link error (fix #2316) #2323

Merged
merged 4 commits into from
Mar 24, 2021
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
8 changes: 8 additions & 0 deletions packages/playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ <h1>CSS</h1>
<p class="sass-at-import">
@import from SASS _index: This should be olive and have bg image
</p>
<p class="sass-at-import-alias">
@import from SASS _index: This should be olive and have bg image which url
contains alias
</p>
<p class="sass-partial">@import from SASS _partial: This should be orchid</p>
<p>Imported SASS string:</p>
<pre class="imported-sass"></pre>
Expand All @@ -29,6 +33,10 @@ <h1>CSS</h1>
<p class="less-at-import">
@import from Less: This should be darkslateblue and have bg image
</p>
<p class="less-at-import-alias">
@import from Less: This should be darkslateblue and have bg image which url
contains alias
</p>
<p>Imported Less string:</p>
<pre class="imported-less"></pre>

Expand Down
5 changes: 5 additions & 0 deletions packages/playground/css/nested/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
color: olive;
background: url(./icon.png) 10px no-repeat;
}

.sass-at-import-alias {
color: olive;
background: url(@/nested/icon.png) 10px no-repeat;
}
5 changes: 5 additions & 0 deletions packages/playground/css/nested/nested.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
color: darkslateblue;
background: url(./icon.png) 10px no-repeat;
}

.less-at-import-alias {
color: darkslateblue;
background: url(@/nested/icon.png) 10px no-repeat;
}
33 changes: 28 additions & 5 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import MagicString from 'magic-string'
import * as Postcss from 'postcss'
import * as Sass from 'sass'
import type Less from 'less'
import { Alias } from 'types/alias'

// const debug = createDebugger('vite:css')

Expand Down Expand Up @@ -535,6 +536,7 @@ async function compileCSS(
case 'sass':
opts = {
includePaths: ['node_modules'],
alias: config.resolve.alias,
...opts
}
break
Expand All @@ -543,6 +545,7 @@ async function compileCSS(
case 'stylus':
opts = {
paths: ['node_modules'],
alias: config.resolve.alias,
...opts
}
}
Expand Down Expand Up @@ -848,6 +851,7 @@ type StylePreprocessor = (
[key: string]: any
additionalData?: PreprocessorAdditionalData
filename: string
alias: Alias[]
Copy link
Member

Choose a reason for hiding this comment

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

nitpick (non-blocking): Plural of alias is aliases (applies also to other lines)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your advice. I've changed the name of alias to aliases.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for going against this change, but we are using alias in other config options, and it is a string of aliases https://vitejs.dev/config/#resolve-alias. It is strange in the code to see aliases = config.resolve.alias. I think the singular form was ok here. @Shinigami92 @crcong what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Mh I complained about it, because it was passed and always used as an array (in this PR)
In convention perspective this would mean that it should be named in plural form

Keep in mind that it is only a code change right now and not a breaking change, so config.resolve.alias is still config.resolve.alias in the public exposed API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found that the configuration config.resolve.alias has been processed into an array inside vite, so I also think it is ok to use the singular here. I will change it back. @Shinigami92 @matias-capeletto Thank you for your suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, sorry for the inconvenience 😅

},
resolvers: CSSAtImportResolvers
) => StylePreprocessorResults | Promise<StylePreprocessorResults>
Expand Down Expand Up @@ -886,7 +890,7 @@ const scss: StylePreprocessor = async (source, root, options, resolvers) => {
importer(url, importer, done) {
resolvers.sass(url, importer).then((resolved) => {
if (resolved) {
rebaseUrls(resolved, options.filename).then(done)
rebaseUrls(resolved, options.filename, options.alias).then(done)
} else {
done(null)
}
Expand Down Expand Up @@ -936,7 +940,8 @@ const sass: StylePreprocessor = (source, root, options, aliasResolver) =>
*/
async function rebaseUrls(
file: string,
rootFile: string
rootFile: string,
alias: Alias[]
): Promise<Sass.ImporterReturnType> {
file = path.resolve(file) // ensure os-specific flashes
// in the same dir, no need to rebase
Expand All @@ -952,6 +957,14 @@ async function rebaseUrls(
}
const rebased = await rewriteCssUrls(content, (url) => {
if (url.startsWith('/')) return url
// match alias, no need to rewrite
for (const { find } of alias) {
const matches =
typeof find === 'string' ? url.startsWith(find) : find.test(url)
if (matches) {
return url
}
}
const absolute = path.resolve(fileDir, url)
const relative = path.relative(rootDir, absolute)
return normalizePath(relative)
Expand All @@ -968,6 +981,7 @@ const less: StylePreprocessor = async (source, root, options, resolvers) => {
const viteResolverPlugin = createViteLessPlugin(
nodeLess,
options.filename,
options.alias,
resolvers
)
source = await getSource(source, options.filename, options.additionalData)
Expand Down Expand Up @@ -1004,16 +1018,23 @@ let ViteLessManager: any
function createViteLessPlugin(
less: typeof Less,
rootFile: string,
alias: Alias[],
resolvers: CSSAtImportResolvers
): Less.Plugin {
if (!ViteLessManager) {
ViteLessManager = class ViteManager extends less.FileManager {
resolvers
rootFile
constructor(rootFile: string, resolvers: CSSAtImportResolvers) {
alias
constructor(
rootFile: string,
resolvers: CSSAtImportResolvers,
alias: Alias[]
) {
super()
this.rootFile = rootFile
this.resolvers = resolvers
this.alias = alias
}
supports() {
return true
Expand All @@ -1032,7 +1053,7 @@ function createViteLessPlugin(
path.join(dir, '*')
)
if (resolved) {
const result = await rebaseUrls(resolved, this.rootFile)
const result = await rebaseUrls(resolved, this.rootFile, this.alias)
let contents
if (result && 'contents' in result) {
contents = result.contents
Expand All @@ -1052,7 +1073,9 @@ function createViteLessPlugin(

return {
install(_, pluginManager) {
pluginManager.addFileManager(new ViteLessManager(rootFile, resolvers))
pluginManager.addFileManager(
new ViteLessManager(rootFile, resolvers, alias)
)
},
minVersion: [3, 0, 0]
}
Expand Down