-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
114 lines (109 loc) · 3.05 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import commonjs from "@rollup/plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import alias from "@rollup/plugin-alias";
import replace from "@rollup/plugin-replace";
import sourcemaps from "rollup-plugin-sourcemaps";
import { nodeResolve } from '@rollup/plugin-node-resolve';
const pkg = require("./package.json");
const path = require("path");
let isProd = process.env.NODE_ENV === "production";
const input = "src/index.ts";
const openSourceMap = true;
const buildOptions = pkg.buildOptions;
buildOptions.formats = process.env.TARGET
? [process.env.TARGET]
: buildOptions.formats;
export default createConfig();
function createConfig() {
return {
input,
external: [...Object.keys(pkg.dependencies), ...Object.keys(pkg.devDependencies)],
output: createOutput(buildOptions),
plugins: createPlugin(),
};
}
function createPlugin() {
let plugin = [];
const aliasPlugin = alias({
resolve: [".ts"],
"@": path.resolve(__dirname, "src"),
});
const tsPlugin = typescript({
check: isProd,
tsconfig: path.resolve(__dirname, "tsconfig.json"),
cacheRoot: path.resolve(__dirname, "node_modules/.rts2_cache"),
tsconfigOverride: {
compilerOptions: {
declaration: isProd,
},
},
});
const replacePlugin = replace({
preventAssignment: true,
__DEV__: !isProd,
__VERSION__: `"${pkg.version}"`
});
plugin = [sourcemaps(),nodeResolve(), commonjs(), tsPlugin, aliasPlugin, replacePlugin];
return plugin;
}
function createOutput(options) {
const output = [];
const filterType = {
//可用script标签导入,会输出一个全局变量
iife: {
sourcemap: openSourceMap,
file: `dist/${options.filename}.iife.js`,
format: "iife",
name: options.var,
},
// amd规范,可用require.js加载
amd: {
sourcemap: openSourceMap,
file: `dist/${options.filename}.amd.js`,
format: "amd",
},
// 兼容 IIFE, AMD, CJS 三种模块规范
umd: {
sourcemap: openSourceMap,
file: `dist/${options.filename}.umd.js`,
format: "umd",
name: options.var,
},
// CommonJS规范
cjs: {
sourcemap: openSourceMap,
file: `dist/${options.filename}.cjs.js`,
format: "cjs",
exports: "auto",
banner: "#!/usr/bin/env node" // 提供命令行的可执行权限
},
// ES2015 Module 规范, 可用 `Webpack`, `Rollup` 加载
esm: {
sourcemap: openSourceMap,
file: `dist/${options.filename}.esm.js`,
format: "es",
},
"esm-browser": {
sourcemap: openSourceMap,
file: `dist/${options.filename}.esm-browser.js`,
format: "es",
},
};
if (options.formats) {
options.formats.forEach((format) => {
if (filterType[format]) {
output.push(filterType[format]);
} else {
console.error("无效类型:" + format);
}
});
} else {
for (const key in filterType) {
if (filterType.hasOwnProperty(key)) {
const element = filterType[key];
output.push(element);
}
}
}
return output;
}