简体   繁体   English

在带有多个文件的ES6模块中创建名称空间。 馊主意?

[英]Creating namespaces in ES6 module with multiple files. Bad idea?

I have a library with multiple files that are currently concatenated into a single file with Gulp. 我有一个库,其中包含多个文件,这些文件当前通过Gulp连接到一个文件中。 Each of these files set a property of the main library object Rune , so color.js will set Rune.Color , math.js will set Rune.Math , and so on. 每个文件都设置了主库对象Rune的属性,因此color.js将设置Rune.Colormath.js将设置Rune.Math ,依此类推。

This is obviously old school, so I want to rewrite it all using ES6 modules, and use Gulp, Browserify and Babel to create the compiled bundle. 这显然是老派了,所以我想使用ES6模块重写所有内容,并使用Gulp,Browserify和Babel创建已编译的包。

However, I will then end up with a compiled library where the namespaces are gone, and people will need to do import color from "rune" to use the color module. 但是,我将最终得到一个编译的库,其中的命名空间已消失,人们将需要import color from "rune"才能使用颜色模块。

So, I was thinking about doing this is my main module file: 所以,我正在考虑做这是我的主要模块文件:

import color from "./color"

const Rune = {
  Color: color
}

export default Rune;

Is that a terrible idea? 那是一个可怕的主意吗? The module is already used in the main file, so I can't really see any downside to it. 该模块已经在主文件中使用,所以我看不到它有任何缺点。

Is that a terrible idea? 那是一个可怕的主意吗?

Having an entry point to your library and exporting all methods from there is not a bad idea. 有一个到您的库的入口点并从那里导出所有方法都不错。 However, I would stick with ES6 imports and exports: 但是,我坚持使用ES6进出口:

// Rune.js
export {default as color} from './color';

Now clients can use: 现在客户可以使用:

import {color} from 'rune';

or 要么

import * as Rune from 'rune';

to import what they need. 导入他们需要的东西。

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

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