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 |
|
15 | Using npm:
|
16 |
|
17 | ```bash
|
18 | npm install rollup-plugin-bundle-inject --save-dev
|
19 | ```
|
20 |
|
21 | ## Usage
|
22 |
|
23 | Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
|
24 |
|
25 | ```js
|
26 | const bundleInject = require("rollup-plugin-bundle-inject");
|
27 |
|
28 | module.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 |
|
43 | Once build successfully, an HTML file should be written to the bundle output destination.
|
44 |
|
45 | ## Options
|
46 |
|
47 | ### `target`
|
48 |
|
49 | Type: `String`<br>
|
50 | Default: `''`
|
51 |
|
52 | Specifies the template.
|
53 |
|
54 | \_Note: This field is required, it will throw an error if you don't specify a valid value.
|
55 |
|
56 | ## Example
|
57 |
|
58 | By default, CSS bundle will inject into the end of the `<head>`, JS bundle will inject into the end of the `<body>`.
|
59 |
|
60 | ```html
|
61 | <!DOCTYPE html>
|
62 | <html>
|
63 | <head>
|
64 | <title>Rollup bundle inject example</title>
|
65 | <meta charset="utf-8" />
|
66 | <style>
|
67 | /* Content of bundle.css will goes here */
|
68 | </style>
|
69 | </head>
|
70 | <body>
|
71 | <h1>Hello World!</h1>
|
72 | <script>
|
73 | /* Content of bundle.js will goes here */
|
74 | </script>
|
75 | </body>
|
76 | </html>
|
77 | ```
|
78 |
|
79 | you could decide where should be the bundle injected, by using the tag `<!-- inject:css -->` and `<!-- inject:js -->`.
|
80 |
|
81 | ```html
|
82 | <!DOCTYPE html>
|
83 | <html>
|
84 | <head>
|
85 | <title>Rollup bundle inject example</title>
|
86 | <meta charset="utf-8" />
|
87 | <!-- inject:css -->
|
88 | <link type="text/css" rel="stylesheet" href="bootstrap.css" />
|
89 | <script src="jquery.js"></script>
|
90 | <!-- inject:js -->
|
91 | </head>
|
92 | <body>
|
93 | <h1>Hello World!</h1>
|
94 | </body>
|
95 | </html>
|
96 | ```
|