-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
vite.config.ts
118 lines (106 loc) · 3.89 KB
/
vite.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
import { defineConfig, UserConfig } from "vite";
import { viteStaticCopy } from "vite-plugin-static-copy";
import path from "path";
import fs from "fs";
export default defineConfig(({ command }) => {
const config: UserConfig = {
build: {
outDir: "dist",
copyPublicDir: false, // Disables the copying of public/ directory during build
rollupOptions: {
input: {
bloomPlayer: "./src/bloom-player-root.ts",
},
output: {
entryFileNames: "bloomPlayer.[hash].js",
globals: {
BloomPlayer: "window.BloomPlayer",
},
// avoid the /assets folder in order to match what webpack was doing
assetFileNames: "[name]-[hash].[ext]",
},
},
sourcemap: true, // Generates separate source map files
},
server: {
open: "/index-for-developing.html",
watch: {
ignored: ["node_modules/**"],
},
// This is one way to get around CORS restrictions when trying to access s3 resources.
// It requires using '/s3' as a prefix on the url in our html files rather than
// the actual bloomlibrary.org url.
// Alternatively, we could always launch the browser with security disabled.
proxy: {
"/s3": {
target: "https://s3.amazonaws.com",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/s3/, ""),
},
},
},
css: {
preprocessorOptions: {
less: {},
},
},
plugins: [
command === "serve" && // only want these when we do "vite dev", not when we do "vite build"
viteStaticCopy({
targets: [
{
src: "index-for-developing.html",
dest: "./",
rename: "index.html",
},
{
src: "src/bloomplayer-for-developing.htm",
dest: "./",
},
],
}),
useCacheBustingHashPlugin(),
],
};
return config;
});
function useCacheBustingHashPlugin() {
return {
name: "copy-html-plugin",
writeBundle(options, bundle) {
// Find the JS file
const jsFile = Object.keys(bundle).find(
(fileName) =>
fileName.startsWith("bloomPlayer") &&
fileName.endsWith(".js"),
);
// Find the CSS file
const cssFile = Object.keys(bundle).find(
(fileName) =>
fileName.startsWith("bloomPlayer") &&
fileName.endsWith(".css"),
);
if (!jsFile) {
return;
}
const srcHtmlPath = path.resolve(__dirname, "src/bloomplayer.htm");
const destHtmlPath = path.resolve(
__dirname,
"dist/bloomplayer.htm",
);
let htmlContent = fs.readFileSync(srcHtmlPath, "utf-8");
// Replace the script tag
htmlContent = htmlContent.replace(
/<script src="bloomPlayer-HASH\.js"><\/script>/,
`<script src="${jsFile}"></script>`,
);
if (cssFile) {
htmlContent = htmlContent.replace(
/<link rel="stylesheet" href="bloomPlayer-HASH\.css"\s*\/?>/,
`<link rel="stylesheet" href="${cssFile}">`,
);
}
fs.writeFileSync(destHtmlPath, htmlContent);
},
};
}