How to publish a module written in ES6 to NPM?

前端 未结 11 662
自闭症患者
自闭症患者 2020-11-29 15:22

I was about to publish a module to NPM, when I thought about rewriting it in ES6, to both future-proof it, and learn ES6. I\'ve used Babel to transpile to ES5, and run tests

11条回答
  •  我在风中等你
    2020-11-29 15:56

    Following José and Marius's approach, (with update of Babel's latest version in 2019): Keep the latest JavaScript files in a src directory, and build with npm's prepublish script and output to the lib directory.

    .npmignore

    /src
    

    .gitignore

    /lib
    /node_modules
    

    Install Babel (version 7.5.5 in my case)

    $ npm install @babel/core @babel/cli @babel/preset-env --save-dev
    

    package.json

    {
      "name": "latest-js-to-npm",
      "version": "1.0.0",
      "description": "Keep the latest JavaScript files in a src directory and build with npm's prepublish script and output to the lib directory.",
      "main": "lib/index.js",
      "scripts": {
        "prepublish": "babel src -d lib"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@babel/cli": "^7.5.5",
        "@babel/core": "^7.5.5",
        "@babel/preset-env": "^7.5.5"
      },
      "babel": {
        "presets": [
          "@babel/preset-env"
        ]
      }
    }
    

    And I have src/index.js which uses the arrow function:

    "use strict";
    
    let NewOneWithParameters = (a, b) => {
      console.log(a + b); // 30
    };
    NewOneWithParameters(10, 20);
    

    Here is the repo on GitHub.

    Now you can publish the package:

    $ npm publish
    ...
    > latest-js-to-npm@1.0.0 prepublish .
    > babel src -d lib
    
    Successfully compiled 1 file with Babel.
    ...
    

    Before the package is published to npm, you will see that lib/index.js has been generated, which is transpiled to es5:

    "use strict";
    
    var NewOneWithParameters = function NewOneWithParameters(a, b) {
      console.log(a + b); // 30
    };
    
    NewOneWithParameters(10, 20);
    

    [Update for Rollup bundler]

    As asked by @kyw, how would you integrate Rollup bundler?

    First, install rollup and rollup-plugin-babel

    npm install -D rollup rollup-plugin-babel
    

    Second, create rollup.config.js in the project root directory

    import babel from "rollup-plugin-babel";
    
    export default {
      input: "./src/index.js",
      output: {
        file: "./lib/index.js",
        format: "cjs",
        name: "bundle"
      },
      plugins: [
        babel({
          exclude: "node_modules/**"
        })
      ]
    };
    

    Lastly, update prepublish in package.json

    {
      ...
      "scripts": {
        "prepublish": "rollup -c"
      },
      ...
    }
    

    Now you can run npm publish, and before the package is published to npm, you will see that lib/index.js has been generated, which is transpiled to es5:

    'use strict';
    
    var NewOneWithParameters = function NewOneWithParameters(a, b) {
      console.log(a + b); // 30
    };
    
    NewOneWithParameters(10, 20);
    

    Note: by the way, you no longer need @babel/cli if you are using the Rollup bundler. You can safely uninstall it:

    npm uninstall @babel/cli
    

提交回复
热议问题