-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollup.config.js
175 lines (166 loc) · 5.98 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pkg from "./package.json" assert { type: "json" };
import { readdirSync, readFileSync } from "fs";
import path from "path";
import typescript from "@rollup/plugin-typescript";
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import inject from "@rollup/plugin-inject";
import globals from "rollup-plugin-node-globals";
import replace from "@rollup/plugin-replace";
import terser from "@rollup/plugin-terser";
import css from "rollup-plugin-import-css";
import htmlTemplate from "rollup-plugin-generate-html-template";
import copy from "rollup-plugin-copy";
import dev from "rollup-plugin-dev";
import dotenv from "dotenv";
dotenv.config({ path: ".dev.vars" });
const build = process.env.BUILD ?? "browser";
const isBrowser = build === "browser";
const isNpm = build === "npm";
const isServer = process.env.SERVER ?? false;
const bannerMessage = readFileSync("./LICENSE")
.toString()
.replace(/\r\n/g, "\n")
.split("\n")
.map((line) => " * " + line)
.join("\n");
const banner = `/**!
${bannerMessage}
*/`;
const exampleDist = isServer ? "example/.dist" : "example/dist";
export default [
// Main Tebex.js build
{
input: "src/index.ts",
plugins: [
nodeResolve(),
commonjs(), // required for zoid
globals(), // required for zoid
inject({
// required for zoid
include: "node_modules/zoid/**/*",
window: path.resolve("src/stubs/window.ts"),
}),
typescript({
outputToFilesystem: true,
exclude: "tests/**/*",
compilerOptions: {
rootDir: "src",
declaration: true,
declarationDir: "dist/types",
},
sourceMap: isBrowser,
}),
// Annoying: Vite/Vitest only includes CSS imports as strings if you append ?inline to their path... which breaks rollup-plugin-import-css
// This just strips the ?inline from th end of those paths so that it works again :)
{
name: "inline-css-fixer",
resolveId(source, importer, options) {
if (source.endsWith(".css?inline"))
return this.resolve(
source.replace("?inline", ""),
importer,
options
);
},
},
css({
minify: true,
}),
replace({
preventAssignment: true,
__VERSION__: JSON.stringify(pkg.version),
}),
],
output: [
// browser-only build
isBrowser && {
banner,
file: "dist/tebex.min.js",
format: "iife",
name: "Tebex",
exports: "named",
sourcemap: true,
plugins: [terser()],
},
// NPM ES module build
isNpm && {
banner,
file: "dist/tebex.mjs",
format: "es",
name: "Tebex",
exports: "named",
},
// NPM legacy build (commonJS, AMD, iife)
isNpm && {
banner,
file: "dist/tebex.cjs",
format: "umd",
name: "Tebex",
exports: "named",
},
].filter(Boolean),
},
// Example build
{
input: "example/index.js",
output: [
{
// Use a different output file for local server builds to preven accidentally overriding the build
// when running locally
file: `${exampleDist}/index.js`,
},
],
plugins: [
nodeResolve(),
commonjs(),
replace({
preventAssignment: true,
__ENDPOINT__: `"${
process.env.CHECKOUT_HOST_ENDPOINT || "https://pay.tebex.io"
}"`,
}),
copy({
targets: [{ src: ["./example/*", '!./example/*.html', '!./example/index.js', '!./example/dist'], dest: exampleDist }],
}),
htmlTemplate({
template: "example/index.html",
target: `${exampleDist}/index.html`,
attrs: ["defer"],
}),
htmlTemplate({
template: "example/components.html",
target: `${exampleDist}/components.html`,
attrs: ["defer"],
}),
isServer &&
dev({
silent: true,
host: "localhost",
port: "8080",
dirs: ["dist", "example/.dist"],
// Proxy all function routes through to locally running cloudflare pages instance
proxy: readdirSync("./functions")
.map((p) => path.parse(p).name)
.map((route) => ({
from: `/${route}`,
to: `http://localhost:8787/${route}`,
})),
// We silence the default rollup server + wrangler console output
onListen: (server) => {
const port = server.server._connectionKey
.split(":")
.at(-1);
console.log(`\x1b[34m`);
console.log(``);
console.log(``);
console.log(`Tebex.js demo server is running!`);
console.log(`Preview it at http://localhost:${port}`);
console.log(``);
console.log(``);
console.log(``);
},
}),
].filter(Boolean),
},
];