简体   繁体   English

Webpack构建CSS和JS文件

[英]Webpack building both css and js files

my problem is that webpack builds both js and css files when the input is sass file. 我的问题是,当输入为sass文件时,webpack会同时构建js和css文件。 Here is what webpack outputs . 这是webpack的输出 I just want css files to be outputed for scss, not both. 我只希望将CSS文件输出为CSS,而不是两者都输出。 I do not know how to do that. 我不知道该怎么做。 I hope someone can help. 我希望有人能帮帮忙。 Here is my code. 这是我的代码。

let path = require("path");
let webpack = require("webpack");
let ExtractTextPlugin = require("extract-text-webpack-plugin");

let SRC = path.join(__dirname, "client", "src");
let DIST = path.join(__dirname, "client", "dist", "bundles");

module.exports = [{
    entry: {
        admin: SRC + "/js/admin/index.js",
        main: SRC + "/js/main/index.js",
        adminstyles: SRC + "/scss/admin/index.scss",
        mainstyles: SRC + "/scss/main/index.scss"

    },
    output: {
        path: DIST,
        filename: "[name]-bundle.js"
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
                name: 'commons',
                filename: 'common.js',
                chunks: ['admin', 'main']
            }
        ),
        new ExtractTextPlugin("[name]-bundle.css")
    ],
    module: {
        loaders: [
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract("css!sass")
            },
            {test: /\.jsx?$/, loader: "babel-loader"}
        ]
    },
    resolve: {
        extensions: ["", ".js", ".jsx"],
        root: [path.join(__dirname, "client", "src", "js", "main")],
        modulesDirectories: ["node_modules"]
    }
}];

Every entry point gets a JavaScript bundle with the webpack bootstrap code, even if the content is extracted with the extract-text-webpack-plugin . 即使内容是使用extract-text-webpack-plugin提取的,每个入口点也会获得带有webpack引导程序代码的JavaScript包。 Instead of using a separate entry point for the CSS you should include them in the corresponding entry point, which also makes sense conceptually as the styles are going to be used in them. 不要在CSS上使用单独的入口点,而应将它们包括在相应的入口点中,这在概念上也很有意义,因为将在其中使用样式。 Each entry also accepts an array. 每个entry还接受一个数组。

Your entry should be: 您的输入应为:

entry: {
    admin: [SRC + "/js/admin/index.js", SRC + "/scss/admin/index.scss"],
    main: [SRC + "/js/main/index.js",  SRC + "/scss/main/index.scss"]
},

If you want to keep the names mainstyles and adminstyles you can also change the extract-text-webpack-plugin to: 如果要保留名称mainstylesadminstyles还可以将extract-text-webpack-plugin更改为:

new ExtractTextPlugin("[name]styles-bundle.css")

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

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