forked from SquidDev-CC/cloud-catcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
174 lines (150 loc) · 4.99 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
162
163
164
165
166
167
168
169
170
171
172
173
174
import commonjs from "@rollup/plugin-commonjs";
import html, { makeHtmlAttributes, RollupHtmlTemplateOptions } from "@rollup/plugin-html";
import nodeResolve, { RollupNodeResolveOptions } from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import typescript from "@rollup/plugin-typescript";
import url from "@rollup/plugin-url";
import { promises as fs } from "fs";
import MagicString from "magic-string";
import type { OutputChunk, OutputOptions, Plugin, RollupOptions } from "rollup";
import license from "rollup-plugin-license";
import livereload from 'rollup-plugin-livereload';
import postcss from "rollup-plugin-postcss";
import { terser } from 'rollup-plugin-terser';
const production = !process.env.ROLLUP_WATCH;
const makeTemplate = (filename: string) => async (options?: RollupHtmlTemplateOptions): Promise<string> => {
if (!options) throw new Error("Must specify RollupHtmlTemplateOptions");
const { attributes, files, publicPath } = options;
const entrypoints = (files.js ?? []).filter(x => (x as OutputChunk).isEntry);
if (entrypoints.length > 1) throw new Error("Too many entrypoints!")
const scripts = entrypoints
.map(({ fileName }) => `<script data-main="${publicPath}${fileName}" src="${publicPath}/require.js" ${makeHtmlAttributes(attributes.script)}></script>`)
.join("\n");
const links = (files.css || [])
.map(({ fileName }) => `<link href="${publicPath}${fileName}" rel="stylesheet"${makeHtmlAttributes(attributes.link)}>`)
.join("\n");
const templateFields: { [filed: string]: unknown } = { scripts, links };
const template = await fs.readFile(filename, { encoding: "utf-8" });
return template.replace(/\$\{([^}]+)\}/g, (_, name) => {
const value = templateFields[name];
if (value === undefined) throw new Error(`Unknown field ${name}`);
return `${value}`;
});
};
const defaultOutput: OutputOptions = {
preferConst: true,
freeze: true,
}
const defaultPlugins = (
{ resolve, outDir, strict }: { resolve: RollupNodeResolveOptions, outDir: string, strict: boolean },
): Array<Plugin> => [
nodeResolve({
moduleDirectories: ["node_modules"],
modulePaths: (process.env.NODE_PATH ?? "").split(/[;:]/),
...resolve,
}),
commonjs({ strictRequires: true }),
typescript({
sourceMap: true,
inlineSources: true,
outDir,
noEmitOnError: strict,
}),
]
const site = (): RollupOptions => {
const outDir = "_site";
return {
input: "src/viewer/index.tsx",
output: {
...defaultOutput,
dir: outDir,
format: "amd",
entryFileNames: "[name]-[hash].js",
paths: {
"monaco-editor": "vs/editor/editor.main",
},
sourcemap: true,
},
context: "window",
external: ["monaco-editor"],
plugins: [
replace({
preventAssignment: true,
__monaco__: "https://cdn.jsdelivr.net/npm/monaco-editor@0.34.0",
}),
postcss({
extract: true,
namedExports: true,
modules: true,
minimize: production,
}),
url({
limit: 1024,
fileName: "[name]-[hash][extname]",
include: ["**/*.worker.js", "**/*.png"],
}),
...defaultPlugins({
outDir,
resolve: { browser: true },
strict: production,
}),
license({
banner:
`<%= pkg.name %>: Copyright <%= pkg.author %> <%= moment().format('YYYY') %>
<% _.forEach(_.sortBy(dependencies, ["name"]), ({ name, author, license }) => { %>
- <%= name %>: Copyright <%= author ? author.name : "" %> (<%= license %>)<% }) %>
@license
`,
thirdParty: { output: `${outDir}/dependencies.txt` },
}),
html({ template: makeTemplate("src/viewer/index.html") as any }),
html({ template: makeTemplate("src/viewer/404.html") as any, fileName: "404.html" }),
// Setup dev server for dev builds
!production && livereload(outDir),
{
name: "cloud-catcher",
async writeBundle() {
await Promise.all([
fs.copyFile("node_modules/requirejs/require.js", `${outDir}/require.js`),
]);
}
},
// Otherwise minify
production && terser(),
],
watch: {
clearScreen: false
}
};
};
const server = (): RollupOptions => {
const outDir = "_bin";
const outFile = `${outDir}/server.cjs`;
return {
input: "src/server/index.ts",
output: {
...defaultOutput,
file: outFile,
format: "commonjs",
sourcemap: true,
interop: "default",
externalLiveBindings: false,
},
plugins: [
...defaultPlugins({
outDir,
resolve: { preferBuiltins: true },
strict: true,
}),
{
name: 'add-cli-entry',
renderChunk(code, _chunkInfo) {
const magicString = new MagicString(code);
magicString.prepend('#!/usr/bin/env node\n');
return { code: magicString.toString(), map: magicString.generateMap({ hires: true }) };
},
},
],
};
}
export default [site(), server()];