Allow to inject javascript files into the head of the html page
This commit is contained in:
parent
9036c15b97
commit
6219714fc7
|
|
@ -65,11 +65,13 @@ Allowed values are as follows:
|
|||
You can specify a subdirectory here too (eg: `assets/admin.html`).
|
||||
- `template`: A html template (supports [blueimp templates](https://github.com/blueimp/JavaScript-Templates)).
|
||||
- `templateContent`: A html string or a function returning the html (supports [blueimp templates](https://github.com/blueimp/JavaScript-Templates)).
|
||||
- `inject`: Inject all assets into the given `template` or `templateContent`.
|
||||
- `inject`: `true | 'head' | 'body' | false` Inject all assets into the given `template` or `templateContent` - When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element.
|
||||
- `favicon`: Adds the given favicon path to the output html.
|
||||
- `minify`: Set to true or pass a [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference) options object to minify the output.
|
||||
- `hash`: if `true` then append a unique webpack compilation hash to all
|
||||
- `minify`: `true | {...} | false` Set to true or pass a [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference) options object to minify the output.
|
||||
- `hash`: `true | false` if `true` then append a unique webpack compilation hash to all
|
||||
included scripts and css files. This is useful for cache busting.
|
||||
- `chunks`: Allows you to add only some chunks (e.g. only the unit-test chunk)
|
||||
- `excludeChunks`: Allows you to skip some chunks (e.g. don't add the unit-test chunk)
|
||||
|
||||
Here's an example webpack config illustrating how to use these options:
|
||||
```javascript
|
||||
|
|
|
|||
28
index.js
28
index.js
|
|
@ -273,18 +273,30 @@ HtmlWebpackPlugin.prototype.injectAssetsIntoHtml = function(html, templateParams
|
|||
styles = styles.map(function(stylePath) {
|
||||
return '<link href="' + stylePath + '" rel="stylesheet">';
|
||||
});
|
||||
// If there is a favicon present, add it above any link-tags
|
||||
// Injections
|
||||
var head = [];
|
||||
var body = [];
|
||||
|
||||
// If there is a favicon present, add it to the head
|
||||
if (assets.favicon) {
|
||||
styles.unshift('<link rel="shortcut icon" href="' + assets.favicon + '">');
|
||||
head.push('<link rel="shortcut icon" href="' + assets.favicon + '">');
|
||||
}
|
||||
// Append scripts to body element
|
||||
html = html.replace(/(<\/body>)/i, function (match) {
|
||||
return scripts.join('') + match;
|
||||
});
|
||||
// Append styles to head element
|
||||
// Add styles to the head
|
||||
head = head.concat(styles);
|
||||
// Add scripts to body or head
|
||||
if (this.options.inject === 'head') {
|
||||
head = body.concat(scripts);
|
||||
} else {
|
||||
body = body.concat(scripts);
|
||||
}
|
||||
// Append assets to head element
|
||||
html = html.replace(/(<\/head>)/i, function (match) {
|
||||
return styles.join('') + match;
|
||||
return head.join('') + match;
|
||||
});
|
||||
// Append assets to body element
|
||||
html = html.replace(/(<\/body>)/i, function (match) {
|
||||
return body.join('') + match;
|
||||
});
|
||||
// Inject manifest into the opening html tag
|
||||
if (assets.manifest) {
|
||||
html = html.replace(/(<html.*)(>)/i, function (match, start, end) {
|
||||
|
|
|
|||
|
|
@ -113,6 +113,40 @@ describe('HtmlWebpackPlugin', function() {
|
|||
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], null, done);
|
||||
});
|
||||
|
||||
it('allows you to inject the assets into the body of the given template', function (done) {
|
||||
testHtmlPlugin({
|
||||
entry: {
|
||||
util: path.join(__dirname, 'fixtures/util.js'),
|
||||
app: path.join(__dirname, 'fixtures/index.js')
|
||||
},
|
||||
output: {
|
||||
path: OUTPUT_DIR,
|
||||
filename: '[name]_bundle.js'
|
||||
},
|
||||
plugins: [new HtmlWebpackPlugin({
|
||||
inject: 'body',
|
||||
template: path.join(__dirname, 'fixtures/plain.html')
|
||||
})]
|
||||
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], null, done);
|
||||
});
|
||||
|
||||
it('allows you to inject the assets into the head of the given template', function (done) {
|
||||
testHtmlPlugin({
|
||||
entry: {
|
||||
util: path.join(__dirname, 'fixtures/util.js'),
|
||||
app: path.join(__dirname, 'fixtures/index.js')
|
||||
},
|
||||
output: {
|
||||
path: OUTPUT_DIR,
|
||||
filename: '[name]_bundle.js'
|
||||
},
|
||||
plugins: [new HtmlWebpackPlugin({
|
||||
inject: 'head',
|
||||
template: path.join(__dirname, 'fixtures/plain.html')
|
||||
})]
|
||||
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], null, done);
|
||||
});
|
||||
|
||||
it('allows you to inject the assets into a html string', function (done) {
|
||||
testHtmlPlugin({
|
||||
entry: {
|
||||
|
|
|
|||
Loading…
Reference in New Issue