fix(webpack): can't resolve absolute url in css (#8555)

This commit is contained in:
Xin Du (Clark) 2020-12-27 23:53:05 +00:00 committed by GitHub
parent ac762299a1
commit b2e38f5c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -104,6 +104,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"localIdentName\\": \\"[local]_[hash:base64:5]\\", \\"localIdentName\\": \\"[local]_[hash:base64:5]\\",
}, },
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -135,6 +136,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"esModule\\": false, \\"esModule\\": false,
\\"importLoaders\\": 2, \\"importLoaders\\": 2,
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -175,6 +177,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"localIdentName\\": \\"[local]_[hash:base64:5]\\", \\"localIdentName\\": \\"[local]_[hash:base64:5]\\",
}, },
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -206,6 +209,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"esModule\\": false, \\"esModule\\": false,
\\"importLoaders\\": 2, \\"importLoaders\\": 2,
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -246,6 +250,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"localIdentName\\": \\"[local]_[hash:base64:5]\\", \\"localIdentName\\": \\"[local]_[hash:base64:5]\\",
}, },
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -283,6 +288,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"esModule\\": false, \\"esModule\\": false,
\\"importLoaders\\": 2, \\"importLoaders\\": 2,
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -329,6 +335,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"localIdentName\\": \\"[local]_[hash:base64:5]\\", \\"localIdentName\\": \\"[local]_[hash:base64:5]\\",
}, },
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -369,6 +376,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"esModule\\": false, \\"esModule\\": false,
\\"importLoaders\\": 2, \\"importLoaders\\": 2,
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -418,6 +426,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"localIdentName\\": \\"[local]_[hash:base64:5]\\", \\"localIdentName\\": \\"[local]_[hash:base64:5]\\",
}, },
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -455,6 +464,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"esModule\\": false, \\"esModule\\": false,
\\"importLoaders\\": 2, \\"importLoaders\\": 2,
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -501,6 +511,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"localIdentName\\": \\"[local]_[hash:base64:5]\\", \\"localIdentName\\": \\"[local]_[hash:base64:5]\\",
}, },
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {
@ -538,6 +549,7 @@ exports[`webpack nuxt webpack module.rules 1`] = `
\\"esModule\\": false, \\"esModule\\": false,
\\"importLoaders\\": 2, \\"importLoaders\\": 2,
\\"sourceMap\\": false, \\"sourceMap\\": false,
\\"url\\": [Function isUrlResolvingEnabled],
}, },
}, },
Object { Object {

View File

@ -1,4 +1,5 @@
import path from 'path' import path from 'path'
import consola from 'consola'
import ExtractCssChunksPlugin from 'extract-css-chunks-webpack-plugin' import ExtractCssChunksPlugin from 'extract-css-chunks-webpack-plugin'
import { wrapArray } from '@nuxt/utils' import { wrapArray } from '@nuxt/utils'
@ -25,6 +26,16 @@ export default class StyleLoader {
return Boolean(this.isServer && this.extractCSS) return Boolean(this.isServer && this.extractCSS)
} }
isUrlResolvingEnabled (url, resourcePath) {
// Ignore absolute URLs, it will be handled by serve-static.
if (url.startsWith('/')) {
consola.warn(`Please use relative path or alias path instead of absolute path ${url}`)
return false
}
return true
}
normalize (loaders) { normalize (loaders) {
loaders = wrapArray(loaders) loaders = wrapArray(loaders)
return loaders.map(loader => (typeof loader === 'string' ? { loader } : loader)) return loaders.map(loader => (typeof loader === 'string' ? { loader } : loader))
@ -71,6 +82,10 @@ export default class StyleLoader {
css (options) { css (options) {
const cssLoader = { loader: this.resolveModule('css-loader'), options } const cssLoader = { loader: this.resolveModule('css-loader'), options }
if (!options.url) {
options.url = this.isUrlResolvingEnabled
}
if (this.exportOnlyLocals) { if (this.exportOnlyLocals) {
options.modules = { options.modules = {
...options.modules, ...options.modules,