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-conditional-import

v1.1.0

Published

A Rollup plugin that allows you to conditionally import modules (CJS) based on environment variable

Downloads

16

Readme

npm size libera manifesto

rollup-plugin-conditional-import

🍣 A Rollup plugin that allows you to conditionally import modules (CJS) based on environment variables or other conditions. This can be useful for including different code in production and development builds, reducing the bundle size.

How it works?

This plugin works by analyzing the AST of the input files and removing the import statements that are not necessary based on the conditions you set. This way, you can include different modules in the output based on the environment variables or other conditions you set.

The motivation behind this plugin is a problem with react and jsx-runtime, where the code generated is:


function requireReactJsxRuntime_production_min() {
  // Bunch of code
}

function requireReactJsxRuntime_development() {
  // Bunch of code
}

if ("production" === 'production') {
    jsxRuntime.exports = requireReactJsxRuntime_production_min();
} else {
    jsxRuntime.exports = requireReactJsxRuntime_development();
}
var jsxRuntimeExports = jsxRuntime.exports;

The mission is to remove the requireReactJsxRuntime_development function and the if statement, and only include the requireReactJsxRuntime_production_min function in the output or vice versa according to the environment variable.

With the rollup-plugin-conditional-import plugin, you can achieve this, generating a smaller bundle:

var reactJsxRuntime_production_min = {};

var f$1 = // Only necessary code for production

function q(c, a, g) {
  // Necessary code only for production
}

reactJsxRuntime_production_min.Fragment = l$2;
reactJsxRuntime_production_min.jsx = q;
reactJsxRuntime_production_min.jsxs = q;

var jsxRuntime = reactJsxRuntime_production_min;

And the same for the development environment.

Requirements

This plugin requires Rollup v3.0.0+ or v4.0.0+.

Install

Using npm:

npm i -D rollup-plugin-conditional-import

Or using pnpm:

pnpm i -D rollup-plugin-conditional-import

Usage

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

import conditionalImport from 'rollup-plugin-conditional-import';

export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs'
  },
  plugins: [conditionalImport()]
};

Then call rollup either via the CLI or the API.

Example

Consider the following code:

// index.js
if (process.env.NODE_ENV === 'production') {
  require('./prodModule.js');
} else {
  require('./devModule.js');
}

With the configuration above, Rollup will bundle both prodModule.js and devModule.js into the output. However, if you use the plugin and set NODE_ENV to development, only devModule.js will be included in the output, this way reducing the bundle size:

// bundle.js
require('./devModule.js');

Or, if NODE_ENV is set to production:

// bundle.js
require('./prodModule.js');

And the commonjs plugin will take care of the rest, compiling only the necessary code, and not both modules (development and production).

Options

env

Type: string Default: process.env.NODE_ENV

By default, the plugin will use the NODE_ENV environment variable to determine the environment. You can override this by setting the env option to a different environment variable.

exclude

Type: string | string[] Default: ["**/*.!(js|jsx)"]

A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default, all files that are not JavaScript/JSX files are ignored. See also the include option.

include

Type: string | string[] Default: null

A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on.

Meta

LICENSE (MIT)