以npm发布es6和es5来源 [英] Releasing es6 and es5 sources with npm

查看:289
本文介绍了以npm发布es6和es5来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用npm注册的两个节点包,并使用es6语法。
每个包只有一个es6模块导出一个类。




  • A 没有依赖关系

  • B 取决于 A li>


A类

  export default class A {...} 

B类

 从'A'导入A 

导出默认类B {...}

每个包都有以下结构

modules /
index.js(es6源)
index.js(commonjs源)




  • 源代码位于 es6 / index.js

  • 使用Babel
  • 将其折叠为es5 / commonjs


问题



我想给我的A和B包选择使用es6(通过导入B类本身将导入一个es6类而不是es5 A)或es5源s(通过要求es5函数B本身需要es5函数A):什么是最好的方法来实现?

解决方案

我会做的是两个单独的发布(两个单独的github项目)


  1. 一个具有es5作为最低要求(使用es5 A和es5 B作为折叠代码)。

  2. 另一个具有es6作为最低要求(具有模块es6 A和es6B)。这个原因是,目前最好是避免将还没有过渡到es6的人与es6文件混淆在一起。将来,在一两年内,es5版本将变得不必要。您希望能够停止对es5版本的支持,而不会显着更改文件组织。



    在es6版本中,考虑将文件命名为index.es6.js。这将有助于防止在不支持es6代码的情况下意外引用es6。


    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?

    解决方案

    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.

    这篇关于以npm发布es6和es5来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆