Skip to content

jacksteamdev/rollup-plugin-bundle-imports

Repository files navigation

rollup-plugin-bundle-imports logo

rollup-plugin-bundle-imports

npm (scoped) GitHub last commit License TypeScript Declarations Included

Fiverr: We make Chrome extensions ko-fi


Bundle imports separately and use the result as a file path or a string of code. Tested to work recursively or as multiple plugin instances with different options.

If you are coming here from rollup-plugin-code-string, the API has become more robust, but the defaults will work the same!

Table of Contents

Getting Started

This is a Rollup plugin, so your project will need to be up and running with Rollup.

Installation

$ npm i rollup-plugin-bundle-imports -D

Usage

// rollup.config.js

import { bundleImports } from 'rollup-plugin-bundle-imports'

export default {
  input: 'index.js',
  output: {
    file: 'dist/bundle-esm.js',
    format: 'esm',
  }
  plugins: [bundleImports()],
}

Default is to import a module that ends in .code.js as a string.

import code from './script.code.js'

Bundle A Service Worker

Use options.importAs to bundle an imported module and emit it as an asset file. The imported value will be the file path to the asset.

// rollup.config.js

import { bundleImports } from 'rollup-plugin-bundle-imports'

export default {
  input: 'register-service-worker.js',
  plugins: [
    bundleImports({
      include: ['**/my-sw.js'],
      // Import as path to bundle
      importAs: 'path',
    }),
  ],
}
// register-service-worker.js

import swPath from './my-sw.js'

navigator.serviceWorker.register(swPath)

Bundle A Chrome Extension Content Script

Bundle a content script to a code string to inject from the background page of a Web or Chrome extension.

// rollup.config.js

import { bundleImports } from 'rollup-plugin-bundle-imports'

export default {
  input: 'background.js',
  plugins: [
    bundleImports({
      include: ['**/content.js', '**/inject.js'],
      // Import as code string to bundle
      importAs: 'code',
    }),
  ],
}
// background.js

import code from './content.js'

// Inject the bundled code to the active tab
browser.tabs.executeScript({ code })

Recursive Usage

Inject the bundled code from the content script of the previous example into the page runtime through a script tag.

// content.js

import code from './inject.js'

const script = document.createElement('script')
script.text = code

document.head.append(script)
script.remove()

Import As Both Code And Paths

If you want to import some files as code and others as file paths, just create two plugins with different settings!

Both plugin instances will work recursively with each other, so you can import a path into a code string, or import a code string into an asset and import the asset path into your entry bundle.

// rollup.config.js

import { bundleImports } from 'rollup-plugin-bundle-imports'

export default {
  input: 'index.js',
  plugins: [
    bundleImports({
      include: ['**/my-sw.js'],
      // Import as path to bundle
      importAs: 'path',
    }),
    bundleImports({
      include: ['**/content.js', '**/inject.js'],
      // Import as code string to bundle
      importAs: 'code',
    }),
  ],
}

Features

Works With TypeScript

TypeScript definitions are included, so no need to install an additional @types library!

Options API

[include]

Type: string[]

Glob patterns to for module names to include.

bundleImports({
  // Include files that end in `.code.js`
  include: ['**/*.code.js'],
})

[exclude]

Type: string[]

Glob patterns to for module names to include.

bundleImports({
  // Exclude files that end in `.code.js`
  include: ['**/*.code.js'],
  // Except for this one...
  exclude: ['src/not-me.code.js'],
})

[importAs]

Type: "path" | "code"

Use "code" to bundle the module and import it as a string.

bundleImports({
  importAs: 'path',
})

Use "path" to emit the module as a file and import the file path as a string. This works well for service workers, for example.

bundleImports({
  importAs: 'path',
})

[options]

Type: string[]

rollup-plugin-bundle-imports bundles the module into an IIFE, and uses the plugins array defined in your Rollup input options by default.

If you need to use other plugins or plugin settings for bundled imports, this is the place. You can set any of the Rollup input options in options.

The properties options.file and options.output will be ignored.

Note that most libraries expect to be bundled into a UMD or IIFE, so using options.format to create an ES2015 module may cause unexpected results.

bundleImports({
  options: {
    // Bundle the module as an ESM2015 module
    format: 'esm',
    // Use a different set of plugins
    plugins: [resolve(), commonjs()],
  },
})

Default options

// rollup.config.js

import { rollup } from 'rollup'
import bundle from 'rollup-plugin-bundle-imports'

// These are the default options
const options = {
  include: ['**/*.code.js'],
  importAs: 'code',
  // Rollup input options for the imported module
  options: {
    plugins: [resolve(), commonjs()],
    output: {
      format: 'iife',
      preferConst: true,
    },
  },
}

export default {
  input: 'index.js',
  output: {
    file: 'dist/bundle-esm.js',
    format: 'esm',
  }
  plugins: [bundleImports()],
  // This is the same as above
  plugins: [bundleImports(options)],
}

Releases

No releases published

Packages

No packages published