Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shgysk8zer0/rollup-import

A RollUp plugin for importing modules
https://github.com/shgysk8zer0/rollup-import

import-map node rollup-plugin

Last synced: 3 months ago
JSON representation

A RollUp plugin for importing modules

Awesome Lists containing this project

README

        

# rolllup-import
A RollUp plugin for importing modules from URLs, paths, and "bare specifiers" using
import maps. You'll no longer need to `npm i` everything you need in front-end code.

- - -
[![CodeQL](https://github.com/shgysk8zer0/rollup-import/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/shgysk8zer0/rollup-import/actions/workflows/codeql-analysis.yml)
![Node CI](https://github.com/shgysk8zer0/rollup-import/workflows/Node%20CI/badge.svg)
![Lint Code Base](https://github.com/shgysk8zer0/rollup-import/workflows/Lint%20Code%20Base/badge.svg)

[![GitHub license](https://img.shields.io/github/license/shgysk8zer0/rollup-import.svg)](https://github.com/shgysk8zer0/rollup-import/blob/master/LICENSE)
[![GitHub last commit](https://img.shields.io/github/last-commit/shgysk8zer0/rollup-import.svg)](https://github.com/shgysk8zer0/rollup-import/commits/master)
[![GitHub release](https://img.shields.io/github/release/shgysk8zer0/rollup-import?logo=github)](https://github.com/shgysk8zer0/rollup-import/releases)
[![GitHub Sponsors](https://img.shields.io/github/sponsors/shgysk8zer0?logo=github)](https://github.com/sponsors/shgysk8zer0)

[![npm](https://img.shields.io/npm/v/@shgysk8zer0/rollup-import)](https://www.npmjs.com/package/@shgysk8zer0/rollup-import)
![node-current](https://img.shields.io/node/v/@shgysk8zer0/rollup-import)
![npm bundle size gzipped](https://img.shields.io/bundlephobia/minzip/@shgysk8zer0/rollup-import)
[![npm](https://img.shields.io/npm/dw/@shgysk8zer0/rollup-import?logo=npm)](https://www.npmjs.com/package/@shgysk8zer0/rollup-import)

[![GitHub followers](https://img.shields.io/github/followers/shgysk8zer0.svg?style=social)](https://github.com/shgysk8zer0)
![GitHub forks](https://img.shields.io/github/forks/shgysk8zer0/rollup-import.svg?style=social)
![GitHub stars](https://img.shields.io/github/stars/shgysk8zer0/rollup-import.svg?style=social)
[![Twitter Follow](https://img.shields.io/twitter/follow/shgysk8zer0.svg?style=social)](https://twitter.com/shgysk8zer0)

[![Donate using Liberapay](https://img.shields.io/liberapay/receives/shgysk8zer0.svg?logo=liberapay)](https://liberapay.com/shgysk8zer0/donate "Donate using Liberapay")
- - -

- [Code of Conduct](./.github/CODE_OF_CONDUCT.md)
- [Contributing](./.github/CONTRIBUTING.md)

## Installation

```bash
npm i @shgysk8zer0/rollup-import
```

## Supports
- External [impormap](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap)
- JSON
- YAML
- Object `{ imports, scope }` for importmap
- Map `new Map([[specifier, value]])` for importmap
- Importing modules from URL and paths and "bare specifiers"
- Resolving `import.meta.url` and `import.meta.resolve('path.ext')`

## Not yet supported
- `import html from 'template.html' with { type: 'html' }` - No spec yet and will have issues with TrustedTypes
- `import style from 'styles.css' with { type: 'style' }` - Would require `new CSSStyleSheet().replace()` or `style-src 'unsafe-inline'`
- `import json from 'data.json' with { type: 'json' }`
- Parsing from `` in an HTML file
- Use of `scope`

## Example

### `rollup.config.js`

```js
import {
rollupImport, // Handles `import '@scope/package' resolving and fetching`
rollupImportMeta // Handles `import.meta.url` and `import.meta.resolve()`,
} from '@shgysk8zer0/rollup-import';

import terser from '@rollup/plugin-terser';

// To load environment variables from `.env`
import { config } from 'dotenv';
config();

export default {
input: 'src/index.mjs',
plugins: [
rollupImport(['path/to/importmap.json']),
rollupImportMeta({
// MUST be a valid URL
baseURL: 'https://example.com', // Defaults to `process.env.URL` if set
// MUST be a `file:` URL
projectRoot: 'file:///home/user/Projects/my-project/', // Dfaults to `file:///${process.cwd()}/`
}),
terser(),
],
output: {
file: 'dest/index.js',
format: 'iife'
}
};
```

### `importmap.json`

```json
{
"imports": {
"leaflet": "https://unpkg.com/[email protected]/dist/leaflet-src.esm.js",
"firebase/": "https://www.gstatic.com/firebasejs/9.16.0/",
"@scope/package": "./node_modules/@scope/package/index.js",
"@shgysk8zer0/polyfills": "https://unpkg.com/@shgysk8zer0/[email protected]/all.min.js",
"@shgysk8zer0/polyfills/": "https://unpkg.com/@[email protected]/polyfills/"
}
}
```

### `index.js`

```js
import '@scope/package';
import '@shgysk8zer0/polyfills';
import '@shgysk8zer0/polyfills/legacy/object.js'; // -> "https://unpkg.com/@[email protected]/polyfills/legacy/ojbect.js"
import { initializeApp } from 'firebase/firebase-app.js';
import { name } from './consts.js';

const stylesheet = document.createElement('link');
stylesheet.rel = 'stylesheet';
stylesheet.href = import.meta.resolve('styles.css');

document.head.append(stylesheet);
```

## Notes

Using `import`s only, you may use only `rollupImport` or `rollupImportMeta`
via `@shgysk8zer0/rollup-import/import` and `@shgysk8zer0/rollup-import/meta`
respectively. To use with `require()`, you MUST import either/both using
`const {rollupImport, rollupImportMeta } = require('@shgysk8zer0/rollup-import')`.

This plugin works well if importing modules without bundling in the dev environment.
In order to do this, however, you **must** include a `<script type="importmap">`
in your HTML - `<script type="importmap" src="...">` will not work.