Switching from Gulp to Webpack

后端 未结 5 2041
轻奢々
轻奢々 2020-12-31 07:08

UPDATE 30.04.20

[ Moving to Webpack ]

My first question is about how recommended is this switch for simple projects, just to Pre-Process/Con

5条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 07:43

    Ok , I'm getting some great achieves thanks to this tutorial

    My first approach is this:

    webpack-config.js

    'use strict';
    
    const webpack    = require('webpack'),
        path         = require('path'),
        autoprefixer = require('autoprefixer'),
        glob         = require('glob');
    
    let script = {
        entry: {
             'scripts': glob.sync('./_javascript/*.js'),
        },
        module: {
            loaders: [
                // Javascript: js, jsx
                {
                    test: /\.jsx?$/,
                    loader: 'babel-loader'
                },
                // CSS: scss, css
                {
                    test: /\.s?css$/,
                    loaders: ['style', 'css', 'sass', 'postcss-loader']
                },
                // SVGs: svg, svg?something
                {
                    test: /\.svg(\?.*$|$)/,
                    loader: 'file-loader?name=/img/[name].[ext]'
                },
                // Images: png, gif, jpg, jpeg
                {
                    test: /\.(png|gif|jpe?g)$/,
                    loader: 'file?name=/img/[name].[ext]'
                },
                // HTML: htm, html
                {
                    test: /\.html?$/,
                    loader: "file?name=[name].[ext]"
                },
                // Font files: eot, ttf, woff, woff2
                {
                    test: /\.(eot|ttf|woff2?)(\?.*$|$)/,
                    loader: 'file?name=/fonts/[name].[ext]'
                }
            ]
        },
        output: {
            path: './resources/js',
            filename: 'bundle--[name].js'
        },
        plugins: [
            new webpack.optimize.CommonsChunkPlugin(['scripts'], 'bundle--[name].js'),
            new webpack.optimize.UglifyJsPlugin({
          compress: {
            warnings: false
        }
    })
    ]
    };
    
    let style = {
        entry: {
            'styles': glob.sync('./_nextcss/*.css'),
        },
        module: {
            loaders: [
                {
                    test: /\.s?css$/,
                    loaders: ['style', 'css', 'sass', 'postcss-loader']
                },
            ]
        },
        postcss: function (webpack) {
            return [
                require("postcss-import")({
                    addDependencyTo: webpack
                }),
                require("postcss-url")(),
                require("postcss-cssnext")(),
                require("postcss-browser-reporter")(),
                require("postcss-reporter")(),
            ]
        },
        output: {
            path: './resources/css',
            filename: 'bundle--[name].css'
        },
    };
    
    module.exports = [
        script,
        style,
    ];
    

    This generates a bundle--script.js successfully , but I'm having some issues with the css part.

    It won't pre-process anything :/

    I will be updating here if I get to work this in the meantime, I will really appreciate much, if someone can guide me.

提交回复
热议问题