forked from facebookresearch/hiplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.web.config.mts
More file actions
172 lines (160 loc) · 5.18 KB
/
vite.web.config.mts
File metadata and controls
172 lines (160 loc) · 5.18 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import fs from "fs";
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
import postcss from "postcss";
import prefixSelector from "postcss-prefix-selector";
// Vite plugin that scopes vendor CSS (Bootstrap, DataTables) under hiplot theme classes
// so styles don't leak into parent applications
function scopeVendorCssVitePlugin(): Plugin {
const vendorPatterns = [/node_modules[/\\]bootstrap/, /node_modules[/\\]datatables\.net/];
const processor = postcss([
prefixSelector({
prefix: ":is(.hip_thm--dark, .hip_thm--light)",
transform: function (prefix, selector, prefixedSelector) {
// Replace root-level selectors with our theme prefix
if (selector === ":root" || selector === "html" || selector === "body") {
return prefix;
}
// Handle universal selector - scope it
if (selector === "*" || selector.startsWith("*,") || selector.startsWith("*, ")) {
return selector.replace(/^\*/, prefix + " *");
}
return prefixedSelector;
},
}),
]);
return {
name: "scope-vendor-css",
async transform(code, id) {
// Only process CSS files from vendor packages
if (!id.endsWith(".css") || !vendorPatterns.some((p) => p.test(id))) {
return null;
}
const result = await processor.process(code, { from: id });
return { code: result.css, map: result.map?.toString() };
},
};
}
type RemToPxOptions = {
propList: string[];
rootValue?: number;
};
function remToPxPlugin(options: RemToPxOptions) {
const rootValue = options.rootValue ?? 16;
const propList = options.propList;
const matchesProp = (prop: string) =>
propList.some((pattern) =>
pattern.endsWith("*") ? prop.startsWith(pattern.slice(0, -1)) : prop === pattern,
);
const remRegex = /(-?\d*\.?\d+)rem\b/g;
return {
postcssPlugin: "rem-to-px",
Declaration(decl: { prop: string; value: string }) {
if (!matchesProp(decl.prop) || !decl.value.includes("rem")) {
return;
}
decl.value = decl.value.replace(remRegex, (_match, num) => {
const px = parseFloat(num) * rootValue;
return `${Number.isNaN(px) ? num : px}px`;
});
},
};
}
remToPxPlugin.postcss = true;
function packageNameFull(isDebug: boolean): string {
const version = process.env.HIPLOT_VERSION ?? "0.0.0";
const packageName = process.env.HIPLOT_PACKAGE ?? "hiplot";
return `bundle-${packageName}-${version}${isDebug ? "-dbg" : ""}`;
}
function ensureDir(dirPath: string) {
fs.mkdirSync(dirPath, { recursive: true });
}
function copyFile(src: string, dest: string) {
ensureDir(path.dirname(dest));
fs.copyFileSync(src, dest);
}
function copyWebBundlesPlugin(entryName: string) {
return {
name: "copy-hiplot-web-bundles",
writeBundle() {
const distPath = path.resolve(__dirname, "npm-dist");
const pyBuilt = path.resolve(__dirname, "hiplot", "static", "built");
const installToFolders = [
path.resolve(pyBuilt, ""),
path.resolve(distPath, "streamlit_component"),
];
if (entryName === "hiplot") {
installToFolders.forEach((sc) => {
copyFile(
path.resolve(distPath, "hiplot.bundle.js"),
path.resolve(sc, "hiplot.bundle.js"),
);
});
}
if (entryName === "hiplot_streamlit") {
installToFolders.forEach((sc) => {
copyFile(
path.resolve(distPath, "hiplot_streamlit.bundle.js"),
path.resolve(sc, "streamlit_component", "hiplot_streamlit.bundle.js"),
);
copyFile(
path.resolve(__dirname, "src", "index_streamlit.html"),
path.resolve(sc, "streamlit_component", "index.html"),
);
});
}
},
};
}
export default defineConfig(({ mode }) => {
const isDebug = mode === "development";
const entryName = process.env.HIPLOT_WEB_NAME ?? "hiplot";
const entryPath =
process.env.HIPLOT_WEB_ENTRY ?? path.resolve(__dirname, "src", "hiplot_web.tsx");
return {
plugins: [
scopeVendorCssVitePlugin(),
react(),
cssInjectedByJsPlugin(),
copyWebBundlesPlugin(entryName),
],
resolve: {
dedupe: ["jquery"],
},
define: {
HIPLOT_PACKAGE_NAME_FULL: JSON.stringify(packageNameFull(isDebug)),
define: "undefined",
},
build: {
outDir: "npm-dist",
emptyOutDir: false,
sourcemap: true,
target: "es2020",
chunkSizeWarningLimit: 2000,
rollupOptions: {
input: entryPath,
output: {
format: "iife",
name: "hiplot",
entryFileNames: `${entryName}.bundle.js`,
inlineDynamicImports: true,
},
},
},
css: {
modules: {
generateScopedName: isDebug ? "[local]_[hash:base64:5]" : "[hash:base64:5]",
},
postcss: {
plugins: [
// Keep rem-to-px conversion (previously applied to global CSS only).
remToPxPlugin({
propList: ["font", "font-size", "line-height", "letter-spacing", "padding*", "border*"],
}),
],
},
},
};
});