This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rollup.config.ts
161 lines (143 loc) · 3.9 KB
/
rollup.config.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fs from "fs";
import path from "path";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
// import babel from "@rollup/plugin-babel";
import { terser } from "rollup-plugin-terser";
// import serve from "rollup-plugin-serve";
import ts from "rollup-plugin-typescript2";
import replace from "@rollup/plugin-replace";
import filesize from "rollup-plugin-filesize";
import camelcase from "camelcase";
const { BUILD_TYPE } = process.env;
const printSizePkg = ["@doras/browser"];
function getPackages() {
// const packages = ["types", "shared", "core"];
const packages = ["types", "shared", "core", "browser"];
const rootPath = path.join(__dirname, "packages");
return packages
.map((pkg) => path.join(rootPath, pkg))
.filter((dir) => fs.statSync(dir).isDirectory())
.map((location) => {
return {
location: location,
pkgJson: require(path.resolve(location, "package.json"))
};
});
}
function configBuilder({ location, pkgJson }) {
const tsPlugin = ts({
check: true,
tsconfig: path.resolve(location, "tsconfig.build.json"),
cacheRoot: path.resolve(__dirname, "node_modules/.rts2_cache"),
useTsconfigDeclarationDir: true,
tsconfigOverride: {
compilerOptions: {
sourceMap: true,
declaration: true,
declarationMap: true
}
}
});
const commonPlugins = [
resolve({
extensions: [".js", ".jsx", ".ts", ".tsx"],
preferBuiltins: true
}),
// babel({
// extensions: [".js", ".jsx", ".ts", ".tsx"],
// babelHelpers: "runtime",
// exclude: ["node_modules/**", "packages/**/node_modules/**"]
// }),
tsPlugin,
commonjs(),
replace({
preventAssignment: true,
__PkgName: pkgJson.name,
__PkgVersion: pkgJson.version,
__BuildTime: new Date().toLocaleString()
})
];
const input = path.join(location, "src", "index.ts");
const external = Object.keys(pkgJson.dependencies || {});
return {
umd: (compress) => {
let file = path.join(location, "dist", "umd.js");
const plugins = [...commonPlugins];
if (compress) {
file = path.join(location, "dist", "umd.min.js");
plugins.push(terser({ output: { comments: false } }));
}
if (printSizePkg.includes(pkgJson.name) && file.indexOf("min.js") > -1) {
plugins.push(filesize());
}
const globalName = camelcase(pkgJson.name);
const globals = {};
external.forEach((pkgName) => {
globals[pkgName] = camelcase(pkgName);
});
return {
input,
external: {},
output: [
{
file,
name: globalName,
format: "umd",
sourcemap: true
}
],
plugins
};
},
module: () => {
const plugins = [...commonPlugins];
return {
input,
external,
output: [
{
file: path.join(location, pkgJson.module),
format: "es",
sourcemap: true
},
{
file: path.join(location, pkgJson.main),
format: "commonjs",
sourcemap: true
}
],
plugins
};
}
};
}
function getProd() {
const pkgConfig = getPackages();
return pkgConfig.reduce((acc, pkg) => {
acc.push(configBuilder(pkg).module());
acc.push(configBuilder(pkg).umd(false));
acc.push(configBuilder(pkg).umd(true));
return acc;
}, []);
}
function getDev() {
const pkgConfig = getPackages();
return pkgConfig.reduce((acc, pkg) => {
acc.push(configBuilder(pkg).module());
acc.push(configBuilder(pkg).umd(false));
return acc;
}, []);
}
let promises = [];
switch (BUILD_TYPE) {
case "dev":
promises.push(...getDev());
break;
case "prod":
promises.push(...getProd());
break;
default:
break;
}
export default Promise.all(promises);