简体   繁体   English

Webpack在样式加载器和extract-text-webpack-plugin之间切换

[英]Webpack switching between style-loader and extract-text-webpack-plugin

I have development and production webpack configurations where I switch between using style-loader and extract-text-webpack-plugin to bundle CSS, respectively. 我有开发和生产webpack的配置,分别在使用style-loaderextract-text-webpack-plugin捆绑CSS之间进行切换。 This is because my development config uses hot module replacement, and thus requires inline-styles, while my production config does not. 这是因为我的开发配置使用热模块替换,因此需要内联样式,而我的生产配置则不需要。

However, upon switching from production to development, I've noticed that the extracted main.css left over from my production webpack build is overriding the inline styles of my style-loader . 但是,从生产切换到开发时,我注意到从生产Webpack构建中遗留下来的提取的main.css 覆盖了我的style-loader的内联样式。 This means that hot reloading does not work on CSS changes. 这意味着热重载不适用于CSS更改。

First, shouldn't my inline styles override my external main.css stylesheet, so why is this occurring? 首先,内联样式不应该覆盖我的外部main.css样式表,那么为什么会发生这种情况? Second, if this is expected behavior, what is the general convention for dealing with this? 其次,如果这是预期的行为,处理该行为的一般约定是什么? Should I use some webpack "cleaning" plugin to remove main.css in my development config? 我应该使用一些webpack的“清理”插件在开发配置中删除main.css吗?

You can do like this 你可以这样

const webpack = require('webpack');
const path = require('path');
const env = require('./env.json');

var conf = {

    resolve: {
        extensions: ['', '.ts', '.js'],
        root: __dirname,
        modulesDirectories: ['node_modules']
    },
    module: {
        rules: [
            //Some common Loaders Here
            {
                test: /\.js$/,
                loader: 'source-map-loader',
                exclude: [
                    'node_modules/rxjs'
                ]
            }

        ],
        loaders: [{
            test: /\.ts$/,
            loader: 'awesome-typescript-loader'
        }],
    },
    plugins: [
        //Some common plugins Here ,
    ],
    output: {
        path: 'src',
        filename: '[name].bundle.js',
        sourceMapFilename: '[name].bundle.map',
        //chunkFilename: '[id].[chunkhash].chunk.js'
    }
};

if (env.dev) {
    conf.module.rules.push({
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
            fallback: "style-loader",
            use: "css-loader"
        })
    });
    conf.plugins.push(new ExtractTextPlugin("styles.css"));

} else {

    conf.module.rules.push({
        test: /\.css$/,
        use: [{
                loader: "style-loader"
            },
            {
                loader: "css-loader"
            }
        ]
    });


}

module.exports = conf;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM