Giter Site home page Giter Site logo

shgysk8zer0 / rollup-import Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 718 KB

A RollUp plugin for importing modules

Home Page: https://www.npmjs.com/package/@shgysk8zer0/rollup-import

License: MIT License

JavaScript 100.00%
import-map node rollup-plugin

rollup-import's Introduction

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.