1. @rollup/plugin-inject
Scan modules for global variables and injects `import` statements where necessary
@rollup/plugin-inject
Package: @rollup/plugin-inject
Created by: rollup
Last modified: Sun, 15 Oct 2023 15:47:01 GMT
Version: 5.0.5
License: MIT
Downloads: 3,416,969
Repository: https://github.com/rollup/plugins

Install

npm install @rollup/plugin-inject
yarn add @rollup/plugin-inject

npm
size
libera manifesto

@rollup/plugin-inject

🍣 A Rollup plugin which scans modules for global variables and injects import statements where necessary.

Requirements

This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.

Install

Using npm:

 npm install @rollup/plugin-inject --save-dev

Usage

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

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

export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [
    inject({
      Promise: ['es6-promise', 'Promise']
    })
  ]
};

Then call rollup either via the CLI or the API.

This configuration above will scan all your files for global Promise usage and plugin will add import to desired module (import { Promise } from 'es6-promise' in this case).

Examples:

 {
  // import { Promise } from 'es6-promise'
  Promise: [ 'es6-promise', 'Promise' ],

  // import { Promise as P } from 'es6-promise'
  P: [ 'es6-promise', 'Promise' ],

  // import $ from 'jquery'
  $: 'jquery',

  // import * as fs from 'fs'
  fs: [ 'fs', '*' ],

  // use a local module instead of a third-party one
  'Object.assign': path.resolve( 'src/helpers/object-assign.js' ),
}

Typically, @rollup/plugin-inject should be placed in plugins before other plugins so that they may apply optimizations, such as dead code removal.

Options

In addition to the properties and values specified for injecting, users may also specify the options below.

exclude

Type: String | Array[...String]

Default: null

A picomatch 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 picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

Meta

CONTRIBUTING

LICENSE (MIT)

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin