1.2.3 • Published 6 months ago

@shgysk8zer0/rollup-import v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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 Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Installation

npm i @shgysk8zer0/rollup-import

Supports

  • External impormap
    • 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 <script type="importmap"> in an HTML file
  • Use of scope

Example

rollup.config.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

{
  "imports": {
    "leaflet": "https://unpkg.com/leaflet@1.9.3/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/polyfills@0.0.5/all.min.js",
    "@shgysk8zer0/polyfills/": "https://unpkg.com/@shgysk8zer0@0.0.5/polyfills/"
  }
}

index.js

import '@scope/package';
import '@shgysk8zer0/polyfills';
import '@shgysk8zer0/polyfills/legacy/object.js'; // -> "https://unpkg.com/@shgysk8zer0@0.0.5/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 imports 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.

1.2.0

10 months ago

1.2.3

6 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.1.2

10 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago