Nutella with Pepperoni and Mushrooms
Wondering what’s next for npm?Check out our public roadmap! »

rollup-plugin-entry-code-injector

1.0.0 • Public • Published

npm libera manifesto

rollup-plugin-entry-code-injector

🍣 A Rollup plugin which injects code into the entry modules at build time. Useful for adding polyfills to a legacy bundle, adding an environment configuration, etc.

Requirements

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

Install

Using npm:

npm install rollup-plugin-entry-code-injector --save-dev

Usage

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

import { entryCodeInjector } from 'rollup-plugin-entry-code-injector';
 
export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [entryCodeInjector()]
};

Then call rollup either via the CLI or the API.

Options

prepend

Type: String
Default: null

Code to be injected at the beginning of the entry module. It will be transformed by the rest of plugins provided in the rollup configurations as well.

// rollup.config.js
import { entryCodeInjector } from 'rollup-plugin-entry-code-injector';

export default {
  input: 'main.js',
  output: {
    file: 'legacy.bundle.js',
    format: 'cjs'
  },
  plugins: [
    entryCodeInjector({
      prepend: `
        import 'regenerator-runtime/runtime';
      `
    })
  ]
}

append

Type: String
Default: null

Code to be injected at the end of the entry module. It will be transformed by the rest of plugins provided in the rollup configurations as well.

// rollup.config.js
import { entryCodeInjector } from 'rollup-plugin-entry-code-injector';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'cjs'
  },
  plugins: [
    entryCodeInjector({
      append: `
        /**
         * Write some amazing things to be appended here...
         */
      `
    })
  ]
}

transformer

Type: Function
Default: null

Method to be used to transform the entry module code. This method will receive all the entry code as the first argument and should return a string to be written to the entry module file.

The generated code will be transformed by the rest of plugins provided in the rollup configurations as well.

// rollup.config.js
import { entryCodeInjector } from 'rollup-plugin-entry-code-injector';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'cjs'
  },
  plugins: [
    entryCodeInjector({
      transformer: function(code) {
        /**
         * Do some code processing here...
         */
        return code;
      }
    })
  ]
}

Meta

LICENSE (MIT)

Install

npm i rollup-plugin-entry-code-injector

DownloadsWeekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

10.5 kB

Total Files

8

Last publish

Collaborators

  • avatar