Webpack提取文本插件输出样式条目的.js和.css文件 [英] Webpack extract text plugin outputting .js and .css file for styles entry

查看:88
本文介绍了Webpack提取文本插件输出样式条目的.js和.css文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于prod构建,我希望我的webpack配置有两个入口点,一个用于JS,一个用于SCSS,我希望将它们输出到两个单独的文件(一个JS,一个CSS)。

For a prod build I want my webpack config to have two entry points, one for JS and one for SCSS, and I want these to be output to two separate files (one JS, one CSS).

但是,extract-text-webpack-plugin正在创建两个JS文件和一个CSS文件;即SCSS的入口点产生了所需的CSS文件和我不想要的JS文件。这个未使用的JS文件只包含webpack样板文件和 //由extract-text-webpack-plugin 删除。所以它正在完成它的工作,但仍然创建这个不必要的文件。我的webpack配置是(显示相关部分):

However extract-text-webpack-plugin is creating two JS files and one CSS file; ie the entry point for SCSS is producing both the desired CSS file plus a JS file that I don't want. This unused JS file contains nothing other than the webpack boilerplate and // removed by extract-text-webpack-plugin. So it's doing its job fine, but still creating this unnecessary file. My webpack config is (showing the pertinent parts):

const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        app: './client/src/app.js',
        style: './client/src/app.scss'
    },
    output: {
        path: __dirname + '/server/assets/',
        publicPath: '/',
        filename: 'bundle.[chunkhash].js',
    },
    module: {
        loaders: [{
        test: /\.js/,
        exclude: /node_modules/,
        include: /src/,
        loader: 'babel-loader'
        },{
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style', 'css', 'sass'),
        },{
        test: /.*\.(woff|woff2|eot|ttf)$/i,
        loader: "url-loader?limit=10000&mimetype=application/font-woff"
        },{
        test: /.*\.(png|svg|jpg)$/i,
        loaders: [
        'file?hash=sha512&digest=hex&name=[hash].[ext]',
        'image-webpack?{progressive:true, optimizationLevel: 7, interlaced: false, pngquant:{quality: "65-90", speed: 4}}'
        ]
        }]
    },
    plugins: [
        new ExtractTextPlugin('bundle.[chunkhash].css', {
        allChunks: true
        })  
    ]
};

所以基本上输出是创建两个.js文件,每个条目一个,然后提取插件是创建实际所需的.css文件。如何防止输出创建不必要的文件?

So essentially the output is creating two .js files, one for each entry and then the extract plugin is creating the actual desired .css file. How can I prevent the output from creating that unnecessary file?

推荐答案

另一个选项是合并 app 样式组合成一个:

Another options is to merge app and style chunks into one:

entry: {
    app: [
        './client/src/app.js',
        './client/src/app.scss'
    ]
}

这样webpack只会产生一个块 - app 。同时 ExtractTextPlugin 将从中删除任何 .scss 模块。内容将被放入包中。[chunkhash] .css

That way webpack will produce only one chunk - app. At the same time ExtractTextPlugin will remove any .scss modules from it. Contents will be placed into bundle.[chunkhash].css.

这篇关于Webpack提取文本插件输出样式条目的.js和.css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆