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

babel-dual-package

v1.1.3

Published

CLI for building a dual ESM and CJS package with Babel.

Downloads

44

Readme

babel-dual-package

CI codecov NPM version

CLI for building a dual ESM and CJS package with Babel. Takes an ES module and produces a compiled ESM and CJS build using the configuration from your babel.config.json file.

Requirements

  • Node >= 16.19.0.
  • Your package uses "type": "module" in package.json.

Getting Started

First install babel-dual-package:

npm install babel-dual-package

Next write your babel.config.json file and include any plugins or presets your project requires.

{
  "presets": [
    ["@babel/preset-env", {
      "modules": false
    }]
  ]
}

Now run babel-dual-package src to get an ESM and CJS build in a dist directory that can be used as exports in a package.json file.

Run babel-dual-package --help to see a list of more options.

Examples

TypeScript and JSX

If your project is using typescript then add @babel/preset-typescript. If it is also using JSX, then add @babel/preset-react.

babel.config.json

{
  "presets": [
    ["@babel/preset-env", {
      "modules": false
    }],
    "@babel/preset-typescript"
    ["@babel/preset-react", {
      "runtime": "automatic"
    }]
  ]
}

Set the declarationDir for your types to the same value used for --out-dir, or dist (the default) if not using --out-dir.

tsconfig.json

{
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "dist",
    "emitDeclarationOnly": true,
    "isolatedModules": true,
    "strict": true
  },
  "include": ["src"]
}

In order to support typescript, you must pass the --extensions used:

babel-dual-package --out-dir dist --extensions .ts,.tsx src

If everything worked you should get an ESM build in dist and a CJS build in dist/cjs with extensions in filenames and specifiers updated correctly.

Now you can add some scripts to your package.json file to help automate the build during CI/CD.

package.json

  "type": "module",
  "scripts": {
    "build:types": "tsc --emitDeclarationOnly",
    "build:dual": "babel-dual-package --out-dir dist --extensions .ts,.tsx src",
    "build": "npm run build:types && npm run build:dual"
  }

Flat build

Given a directory structure like the following,

app/
  package.json
  src/
    one/
      file1.js
    two/
      file2.js
    file.js

and by using --out-file-extension to make sure each build has unique filenames, you can create a flat build while still supporting conditional exports.

For example, running

babel-dual-package --no-cjs-dir --out-file-extension esm:.esm.js,cjs:.cjs.js src/*.js src/one src/two

Will produce the following build output:

dist/
  file.esm.js
  file.cjs.js
  file1.esm.js,
  file1.cjs.js,
  file2.esm.js,
  file2.cjs.js

Import query parameters

You can run a build by importing this package and passing an args query parameter in the import specifier. Boolean options like --no-cjs-dir should have an empty string as their value (''), and instead of positional arguments, you pass a files array (globs supported).

build.js

await import(
  `./node_modules/.bin/babel-dual-package?args=${encodeURIComponent(
    JSON.stringify({
      '--out-dir': 'dist',
      '--extensions': '.ts',
      '--no-cjs-dir': '',
      files: ['src/*.js']
    })
  )}`
)

Options

There are options that can be passed to provide custom output locations, file extensions, and more.

You can run babel-dual-package --help to get more info. Below is the output of that:

user@comp ~ $ ./node_modules/.bin/babel-dual-package --help
Usage: babel-dual-package [options] <files ...>

Options:
--out-dir [out] 		 Compile the modules in <files ...> into an output directory.
--root-mode [mode] 		 The project-root resolution mode. One of 'root' (the default), 'upward', or 'upward-optional'.
--cjs-dir-name [string] 	 The name of the --out-dir subdirectory to output the CJS build. [cjs]
--extensions [extensions] 	 List of extensions to compile when a directory is part of the <files ...> input. [.js,.jsx,.mjs,.cjs]
--out-file-extension [extmap] 	 Use a specific extension for esm/cjs files. [esm:.js,cjs:.cjs]
--keep-file-extension 		 Preserve the file extensions of the input files.
--no-cjs-dir 			 Do not create a subdirectory for the CJS build in --out-dir.
--no-comments 			 Remove comments from generated code.
--source-maps 			 Generate an external source map.
--minified  			 Save as many bytes when printing (false by default).
--copy-files 			 When compiling a directory copy over non-compilable files.
--help 				 Output usage information (this information).

Questions

Q Can I use "type": "commonjs" in my package.json file, or just not include the field?

A No. Converting from CJS to ESM is a codemod, not transpiling via Babel.