1.0.0 ā€¢ Published 4 years ago

rollup-plugin-entry-code-injector v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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)