Skip to content

Commit

Permalink
fix(css): alias for background url in sass/less link error (fix #2316) (
Browse files Browse the repository at this point in the history
  • Loading branch information
crcong committed Mar 24, 2021
1 parent acb58a0 commit 9499d26
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
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[]
},
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

0 comments on commit 9499d26

Please sign in to comment.