GithubHelp home page GithubHelp logo

n0th1ng-else / dual-publish Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ai/dual-publish

0.0 0.0 0.0 1.16 MB

Publish JS project as dual ES modules and CommonJS package to npm

License: MIT License

JavaScript 100.00%

dual-publish's Introduction

Dual Publish

Publish JS project as dual ES modules and CommonJS package to npm.

  • Tested on projects with 16M downloads per month.
  • Works with Node.js, browsers, React Native, and bundlers like webpack or Parcel.
  • No build step. No need for separated src/ and dist/ dirs in repository. You will be able to test branch by installing version from GitHub like npm i example@you/example#fix.
  • Keep sources readable.
  • Multiple files support. Your user will be able to import separated files like import { nanoid } from 'nanoid/async'.
  • Cleans npm package from development configs before publishing.

You write CommonJS in your npm library sources:

// index.js
module.exports = { lib }

npx dual-publish compiles your library during publishing to npm:

// index.js
export { lib }

// index.cjs
module.exports = { lib }

// package.json
{
  
  "type": "module",
  "module": "index.js",
  "main": "index.cjs",
  "exports": {
    "require": "./index.cjs",
    "import": "./index.js"
  }
}

Now your library can be imported natively as ESM or CommonJS:

// CommonJS
let { lib } = require('lib')

// ESM in Node.js, webpack, Parcel, and Rollup
import { lib } from 'lib'

// ESM in browser
import { lib } from 'https://cdn.jsdelivr.net/npm/lib/index.js'
Sponsored by Evil Martians

Limits

  • We recommend to avoid default export because of bug in webpack.
  • Every JS file should be in own dir. lib/index.js instead of lib.js. We need it to put package.json with module.

Usage

  1. Take a normal CommonJS project with require() and module.exports =.

  2. Because of bug in webpack we recommend to use only named exports:

    const NAME = 'a'
    function change {
      
    }
    
    module.exports = { NAME, change }
  3. Move all files into separated dirs. Rename lib.js to lib/index.js. Old require('./lib') will work.

  4. Add dual-publish to the project:

    npm i --save-dev dual-publish
  5. Test the result by calling npx dual-publish --check. It will create a folder in your project with converted files. Review them manually.

  6. Publish your project with npx dual-publish instead of npm publish.

    npx dual-publish

dual-publish's People

Contributors

ai avatar vankop avatar alik0211 avatar farwayer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.