npm init -y
增加
-y
参数不用一直按 Enter,全部采用默认设置
├── build # 编译脚本
│ └── rollup.config.js
├── dist # 编译结果
│ └── index.js
├── example # HTML引用例子
│ └── index.html
├── package.json
└── src # ES6源码
└── index.js
## 安装 rollup.js 基础模块
npm i --save-dev rollup
- 配置文件路径
../build/rollup.config.js
const path = require("path");
const resolve = function (filePath) {
return path.join(__dirname, "..", filePath);
};
module.exports = {
input: resolve("src/index.js"),
output: {
file: resolve("dist/index.js"),
format: "iife",
},
};
package.json
配置编译命令
{
"scripts": {
"build": "rollup -c ./build/rollup.config.js"
}
}
- 源码路径
../src/index.js
- 源码内容
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const result = [...arr1, ...arr2];
console.log(result);
- 在项目根目录下执行
npm run build
- 编译结果在
../dist/
目录下 - 示例源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/babel-polyfill/7.12.1/polyfill.js"></script>
<script src="../dist/index.js"></script>
</head>
<body></body>
</html>
- 访问
http://localhost:3001/