简体   繁体   English

如何发布具有 commonjs 和 es6 版本的 NPM 模块?

[英]How can I publish an NPM module with both commonjs and es6 versions?

I have a module I want to publish to npm.我有一个模块要发布到 npm。 I have found some "solutions" that are 4+ years old, examples using babel 5.x, and other problems that made the examples not work as shown.我发现了一些 4 年以上的“解决方案”,使用 babel 5.x 的示例,以及其他使示例无法正常工作的问题。

Ideally I want to write my code using es6 and build/transpile with babel such that it can be imported with require() in scripts or import in modules.理想情况下,我想使用 es6 编写代码并使用 babel 构建/转换,以便可以在脚本中使用require() import或在模块中导入。

Right now, here's my (sample) module that shows what I've tried.现在,这是我的(示例)模块,它显示了我尝试过的内容。

// index.js
export default function speak () {
  console.log("Hello, World!");
}
// .babelrc
{
  "comments":false,
  "presets": [
      ["@babel/env", {"modules": "commonjs"}]
  ],
  "plugins": [
    "@babel/plugin-transform-modules-commonjs",
    "add-module-exports"
  ]
}
// package.json
{
  "name": "foo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "babel index.js -d dist"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/core": "^7.10.5",
    "@babel/plugin-transform-modules-commonjs": "^7.10.4",
    "@babel/preset-env": "^7.10.4",
    "babel-plugin-add-module-exports": "^1.0.2"
  }
}

And, finally, a demo script (demo.js)最后,一个演示脚本 (demo.js)

// demo.js
const speak = require('./dist/index.js');
speak();

When I do npm run build and then node demo.js , it works as expected:当我执行npm run build然后node demo.js ,它按预期工作:

Hello, World!

I would also like to be able to have a module (add "type":"module" to package.json) and then use a demo.js file this way:我还希望能够拥有一个模块(将"type":"module"添加到 package.json),然后以这种方式使用 demo.js 文件:

import speak from './dist/index.js';
speak();

However, I get this an error that a default export isn't available:但是,我收到默认导出不可用的错误消息:

import speak from './dist/index.js';
       ^^^^^
SyntaxError: The requested module './dist/index.js' does not provide an export named 'default'

I don't really care what the answer is, I'd just like to know what the best practices are.我真的不在乎答案是什么,我只想知道最佳实践是什么。 Should I just export as ES6?我应该只导出为 ES6 吗? Should I just require commonjs?我应该只需要commonjs吗? Is there a way of transpiling with two available targets?有没有办法用两个可用目标进行转换?

Note:笔记:

  • node v14.5.0节点 v14.5.0
  • npm v6.14.6 npm v6.14.6
  • @babel/core v7.10.5 @babel/核心 v7.10.5

You can use a bundler like webpack or rollup in combination with babel .您可以使用webpack类的捆绑程序,或者将rollupbabel结合使用。 They provide options to compile to multiple targets.它们提供了编译到多个目标的选项。 Normally any library code is compiled to below targets:通常,任何库代码都会编译为以下目标:

  1. ESM or MJS (Ecmascript modules) ESM 或 MJS(Ecmascript 模块)
  2. UMD (Universal Modules) UMD(通用模块)

You can also compile to CJS (CommonJS module) or IIFE (Immediately invoked function expression).您还可以编译为 CJS(CommonJS 模块)或 IIFE(立即调用 function 表达式)。

UMD and ESM are pretty much standard these days (esp. UMD because it is combination of IIFE, CJS and AMD).如今,UMD 和 ESM 几乎是标准的(尤其是 UMD,因为它是 IIFE、CJS 和 AMD 的组合)。

You can explore official documentation for Webpack or Rollup.您可以浏览 Webpack 或 Rollup 的官方文档。 However, I have created a tool which you can use to achieve the output you are looking for.但是,我创建了一个工具,您可以使用它来实现您正在寻找的 output。 https://www.npmjs.com/package/rollup-boilerplate https://www.npmjs.com/package/rollup-boilerplate

You can check it out, play around with it, hack it.你可以检查它,玩弄它,破解它。 I think it can be good starting point.我认为这可能是一个很好的起点。 You can checkout this article to get started: https://medium.com/@contactsachinsingh/setting-up-a-rollup-project-under-two-minutes-fc838be02d0e您可以查看这篇文章以开始: https://medium.com/@contactsachinsingh/setting-up-a-rollup-project-under-two-minutes-fc838be02d0e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM