Home

Awesome

libera manifesto

rollup-plugin-import-assertions

🍣 A Rollup plugin which bundles import assertions.

Two types of assertions are supported: json and css.

Currently, dynamic imports are not supported (PR welcomed).

Install

Using npm:

npm install rollup-plugin-import-assertions --save-dev

Usage

Create a rollup.config.js configuration file and import the plugin:

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 or the API.

With an accompanying file src/index.js, the local package.json file would now be importable as seen below:

// 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:

// 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 = `<div>My custom element</div>`;
  }
}

customElements.define('my-element', MyElement);

Options

For the json type of assertions, this plugin accepts the same options as those of @rollup/plugin-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<br> Default: false

If true, instructs the plugin to ignore indent and generates the smallest code.

exclude

Type: String | Array[...String]<br> Default: null

A minimatch pattern, 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]<br> Default: null

A minimatch pattern, 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<br> Default: '\t'

Specifies the indentation for the generated default export.

namedExports (type: 'json')

Type: Boolean<br> Default: true

If true, instructs the plugin to generate a named export for every property of the JSON object.

preferConst (type: 'json')

Type: Boolean<br> 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:

License

license