-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.ts
More file actions
117 lines (105 loc) · 3.2 KB
/
Copy pathrollup.config.ts
File metadata and controls
117 lines (105 loc) · 3.2 KB
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
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import nodeResolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import type { Plugin, RollupOptions } from "rollup";
const dev = process.env.NODE_ENV === "development";
const terserPlugin = terser({
compress: {
ecma: 2020,
passes: 3,
unsafe: true,
unsafe_arrows: true,
unsafe_comps: true,
pure_getters: true,
drop_debugger: true,
collapse_vars: true,
reduce_vars: true,
},
mangle: { safari10: true, toplevel: true },
format: { comments: false },
});
const VIRTUAL_ID = "virtual:civil-ext-shim-source";
const RESOLVED_ID = "\0virtual:civil-ext-shim-source";
function inlineShimPlugin(): Plugin {
return {
name: "inline-shim",
resolveId(id) {
if (id === VIRTUAL_ID) return RESOLVED_ID;
return null;
},
load(id) {
if (id !== RESOLVED_ID) return null;
if (dev) {
return `export default "";`;
}
const shimPath = resolve("dist", "civil-ext-shim.js");
let src: string;
try {
src = readFileSync(shimPath, "utf8");
} catch {
this.error(
`[inline-shim] Cannot read ${shimPath}. ` +
"Ensure the shim IIFE (build #1) is written before the host module (build #2). " +
"Run 'bun run build' (not --watch) so builds complete sequentially.",
);
return null;
}
const escaped = src
.replace(/\\/g, "\\\\")
.replace(/`/g, "\\`")
.replace(/\$\{/g, "\\${");
return `export default \`${escaped}\`;`;
},
};
}
function tsPlugin(declaration: boolean): Plugin {
return typescript({
tsconfig: "./tsconfig.json",
declaration,
declarationDir: declaration ? "dist" : undefined,
sourceMap: dev,
inlineSources: dev,
}) as Plugin;
}
const shimBundle: RollupOptions = {
input: "src/shim/index.ts",
output: {
file: "dist/civil-ext-shim.js",
format: "iife",
name: "__civilExtShim__",
compact: !dev,
generatedCode: { arrowFunctions: true, constBindings: true },
sourcemap: dev,
},
treeshake: {
propertyReadSideEffects: false,
unknownGlobalSideEffects: false,
},
plugins: [
nodeResolve({ browser: true }),
tsPlugin(false),
...(dev ? [] : [terserPlugin]),
],
};
const hostBundle: RollupOptions = {
input: "src/index.ts",
output: {
file: "dist/index.js",
format: "esm",
sourcemap: dev,
generatedCode: { arrowFunctions: true, constBindings: true },
},
treeshake: {
propertyReadSideEffects: false,
unknownGlobalSideEffects: false,
},
plugins: [
nodeResolve({ browser: true }),
tsPlugin(true),
inlineShimPlugin(),
...(dev ? [] : [terserPlugin]),
],
};
export default [shimBundle, hostBundle];