在应用中一直对es6 和 commonjs 中的模块管理没有明确的界限和区别一直是混这用,感觉有点乱,下面对两个标准的模块管理进行对比和学习

ES6 模块管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// --------  lib.js ------
export function a(){} //Named exports 命名导出
export default {b:'v'}
// ------- main.js --------
import lib,{a} from 'lib' //引入方式一 lib 里包含的是 export default 内容 a 为lib.js 命名方式导出 的 a 方法
import * as lib fron 'lib' //引入方式二 lib 里包含所有的 lib.js 中export 的方法 和 export default 里的内容

//更多的导入到处方法
export * from 'src/other_module';
import 'lib' //只加载模块不 import 任何东西
export { foo, bar } from 'src/other_module';
export { foo as myFoo, bar } from 'src/other_module';

System.import('some_module')
.then(some_module => {
// Use some_module
})
.catch(error => {
...
});

commonjs 模块管理

1
2
3
4
5
6

// ---- lib.js ----
module.exports ={} //导出方式 一
exports.a = function(){} //到处方式二
// --- main.js ----
var lib = require('lib') //引入方式