-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
147 lines (128 loc) · 3.57 KB
/
rollup.config.mjs
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
/* eslint-disable @typescript-eslint/no-var-requires */
import path from "path";
import ts from "rollup-plugin-typescript2";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
if (!process.env.TARGET) {
throw new Error("TARGET package must be specified via --environment flag.");
}
const pkg = require(
path.join(__dirname, "packages", process.env.TARGET, "package.json"),
);
const banner = `/*!
* ${pkg.name} v${pkg.version}
* (c) ${new Date().getFullYear()} ragokan
* @license MIT
*/`;
let hasTSChecked = false;
const outputConfigs = {
mjs: {
file: pkg.module,
format: "es",
},
cjs: {
file: "dist/index.cjs",
format: "cjs",
},
global: {
file: pkg.unpkg,
format: "iife",
},
browser: {
file: "dist/index.esm-browser.js",
format: "es",
},
};
const packageBuilds = Object.keys(outputConfigs);
const packageConfigs = packageBuilds.map((buildName) =>
createConfig(buildName, outputConfigs[buildName]),
);
packageBuilds.forEach((buildName) => {
if (buildName === "cjs") {
packageConfigs.push(createProductionConfig(buildName));
} else if (buildName === "global") {
packageConfigs.push(createMinifiedConfig(buildName));
}
});
export default packageConfigs;
/**
* @param {string} buildName
* @param {{ file: any; format?: any; sourcemap?: any; banner?: any; externalLiveBindings?: any; globals?: any; name?: any; }} output
* @param {any[]} plugins
*/
function createConfig(buildName, output, plugins = []) {
if (!output) {
console.error(`Invalid format: "${buildName}"`);
process.exit(1);
}
output.sourcemap = !!process.env.SOURCE_MAP;
output.banner = banner;
output.externalLiveBindings = false;
output.globals = { vue: "Vue" };
const isProductionBuild = /\.prod\.[cm]?js$/.test(output.file);
const isGlobalBuild = buildName === "global";
const isNodeBuild = output.file?.includes(".node.") || buildName === "cjs";
if (isGlobalBuild) {
output.name = "index";
}
const shouldEmitDeclarations = !hasTSChecked;
const tsPlugin = ts({
check: !hasTSChecked,
tsconfig: path.join(path.resolve(), "tsconfig.json"),
cacheRoot: path.join(path.resolve(), "node_modules/.rts2_cache"),
tsconfigOverride: {
compilerOptions: {
sourceMap: output.sourcemap,
declaration: shouldEmitDeclarations,
declarationMap: shouldEmitDeclarations,
rootDir: "./src",
},
exclude: ["tests", "*.spec.ts"],
},
});
hasTSChecked = true;
const external = ["vue"];
if (!isGlobalBuild && !(isProductionBuild && isNodeBuild)) {
external.push("@vue/devtools-api");
}
const nodePlugins = [resolve(), commonjs()];
return {
input: "src/index.ts",
external,
plugins: [tsPlugin, ...nodePlugins, ...plugins],
output,
};
}
/**
* @param {string} format
*/
function createProductionConfig(format) {
const extension = format === "cjs" || format === "mjs" ? format : "js";
const descriptor = format === "cjs" || format === "mjs" ? "" : `.${format}`;
return createConfig(format, {
file: `dist/index${descriptor}.prod.${extension}`,
format: outputConfigs[format].format,
});
}
/**
* @param {string} format
*/
function createMinifiedConfig(format) {
const terser = require("@rollup/plugin-terser");
return createConfig(
format,
{
file: `dist/index.${format}.prod.js`,
format: outputConfigs[format].format,
},
[
terser.default({
module: /^esm/.test(format),
compress: {
ecma: 2015,
pure_getters: true,
},
}),
],
);
}