diff --git a/README.md b/README.md index 08bfd7d..31a0789 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ The constructor takes a configuration object with the following properties. | `outputPath` | string | The path (relative to your Webpack `outputPath`) to store externals copied over by this plugin. | `vendor` | | `publicPath` | string \| null | Override Webpack config's `publicPath` for the externals files, or `null` to use the default `output.publicPath` value. | `null` | | `files` | string \| array<string> \| null | If you have multiple instances of HtmlWebpackPlugin, use this to specify globs of which files you want to inject assets into. Will add assets to all files by default. | `null` | +| `enabled` | boolean | Set to `false` to disable the plugin (useful for disabling in development mode). | `true` | ## Examples @@ -285,3 +286,20 @@ new HtmlWebpackExternalsPlugin({ files: ['about.html'], }) ``` + +### Disabling the plugin + +Sometimes you only want the plugin to be activated in certain environments. Rather than create separate Webpack configs or mess with splicing the plugins array, simply set the `enabled` option to `false` to disable the externals plugin entirely. + +```js +new HtmlWebpackExternalsPlugin({ + externals: [ + { + module: 'jquery', + entry: 'dist/jquery.min.js', + global: 'jQuery', + }, + ], + enabled: process.env.NODE_ENV === 'production', +}) +``` diff --git a/package-lock.json b/package-lock.json index f421d3d..689ed4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -151,6 +151,12 @@ "util": "0.10.3" } }, + "assertion-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.2.tgz", + "integrity": "sha1-E8pRXYYgbaC6xm6DTdOX2HWBCUw=", + "dev": true + }, "async": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/async/-/async-2.5.0.tgz", @@ -2913,22 +2919,22 @@ } } }, - "string-width": { - "version": "1.0.2", + "string_decoder": { + "version": "1.0.1", "bundled": true, "dev": true, "requires": { - "code-point-at": "1.1.0", - "is-fullwidth-code-point": "1.0.0", - "strip-ansi": "3.0.1" + "safe-buffer": "5.0.1" } }, - "string_decoder": { - "version": "1.0.1", + "string-width": { + "version": "1.0.2", "bundled": true, "dev": true, "requires": { - "safe-buffer": "5.0.1" + "code-point-at": "1.1.0", + "is-fullwidth-code-point": "1.0.0", + "strip-ansi": "3.0.1" } }, "stringstream": { @@ -5680,6 +5686,15 @@ "integrity": "sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=", "dev": true }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "dev": true, + "requires": { + "safe-buffer": "5.1.1" + } + }, "string-width": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", @@ -5691,15 +5706,6 @@ "strip-ansi": "3.0.1" } }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "dev": true, - "requires": { - "safe-buffer": "5.1.1" - } - }, "strip-ansi": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", diff --git a/package.json b/package.json index 30cc75b..0a2c7c1 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ }, "homepage": "https://github.com/mmiller42/html-webpack-externals-plugin#readme", "devDependencies": { + "assertion-error": "^1.0.2", "babel-cli": "^6.24.1", "babel-core": "^6.25.0", "babel-plugin-add-module-exports": "^0.2.1", diff --git a/src/HtmlWebpackExternalsPlugin.js b/src/HtmlWebpackExternalsPlugin.js index 1a80791..d171f54 100644 --- a/src/HtmlWebpackExternalsPlugin.js +++ b/src/HtmlWebpackExternalsPlugin.js @@ -25,11 +25,12 @@ export default class HtmlWebpackExternalsPlugin { this.assetsToCopy = [] this.externals = {} - const { externals, hash, outputPath, publicPath, files } = config + const { externals, hash, outputPath, publicPath, files, enabled } = config this.hash = hash this.outputPath = outputPath this.publicPath = publicPath this.files = files + this.enabled = enabled externals.forEach(({ module, entry, global, supplements, append }) => { this.externals[module] = global @@ -63,6 +64,10 @@ export default class HtmlWebpackExternalsPlugin { } apply(compiler) { + if (!this.enabled) { + return + } + if (!compiler.options.externals) { compiler.options.externals = this.externals } else if (Array.isArray(compiler.options.externals)) { diff --git a/src/configSchema.json b/src/configSchema.json index 5746ca9..35d407a 100644 --- a/src/configSchema.json +++ b/src/configSchema.json @@ -75,6 +75,10 @@ }, "minItems": 1, "default": null + }, + "enabled": { + "type": "boolean", + "default": true } }, "required": ["externals"] diff --git a/test/HtmlWebpackExternalsPlugin.spec.js b/test/HtmlWebpackExternalsPlugin.spec.js index eba5be3..12f0b99 100644 --- a/test/HtmlWebpackExternalsPlugin.spec.js +++ b/test/HtmlWebpackExternalsPlugin.spec.js @@ -1,4 +1,5 @@ import assert from 'assert' +import AssertionError from 'assertion-error' import HtmlWebpackPlugin from 'html-webpack-plugin' import HtmlWebpackExternalsPlugin from '../lib/' import { @@ -288,11 +289,58 @@ describe('HtmlWebpackExternalsPlugin', function() { ) .then(() => reject( - 'index.html should not have had the assets inserted into the HTML' + new AssertionError( + 'index.html should not have had the assets inserted into the HTML' + ) ) ) - .catch(() => resolve()) + .catch(resolve) }) }) }) + + it('does not run when enabled is false', function() { + const wp = runWebpack( + new HtmlWebpackPlugin(), + new HtmlWebpackExternalsPlugin({ + externals: [ + { + module: 'jquery', + entry: 'dist/jquery.min.js', + global: 'jQuery', + }, + ], + enabled: false, + }) + ) + + return Promise.all([ + new Promise((resolve, reject) => { + wp + .then(() => checkBundleExcludes('jQuery')) + .then(() => + reject(new AssertionError('Plugin should not have excluded jQuery')) + ) + .catch(resolve) + }), + new Promise((resolve, reject) => { + wp + .then(() => checkCopied('vendor/jquery/dist/jquery.min.js')) + .then(() => + reject(new AssertionError('Plugin should not have copied jQuery')) + ) + .catch(resolve) + }), + new Promise((resolve, reject) => { + wp + .then(() => + checkHtmlIncludes('vendor/jquery/dist/jquery.min.js', 'js') + ) + .then(() => + reject(new AssertionError('Plugin should not have injected jQuery')) + ) + .catch(resolve) + }), + ]) + }) })