swc-plugin-cjs2esm
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

swc-plugin-cjs2esm

SWC plugin to convert cjs to esm [Experiment]

This plugin is is written as a part of rollup-plugin-swc-core.

This is an experiment to convert CommonJS module to ESM module. It is slippery slope with many gotchas but the intention is to make it work for day-to-day used packages.

It is using multi-pass approche, traversing AST multiple times, may not good for performance. The initial attempt is to make it work and not focussing on performance, code quality etc.

The Rust based plugin system for SWC is evolving, once stable, it can be re-written in Rust for better perf.

Usage

npm i swc-plugin-cjs2esm -D
const swc = require("@swc/core");
const { createCsm2MjsPlugin } = require("swc-plugin-cjs2esm");

swc
  .transform("common js source code", {
    filename,
    jsc: {
      parser: {
        syntax: "ecmascript",
      },
      transform: {},
    },
    plugin: createCsm2MjsPlugin({
      replace: {
        "process.env.NODE_ENV": JSON.stringify("production"),
      },
    }),
  })
  .then(({ code }) => {
    console.log(code);
  });

/swc-plugin-cjs2esm/

    Package Sidebar

    Install

    npm i swc-plugin-cjs2esm

    Weekly Downloads

    0

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    57.5 kB

    Total Files

    32

    Last publish

    Collaborators

    • mohdovais