webpack-bootstrap-ui-kit/webpack.config.js

381 lines
13 KiB
JavaScript
Raw Normal View History

2021-01-20 16:31:34 +01:00
/*
* Production assets generation
*/
2021-05-03 19:00:50 +02:00
const common = require('./webpack.config.common.js');
const conf = common.configuration;
2019-06-08 17:20:51 +02:00
const webpack = require('webpack');
2021-06-29 13:44:03 +02:00
const {
2021-09-12 22:49:47 +02:00
merge,
2021-06-29 13:44:03 +02:00
} = require('webpack-merge');
2021-01-20 16:31:34 +01:00
2021-05-03 19:00:50 +02:00
const fs = require('fs');
2021-01-20 16:31:34 +01:00
const path = require('path');
2021-01-20 16:31:34 +01:00
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
2019-10-20 03:19:20 +02:00
const TerserPlugin = require('terser-webpack-plugin');
2021-05-03 02:55:31 +02:00
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
2022-02-17 05:54:57 +01:00
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2021-01-20 16:31:34 +01:00
2021-05-03 19:00:50 +02:00
//const ImageSpritePlugin = require('@a2nt/image-sprite-webpack-plugin');
2021-01-20 16:31:34 +01:00
const HtmlWebpackPlugin = require('html-webpack-plugin');
2022-05-27 16:30:25 +02:00
const WebpackBuildNotifications = require('webpack-build-notifications');
2021-10-28 03:12:01 +02:00
const UIInfo = require('./package.json');
2022-05-27 16:30:25 +02:00
const UINAME = JSON.stringify(UIInfo.name);
2021-10-28 03:12:01 +02:00
const UIVERSION = JSON.stringify(UIInfo.version);
const UIMetaInfo = require('./node_modules/@a2nt/meta-lightbox-js/package.json');
const NODE_ENV = conf.NODE_ENV || process.env.NODE_ENV;
const COMPRESS = NODE_ENV === 'production' ? true : false;
2021-02-21 08:05:58 +01:00
const IP = process.env.IP || conf.HOSTNAME;
const PORT = process.env.PORT || conf.PORT;
2021-10-28 03:12:01 +02:00
console.log('NODE_ENV: ' + NODE_ENV);
console.log('COMPRESS: ' + COMPRESS);
console.log('WebP images: ' + conf['webp']);
console.log('GRAPHQL_API_KEY: ' + conf['GRAPHQL_API_KEY']);
let plugins = [
new webpack.ProvidePlugin({
react: 'React',
'react-dom': 'ReactDOM',
/*$: 'jquery',
jQuery: 'jquery',*/
}),
new webpack.DefinePlugin({
NODE_ENV: JSON.stringify(NODE_ENV),
2022-05-27 16:30:25 +02:00
UINAME: UINAME,
2022-01-15 14:23:36 +01:00
UIVERSION: UIVERSION,
UIAUTHOR: JSON.stringify(UIInfo.author),
UIMetaNAME: JSON.stringify(UIMetaInfo.name),
UIMetaVersion: JSON.stringify(UIMetaInfo.version),
GRAPHQL_API_KEY: JSON.stringify(conf['GRAPHQL_API_KEY']),
SWVERSION: JSON.stringify(`sw-${new Date().getTime()}`),
BASE_HREF: JSON.stringify(''),
}),
2021-10-28 03:12:01 +02:00
new webpack.LoaderOptionsPlugin({
minimize: COMPRESS,
debug: !COMPRESS,
}),
new MiniCssExtractPlugin({
experimentalUseImportModule: false,
filename: 'css/[name].css',
//allChunks: true,
}),
2022-05-27 16:30:25 +02:00
new WebpackBuildNotifications({
title: UINAME + ' ' + UIVERSION,
logo: path.join(__dirname, conf.APPDIR, conf.SRC, 'favicon.png'),
suppressWarning: true,
}),
2021-10-28 03:12:01 +02:00
];
if (COMPRESS) {
2021-09-12 22:49:47 +02:00
plugins.push(require('autoprefixer'));
2021-09-12 22:49:47 +02:00
/*plugins.push(
new ImageSpritePlugin({
exclude: /exclude|original|default-|icons|sprite|svg|logo|favicon/,
commentOrigin: false,
compress: COMPRESS,
extensions: ['png'],
indent: '',
log: true,
//outputPath: path.join(__dirname, conf.APPDIR, conf.DIST),
outputFilename: 'img/sprite-[hash].png',
padding: 0,
}),
);*/
}
2019-10-20 04:40:30 +02:00
2021-01-20 16:31:34 +01:00
const indexPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'index.html');
2021-05-03 19:00:50 +02:00
if (fs.existsSync(indexPath)) {
2021-09-12 22:49:47 +02:00
plugins.push(
new HtmlWebpackPlugin({
publicPath: '',
template: path.join(conf.APPDIR, conf.SRC, 'index.html'),
templateParameters: {
NODE_ENV: NODE_ENV,
GRAPHQL_URL: conf['GRAPHQL_URL'],
STATIC_URL: conf['STATIC_URL'],
REACT_SCRIPTS: NODE_ENV === 'production' ?
'<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>' : '<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>',
2021-03-21 20:57:49 +01:00
},
2021-09-12 22:49:47 +02:00
xhtml: true,
2021-03-21 20:57:49 +01:00
}),
2021-09-12 22:49:47 +02:00
);
2021-01-20 16:31:34 +01:00
}
2021-02-21 19:01:25 +01:00
const faviconPath = path.join(__dirname, conf.APPDIR, conf.SRC, 'favicon.png');
2021-05-03 19:00:50 +02:00
if (fs.existsSync(faviconPath)) {
2021-09-12 22:49:47 +02:00
plugins.push(
new FaviconsWebpackPlugin({
title: 'Webpack App',
logo: faviconPath,
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,
2021-03-21 20:57:49 +01:00
},
}),
2021-09-12 22:49:47 +02:00
);
2019-10-20 04:40:30 +02:00
}
2021-01-20 16:31:34 +01:00
// add themes favicons
2021-05-03 19:00:50 +02:00
common.themes.forEach((theme) => {
2021-03-21 20:57:49 +01:00
const faviconPath = path.join(__dirname, theme, conf.SRC, 'favicon.png');
2021-05-03 19:00:50 +02:00
if (fs.existsSync(faviconPath)) {
2021-09-12 22:49:47 +02:00
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,
2021-03-21 20:57:49 +01:00
},
}),
2021-09-12 22:49:47 +02:00
);
2021-03-21 20:57:49 +01:00
}
2021-09-12 22:49:47 +02:00
});
2021-01-20 16:31:34 +01:00
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
2021-03-21 20:57:49 +01:00
.BundleAnalyzerPlugin;
2021-01-20 16:31:34 +01:00
plugins.push(
2021-03-21 20:57:49 +01:00
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
2021-09-12 22:49:47 +02:00
}),
2021-01-20 16:31:34 +01:00
);
2021-05-03 19:00:50 +02:00
const cfg = merge(common.webpack, {
2021-03-21 20:57:49 +01:00
mode: NODE_ENV,
cache: {
type: 'filesystem',
2021-09-12 22:49:47 +02:00
},
2021-03-21 20:57:49 +01:00
recordsPath: path.join(__dirname, conf.APPDIR, conf.DIST, 'records.json'),
optimization: {
//removeAvailableModules: false,
//realContentHash: false,
splitChunks: {
name: 'vendor',
minChunks: 2,
2021-09-12 22:49:47 +02:00
},
2021-03-21 20:57:49 +01:00
concatenateModules: true, //ModuleConcatenationPlugin
minimizer: [
new TerserPlugin({
terserOptions: {
module: false,
parse: {
// 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,
2021-09-12 22:49:47 +02:00
},
2021-03-21 20:57:49 +01:00
compress: {
2021-10-28 03:12:01 +02:00
ecma: 6,
2021-03-21 20:57:49 +01:00
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,
2021-09-12 22:49:47 +02:00
},
2021-03-21 20:57:49 +01:00
keep_fnames: true,
keep_classnames: true,
2021-01-20 16:31:34 +01:00
2021-03-21 20:57:49 +01:00
mangle: {
safari10: true,
keep_fnames: true,
keep_classnames: true,
reserved: ['$', 'jQuery', 'jquery'],
2021-09-12 22:49:47 +02:00
},
2021-03-21 20:57:49 +01:00
output: {
ecma: 5,
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,
2021-09-12 22:49:47 +02:00
},
},
2021-03-21 20:57:49 +01:00
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
parallel: true,
2021-09-12 22:49:47 +02:00
}),
2021-05-03 02:55:31 +02:00
new CssMinimizerPlugin({
parallel: true,
minimizerOptions: [{
preset: [
'default',
{
2021-06-29 13:44:03 +02:00
discardComments: {
2021-09-12 22:49:47 +02:00
removeAll: true,
},
2021-05-03 02:55:31 +02:00
zindex: true,
cssDeclarationSorter: true,
reduceIdents: false,
mergeIdents: true,
mergeRules: true,
mergeLonghand: true,
discardUnused: true,
discardOverridden: true,
discardDuplicates: true,
2021-09-12 22:49:47 +02:00
},
2021-05-03 02:55:31 +02:00
],
2022-02-17 05:54:57 +01:00
}, ],
2021-05-03 02:55:31 +02:00
minify: [
CssMinimizerPlugin.cssnanoMinify,
//CssMinimizerPlugin.cleanCssMinify,
2021-09-12 22:49:47 +02:00
],
}),
2021-03-21 20:57:49 +01:00
],
2021-09-12 22:49:47 +02:00
},
2021-01-20 16:31:34 +01:00
2021-03-21 20:57:49 +01:00
output: {
2021-10-28 03:12:01 +02:00
publicPath: path.join(conf.APPDIR, conf.DIST) + '/',
path: path.join(__dirname, conf.APPDIR, conf.DIST) + '/',
2021-03-21 20:57:49 +01:00
filename: path.join('js', '[name].js'),
2021-09-12 22:49:47 +02:00
},
2021-01-20 16:31:34 +01:00
2021-03-21 20:57:49 +01:00
module: {
2021-09-12 23:19:05 +02:00
rules: [{
test: /\.(js|ts)x?$/,
2021-06-29 13:44:03 +02:00
//exclude: /node_modules/,
use: {
loader: 'babel-loader', //'@sucrase/webpack-loader',
options: {
//transforms: ['jsx']
presets: [
'@babel/preset-env',
'@babel/react',
{
plugins: [
'@babel/plugin-proposal-class-properties',
],
2021-09-12 22:49:47 +02:00
},
2021-10-28 03:12:01 +02:00
], //Preset used for env setup
2021-06-29 13:44:03 +02:00
plugins: [
2021-09-12 23:19:05 +02:00
'@babel/plugin-transform-typescript',
'@babel/transform-react-jsx',
2021-06-29 13:44:03 +02:00
],
cacheDirectory: true,
cacheCompression: true,
2021-09-12 22:49:47 +02:00
},
},
},
2021-06-29 13:44:03 +02:00
{
test: /\.s?css$/,
use: [{
2021-10-28 03:12:01 +02:00
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
},
2021-09-12 22:49:47 +02:00
},
2021-10-28 03:12:01 +02:00
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
2021-09-12 22:49:47 +02:00
},
2021-10-28 03:12:01 +02:00
],
2021-09-12 22:49:47 +02:00
},
2021-06-29 13:44:03 +02:00
{
2021-10-28 03:12:01 +02:00
test: /fontawesome([^.]+).(ttf|otf|eot|woff(2)?)(\?[a-z0-9]+)?$/,
2021-09-12 22:49:47 +02:00
type: 'asset/resource',
},
2021-06-29 13:44:03 +02:00
{
test: /\.(ttf|otf|eot|woff(2)?)$/,
2021-09-12 22:49:47 +02:00
type: 'asset/resource',
2021-10-28 03:12:01 +02:00
}, {
2021-06-29 13:44:03 +02:00
test: /\.(png|webp|jpg|jpeg|gif|svg)$/,
2021-09-12 22:49:47 +02:00
type: 'javascript/auto',
2021-10-28 03:12:01 +02:00
use: [
{
2021-06-29 13:44:03 +02:00
loader: 'img-optimize-loader',
options: {
name: '[name].[ext]',
outputPath: 'img/',
publicPath: '../img/',
compress: {
// This will take more time and get smaller images.
mode: 'low', // 'lossless', 'high', 'low'
disableOnDevelopment: true,
webp: conf['webp'],
// loseless compression for png
optipng: {
optimizationLevel: 4,
2021-09-12 22:49:47 +02:00
},
2021-06-29 13:44:03 +02:00
// lossy compression for png. This will generate smaller file than optipng.
pngquant: {
quality: [0.2, 0.8],
2021-09-12 22:49:47 +02:00
},
2021-06-29 13:44:03 +02:00
// Compression for svg.
svgo: true,
// Compression for gif.
gifsicle: {
optimizationLevel: 3,
2021-09-12 22:49:47 +02:00
},
2021-06-29 13:44:03 +02:00
// Compression for jpg.
mozjpeg: {
progressive: true,
quality: 60,
2021-09-12 22:49:47 +02:00
},
},
2021-06-29 13:44:03 +02:00
inline: {
limit: 1,
2021-09-12 22:49:47 +02:00
},
},
2022-02-17 05:54:57 +01:00
}, ],
}, ],
2021-09-12 22:49:47 +02:00
},
2021-01-20 16:31:34 +01:00
2021-03-21 20:57:49 +01:00
plugins: plugins,
2021-09-12 22:49:47 +02:00
});
2019-06-08 17:20:51 +02:00
2021-01-20 16:31:34 +01:00
console.log(cfg);
module.exports = cfg;