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

View File

@ -1,4 +1,5 @@
import path from 'path'
import consola from 'consola'
import ExtractCssChunksPlugin from 'extract-css-chunks-webpack-plugin'
import { wrapArray } from '@nuxt/utils'
@ -25,6 +26,16 @@ export default class StyleLoader {
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) {
loaders = wrapArray(loaders)
return loaders.map(loader => (typeof loader === 'string' ? { loader } : loader))
@ -71,6 +82,10 @@ export default class StyleLoader {
css (options) {
const cssLoader = { loader: this.resolveModule('css-loader'), options }
if (!options.url) {
options.url = this.isUrlResolvingEnabled
}
if (this.exportOnlyLocals) {
options.modules = {
...options.modules,