Skip to content

mmirca/rollup-plugin-entry-code-injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

A rollup plugin that injects code in the entry file. Can be used for legacy builds where you might want to add polyfills to the bundle.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors