silverstripe-webpack/webpack.config.prod.js

293 lines
7.6 KiB
JavaScript
Raw Normal View History

2018-02-22 14:39:58 +01:00
/*
* Production assets generation
*/
2018-02-05 12:11:01 +01:00
const webpack = require('webpack');
2019-05-23 13:02:32 +02:00
const commonVariables = require('./webpack.configuration');
const conf = commonVariables.configuration;
2018-02-05 12:11:01 +01:00
const merge = require('webpack-merge');
const common = require('./webpack.config.common.js');
2018-02-22 14:39:58 +01:00
2019-05-23 13:02:32 +02:00
const filesystem = require('fs');
2018-02-22 14:39:58 +01:00
const path = require('path');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('mini-css-extract-plugin');
2018-04-24 07:44:56 +02:00
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
2019-09-07 05:06:25 +02:00
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ImageminPlugin = require('imagemin-webpack');
2020-01-23 03:06:34 +01:00
const ImageSpritePlugin = require('@a2nt/image-sprite-webpack-plugin');
2018-02-05 12:11:01 +01:00
const COMPRESS = true;
2019-05-23 13:02:32 +02:00
let plugins = [
2019-06-08 17:54:43 +02:00
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
2019-06-08 17:54:43 +02:00
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
2019-06-08 17:54:43 +02:00
}),
new ExtractTextPlugin({
filename: 'css/[name].css',
allChunks: true,
2019-06-08 17:54:43 +02:00
}),
2019-09-07 05:06:25 +02:00
new OptimizeCssAssetsPlugin({
//assetNameRegExp: /\.optimize\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
2019-12-17 21:43:28 +01:00
preset: ['default'],
},
cssProcessorOptions: {
zindex: true,
cssDeclarationSorter: true,
reduceIdents: false,
mergeIdents: true,
mergeRules: true,
mergeLonghand: true,
discardUnused: true,
discardOverridden: true,
discardDuplicates: true,
discardComments: {
removeAll: true,
2019-12-17 21:43:28 +01:00
},
2019-09-07 05:06:25 +02:00
},
canPrint: true,
}),
2020-01-23 03:06:34 +01:00
new FaviconsWebpackPlugin({
title: 'Webpack App',
logo: path.join(__dirname, conf.APPDIR, conf.SRC, 'favicon.png'),
prefix: '/icons/',
emitStats: false,
persistentCache: true,
inject: false,
statsFilename: path.join(conf.APPDIR, conf.DIST, 'icons', 'iconstats.json'),
icons: {
android: true,
appleIcon: true,
appleStartup: true,
coast: true,
favicons: true,
firefox: true,
opengraph: true,
twitter: true,
yandex: true,
windows: true,
},
}),
new ImageminPlugin({
bail: false, // Ignore errors on corrupted images
cache: true,
2020-01-23 03:06:34 +01:00
maxConcurrency: 3,
exclude: /original/,
filter: (source, sourcePath) => {
2020-01-23 03:06:34 +01:00
return source.byteLength < 512000;
},
imageminOptions: {
plugins: [
['gifsicle', { interlaced: true }],
['jpegtran', { progressive: true }],
['optipng', { optimizationLevel: 5 }],
[
'svgo',
{
plugins: [
{
removeViewBox: false,
},
],
},
],
],
},
2019-09-07 05:06:25 +02:00
}),
2020-01-23 03:06:34 +01:00
new ImageSpritePlugin({
exclude: /exclude|original|default-|icons|sprite/,
commentOrigin: false,
compress: true,
extensions: ['png'],
indent: '',
log: true,
//outputPath: path.join(__dirname, conf.APPDIR, conf.DIST),
outputFilename: 'img/sprite-[hash].png',
padding: 0,
}),
2019-05-23 13:02:32 +02:00
];
// add themes favicons
commonVariables.themes.forEach(theme => {
2019-06-08 17:54:43 +02:00
const faviconPath = path.join(__dirname, theme, conf.SRC, 'favicon.png');
if (filesystem.existsSync(faviconPath)) {
plugins.push(
new FaviconsWebpackPlugin({
title: 'Webpack App',
logo: faviconPath,
prefix: '/' + theme + '-icons/',
emitStats: false,
persistentCache: true,
inject: false,
statsFilename: path.join(
conf.APPDIR,
conf.DIST,
theme + '-icons',
'iconstats.json',
),
icons: {
android: true,
appleIcon: true,
appleStartup: true,
coast: true,
favicons: true,
firefox: true,
opengraph: true,
twitter: true,
yandex: true,
windows: true,
},
}),
);
2019-06-08 17:54:43 +02:00
}
2019-05-23 13:02:32 +02:00
});
2018-02-05 12:11:01 +01:00
module.exports = merge(common, {
2019-06-08 17:54:43 +02:00
mode: 'production',
optimization: {
2020-02-12 21:31:59 +01:00
removeAvailableModules: false,
2019-06-08 17:54:43 +02:00
namedModules: true, // NamedModulesPlugin()
splitChunks: {
// CommonsChunkPlugin()
2019-06-08 17:54:43 +02:00
name: 'vendor',
minChunks: 2,
2019-05-23 14:44:10 +02:00
},
2019-06-08 17:54:43 +02:00
noEmitOnErrors: true, // NoEmitOnErrorsPlugin
concatenateModules: true, //ModuleConcatenationPlugin
minimizer: [
new TerserPlugin({
terserOptions: {
parse: {
2020-02-12 21:31:59 +01:00
// we want terser to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
2020-02-12 21:31:59 +01:00
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
2020-02-12 21:31:59 +01:00
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
2020-02-12 21:31:59 +01:00
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
parallel: true,
// Enable file caching
cache: true,
}),
],
2019-06-08 17:54:43 +02:00
},
2019-05-23 14:44:10 +02:00
2019-06-08 17:54:43 +02:00
devtool: '',
2018-02-05 12:11:01 +01:00
2019-06-08 17:54:43 +02:00
output: {
path: path.join(__dirname, conf.APPDIR, conf.DIST),
filename: path.join('js', '[name].js'),
publicPath: path.join(conf.APPDIR, conf.DIST),
},
2018-02-05 12:11:01 +01:00
2019-06-08 17:54:43 +02:00
module: {
rules: [
{
test: /\.s?css$/,
use: [
{
loader: ExtractTextPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: !COMPRESS,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: !COMPRESS,
plugins: [autoprefixer()],
},
},
{
loader: 'resolve-url-loader',
},
{
loader: 'sass-loader',
options: {
sourceMap: !COMPRESS,
},
},
],
},
{
test: /fontawesome([^.]+).(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
publicPath: '../fonts/',
},
},
],
},
{
test: /\.(ttf|otf|eot|svg|woff(2)?)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
publicPath: '../fonts/',
},
},
],
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
2019-06-08 17:54:43 +02:00
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'img/',
publicPath: '../img/',
2019-06-08 17:54:43 +02:00
/*,
name(file) {
//return 'public/[path][name].[ext]';
return '[hash].[ext]';
},*/
},
2019-06-08 17:54:43 +02:00
},
],
2019-06-08 17:54:43 +02:00
},
2018-02-05 12:11:01 +01:00
2019-06-08 17:54:43 +02:00
plugins: plugins,
});