Added minimization, set up css extract plugin, added style chunking, fixed paths

This commit is contained in:
Paul Graffam 2019-01-29 16:56:13 -05:00
parent 11dd93aff6
commit e8177659ac
1 changed files with 47 additions and 13 deletions

View File

@ -2,12 +2,16 @@
const webpack = require('webpack'); const webpack = require('webpack');
const path = require('path'); const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
// Paths // Paths
const entry = './src/js/app.js'; const entry = './src/js/app.js';
const includePath = path.join(__dirname, 'src/js'); const includePath = path.join(__dirname, 'src/js');
const nodeModulesPath = path.join(__dirname, 'node_modules'); const nodeModulesPath = path.join(__dirname, 'node_modules');
let outputPath = path.join(__dirname, 'src/public/assets/js');
let outputPath = path.join(__dirname, 'src/public/js');
module.exports = env => { module.exports = env => {
// Dev environment // Dev environment
@ -25,7 +29,7 @@ module.exports = env => {
devtool = 'hidden-source-map'; devtool = 'hidden-source-map';
mode = 'production'; mode = 'production';
stats = 'none'; stats = 'none';
outputPath = `${__dirname}/build/public/assets/js`; outputPath = `${__dirname}/build/js`;
} }
console.log('Webpack build -'); console.log('Webpack build -');
@ -47,7 +51,7 @@ module.exports = env => {
// must be an absolute path (use the Node.js path module) // must be an absolute path (use the Node.js path module)
path: outputPath, path: outputPath,
// the url to the output directory resolved relative to the HTML page // the url to the output directory resolved relative to the HTML page
publicPath: 'assets/js', publicPath: 'js',
// the filename template for entry chunks // the filename template for entry chunks
filename: 'app.js' filename: 'app.js'
}, },
@ -78,12 +82,20 @@ module.exports = env => {
exclude: nodeModulesPath, exclude: nodeModulesPath,
}, },
{ {
test: /\.scss$/, test: /\.(s*)css$/,
use: [ use: [
'style-loader', {
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: 'css'
}
},
'css-loader', 'css-loader',
'sass-loader' 'postcss-loader',
] 'sass-loader',
],
} }
] ]
}, },
@ -94,11 +106,11 @@ module.exports = env => {
// directories where to look for modules, // directories where to look for modules,
modules: [ modules: [
'node_modules', 'node_modules',
path.resolve(__dirname, 'app') path.resolve(__dirname, 'src')
], ],
// extensions that are used // extensions that are used
extensions: ['.js', '.json', '.css'], extensions: ['.js', '.json'],
}, },
performance: { performance: {
@ -116,12 +128,28 @@ module.exports = env => {
contentBase: 'src/public' contentBase: 'src/public'
}, },
plugins: plugins.concat(new HtmlWebpackPlugin({ plugins: plugins.concat(
template: './src/html/index.html', new HtmlWebpackPlugin({
filename: '../../index.html', title: 'Three.js Webpack ES6 Boilerplate',
})), template: path.join(__dirname, 'src/html/index.html'),
filename: '../index.html',
env: env.NODE_ENV,
}),
new MiniCssExtractPlugin({
filename: '../css/[name].css',
chunkFilename: '../css/[id].css'
})
),
optimization: { optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
],
runtimeChunk: 'single', runtimeChunk: 'single',
splitChunks: { splitChunks: {
cacheGroups: { cacheGroups: {
@ -129,6 +157,12 @@ module.exports = env => {
test: /[\\\/]node_modules[\\\/]/, test: /[\\\/]node_modules[\\\/]/,
name: 'vendors', name: 'vendors',
chunks: 'all' chunks: 'all'
},
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true
} }
} }
} }