Skip to content
This repository has been archived by the owner on Apr 5, 2021. It is now read-only.

Commit

Permalink
Add an enabled option
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Miller committed Jul 31, 2017
1 parent fa54398 commit 62ad9c6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 20 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',
})
```
40 changes: 23 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion src/HtmlWebpackExternalsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 4 additions & 0 deletions src/configSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
},
"minItems": 1,
"default": null
},
"enabled": {
"type": "boolean",
"default": true
}
},
"required": ["externals"]
Expand Down
52 changes: 50 additions & 2 deletions test/HtmlWebpackExternalsPlugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert'
import AssertionError from 'assertion-error'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import HtmlWebpackExternalsPlugin from '../lib/'
import {
Expand Down Expand Up @@ -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)
}),
])
})
})

0 comments on commit 62ad9c6

Please sign in to comment.