-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcss.mjs
More file actions
52 lines (46 loc) · 1.54 KB
/
css.mjs
File metadata and controls
52 lines (46 loc) · 1.54 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
import { getarg } from "./getarg.mjs";
import { bundleAsync } from "lightningcss";
import fs from "fs";
import path from "path";
const DEBUG = getarg("--debug");
const DEFAULT_RESOLVER = {
resolve(specifier, originatingFile) {
if (/^https?:\/\//.test(specifier)) {
return specifier;
}
if (specifier.startsWith("perspective-viewer-")) {
const viewerCssDir = path.resolve(
"node_modules/@perspective-dev/viewer/dist/css",
);
const normalized = specifier.replace(/^perspective-viewer-/, "");
const normalizedPath = path.join(viewerCssDir, normalized);
if (fs.existsSync(normalizedPath)) {
return normalizedPath;
}
return path.join(viewerCssDir, specifier);
}
return path.resolve(path.dirname(originatingFile), specifier);
},
};
const bundle_one = async (file, resolver) => {
const { code } = await bundleAsync({
filename: path.resolve(file),
minify: !DEBUG,
sourceMap: false,
resolver: resolver || DEFAULT_RESOLVER,
});
const outName = path.basename(file);
fs.mkdirSync("./dist/css", { recursive: true });
fs.writeFileSync(path.join("./dist/css", outName), code);
};
export const bundle_css = async (root = "src/css/index.css", resolver = null) => {
const resolved = path.resolve(root);
if (fs.statSync(resolved).isDirectory()) {
const files = fs.readdirSync(resolved).filter((f) => f.endsWith(".css"));
for (const file of files) {
await bundle_one(path.join(root, file), resolver);
}
} else {
await bundle_one(root, resolver);
}
}