forked from facebookresearch/hiplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.lib.config.mts
More file actions
121 lines (114 loc) · 3.61 KB
/
vite.lib.config.mts
File metadata and controls
121 lines (114 loc) · 3.61 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
import { defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
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";
return `lib-hiplot-${version}${isDebug ? "-dbg" : ""}`;
}
export default defineConfig(({ mode }) => {
const isDebug = mode === "development";
return {
plugins: [scopeVendorCssVitePlugin(), react(), cssInjectedByJsPlugin()],
resolve: {
dedupe: ["jquery"],
},
define: {
HIPLOT_PACKAGE_NAME_FULL: JSON.stringify(packageNameFull(isDebug)),
define: "undefined",
},
build: {
outDir: "npm-dist",
emptyOutDir: false,
sourcemap: true,
target: "es2017",
chunkSizeWarningLimit: 2000,
lib: {
entry: path.resolve(__dirname, "src", "hiplot.tsx"),
name: "hiplot",
formats: ["umd"],
fileName: () => "hiplot.lib.js",
},
rollupOptions: {
external: ["react"],
output: {
globals: {
react: "React",
},
},
},
},
css: {
modules: {
generateScopedName: isDebug ? "[local]_[hash:base64:5]" : "[hash:base64:5]",
},
postcss: {
plugins: [
remToPxPlugin({
propList: ["font", "font-size", "line-height", "letter-spacing", "padding*", "border*"],
}),
],
},
},
};
});