-
Notifications
You must be signed in to change notification settings - Fork 7
/
rollup.config.js
105 lines (90 loc) · 2.72 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
import { join } from "path";
import { existsSync, readFileSync } from "fs";
import nodeResolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import babel from "@rollup/plugin-babel";
import {terser} from "rollup-plugin-terser";
// eslint-disable-next-line no-undef
const isProd = process.env.NODE_ENV === "production";
const packages = ["core", "modules/media_session", "ui"];
const commonPlugins = [
typescript(),
babel({ babelHelpers: "bundled" })
];
const prodPlugins = [
terser({
format: { comments: false }
})
];
function getResult(input, file, format, min = false, plugins = []) {
return {
input,
output: {
sourcemap: !isProd,
name: "PPlayer",
file: min ? file.replace(/\.(m)?js$/, ".min.$1js") : file,
format,
exports: "default"
},
plugins: [
...plugins,
...commonPlugins,
...(min ? prodPlugins : [])
]
};
}
function isPackageValid(p) {
if (!existsSync(p)) {
console.log(`Package ${p} cannot be found`);
return false;
}
if (!existsSync(join(p, "package.json"))) {
console.log(`${p} is not a valid package`);
return false;
}
if (!existsSync(join(p, "src/index.ts"))) {
console.log(`No entry point found for package ${p}`);
return false;
}
return true;
}
const prodOnly = (input) => isProd ? input : undefined;
async function main() {
/**
* @type {import('rollup').RollupOptions[]}
*/
const results = [];
const addResult = (...r) => results.push(...r.filter(x => x !== undefined));
for (const p of packages) {
if (!isPackageValid(p))
continue;
const {
//name,
//version,
main,
module,
bundle,
entry
} = JSON.parse(readFileSync(join(p, "package.json")).toString());
const input = join(p, entry || "src/index.ts");
addResult(
getResult(input, join(p, main), "cjs"),
getResult(input, join(p, module), "esm"),
prodOnly(getResult(input, join(p, main), "cjs", true)),
prodOnly(getResult(input, join(p, module), "esm", true))
);
if (bundle) {
const bundlePlugs = [
nodeResolve({
moduleDirectories: [join(p, "node_modules")]
})
];
addResult(
getResult(input, join(p, bundle), "umd", false, bundlePlugs),
prodOnly(getResult(input, join(p, bundle), "umd", true, bundlePlugs))
);
}
}
return results;
}
export default main();