Skip to content

swiing/rollup-plugin-import-attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Dec 22, 2024
d0dc2b4 · Dec 22, 2024

History

45 Commits
Apr 26, 2023
Mar 16, 2022
Dec 22, 2024
Dec 22, 2024
Mar 16, 2022
Mar 16, 2022
Mar 19, 2022
Apr 9, 2022
Mar 16, 2022
Dec 22, 2024
Mar 15, 2022
Dec 22, 2024
Dec 22, 2024
Feb 15, 2024
Mar 19, 2022

Repository files navigation

libera manifesto

rollup-plugin-import-attributes

🍣 A Rollup plugin which bundles import attributes.

Two types of attributes are supported: json and css.

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

Install

Using npm:

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

Usage

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

import importAttributes from 'rollup-plugin-import-attributes';

export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [importAttributes()]
};

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 aattribute, this plugin accepts the same options as those of @rollup/plugin-json. This makes it straight-forward to move to import attributes, should one wish so.

For the css type of attribute, 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, 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, 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:

License

license