234 lines
5.8 KiB
JavaScript
234 lines
5.8 KiB
JavaScript
const fastGlob = require('fast-glob');
|
|
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
const TerserPlugin = require('terser-webpack-plugin');
|
|
const {statSync} = require('fs');
|
|
const {resolve, parse} = require('path');
|
|
const {SourceMapDevToolPlugin} = require('webpack');
|
|
const webpack = require('webpack');
|
|
|
|
const glob = (pattern) => fastGlob.sync(pattern, {cwd: __dirname, absolute: true});
|
|
|
|
const isProduction = process.env.NODE_ENV !== 'development';
|
|
|
|
const filterCssImport = (url, ...args) => {
|
|
const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import
|
|
const importedFile = url.replace(/[?#].+/, '').toLowerCase();
|
|
|
|
if (cssFile.includes('font-awesome') && /(eot|ttf|otf|woff|svg)$/.test(importedFile)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
module.exports = {
|
|
mode: isProduction ? 'production' : 'development',
|
|
entry: {
|
|
index: [
|
|
resolve(__dirname, 'wwwroot/js/jquery.js'),
|
|
resolve(__dirname, 'wwwroot/js/index.js'),
|
|
resolve(__dirname, 'wwwroot/css/index.css'),
|
|
resolve(__dirname, 'wwwroot/less/index.less'),
|
|
],
|
|
},
|
|
devtool: false,
|
|
output: {
|
|
path: resolve(__dirname, 'public'),
|
|
publicPath: resolve(__dirname, 'public/'),
|
|
filename: ({chunk}) => {
|
|
// serviceworker can only manage assets below it's script's directory so
|
|
// we have to put it in / instead of /js/
|
|
return chunk.name === 'serviceworker' ? '[name].js' : 'js/[name].js';
|
|
},
|
|
},
|
|
optimization: {
|
|
minimize: isProduction,
|
|
minimizer: [
|
|
new TerserPlugin({
|
|
extractComments: false,
|
|
parallel: true,
|
|
terserOptions: {
|
|
output: {
|
|
comments: false,
|
|
},
|
|
},
|
|
}),
|
|
new CssMinimizerPlugin({
|
|
sourceMap: true,
|
|
minimizerOptions: {
|
|
map: {
|
|
inline: false,
|
|
annotation: true,
|
|
},
|
|
preset: [
|
|
'default',
|
|
{
|
|
discardComments: {
|
|
removeAll: true,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
],
|
|
splitChunks: {
|
|
chunks: 'async',
|
|
name: (_, chunks) => chunks.map((item) => item.name).join('-'),
|
|
},
|
|
moduleIds: 'named',
|
|
chunkIds: 'named',
|
|
},
|
|
module: {
|
|
rules: [
|
|
{
|
|
test: /\.js$/,
|
|
exclude: /node_modules/,
|
|
use: [
|
|
{
|
|
loader: 'babel-loader',
|
|
options: {
|
|
sourceMaps: true,
|
|
cacheDirectory: true,
|
|
cacheCompression: false,
|
|
cacheIdentifier: [
|
|
resolve(__dirname, 'package.json'),
|
|
resolve(__dirname, 'package-lock.json'),
|
|
resolve(__dirname, 'webpack.config.js'),
|
|
].map((path) => statSync(path).mtime.getTime()).join(':'),
|
|
presets: [
|
|
[
|
|
'@babel/preset-env',
|
|
{
|
|
useBuiltIns: 'usage',
|
|
corejs: 3,
|
|
},
|
|
],
|
|
],
|
|
plugins: [
|
|
[
|
|
'@babel/plugin-transform-runtime',
|
|
{
|
|
regenerator: true,
|
|
}
|
|
],
|
|
],
|
|
generatorOpts: {
|
|
compact: false,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
test: /.css$/i,
|
|
use: [
|
|
{
|
|
loader: MiniCssExtractPlugin.loader,
|
|
},
|
|
{
|
|
loader: 'style-loader',
|
|
loader: 'css-loader',
|
|
options: {
|
|
sourceMap: true,
|
|
url: filterCssImport,
|
|
import: filterCssImport,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
test: /.less$/i,
|
|
use: [
|
|
{
|
|
loader: MiniCssExtractPlugin.loader,
|
|
},
|
|
{
|
|
loader: 'css-loader',
|
|
options: {
|
|
sourceMap: true,
|
|
importLoaders: 1,
|
|
url: filterCssImport,
|
|
import: filterCssImport,
|
|
},
|
|
},
|
|
{
|
|
loader: 'less-loader',
|
|
options: {
|
|
sourceMap: true,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
test: /\.svg$/,
|
|
use: ["svg-loader"]
|
|
},
|
|
{
|
|
test: /\.(ttf|woff|woff2|eot|svg?)$/i,
|
|
use: [
|
|
{
|
|
loader: 'file-loader',
|
|
options: {
|
|
name: '[name].[ext]',
|
|
outputPath: 'fonts/',
|
|
publicPath: (url) => `../fonts/${url}`, // required to remove css/ path segment
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
test: /\.(png|woff|woff2|eot|ttf|svg|ico)$/,
|
|
use: ["url-loader"]
|
|
},
|
|
],
|
|
},
|
|
plugins: [
|
|
new webpack.ProvidePlugin({
|
|
$: 'jquery',
|
|
jQuery: 'jquery'
|
|
}),
|
|
new MiniCssExtractPlugin({
|
|
filename: 'css/[name].css',
|
|
chunkFilename: 'css/[name].css',
|
|
}),
|
|
new SourceMapDevToolPlugin({
|
|
filename: '[file].map',
|
|
include: [
|
|
'js/index.js',
|
|
'css/index.css',
|
|
],
|
|
}),
|
|
],
|
|
performance: {
|
|
hints: false,
|
|
maxEntrypointSize: Infinity,
|
|
maxAssetSize: Infinity,
|
|
},
|
|
watchOptions: {
|
|
ignored: [
|
|
'node_modules/**',
|
|
],
|
|
},
|
|
stats: {
|
|
assetsSort: 'name',
|
|
assetsSpace: Infinity,
|
|
cached: false,
|
|
cachedModules: false,
|
|
children: false,
|
|
chunkModules: false,
|
|
chunkOrigins: false,
|
|
chunksSort: 'name',
|
|
colors: true,
|
|
entrypoints: false,
|
|
excludeAssets: [],
|
|
groupAssetsByChunk: false,
|
|
groupAssetsByEmitStatus: false,
|
|
groupAssetsByInfo: false,
|
|
groupModulesByAttributes: false,
|
|
modules: false,
|
|
reasons: false,
|
|
runtimeModules: false,
|
|
},
|
|
};
|