如何获取一堆CSS文件并为所有应用程序制作一个CSS文件

时间:2018-12-20 08:37:11

标签: css webpack sass gulp

我有webpack.config,不知道我需要在其中放置什么模块,因为我找到了模块,因此只能最小化css或scss文件,而不能收集它们。

所以我有8-9个scss文件,并且需要一个css文件,该文件从其中收集所有代码

var path = require('path');
module.exports = {
    entry: {
        home: './src/main/js/home/home.js',
        products: './src/main/js/products/products.js',
        product: './src/main/js/product/product.js',
        profile: './src/main/js/profile/profile.js',
        topics: './src/main/js/topics/topics.js',
        topic: './src/main/js/topic/topic.js',

    },
    cache: true,
    mode: 'development',
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/[name].bundle.js'

    },
    module: {
        rules: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }]
            }
        ]
    }
};

我应该安装什么模块,以及需要在webpack.config中放置代码的位置。请帮助,我从未使用过webpack!

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

module.exports = {
    entry: {
        home: './src/main/js/home/home.js',
        products: './src/main/js/products/products.js',
        product: './src/main/js/product/product.js',
        profile: './src/main/js/profile/profile.js',
        topics: './src/main/js/topics/topics.js',
        topic: './src/main/js/topic/topic.js',
        _article: './src/main/resources/static/sass/_article.scss',
        _catalog: './src/main/resources/static/sass/_catalog.scss',
        _home: './src/main/resources/static/sass/_home.scss',
        _header: './src/main/resources/static/sass/_header.scss',
        _footer: './src/main/resources/static/sass/_footer.scss',

    },
    cache: true,
    mode: 'development',
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/[name].bundle.js'

    },
    module: {
        rules: [
            {
                test: [ /\.scss$/, path.join(__dirname, '.')],
                exclude: /(node_modules)/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'sass-loader']
                })[{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }]
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin("styles.css"),
    ]
};

那是我的webpack.config

1 个答案:

答案 0 :(得分:1)

webpack的工作原理与gulp不同。 Gulp是一个任务运行程序,可以使用globs和文件模式。 Webpack是一个捆绑程序,它根据给定的入口点来分析依赖关系树,以便能够创建捆绑程序。

为了使用webpack并捆绑所有scss文件,需要从入口点将其导入。这是webpack知道这些文件存在的唯一方法。

编辑:正确的配置。

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    entry: {
        home: './src/main/js/home/home.js',
        products: './src/main/js/products/products.js',
        product: './src/main/js/product/product.js',
        profile: './src/main/js/profile/profile.js',
        topics: './src/main/js/topics/topics.js',
        topic: './src/main/js/topic/topic.js',
        _article: './src/main/resources/static/sass/_article.scss',
        _catalog: './src/main/resources/static/sass/_catalog.scss',
        _home: './src/main/resources/static/sass/_home.scss',
        _header: './src/main/resources/static/sass/_header.scss',
        _footer: './src/main/resources/static/sass/_footer.scss',

    },
    cache: true,
    mode: 'development',
    output: {
        path: __dirname,
        filename: './src/main/resources/static/built/[name].bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin()
    ]
};