技術めも

webpack v2 - JS

web+DB Vol.97

npm init -y
npm install --save react react-dom
npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react webpack
npm install --save-dev webpack-dev-server
npm install --save-dev css-loader postcss-loader postcss-easy-import extract-text-webpack-plugin
  • webpack.config.js
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: ['./src/main.js', './src/main.css'],
    output: { path: __dirname + '/public', filename: 'bundle.js' },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: 'babel-loader'
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    loader: 'css-loader!postcss-loader',
                }),
            }
        ],
    },
    devServer: {
        contentBase: 'public',
        port: 8080,
    },
    plugins: [
        new ExtractTextPlugin({
            filename: 'bundle.css'
        }),
    ],
    devtool: 'source-map',
};
  • postcss.config.js
module.exports = {
    plugins: [
        require('postcss-easy-import')({glob: true})
    ],
}
  • command
./node_modules/.bin/webpack
./node_modules/.bin/webpack-dev-server