前端性能优化-静态资源优化6-JS 模块化

本文地址:  https://www.cnblogs.com/veinyin/p/14274281.html

 

1 CommonJS

同步加载,在浏览器之外为 JS 构建模块生态系统,node.js 就是受它影响,也是同步的

// CommonJS
const xx = require(xxx)
exports = fn() {}
​
// NodeJS
const xx = require(xxx)
modules.exports = xxx

  

2 AMD(异步模块定义)

主要用于浏览器上,如 Require.js,需要用标签引入

3 CMD(通用模块定义)

如 Sea.js

// 方法1
define(function(require, exports, module) {
    // 模块代码
})
​
// 方法2
define('moduleName', ['依赖模块1', 'module2'], function(require, exports, module){
  // 模块代码
})

  

1.6.4 ES6

这个太常用了,importimport,具体查看 MDN

需要 Babel 编译之后,才能保证所有浏览器都支持

 

 

 

原文地址:https://www.cnblogs.com/veinyin/p/14274281.html