Releasing es6 and es5 sources with npm

2020-07-16 08:19发布

问题:

I am developing two node packages registered with npm, and using es6 syntax. Each package has only one es6 module exporting a single class.

  • package A has no dependencies
  • package B depends on A

Class A

export default class A {...}

Class B

import A from 'A'

export default class B {...}

Each package has the following structure modules/ index.js (es6 source) index.js (commonjs source)

  • Source code is in es6/index.js
  • It is transpiled to es5 / commonjs using Babel

Question

I want to give users of my A and B packages the choice to use es6 (by importing B class which itself would import A es6 class and not es5 A) or es5 sources (by requiring es5 function B which itself requires es5 function A): What is the best way to achieve it?

回答1:

What I would do is to have two separate releases (two separate github projects)

  1. One that has es5 as minimum requirement (with es5 A and es5 B as transpiled code).
  2. Another one that has es6 as minimum requirement (with modules es6 A and es6B).

Reason for this is that, presently, best is to avoid to confuse people who have not yet made the transition to es6 with es6 files. In the future, within a year or two, the es5 version would become unnecessary. You want to be able to discontinue support for the es5 version without dramatically changing the file organisation.

In the es6 release, consider naming your files index.es6.js. This will help prevent accidental reference to es6 code in an environment that has no support for it.