AMD 文件类型编译(上)
前言
一般项目中的js文件都有IIFE
, AMD
, CommonJS
,UMD
,四种模块化格式,具体的解释如下
IIFE
Imdiately Invoked Function Expression 立即执行函数AMD
Asynchronous Module Definition 异步模块规范CommonJS
CommonJS规范,是Node.js的官方模块化规范UMD
, Universal Module Definition 通用模块规范
本篇主要讲述 AMD
编译的配置,AMD
规范就是将模块定义注册到缓存中,通过沙箱化和模块化去使用和执行。
定义模块
defie('module-01', [], function() {
return {
init(){
console.log('hello')
}
}
})
自执行模块
define(['module-01'], function(mod1){
mod1.init();
})
但是有了 ES6+ 语法后,之前的模块化实现可以基于ES6+语法,编写,最后的模块编译结果由webpack
或者rollup.js
等去处理,本篇就是讲述如何用rollup.js
去将 ES6+语法编译成 amd
模式
实现例子
demo例子
https://github.com/chenshenhai/rollupjs-note/blob/master/demo/chapter-02-02-01/
npm i
npm run build
## 访问连接 http://127.0.0.1:3000/example/index.html
实现步骤
步骤1: 目录和准备
.
├── build ## rollup.js 编译脚本
│ ├── rollup.config.dev.js
│ ├── rollup.config.js
│ └── rollup.config.prod.js
├── dist ## 编译结果
│ ├── index.js
│ └── index.js.map
├── example ## 例子
│ ├── index.html
│ ├── index.js
│ └── main.js
├── package.json
└── src ## 源码
├── index.js
└── lib
└── demo.js
安装对应编译的npm模块
## 安装 rollup.js 基础模块
npm i --save-dev rollup
## 安装 rollup.js 编译本地开发服务插件
npm i --save-dev rollup-plugin-serve
## 安装 rollup.js 编译代码混淆插件
npm i --save-dev rollup-plugin-uglify
## 安装 rollup.js 编译ES6+的 babel 模块
npm i --save-dev @rollup/plugin-babel @babel/core @babel/preset-env
步骤2: rollup配置
- 编译基本配置
./build/rollup.config.js
const path = require('path');
const { babel } = require('@rollup/plugin-babel');
const resolveFile = function(filePath) {
return path.join(__dirname, '..', filePath)
}
const babelOptions = {
"presets": ['@babel/preset-env'],
}
module.exports = [
{
input: resolveFile('src/index.js'),
output: {
file: resolveFile('dist/index.js'),
format: 'amd',
},
plugins: [
babel(babelOptions),
],
},
]
开发模式
配置基本./build/rollup.config.dev.js
const path = require('path');
const serve = require('rollup-plugin-serve');
const configList = require('./rollup.config');
const resolveFile = function(filePath) {
return path.join(__dirname, '..', filePath)
}
const PORT = 3000;
const devSite = `http://127.0.0.1:${PORT}`;
const devPath = path.join('example', 'index.html');
const devUrl = `${devSite}/${devPath}`;
setTimeout(()=>{
console.log(`[dev]: ${devUrl}`)
}, 1000);
configList.map((config, index) => {
config.output.sourcemap = true;
if( index === 0 ) {
config.plugins = [
...config.plugins,
...[
serve({
port: PORT,
contentBase: [resolveFile('')]
})
]
]
}
return config;
})
module.exports = configList;
生产模式
配置基本./build/rollup.config.prod.js
const { uglify } = require('rollup-plugin-uglify');
const configList = require('./rollup.config');
const resolveFile = function(filePath) {
return path.join(__dirname, '..', filePath)
}
configList.map((config, index) => {
config.output.sourcemap = false;
config.plugins = [
...config.plugins,
...[
uglify()
]
]
return config;
})
module.exports = configList;
- 在
./package.json
配置编译执行脚本{ "scripts": { "build": "node_modules/.bin/rollup -c ./build/rollup.config.prod.js", "dev": "node_modules/.bin/rollup -w -c ./build/rollup.config.dev.js" }, }
步骤3: 待编译ES6源码
./src/index.js
源码内容
import demo from './lib/demo';
export default {
init() {
const arr1 = [1,2,3];
const arr2 = [4,5,6];
console.log([...arr1, ...arr2]);
async function initDemo () {
let data = await demo();
console.log(data);
}
initDemo();
}
}
./src/lib/demo.js
源码内容
function demo() {
return new Promise((resolve, reject) => {
try {
setTimeout(()=>{
const obj1 = {a:1};
const obj2 = {b:2};
const obj3 = {c:3};
const obj4 = {d:4};
const result = {...obj1, ...obj2, ...obj3, ...obj4};
resolve(result);
}, 1000)
} catch (err) {
reject(err);
}
})
}
export default demo;
步骤4: 编译结果
- 在项目目录下执行
npm run build
- 编译结果在目录
./dist/
下 - 编译成ES5结果为
define(function () { 'use strict';
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function demo() {
return new Promise(function (resolve, reject) {
try {
setTimeout(function () {
var obj1 = {
a: 1
};
var obj2 = {
b: 2
};
var obj3 = {
c: 3
};
var obj4 = {
d: 4
};
var result = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, obj1), obj2), obj3), obj4);
resolve(result);
}, 1000);
} catch (err) {
reject(err);
}
});
}
var index = {
init: function init() {
var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
console.log([].concat(arr1, arr2));
function initDemo() {
return _initDemo.apply(this, arguments);
}
function _initDemo() {
_initDemo = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var data;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return demo();
case 2:
data = _context.sent;
console.log(data);
case 4:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _initDemo.apply(this, arguments);
}
initDemo();
}
};
return index;
});
//# sourceMappingURL=index.js.map
步骤5: 浏览器查看结果
- example目录
./example/index.html
- example源码
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.bootcss.com/babel-polyfill/6.26.0/polyfill.js"></script>
<script data-main="main" src="https://cdn.bootcss.com/require.js/2.3.5/require.js"></script>
</head>
<body>
<p>hello world</p>
<!-- <script src="./../dist/index.js"></script> -->
</body>
</html>
require.js
引用的配置入口example/main.js
文件
requirejs.config({
baseUrl: '/',
paths: {}
});
define(function (require) {
var demo = require('dist/index');
demo.init()
});
- 访问 http://127.0.0.1:3000/example/index.html
- 打开工作台console 就会显示可运行结果
[1, 2, 3, 4, 5, 6]
this is lib.js
{a: 1, b: 2, c: 3, d: 4}
>
后记
本篇讲述了 rollup.js
编译ES6+语法成 AMD 规范的ES5代码,举的例子是实现最单简单的自执行模块,但是在实际项目中,使用AMD时,存在着定义模块和模块间依赖的情况,下一篇将讲述rollup.js
处理 AMD 模块依赖编译的配置