From 243ca9a7f9149d9e1f403b869c23746bac00a855 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 18 Jul 2020 15:10:50 -0400 Subject: [PATCH] implements ignorePath for webpack server side assets --- src/webpack/getWebpackDevConfig.js | 40 +++++++++++++++++++------- src/webpack/getWebpackProdConfig.js | 44 +++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/webpack/getWebpackDevConfig.js b/src/webpack/getWebpackDevConfig.js index de86ed1f66..30a4798d10 100644 --- a/src/webpack/getWebpackDevConfig.js +++ b/src/webpack/getWebpackDevConfig.js @@ -1,4 +1,5 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); +const { IgnorePlugin } = require('webpack'); const path = require('path'); const webpack = require('webpack'); const Dotenv = require('dotenv-webpack'); @@ -116,16 +117,6 @@ module.exports = (config) => { }, ], }, - plugins: [ - new HtmlWebpackPlugin({ - template: path.resolve(__dirname, '../client/index.html'), - filename: './index.html', - }), - new webpack.HotModuleReplacementPlugin(), - new Dotenv({ - silent: true, - }), - ], resolve: { modules: ['node_modules', path.resolve(__dirname, '../../node_modules')], alias: { @@ -135,6 +126,35 @@ module.exports = (config) => { }, }; + const plugins = [ + new HtmlWebpackPlugin({ + template: path.resolve(__dirname, '../client/index.html'), + filename: './index.html', + }), + new webpack.HotModuleReplacementPlugin(), + new Dotenv({ + silent: true, + }), + ]; + + if (config.webpackIgnorePlugin instanceof RegExp) { + plugins.push(new IgnorePlugin(config.webpackIgnorePlugin)); + } else if (typeof config.webpackIgnorePlugin === 'string') { + plugins.push(new IgnorePlugin(new RegExp(`^${config.webpackIgnorePlugin}$`, 'is'))); + } + + if (Array.isArray(config.webpackIgnorePlugin)) { + config.webpackIgnorePlugin.forEach((ignorePath) => { + if (ignorePath instanceof RegExp) { + plugins.push(new IgnorePlugin(ignorePath)); + } else if (typeof ignorePath === 'string') { + plugins.push(new IgnorePlugin(new RegExp(`^${ignorePath}$`, 'is'))); + } + }); + } + + webpackConfig.plugins = plugins; + if (config.paths.scss) { webpackConfig.resolve.alias['payload-scss-overrides'] = config.paths.scss; } else { diff --git a/src/webpack/getWebpackProdConfig.js b/src/webpack/getWebpackProdConfig.js index 00ea803033..19bac79fbd 100644 --- a/src/webpack/getWebpackProdConfig.js +++ b/src/webpack/getWebpackProdConfig.js @@ -1,6 +1,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); const Dotenv = require('dotenv-webpack'); // const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); +const { IgnorePlugin } = require('webpack'); const path = require('path'); const getStyleLoaders = require('./getStyleLoaders'); @@ -104,17 +105,6 @@ module.exports = (config) => { }, ], }, - plugins: [ - // new BundleAnalyzerPlugin(), - new HtmlWebpackPlugin({ - template: path.resolve(__dirname, '../client/index.html'), - filename: './index.html', - minify: true, - }), - new Dotenv({ - silent: true, - }), - ], resolve: { modules: ['node_modules', path.resolve(__dirname, '../../node_modules')], alias: { @@ -124,6 +114,36 @@ module.exports = (config) => { }, }; + const plugins = [ + // new BundleAnalyzerPlugin(), + new HtmlWebpackPlugin({ + template: path.resolve(__dirname, '../client/index.html'), + filename: './index.html', + minify: true, + }), + new Dotenv({ + silent: true, + }), + ]; + + if (config.webpackIgnorePlugin instanceof RegExp) { + plugins.push(new IgnorePlugin(config.webpackIgnorePlugin)); + } else if (typeof config.webpackIgnorePlugin === 'string') { + plugins.push(new IgnorePlugin(new RegExp(`^${config.webpackIgnorePlugin}$`, 'is'))); + } + + if (Array.isArray(config.webpackIgnorePlugin)) { + config.webpackIgnorePlugin.forEach((ignorePath) => { + if (ignorePath instanceof RegExp) { + plugins.push(new IgnorePlugin(ignorePath)); + } else if (typeof ignorePath === 'string') { + plugins.push(new IgnorePlugin(new RegExp(`^${ignorePath}$`, 'is'))); + } + }); + } + + webpackConfig.plugins = plugins; + if (config.paths.scss) { webpackConfig.resolve.alias['payload-scss-overrides'] = config.paths.scss; } else { @@ -131,7 +151,7 @@ module.exports = (config) => { } if (config.webpack && typeof config.webpack === 'function') { - webpackConfig = config.webpack(webpackConfig); + webpackConfig = config.webpack(webpackConfig, 'production'); } return webpackConfig;