Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/swiing/rollup-plugin-import-assertions
A Rollup plugin which bundles TC39 import assertions.
https://github.com/swiing/rollup-plugin-import-assertions
assertions css import json plugin rollup
Last synced: 25 days ago
JSON representation
A Rollup plugin which bundles TC39 import assertions.
- Host: GitHub
- URL: https://github.com/swiing/rollup-plugin-import-assertions
- Owner: swiing
- License: mit
- Created: 2022-03-09T17:36:46.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-15T15:08:11.000Z (10 months ago)
- Last Synced: 2024-05-16T13:23:29.799Z (7 months ago)
- Topics: assertions, css, import, json, plugin, rollup
- Language: JavaScript
- Homepage:
- Size: 383 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome - import-assertions - Add support for TC39 import assertions (e.g. assert types `css` and `json`) (Plugins / Modules)
README
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
# rollup-plugin-import-assertions
🍣 A Rollup plugin which bundles [import assertions](https://github.com/tc39/proposal-import-assertions).
Two types of assertions are supported: `json` and `css`.
Currently, dynamic imports are not supported (PR welcomed).
## Install
Using npm:
```console
npm install rollup-plugin-import-assertions --save-dev
```## Usage
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
```js
import importAssertions from 'rollup-plugin-import-assertions';export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [importAssertions()]
};
```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).
With an accompanying file `src/index.js`, the local `package.json` file would now be importable as seen below:
```js
// src/index.js
import pkg from '../package.json' assert { type: 'json' };
console.log(`running version ${pkg.version}`);
```It is also possible to import css stylesheets, typically when designing web components:
```js
// src/mycomponent.js
import style from './style.css' assert { type: 'css' };class MyElement extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({ mode: 'open' });
root.adoptedStyleSheets = [styles];
root.innerHTML = `My custom element`;
}
}customElements.define('my-element', MyElement);
```## Options
For the `json` type of assertions, this plugin accepts the same options
as those of [@rollup/plugin-json](https://github.com/rollup/plugins/tree/master/packages/json/).
This makes it straight-forward to move to import assertions, should one wish so.For the `css` type of assertions, this plugin accepts the usual `include` and `exclude` options.
### `compact` (type: 'json')
Type: `Boolean`
Default: `false`If `true`, instructs the plugin to ignore `indent` and generates the smallest code.
### `exclude`
Type: `String` | `Array[...String]`
Default: `null`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.
### `include`
Type: `String` | `Array[...String]`
Default: `null`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.
### `indent` (type: 'json')
Type: `String`
Default: `'\t'`Specifies the indentation for the generated default export.
### `namedExports` (type: 'json')
Type: `Boolean`
Default: `true`If `true`, instructs the plugin to generate a named export for every property of the JSON object.
### `preferConst` (type: 'json')
Type: `Boolean`
Default: `false`If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking.
## Credits
Credits to:
- [@rollup/plugin-json](https://github.com/rollup/plugins/tree/master/packages/json/),
on top of which this plugin shamelessly builds.- [rollup-plugin-import-assert](https://github.com/calebdwilliams/rollup-plugin-import-assert) which was inspirational to start with.
## License
![license](https://img.shields.io/github/license/swiing/rollup-plugin-import-assertions)