The problem of loading image resources in webpack4.0 html and css
when packaging files with webpack, it is found that the application path is always misaligned, and the reference to img in html and css always goes wrong.
this is the entire file directory structure
html
css
webpack.config.js
The path to the img file in
html is correct. But the path of css becomes img, in the css directory
upstairs is wrong, you can look here.
here is an example of getting started with webpack4. You can take a look at https://github.com/crlang/eas..
Let's talk about my problem first. Here is my directory structure (the following files / folders named with test are used for this test)
webpack
sass
src/img/test/test.pngdist/img/test/test.png
dist/imgcssdist/css/img
src
publicPath
,publicPathcsspublicPathpublicPathname
dist/imgfileurl
emm,dist/imgQAQfileurlnameQAQ/test.png
namefile'QAQ/',publicPathurl
you can see that this has achieved the desired effect, and the image reference path in the packaged css is fine.
I hope this will provide some help to the landlord. In addition, if there are any mistakes, please point out that, after all, the language of writing something for the first time is a bit confusing (lying down)
.
build==>utils.js:
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
publicPath:'../../' //
})
} else {
return ['vue-style-loader'].concat(loaders)
}
cssjsconfig==>index.js:
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './', //'/''./'
productionSourceMap: false,
...
}