Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ On final bundle generation the provided template file will have a `script` tag i

- `template`: **(required)** The path to the source template.
- `target`: The directory and file name to use for the html file generated with the bundle.
- `attrs`: The attributes provided to the generated bundle script tag. Passed as an array of strings
- `attrs`: The attributes provided to the generated bundle script tag. Passed as an array of strings \
Example: `attrs: ['async', 'defer]` will generate `<script async defer src="bundle.js"></script>`
- `replaceVars`: An object containing variables that will be replaced in the generated html.
- `replaceVars`: An object containing variables that will be replaced in the generated html. \
Example: `replaceVars: { '__CDN_URL__': process.env.NODE_ENV === 'production' ? 'https://mycdn.com' : '' }` will replace all instances of `__CDN_URL__` with `http://mycdn.com` if the environment is production
- `scriptIncludeFilter`: A filter function `(id, `[`AssetInfo` or `ChunkInfo`](https://rollupjs.org/guide/en/#generatebundle)`) => boolean` controlling which .js chunks are injected as script tags. \
Example: ``(id, fileInfo) => fileInfo.isEntry`` will only inject static entry points (restores behavior of <= v1.6.1), ``(id, fileInfo) => fileInfo.isEntry && id.match(/^index.*\.js$/i)`` will only inject those matching index*.js

## License

Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const INVALID_ARGS_ERROR =
*/
export default function htmlTemplate(options = {}) {
const { template, target, prefix, attrs, replaceVars } = options;
const scriptIncludeFilter = options.scriptIncludeFilter || (() => true);
const scriptTagAttributes = attrs && attrs.length > 0 ? attrs : [];
return {
name: "html-template",
Expand Down Expand Up @@ -81,7 +82,11 @@ export default function htmlTemplate(options = {}) {
injected = [
injected.slice(0, bodyCloseTag),
...bundleKeys
.filter(f => path.extname(f) === ".js")
.filter(
f =>
path.extname(f) === ".js" &&
scriptIncludeFilter(f, bundleInfo[f])
)
.map(
b =>
`<script ${scriptTagAttributes.join(
Expand Down