wp-73k/webpack.config.js

205 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-05-23 12:30:31 -04:00
const path = require('path');
2021-07-04 08:27:31 -04:00
2019-05-23 12:30:31 -04:00
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2021-07-04 08:27:31 -04:00
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
2019-05-23 12:30:31 -04:00
const CopyWebpackPlugin = require('copy-webpack-plugin');
2021-07-04 09:26:44 -04:00
const SpriteLoaderPlugin = require("svg-sprite-loader/plugin");
2021-07-04 08:27:31 -04:00
2019-05-23 12:30:31 -04:00
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const PurgeCSS = require('@fullhuman/postcss-purgecss');
2021-07-04 08:27:31 -04:00
2019-05-23 12:30:31 -04:00
const isProduction = 'production' === process.env.NODE_ENV;
// Set the build prefix.
let prefix = isProduction ? '.min' : '';
// Set the PostCSS Plugins.
const post_css_plugins = [
2021-07-04 18:16:33 -04:00
require('postcss-import'),
require('postcss-nested'),
require('postcss-custom-properties'),
require('autoprefixer')
2019-05-23 12:30:31 -04:00
]
// Add PurgeCSS for production builds.
if ( isProduction ) {
2021-07-04 18:16:33 -04:00
post_css_plugins.push(
PurgeCSS({
content: [
'./*.php',
'./src/**/*.php',
'./page-templates/*.php',
'./assets/images/**/*.svg',
'./../../mu-plugins/app/src/components/**/*.php',
],
// Use Extractor configuration from Tailwind Docs
// https://tailwindcss.com/docs/controlling-file-size#setting-up-purge-css-manually
defaultExtractor: content => {
// Capture as liberally as possible, including things like `h-(screen-1.5)`
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || []
2021-07-04 18:16:33 -04:00
// Capture classes within other delimiters like .block(class="w-1/2") in Pug
const innerMatches = content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || []
2021-07-04 18:16:33 -04:00
return broadMatches.concat(innerMatches)
},
whitelistPatterns: getCSSWhitelistPatterns()
})
)
2019-05-23 12:30:31 -04:00
}
const config = {
2021-07-04 18:16:33 -04:00
entry: './assets/js/main.js',
output: {
filename: `[name]${prefix}.js`,
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
2021-07-04 18:16:33 -04:00
loader: 'babel-loader',
options: {
presets: [
[
"@babel/preset-env"
]
]
}
},
{
test: /\.[s]?css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader",
"postcss-loader",
],
2021-07-04 18:16:33 -04:00
},
2021-07-04 08:27:31 -04:00
{
test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: "file-loader",
options: {
esModule: false,
name: "[name].[ext]",
outputPath: "./fonts",
},
},
],
},
2021-07-04 09:26:44 -04:00
{
test: /\.svg$/,
loader: "svg-sprite-loader",
options: {
extract: true,
spriteFilename: "icons.svg",
publicPath: "./images/",
symbolId: (filePath) => {
if (filePath.includes("bootstrap-icons")) {
return `bi-${path.basename(filePath).slice(0, -4)}`;
} else if (filePath.includes("@mdi")) {
return `mdi-${path.basename(filePath).slice(0, -4)}`;
} else if (filePath.includes("heroicons")) {
if (filePath.includes("outline")) {
return `hio-${path.basename(filePath).slice(0, -4)}`;
} else {
return `his-${path.basename(filePath).slice(0, -4)}`;
}
} else {
return `${path.basename(filePath).slice(0, -4)}`;
}
},
},
},
2021-07-04 08:27:31 -04:00
]
2021-07-04 18:16:33 -04:00
},
optimization: {
minimizer: ["...", new CssMinimizerPlugin()],
},
mode: process.env.NODE_ENV,
resolve: {
alias: {
'@' : path.resolve('assets'),
'@images': path.resolve('../images')
}
},
plugins: [
new MiniCssExtractPlugin({ filename: `[name]${prefix}.css` }),
2021-07-04 09:26:44 -04:00
new SpriteLoaderPlugin({ plainSprite: true }),
2021-07-04 18:16:33 -04:00
new CopyWebpackPlugin({
patterns: [{
from: './assets/images/',
to: 'images',
globOptions: {
ignore: [
'**/.DS_Store'
]
}
}]
}),
new ImageminPlugin({ test: /\.(jpe?g|png|gif)$/i })
]
2019-05-23 12:30:31 -04:00
}
// Fire up a local server if requested
if (process.env.SERVER) {
2021-07-04 18:16:33 -04:00
config.plugins.push(
new BrowserSyncPlugin(
{
proxy: 'http://127.0.0.1:9764',
files: [
'**/*.php',
'**/*.scss'
],
port: 9765,
notify: false,
}
)
)
2019-05-23 12:30:31 -04:00
}
/**
* List of RegExp patterns for PurgeCSS
* @returns {RegExp[]}
*/
function getCSSWhitelistPatterns() {
2021-07-04 18:16:33 -04:00
return [
/^home(-.*)?$/,
/^blog(-.*)?$/,
/^archive(-.*)?$/,
/^date(-.*)?$/,
/^error404(-.*)?$/,
/^admin-bar(-.*)?$/,
/^search(-.*)?$/,
/^nav(-.*)?$/,
/^wp(-.*)?$/,
/^screen(-.*)?$/,
/^navigation(-.*)?$/,
/^(.*)-template(-.*)?$/,
/^(.*)?-?single(-.*)?$/,
/^postid-(.*)?$/,
/^post-(.*)?$/,
/^attachmentid-(.*)?$/,
/^attachment(-.*)?$/,
/^page(-.*)?$/,
/^(post-type-)?archive(-.*)?$/,
/^author(-.*)?$/,
/^category(-.*)?$/,
/^tag(-.*)?$/,
/^menu(-.*)?$/,
/^tags(-.*)?$/,
/^tax-(.*)?$/,
/^term-(.*)?$/,
/^date-(.*)?$/,
/^(.*)?-?paged(-.*)?$/,
/^depth(-.*)?$/,
/^children(-.*)?$/,
];
2019-05-23 12:30:31 -04:00
}
module.exports = config