|
3 | 3 | # rollup-plugin-import-assertions
|
4 | 4 |
|
5 | 5 | 🍣 A Rollup plugin which bundles [import assertions](https://github.com/tc39/proposal-import-assertions).
|
| 6 | + |
| 7 | +Two types of assertions are supported: `json` and `css`. |
| 8 | + |
| 9 | +Currently, dynamic imports are not supported (PR welcomed). |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin: |
| 14 | + |
| 15 | +```js |
| 16 | +import importAssertions from 'rollup-plugin-import-assertions'; |
| 17 | + |
| 18 | +export default { |
| 19 | + input: 'src/index.js', |
| 20 | + output: { |
| 21 | + dir: 'output', |
| 22 | + format: 'cjs' |
| 23 | + }, |
| 24 | + plugins: [importAssertions()] |
| 25 | +}; |
| 26 | +``` |
| 27 | + |
| 28 | +Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). |
| 29 | + |
| 30 | +With an accompanying file `src/index.js`, the local `package.json` file would now be importable as seen below: |
| 31 | + |
| 32 | +```js |
| 33 | +// src/index.js |
| 34 | +import pkg from './package.json' assert { type: 'json' }; |
| 35 | +console.log(`running version ${pkg.version}`); |
| 36 | +``` |
| 37 | + |
| 38 | +It is also possible to import css stylesheets, typically when designing web components: |
| 39 | + |
| 40 | +```js |
| 41 | +// src/mycomponent.js |
| 42 | +import style from './style.css' assert { type: 'css' }; |
| 43 | + |
| 44 | +class MyElement extends HTMLElement { |
| 45 | + constructor() { |
| 46 | + super(); |
| 47 | + const root = this.attachShadow({ mode: 'open' }); |
| 48 | + root.adoptedStyleSheets = [ styles ]; |
| 49 | + root.innerHTML = `<div>My custom element</div>`; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +customElements.define('my-element', MyElement); |
| 54 | +``` |
| 55 | + |
| 56 | +## Options |
| 57 | + |
| 58 | +For the `json` type of assertions, this plugin accepts the same options |
| 59 | +as those of [@rollup/plugin-json](https://github.com/rollup/plugins/tree/master/packages/json/). |
| 60 | +This makes it straight-forward to move to import assertions, should one wish so. |
| 61 | + |
| 62 | +For the `css` type of assertions, this plugin accepts the usual `include` and `exclude` options. |
| 63 | + |
| 64 | +### `compact` (type: 'json') |
| 65 | + |
| 66 | +Type: `Boolean`<br> |
| 67 | +Default: `false` |
| 68 | + |
| 69 | +If `true`, instructs the plugin to ignore `indent` and generates the smallest code. |
| 70 | + |
| 71 | +### `exclude` |
| 72 | + |
| 73 | +Type: `String` | `Array[...String]`<br> |
| 74 | +Default: `null` |
| 75 | + |
| 76 | +A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored. |
| 77 | + |
| 78 | +### `include` |
| 79 | + |
| 80 | +Type: `String` | `Array[...String]`<br> |
| 81 | +Default: `null` |
| 82 | + |
| 83 | +A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted. |
| 84 | + |
| 85 | +### `indent` (type: 'json') |
| 86 | + |
| 87 | +Type: `String`<br> |
| 88 | +Default: `'\t'` |
| 89 | + |
| 90 | +Specifies the indentation for the generated default export. |
| 91 | + |
| 92 | +### `namedExports` (type: 'json') |
| 93 | + |
| 94 | +Type: `Boolean`<br> |
| 95 | +Default: `true` |
| 96 | + |
| 97 | +If `true`, instructs the plugin to generate a named export for every property of the JSON object. |
| 98 | + |
| 99 | +### `preferConst` (type: 'json') |
| 100 | + |
| 101 | +Type: `Boolean`<br> |
| 102 | +Default: `false` |
| 103 | + |
| 104 | +If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking. |
| 105 | + |
| 106 | +## Credits |
| 107 | + |
| 108 | +Credits to: |
| 109 | + |
| 110 | +- [@rollup/plugin-json](https://github.com/rollup/plugins/tree/master/packages/json/), |
| 111 | +on top of which this plugin shamelessly builds. |
| 112 | + |
| 113 | +- [rollup-plugin-import-assert](https://github.com/calebdwilliams/rollup-plugin-import-assert) which was inspirational to start with. |
0 commit comments