script-ext-html-webpack-plugin
Advanced tools
Comparing version 1.7.4 to 1.8.0
@@ -5,2 +5,4 @@ 'use strict'; | ||
const isScript = (tag) => tag.tagName === 'script'; | ||
const hasScriptName = tag => tag.attributes && tag.attributes.src; | ||
@@ -43,3 +45,4 @@ | ||
hasScriptName, | ||
isScript, | ||
matches | ||
}; |
@@ -12,2 +12,7 @@ 'use strict'; | ||
}); | ||
const DEFAULT_CUSTOM_HASH = Object.freeze({ | ||
test: [], | ||
attribute: '', | ||
value: true | ||
}); | ||
const DEFAULT_OPTIONS = Object.freeze({ | ||
@@ -22,9 +27,11 @@ inline: DEFAULT_HASH, | ||
defaultAttribute: 'sync', | ||
removeInlinedAssets: true | ||
removeInlinedAssets: true, | ||
custom: [] | ||
}); | ||
const POSSIBLE_VALUES = ['chunks', 'attribute', 'value']; | ||
const denormaliseOptions = options => { | ||
const normaliseOptions = options => { | ||
if (!options) return DEFAULT_OPTIONS; | ||
validate(options); | ||
const denormalised = Object.assign({}, DEFAULT_OPTIONS, options); | ||
const normalised = Object.assign({}, DEFAULT_OPTIONS, options); | ||
// now overwrite values which are not of DEFAULT_HASH form | ||
@@ -39,8 +46,11 @@ Object.keys(options).forEach(key => { | ||
case 'module': | ||
denormalised[key] = denormaliseValue(value, DEFAULT_HASH); | ||
normalised[key] = normaliseAttribute(value); | ||
break; | ||
case 'prefetch': | ||
case 'preload': | ||
denormalised[key] = denormaliseValue(value, DEFAULT_RESOURCE_HINT_HASH); | ||
normalised[key] = normaliseResourceHint(value); | ||
break; | ||
case 'custom': | ||
normalised[key] = normaliseCustomArray(value); | ||
break; | ||
default: | ||
@@ -50,3 +60,3 @@ break; | ||
}); | ||
return denormalised; | ||
return normalised; | ||
}; | ||
@@ -63,10 +73,8 @@ | ||
const denormaliseValue = (value, defaultProps) => { | ||
let denormalised = Object.assign({}, defaultProps); | ||
const normaliseValue = (defaultProps, value) => { | ||
let normalised = Object.assign({}, defaultProps); | ||
if (value) { | ||
denormalised.test = convertToArray(value, () => { | ||
normalised.test = convertToArray(value, () => { | ||
if (typeof value === 'object') { | ||
if (value.chunks) { | ||
denormalised.chunks = value.chunks; | ||
} | ||
POSSIBLE_VALUES.forEach(key => copyValue(key, normalised, value)); | ||
if (value.test) { | ||
@@ -80,5 +88,16 @@ return convertToArray(value.test, error); | ||
} | ||
return denormalised; | ||
return normalised; | ||
}; | ||
const normaliseAttribute = normaliseValue.bind(null, DEFAULT_HASH); | ||
const normaliseResourceHint = normaliseValue.bind(null, DEFAULT_RESOURCE_HINT_HASH); | ||
const normaliseCustomAttribute = normaliseValue.bind(null, DEFAULT_CUSTOM_HASH); | ||
const normaliseCustomArray = value => { | ||
const array = Array.isArray(value) ? value : [value]; | ||
return array.map(normaliseCustomAttribute); | ||
}; | ||
const convertToArray = (value, elseFn) => { | ||
@@ -96,3 +115,9 @@ if (typeof value === 'string') { | ||
module.exports = denormaliseOptions; | ||
const copyValue = (key, to, from) => { | ||
if (from.hasOwnProperty(key)) { | ||
to[key] = from[key]; | ||
} | ||
}; | ||
module.exports = normaliseOptions; | ||
module.exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS; |
@@ -7,3 +7,3 @@ 'use strict'; | ||
const matches = require('./common.js').matches; | ||
const denormaliseOptions = require('./config.js'); | ||
const normaliseOptions = require('./config.js'); | ||
const shouldAddResourceHints = require('./resource-hints.js').shouldAddResourceHints; | ||
@@ -13,2 +13,3 @@ const addInitialChunkResourceHints = require('./initial-chunk-resource-hints.js'); | ||
const elements = require('./elements.js'); | ||
const customAttributes = require('./custom-attributes.js'); | ||
@@ -33,3 +34,3 @@ const debugEvent = msg => debug(`${EVENT}: ${msg}`); | ||
constructor (options) { | ||
this.options = denormaliseOptions(options); | ||
this.options = normaliseOptions(options); | ||
} | ||
@@ -59,2 +60,8 @@ apply (compiler) { | ||
} | ||
if (customAttributes.shouldAdd(options)) { | ||
debugEvent('adding custom attribues to <head> <script> elements'); | ||
pluginArgs.head = customAttributes.add(options, pluginArgs.head); | ||
debugEvent('adding custom attributes to <body> <script> elements'); | ||
pluginArgs.body = customAttributes.add(options, pluginArgs.body); | ||
} | ||
debugEvent('completed'); | ||
@@ -61,0 +68,0 @@ callback(null, pluginArgs); |
{ | ||
"name": "script-ext-html-webpack-plugin", | ||
"version": "1.7.4", | ||
"version": "1.8.0", | ||
"description": "Enhances html-webpack-plugin functionality with async and defer attributes for script elements", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,2 +13,3 @@ Script Extension for HTML Webpack Plugin | ||
- [`type="module"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Attributes) attribute; | ||
- any custom attributes you wish to add; | ||
- inlining; | ||
@@ -21,3 +22,3 @@ - [`preload`](https://www.w3.org/TR/preload/) resource hint; | ||
The raw [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) incorporates all webpack-generated javascipt as synchronous`<script>` elements in the generated html. This plugin allows you to: | ||
- add attributes to these elements; | ||
- add standard and custom attributes to these elements; | ||
- inline the code in the elements; | ||
@@ -87,2 +88,6 @@ - add prefetch and preload resource hints for initial and dynamically loaded scripts. | ||
- `prefetch`: a __script matching pattern__ defining scripts that should have accompanying prefetch resource hints (default: `[]`); | ||
- `custom`: a single hash or an array of hashes with the following structure: | ||
- `test`: a **script matching pattern** defining scripts that should have a custom attribute added; | ||
- `attribute`: a `String` attribute to add; | ||
- `value`: (optional) a `String` value for the attribute; if not set the attribute has no value set (equivalent of `true`). | ||
@@ -181,4 +186,23 @@ A __script matching pattern__ matches against a script's name. It can be one of: | ||
All scripts have custom attribute `type='text/paperscript'` and ui.js also has a custom attribute of `id='1235'`: | ||
```javascript | ||
plugins: [ | ||
new HtmlWebpackPlugin(), | ||
new ScriptExtHtmlWebpackPlugin({ | ||
custom: [ | ||
{ | ||
test: /\.js$/, | ||
attribute: 'type', | ||
value: 'text/paperscript' | ||
}, | ||
{ | ||
test: 'ui.js', | ||
attribute: 'id', | ||
value: '12345' | ||
} | ||
] | ||
}) | ||
] | ||
``` | ||
And so on, to craziness: | ||
@@ -267,2 +291,5 @@ ```javascript | ||
v1.8.x | ||
* added custom attributes | ||
v1.7.x | ||
@@ -269,0 +296,0 @@ * updated for Webpack 2.5.x and updated all dependencies |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29542
13
423
326