UNPKG

2.74 kBMarkdownView Raw
1[npm]: https://img.shields.io/npm/v/rollup-plugin-bundle-inject
2[npm-url]: https://www.npmjs.com/package/rollup-plugin-bundle-inject
3[size]: https://packagephobia.now.sh/badge?p=rollup-plugin-bundle-inject
4[size-url]: https://packagephobia.now.sh/result?p=rollup-plugin-bundle-inject
5
6[![npm][npm]][npm-url]
7[![size][size]][size-url]
8
9# rollup-plugin-bundle-inject
10
11🍣 Inject JS or CSS bundle into a template where necessary
12
13## Install
14
15Using npm:
16
17```bash
18npm install rollup-plugin-bundle-inject --save-dev
19```
20
21## Usage
22
23Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
24
25```js
26const bundleInject = require("rollup-plugin-bundle-inject");
27
28module.exports = {
29 input: "./src/index.js",
30 output: {
31 dir: "./public/dist",
32 format: "cjs",
33 },
34 plugins: [
35 bundleInject({
36 // specify the template
37 target: "./public/index.html",
38 }),
39 ],
40};
41```
42
43Once build successfully, an HTML file should be written to the bundle output destination.
44
45## Options
46
47### `target`
48
49Type: `String`<br>
50Default: `''`
51
52Specifies the template.
53
54\_Note: This field is required, it will throw an error if you don't specify a valid value.
55
56### `rename`
57
58Type: `String`<br>
59Default: `''`
60
61Rename the output template.
62
63### `minify`
64
65Type: `Boolean`<br>
66Default: `true`
67
68Minify the output html bundle.
69
70### `minifierOptions`
71
72Type: `html-minifier.Options` ([See options here](https://github.com/kangax/html-minifier#options-quick-reference))<br>
73Defaults:
74
75```js
76{
77 removeComments: true,
78 collapseWhitespace: true,
79 collapseBooleanAttributes: true,
80 removeAttributeQuotes: true,
81 removeRedundantAttributes: true,
82 useShortDoctype: true,
83 removeEmptyAttributes: true,
84 removeEmptyElements: true,
85}
86```
87
88Options to use for `html-minifier`.
89
90## Example
91
92By default, CSS bundle will inject into the end of the `<head>`, JS bundle will inject into the end of the `<body>`.
93
94```html
95<!DOCTYPE html>
96<html>
97 <head>
98 <title>Rollup bundle inject example</title>
99 <meta charset="utf-8" />
100 <style>
101 /* Content of bundle.css will goes here */
102 </style>
103 </head>
104 <body>
105 <h1>Hello World!</h1>
106 <script>
107 /* Content of bundle.js will goes here */
108 </script>
109 </body>
110</html>
111```
112
113you could decide where should be the bundle injected, by using the tag `<!-- inject:css -->` and `<!-- inject:js -->`.
114
115```html
116<!DOCTYPE html>
117<html>
118 <head>
119 <title>Rollup bundle inject example</title>
120 <meta charset="utf-8" />
121 <!-- inject:css -->
122 <link type="text/css" rel="stylesheet" href="bootstrap.css" />
123 <script src="jquery.js"></script>
124 <!-- inject:js -->
125 </head>
126 <body>
127 <h1>Hello World!</h1>
128 </body>
129</html>
130```