Skip to content

Commit c5d4b86

Browse files
atstpjantimon
authored andcommittedFeb 17, 2019
feat: Add support for <base> tag (#1160)
1 parent f4cb241 commit c5d4b86

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
 

‎README.md

+34
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Allowed values are as follows
147147
|**`inject`**|`{Boolean\|String}`|`true`|`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|
148148
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
149149
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
150+
|**`base`**|`{Object\|String\|false}`|`false`|Inject a [`base`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tag. E.g. `base: "https://example.com/path/page.html`|
150151
|**`minify`**|`{Boolean\|Object}`|`true` if `mode` is `'production'`, otherwise `false`|Controls if and in what ways the output should be minified. See [minification](#minification) below for more details.|
151152
|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|
152153
|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|
@@ -387,6 +388,39 @@ plugins: [
387388
]
388389
```
389390

391+
### Base Tag
392+
393+
When the `base` option is used,
394+
html-webpack-plugin will inject a [base tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base).
395+
By default, a base tag will not be injected.
396+
397+
The following two are identical and will both insert `<base href="http://example.com/some/page.html">`:
398+
399+
```js
400+
new HtmlWebpackPlugin({
401+
'base': 'http://example.com/some/page.html'
402+
})
403+
```
404+
405+
```js
406+
new HtmlWebpackPlugin({
407+
'base': { 'href': 'http://example.com/some/page.html' }
408+
})
409+
```
410+
411+
The `target` can be specified with the corresponding key:
412+
413+
```js
414+
new HtmlWebpackPlugin({
415+
'base': {
416+
'href': 'http://example.com/some/page.html',
417+
'target': '_blank'
418+
}
419+
})
420+
```
421+
422+
which will inject the element `<base href="http://example.com/some/page.html" target="_blank">`.
423+
390424
### Long Term Caching
391425

392426
For long term caching add `contenthash/templatehash` to the filename.

‎index.js

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class HtmlWebpackPlugin {
5353
excludeChunks: [],
5454
chunksSortMode: 'auto',
5555
meta: {},
56+
base: false,
5657
title: 'Webpack App',
5758
xhtml: false
5859
};
@@ -223,6 +224,7 @@ class HtmlWebpackPlugin {
223224
scripts: self.generatedScriptTags(assets.js),
224225
styles: self.generateStyleTags(assets.css),
225226
meta: [
227+
...self.generateBaseTag(self.options.base),
226228
...self.generatedMetaTags(self.options.meta),
227229
...self.generateFaviconTags(assets.favicon)
228230
]
@@ -713,6 +715,28 @@ class HtmlWebpackPlugin {
713715
}));
714716
}
715717

718+
/**
719+
* Generate an optional base tag
720+
* @param { false
721+
| string
722+
| {[attributeName: string]: string} // attributes e.g. { href:"http://example.com/page.html" target:"_blank" }
723+
} baseOption
724+
* @returns {Array<HtmlTagObject>}
725+
*/
726+
generateBaseTag (baseOption) {
727+
if (baseOption === false) {
728+
return [];
729+
} else {
730+
return [{
731+
tagName: 'base',
732+
voidTag: true,
733+
attributes: (typeof baseOption === 'string') ? {
734+
href: baseOption
735+
} : baseOption
736+
}];
737+
}
738+
}
739+
716740
/**
717741
* Generate all meta tags for the given meta configuration
718742
* @param {false | {

‎spec/basic.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,41 @@ describe('HtmlWebpackPlugin', () => {
15881588
}, [/<link rel="shortcut icon" href="[^"]+\.ico">/], null, done);
15891589
});
15901590

1591+
it('adds a base tag with attributes', done => {
1592+
testHtmlPlugin({
1593+
mode: 'production',
1594+
entry: path.join(__dirname, 'fixtures/index.js'),
1595+
output: {
1596+
path: OUTPUT_DIR,
1597+
filename: 'index_bundle.js'
1598+
},
1599+
plugins: [
1600+
new HtmlWebpackPlugin({
1601+
base: {
1602+
href: 'http://example.com/page.html',
1603+
target: '_blank'
1604+
}
1605+
})
1606+
]
1607+
}, [/<base href="http:\/\/example\.com\/page\.html" target="_blank">/], null, done);
1608+
});
1609+
1610+
it('adds a base tag short syntax', done => {
1611+
testHtmlPlugin({
1612+
mode: 'production',
1613+
entry: path.join(__dirname, 'fixtures/index.js'),
1614+
output: {
1615+
path: OUTPUT_DIR,
1616+
filename: 'index_bundle.js'
1617+
},
1618+
plugins: [
1619+
new HtmlWebpackPlugin({
1620+
base: 'http://example.com/page.html'
1621+
})
1622+
]
1623+
}, [/<base href="http:\/\/example\.com\/page\.html">/], null, done);
1624+
});
1625+
15911626
it('adds a meta tag', done => {
15921627
testHtmlPlugin({
15931628
mode: 'production',

0 commit comments

Comments
 (0)
Please sign in to comment.