Move from Webpack 1 to Webpack 2/3

2019-04-11 21:02发布

9 months ago I was struggling with Webpack One:

[ Switching from Gulp to Webpack ]

But with the recent arrival of Webpack 3, now we are willing to update our workflow.

UPDATE 24.07.17 || v1

We succeed finally, it was a simple syntaxis problem:

//-- webpack.config.js

'use strict';

const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractStyles = new ExtractTextPlugin('styles.bundle.css');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');


let start = {

    context: path.resolve(__dirname, 'src'),
    entry: {
        scripts: './_scripts.js',
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'postcss-loader']
                })
            },

            {
                test: /\.(woff|woff2)$/,
                use: ['url-loader'],
            },
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015']
                }
            }
        ],
    },
    output: {
        path: path.resolve(__dirname, 'dist/assets'),
        filename: '[name].bundle.js',
    },
    resolve: {
        modules: [path.resolve(__dirname, 'src'), 'node_modules'],
    },
    plugins: [
        extractStyles,
    ],

};

module.exports = start;

//-- postcss.config.js
//-- Correct Syntaxis

module.exports = {
    plugins: {
        'postcss-import': {},
        'precss': {},
        'postcss-url': {},
        'postcss-cssnext': {},
        'postcss-font-magician': {},
        'postcss-reporter': {},
        'postcss-browser-reporter': {},
        'postcss-inline-svg': {},
        'postcss-urlrev': {},
        'postcss-fontpath': {},
        'postcss-object-fit-images': {}
    }
};


//-- Wrong Syntaxis:

module.exports = {
    plugins: [
        ("postcss-import")(),
        ("precss")(),
        ("postcss-url")(),
        ("postcss-cssnext")(),
        ('postcss-font-magician')(),
        ("postcss-reporter")(),
        ("postcss-browser-reporter")(),
        ('postcss-inline-svg')(),
        ('postcss-urlrev')(),
        ('postcss-fontpath')(),
        ('postcss-object-fit-images')()
    ]
};

UPDATE 20.07.17 | v0.1a | This part contain our first tryouts without success

So far, I have this:

'use strict';

const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractStyles = new ExtractTextPlugin('styles.bundle.css');
const extractFonts = new ExtractTextPlugin('fonts.css');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');


let start = {

    context: path.resolve(__dirname, 'src'),
    entry: {
        scripts: './_scripts.js',
    },
    module: {
        rules: [
            {
                test: /style\.bundle\.css/,
                use: extractStyles.extract({
                    use: [
                        'style-loader',
                        'css-loader!',
                        'postcss-loader!'
                    ],
                }),
            },
            {
                test: /fonts\.css/,
                use: extractStyles.extract({
                    use: [
                        'style-loader',
                        'css-loader',
                        'postcss-loader'
                    ],
                }),
            },
            {
                test: /\.(woff|woff2)$/,
                use: ['url-loader'],
            },
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015']
                }
            }
        ],
    },
    output: {
        path: path.resolve(__dirname, 'dist/assets'),
        filename: '[name].bundle.js',
    },
    resolve: {
        modules: [path.resolve(__dirname, 'src'), 'node_modules'],
    },
    plugins: [
        extractStyles,
        extractFonts,
    ],

};

module.exports = start;

And We got this Warnings and Errors:

Warning 1 : (node:16532) DeprecationWarning: Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead.

ERROR in ./_cssnext/_styles.css Module parse failed: /Users/development/htdocs/src/_cssnext/_styles.css Unexpected charac ter '@' (2:0) You may need an appropriate loader to handle this file type. | /* Shared */ | @import "shared/colors.css"; | @import "shared/typography.css"; | @ ./_scripts.js 3:14-47

I thought something was wrong with cssnext so I decide to add a postcss.config.js

with:

module.exports = {
    plugins: [
        require("postcss-import")(),
        require("precss")(),
        require("postcss-url")(),
        require("postcss-cssnext")(),
        require('postcss-font-magician')(),
        require("postcss-reporter")(),
        require("postcss-browser-reporter")(),
        require('postcss-inline-svg')(),
        require('postcss-urlrev')(),
        require('postcss-fontpath')(),
        require('postcss-object-fit-images')()
    ]
};

But I keep getting this errors and warning.

What do you suggest?

----

Thank you

0条回答
登录 后发表回答