添加脚本命令
打包输出多个格式
import json from "@rollup/plugin-json";
// 打包第三方库
import resolve from "@rollup/plugin-node-resolve";
// commonjs 模块转 esModule 模块进行打包
import commonjs from "@rollup/plugin-commonjs";
// 压缩代码
import { terser } from "rollup-plugin-terser";
export default [
{
input: "index.js",
external: ["react"], // 指出应将哪些模块视为外部模块
plugins: [resolve(), commonjs(), json()], // 插件是按顺序执行的
output: {
file: "dist/index.umd.js",
format: "umd",
name: "index",
// plugins: [terser()], // 这里也可以使用插件,编译之后才执行的插件
banner: "/** Hello This is Banner **/", // 在打包的文件,加注释声明
},
},
{
input: "index.js",
external: ["react"],
plugins: [resolve(), commonjs(), json()],
output: {
file: "dist/index.es.js",
format: "es",
name: "index",
plugins: [terser()],
},
},
];
- output 也可以是数组
```javascript
import json from “@rollup/plugin-json”;
// 打包第三方库
import resolve from “@rollup/plugin-node-resolve”;
// commonjs 模块转 esModule 模块进行打包
import commonjs from “@rollup/plugin-commonjs”;
// 压缩代码
import { terser } from “rollup-plugin-terser”;
export default [
{
input: “index.js”,
external: [“react”], // 指出应将哪些模块视为外部模块
plugins: [resolve(), commonjs(), json()], // 插件是按顺序执行的
output: [
{
file: "dist/index.umd.js",
format: "umd",
name: "index",
// plugins: [terser()], // 这里也可以使用插件,编译之后才执行的插件
banner: "/** Hello This is Banner **/", // 在打包的文件,加注释声明
},
{
file: "dist/index.es.js",
format: "es",
name: "index",
plugins: [terser()],
},
],
},
];
一般建议整个配置文件作为数组,相同的配置声明变量
```javascript
import json from "@rollup/plugin-json";
// 打包第三方库
import resolve from "@rollup/plugin-node-resolve";
// commonjs 模块转 esModule 模块进行打包
import commonjs from "@rollup/plugin-commonjs";
// 压缩代码
import { terser } from "rollup-plugin-terser";
const entry = "index.js";
const plugins = [resolve(), commonjs(), json()];
export default [
{
input: entry,
external: ["react"], // 指出应将哪些模块视为外部模块
plugins: plugins, // 插件是按顺序执行的
output: {
file: "dist/index.umd.js",
format: "umd",
name: "index",
// plugins: [terser()], // 这里也可以使用插件,编译之后才执行的插件
banner: "/** Hello This is Banner **/", // 在打包的文件,加注释声明
},
},
{
input: entry,
external: ["react"],
plugins: plugins,
output: {
file: "dist/index.es.js",
format: "es",
name: "index",
plugins: [terser()],
},
},
];