npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-plugin-bundle-imports

v1.5.1

Published

Bundle imports separately and use the result as a file path or a string of code.

Downloads

1,355

Readme

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)],
}