-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
150 lines (130 loc) · 4.02 KB
/
rollup.config.js
File metadata and controls
150 lines (130 loc) · 4.02 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
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
import { createServer } from "http";
import { cpSync, readdirSync, mkdirSync, existsSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import terser from "@rollup/plugin-terser";
import { rollupPluginHTML as html } from "@web/rollup-plugin-html";
import { copy } from "@web/rollup-plugin-copy";
import { bundle } from "lightningcss";
const __dirname = dirname(fileURLToPath(import.meta.url));
const isDev = process.env.DEV === "1";
const isWatch = process.argv.includes("-w") || process.argv.includes("--watch");
const LIVE_RELOAD_PORT = 35729;
const LIVE_RELOAD_STATE_KEY = "__angularSeedLiveReload";
/**
* Minimal SSE live-reload rollup plugin (zero dependencies).
* Starts an HTTP server that streams Server-Sent Events.
* Every `writeBundle` pushes a "reload" event to every connected browser.
*/
function liveReloadPlugin(port = LIVE_RELOAD_PORT) {
let state = globalThis[LIVE_RELOAD_STATE_KEY];
if (!state || state.port !== port || !state.server.listening) {
const clients = new Set();
const server = createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
"Access-Control-Allow-Origin": "*",
});
clients.add(res);
req.on("close", () => clients.delete(res));
});
server.listen(port, () =>
console.log(`Live-reload SSE server on http://localhost:${port}`),
);
state = { clients, port, server };
globalThis[LIVE_RELOAD_STATE_KEY] = state;
}
return {
name: "live-reload",
writeBundle() {
for (const res of state.clients) {
res.write("data: reload\n\n");
}
},
};
}
/**
* Copy @ionic/core lazy-loaded Stencil files to dist/ root.
* Entry files use dynamic import() with computed paths at runtime and cannot
* be resolved by rollup. They also import shared helper modules from the
* same directory.
*/
function copyIonicEntries() {
return {
name: "copy-ionic-entries",
writeBundle(options) {
const outDir = options.dir || "dist";
const ionicEsm = join(__dirname, "node_modules/@ionic/core/dist/esm");
// Skip non-hashed files that would collide with rollup output
const skip = new Set(["index.js", "loader.js"]);
if (!existsSync(outDir)) mkdirSync(outDir, { recursive: true });
for (const file of readdirSync(ionicEsm)) {
if (file.endsWith(".js") && !skip.has(file)) {
cpSync(join(ionicEsm, file), join(outDir, file));
}
}
console.log(`Copied @ionic/core ESM files to ${outDir}`);
},
};
}
function onwarn(warning, defaultHandler) {
const isIonicPwaElementsThisWarning =
warning.code === "THIS_IS_UNDEFINED" &&
warning.id?.includes("@ionic/pwa-elements/dist/esm-es5/");
if (isIonicPwaElementsThisWarning) {
return;
}
defaultHandler(warning);
}
const plugins = [
html({
rootDir: "./app",
minify: !isDev,
flattenOutput: false,
transformAsset: (_content, filePath) => {
if (filePath.endsWith(".css")) {
const { code } = bundle({
filename: filePath,
minify: !isDev,
});
return new TextDecoder("utf-8").decode(code);
}
return undefined;
},
}),
copy({
patterns: [
"./*.{txt,webmanifest}",
"./apps/router/_*.html",
"./apps/ionic/assets/**",
"./apps/ionic/manifest.json",
"./apps/ionic/capacitor.config.json",
],
rootDir: "./app",
exclude: ["node_modules"],
}),
resolve(),
commonjs(),
...(isDev && isWatch ? [liveReloadPlugin()] : isDev ? [] : [terser()]),
copyIonicEntries(),
];
export default [
{
input: [
"./index.html",
"./apps/todo/todo.html",
"./apps/router/router.html",
"./apps/ionic/ionic.html",
],
onwarn,
output: {
dir: "dist",
entryFileNames: isDev ? "[name].js" : "[name].[hash].js",
},
plugins,
},
];