WebPack如何将所有css文件放入一个css文件中

Fre*_*red 5 javascript css webpack html-webpack-plugin

我对WebPack还不熟悉,我希望能够获取CSS文件目录(.app/styles/[css files ...])并将它们输出到一个CSS文件(dist/styles.css)中.

目前,所有的JavaScript文件都被编译成一个单独的"index_bundle.js"文件,这是完美的,但我想为我的CSS文件实现相同的目标.

经过大量的"Google搜索"后,我发现WebPack的ExtractTextPlugin应该可以帮助解决这个问题,但这只适用于添加到"entry"属性的一个CSS文件(例如:entry:{style:". /app/styles/style.css"})然后将其作为链接标记添加到html的头部,这很好,但我希望我的所有css文件都进入一个styles.css文件,然后在html中使用它作为一个链接.

我当前的WebPack配置如下所示:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: __dirname + '/app/index.html',
    filename: 'index.html',
    inject: 'body'
});

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ExtractTextPluginConfig = new ExtractTextPlugin(
    "styles.css", 
    {
        allChunks: false
    }
);

module.exports = {
    entry: {
        index: "./app/index.js"//,
        //styles: "./app/styles/style1.css" // I don't want one file, I want to use a directory eg: "./app/styles/"
    },
    output: {
        path: __dirname + '/dist',
        filename: 'index_bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/, 
                exclude: /node_modules/, 
                loader: 'babel-loader'
            },
            { 
                test: /\.css$/, 
                loader: ExtractTextPlugin.extract("style-loader", "css-loader")
            }
        ]
    },
    plugins: [
        HtmlWebpackPluginConfig,
        ExtractTextPluginConfig
    ]
}
Run Code Online (Sandbox Code Playgroud)

有人可以指点我正确的方向吗?即使它是另一个插件或不同的方法.任何帮助将不胜感激!

编辑:我的新WebPack配置如下所示:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: __dirname + '/app/index.html',
    filename: 'index.html',
    inject: 'body'
});

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ExtractTextPluginConfig = new ExtractTextPlugin(
    "index_bundle.css"
);

module.exports = {
    entry: [
        './app/index.js',
        './app/index.css'
    ],
    output: {
        path: __dirname + '/dist',
        filename: 'index_bundle.js'
    },
    module: {
        preloaders: [
            {
                test: /\.css/,
                exclude: /styles/, 
                loader: 'import-glob-loader'
            }
        ],
        loaders: [
            {
                test: /\.js$/, 
                exclude: /node_modules/, 
                loader: 'babel-loader'
            },
            { 
                test: /styles\.css$/, 
                loader: ExtractTextPlugin.extract("style-loader", "css-loader")
            },
            { 
                test: /\.json$/, 
                loader: 'json' 
            }
        ]
    },
    devServer: {
        historyApiFallback: true
    },
    plugins: [
        HtmlWebpackPluginConfig,
        ExtractTextPluginConfig
    ]
}
Run Code Online (Sandbox Code Playgroud)

Fre*_*red 6

好的,所以它似乎是一个驼峰式问题.

Davin Tryon的帮助下,我能够解决我的问题 - 谢谢!

如果你看一下:https://www.npmjs.com/package/import-glob-loader 他们有以下内容:

preloaders: [{
    test: /\.scss/,
    loader: 'import-glob-loader'
}]
Run Code Online (Sandbox Code Playgroud)

它应该是:

preLoaders: [{
    test: /\.scss/,
    loader: 'import-glob-loader'
}]
Run Code Online (Sandbox Code Playgroud)

所以最后,我的整个webpack.config.json看起来像这样:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: __dirname + '/app/index.html',
    filename: 'index.html',
    inject: 'body'
});

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ExtractTextPluginConfig = new ExtractTextPlugin(
    "index_bundle.css"
);

module.exports = {
    entry: [
        './app/index.js',
        './app/index.css'
    ],
    output: {
        path: __dirname + '/dist',
        filename: 'index_bundle.js'
    },
    module: {
        preLoaders: [
            {
                test: /\.css$/,
                exclude: /styles/, 
                loader: 'import-glob-loader'
            }
        ],
        loaders: [
            {
                test: /\.js$/, 
                exclude: /node_modules/, 
                loader: 'babel-loader'
            },
            { 
                test: /\.css$/, 
                loader: ExtractTextPlugin.extract("style-loader", "css-loader")
            },
            { 
                test: /\.json$/, 
                loader: 'json' 
            }
        ]
    },
    devServer: {
        historyApiFallback: true
    },
    plugins: [
        HtmlWebpackPluginConfig,
        ExtractTextPluginConfig
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的index.css文件如下所示:

@import './styles/**/*';
Run Code Online (Sandbox Code Playgroud)

这对我有用,我得到一个单独的css输出文件"index_bundle.css".样式和脚本也会自动注入到html模板中.

index.html之前的index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Admin - Login</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css"/>
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

注入/ dist文件夹后的index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Admin - Login</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css"/>
    <link href="index_bundle.css" rel="stylesheet"></head>
    <body>
        <div id="app"></div>
    <script type="text/javascript" src="index_bundle.js"></script></body>
</html>
Run Code Online (Sandbox Code Playgroud)

  • 注意:`module.preLoaders` 已经[在 Webpack 2 中移除](https://webpack.js.org/guides/migrating/#module-preloaders-and-module-postloaders-were-removed-) (4认同)