Recursively publish ESM packages as CommonJS!

Overview

Commonify.js

For us who are still relying on CommonJS, or using Electron which does not support ESM.

πŸ†• See also build-electron

I made this tool that makes it easy to:

  • Pull any ESM package from npm
  • Transpile it from ESM to CommonJS using babel
  • Publish it under a custom npm scope
  • Recursively do the same for all dependencies

It is mostly an experiment, and it feels like a step back in terms of the ecosystem moving to ESM, so if anyone has any other great ideas, please submit an issue! I have thought about the possibility of doing something similar to the node_modules folder in an npm postinstall script.

Usage

First create your scope here: https://www.npmjs.com/org/create

git clone https://github.com/mifi/commonify.git
cd commonify

yarn
node cli.mjs lowdb 3.0.0 myscope

This will create the package @myscope/lowdb and its dependencies ready to publish to npm.

Example packages

I will publish commonified packages to the @commonify scope:

  • @commonify/lowdb
  • @commonify/steno
  • @commonify/execa
  • @commonify/strip-final-newline
  • @commonify/onetime
  • @commonify/mimic-fn
  • @commonify/npm-run-path
  • @commonify/path-key
  • @commonify/is-stream

Todo

  • Support already scoped packages
  • Must support dual modules (they also have "type": "module" set). Example: https://github.com/yargs/yargs-parser/blob/main/package.json
  • Automate when upstream package version updates
  • Auto-generate a list of versions and their version corresponding to upstream version
You might also like...

Node module for synchronously and recursively merging multiple folders or collections of files into one folder.

merge-dirs Node module for synchronously and recursively merging multiple folders or collections of files into one folder. Install yarn add @nooooooom

Mar 20, 2022

Node.js ESM loader for chaining multiple custom loaders.

ESMultiloader Node.js ESM loader for chaining multiple custom loaders. Fast and lightweight No configuration required, but configurable if needed Usag

Sep 12, 2022

CLI utility that parses argv, loads your specified file, and passes the parsed argv into your file's exported function. Supports ESM/TypeScript/etc out of the box.

cleffa CLI tool that: Parses argv into an object (of command-line flags) and an array of positional arguments Loads a function from the specified file

Mar 6, 2022

Node.js loader for compiling TypeScript modules to ESM

esm-loader Node.js import hook to instantaneously transform TypeScript to ESM on demand using esbuild. Features Transforms TypeScript to ESM on demand

Jan 4, 2023

testing rollup dist for cjs/esm

std.module.format version 0.1.3 std.module.format Overview TLDR Avoid Default Exports and Prefer Named Exports Context Summary Decision ECMAScript Mod

Dec 11, 2022

This React-Based WebPage allows the client/user system to create their own blog, where users can publish their own opinions.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Jul 28, 2022

Like Obsidian Publish but for self-hosting. Plugin integrations for dataview, admonition, and more.

Obsidian Export Obsidian Publish is great but lacks support for many of the plugins we Obsidian addicts have grown accustomed to β€” in particular Datav

Nov 28, 2022

Strapi V4 Plugin to schedule publish and depublish actions

Strapi plugin scheduler This plugin allows you to publish and depublish collection types in the future. There are a couple of steps necessary to get t

Nov 24, 2022

πŸš€ A GitHub action to publish a new release of the repository

Create a JavaScript Action using TypeScript Use this template to bootstrap the creation of a TypeScript action. πŸš€ This template includes compilation

Nov 1, 2022
Comments
  • Can't be imported from CommonJS and/or instructions not clear?

    Can't be imported from CommonJS and/or instructions not clear?

    This is a really neat idea! However, my first attempt at forking a package and republishing it (by following the instructions in the README.md) resulted in the following error when I tried to run my CJS project using the package I just forked:

    /node_modules/@vallerance/globby/index.js:1
    import fs from 'node:fs';
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
        at Object.compileFunction (node:vm:352:18)
        at wrapSafe (node:internal/modules/cjs/loader:1031:15)
        at Module._compile (node:internal/modules/cjs/loader:1065:27)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at Module.require (node:internal/modules/cjs/loader:1005:19)
        at require (node:internal/modules/cjs/helpers:102:18)
        at Object.@vallerance/globby (/home/joshua/WebDev/projects/no-code/dist/packages/tsc-compilers/main.js:606:18)
        at __webpack_require__ (/home/joshua/WebDev/projects/no-code/dist/packages/tsc-compilers/main.js:672:41)
    

    This was because index.js had ESM style code.

    I figured out I could solve this by doing the following for each forked package:

    cd package/
    cp ../transpiled ./
    

    This replaced the ESM style code with CJS style code. From there, I could simply increment the version and republish. This resolved the error I was getting from my project and it ran fine with the forked packages.

    I'm not sure if this is a bug in this project somewhere, or if maybe the instructions weren't clear (or maybe missing a step).

    opened by JoshuaCWebDeveloper 1
  • you did replicate something existing.

    you did replicate something existing.

    https://www.npmjs.com/package/@vercel/ncc

    is pritty capable of solving your cjs versions generation on a project base you will never want to publish the CJS dependencies you should adopt the following pattern.

    you create so called dev bundels of your dependencies so your dependencies get a own package.json and bundler config then you run that only when you add or remove external dependencies to your project.

    then you use the resulting bundle in your main project. and also commit that to the main project as it is a required dependencie

    this is called a dev-bundle.

    Short version

    You should avoid publishing your own CJS dependencies as packages. you should bundle them and commit that bundle to your main project and create a indipendent package or project to build only your external dependencies as CJS modules.

    opened by frank-dspeed 0
  • Idea for handling automatic updates

    Idea for handling automatic updates

    Maybe projects that have been run through commonify could make use of something like GitHub Actions to run a cron job automatically to find new versions, commonify them, and publish them. Mainly submitting this issue for feedback on the idea, if people think it's a good idea I can probably submit a PR sometime soon adding this.

    opened by RDIL 0
Owner
Mikael Finstad
App developer πŸ‡³πŸ‡΄ I code JS. Creator of LosslessCut
Mikael Finstad
Node.js loader for compiling ESM & TypeScript modules to CommonJS

cjs-loader Node.js require() hook to instantaneously transform ESM & TypeScript to CommonJS on demand using esbuild. Features Transforms ESM & TypeScr

esbuild kit 40 Dec 13, 2022
Universal importer for CommonJS and ESM in Node.js

ModuleImporter by Nicholas C. Zakas If you find this useful, please consider supporting my work with a donation. Description A utility for seamlessly

Human Who Codes 18 Dec 2, 2022
πŸ“¦ 🍣 Zero-config JS bundler for ESM, CommonJS, and .d.ts outputs. (Forked from pkgroll)

?? ?? puild (A fork of pkgroll) Write your code in ESM & TypeScript and bundle it to get ESM, CommonJS, and type declaration outputs with a single com

ʀᴀʏ 6 Sep 6, 2022
Minimal utility to convert to or from any timezone. Deno/Node/Browser. ESM/CommonJS.

minitz Features Convert dates between any timezone supported by the system. Parses ISO8601 time strings. MIT licensed, use the library any way you wan

Hexagon 14 Oct 10, 2022
An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

Snyk Labs 57 Dec 28, 2022
utility library for promise, support both commonjs and ESM

promising-utils A utility library for promise, supports both commonjs and ESM npm install promising-utils --save yarn add promising-utils wait Used wh

Qiang Li 4 Oct 18, 2022
Babel plugin and helper functions for interoperation between Node.js native ESM and Babel ESM

babel-plugin-node-cjs-interop and node-cjs-interop: fix the default import interoperability issue in Node.js The problem to solve Consider the followi

Masaki Hara 15 Nov 6, 2022
Easily publish notes to the web This plugin integrates with obsius.site to publish markdown notes on the web.

Obsius Publish Easily publish notes to the web This plugin integrates with obsius.site to publish markdown notes on the web. Limitations The type of c

Jon Grythe StΓΈdle 66 Dec 20, 2022
Micro.publish is an Obsidian plugin to publish notes directly to Micro.blog, written in TypeScript

Micro.publish Micro.publish is a community maintained plugin for Obsidian to publish notes to a Micro.blog blog. Installing This plugin will be availa

Otavio Cordeiro 14 Dec 9, 2022
Semantic Release plugin to create and publish NuGet packages.

semantic-release-nuget semantic-release plugin to create and publish a NuGet package. Step Description verifyConditions Verify the presence of the NUG

DroidSolutions 6 Jan 2, 2023