简体   繁体   English

extract-text-webpack-plugin输出css文件,图片路径错误?

[英]extract-text-webpack-plugin output css file, image path wrong?

When I use extract-text-webpack-plugin output css file into a folder, the image url is wrong, my webpack config is below: 当我将extract-text-webpack-plugin输出css文件用于文件夹时,图片网址错误,我的webpack配置如下:

"use strict";
var path = require('path');
var webpack = require('webpack');

// webpack plugins
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');


module.exports = {
    entry: path.resolve(__dirname, 'app/scripts/main.js'),
    output: {
        path: path.resolve(__dirname, "build"),
        //publicPath: path.resolve(__dirname, "build"),
        filename: 'js/[name]-[hash].js'
    },
    module: {
        loaders: [{
                test: /\.jsx?$/,   
                loader: 'babel',    
                exclude: /(node_modules|bower_components)/,
                query: {
                    presets: ['react', 'es2015']
                }
        },{
                test: /\.scss$/,
                exclude: /(node_modules|bower_components)/,
                loader: ExtractTextPlugin.extract('style', 'css!sass!postcss')
        },{
                test: /\.css$/,
                exclude: /(node_modules|bower_components)/,
                loader: ExtractTextPlugin.extract('style', 'css!postcss')
        },{
                test: /\.(woff|woff2|svg|eot|ttf)/,
                loader: 'url?prefix=font/&limit=10000'
        },{
                test: /\.(png|jpe?g|gif|svg)$/,
                loaders: [
                    'file?name=images/[name].[ext]',
                    //'image-webpack'
                ]
            }
        ]
    },

    // image handle
    // imageWebpackLoader: {
    // progressive: true,
    // optimizationLevel: 3,
    // interlaced: false,
    // pngquant: {quality: "65-70", speed: 4}
    // },

    postcss: [
        require('autoprefixer')
    ],

    plugins: [
        new webpack.BannerPlugin('This file is created by yugasun'),
        new HtmlWebpackPlugin({
            title: 'RedChild',
            template: __dirname + '/app/template/index.tmpl.html'
        }),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin(),
        new ExtractTextPlugin('css/[name]-[hash].css')
    ]
};

my output 'build' folder tree like this: 我的输出'build'文件夹树像这样:

.
├── css
│   └── main-72c33a6c0ad73a5a0403.css
├── images
│   ├── arrow.png
|   |.....
│   └── wrong_title.png
├── index.html
└── js
    └── main-72c33a6c0ad73a5a0403.js

But in my output css file the image url is 'images/arrow.png', what I want is like '../images/arrow.png'. 但是在我的输出css文件中,图像网址是'images / arrow.png',我想要的就是'../ images/arrow.png'。

Then I add the extract-text-webpack-plugin options like below: 然后我添加extract-text-webpack-plugin选项,如下所示:

Change: 更改:

new ExtractTextPlugin('css/[name]-[hash].css')

To: 至:

new ExtractTextPlugin('css/[name]-[hash].css', {
            publicPath: '../'
        })

But the parameter 'publicPath' doesn't work, in my output css file the image url is still 'images/arrow.png'. 但参数'publicPath'不起作用,在我的输出css文件中,图像网址仍然是'images / arrow.png'。

My package.json is like bellow: 我的package.json就像下面一样:

{
  "name": "h5-webpack-template",
  "version": "1.0.0",
  "description": "H5 Develop template.",
  "main": "bundle.js",
  "scripts": {
    "dev": "webpack-dev-server --progress --colors --content-base build --port 8014 --host 0.0.0.0",
    "build": "NODE_ENV=production webpack --config ./webpack.production.js --progress"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/yugasun/H5%2Bwebpack.git"
  },
  "keywords": [
    "webpack",
    "ES6",
    "HTML5"
  ],
  "author": "yugasun",
  "license": "MIT",
  "devDependencies": {
    "autoprefixer": "^6.4.0",
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-react": "^6.11.1",
    "clean-webpack-plugin": "^0.1.10",
    "css-loader": "^0.21.0",
    "extract-text-webpack-plugin": "^1.0.1",
    "file-loader": "^0.8.5",
    "html-webpack-plugin": "^2.22.0",
    "image-webpack-loader": "^2.0.0",
    "imagemin-webpack-plugin": "^1.0.8",
    "node-sass": "^3.4.2",
    "normalize.css": "^4.2.0",
    "postcss-loader": "^0.9.1",
    "sass-loader": "^3.1.2",
    "style-loader": "^0.13.0",
    "url-loader": "^0.5.7",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.15.2",
    "webpack-spritesmith": "^0.2.6"
  },
  "bugs": {
    "url": "https://github.com/yugasun/H5%2Bwebpack/issues"
  },
  "homepage": "https://github.com/yugasun/H5%2Bwebpack#readme",
  "dependencies": {
    "weui": "^0.4.3"
  }
}

Why don't you use output.publicPath which actually specifies the public url of your output files? 为什么不使用output.publicPath实际指定输出文件的公共URL? This works for me well. 这对我很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack 4:提取文本Webpack插件错误 - Webpack 4: extract-text-webpack-plugin error 使用Webpack 2和extract-text-webpack-plugin - Using Webpack 2 and extract-text-webpack-plugin webpack 5 中的 extract-text-webpack-plugin 替换 - extract-text-webpack-plugin replacement in webpack 5 使用extract-text-webpack-plugin和在HTML标头中链接合并的CSS文件之间有什么区别? - What is the difference between using extract-text-webpack-plugin and linking a merged CSS file in an HTML header? Webpack - extract-text-webpack-plugin 找不到模块 - Webpack - extract-text-webpack-plugin Cannot find module 使用extract-text-webpack-plugin正确加载Webpack引导程序 - Webpack proper bootstrap loading with extract-text-webpack-plugin CSS_MODULES:模块构建失败,带有extract-text-webpack-plugin - CSS_MODULES: Module build failed with extract-text-webpack-plugin 错误:找不到模块“ extract-text-webpack-plugin” - Error: Cannot find module 'extract-text-webpack-plugin' 使用extract-text-webpack-plugin时出现意外的字符 - unexpected character when using extract-text-webpack-plugin extract-text-webpack-plugin-如何创建哈希名称(仅在文件更改时使用唯一名称) - extract-text-webpack-plugin - How to create hash name (unique name only if file change)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM