-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
53 lines (49 loc) · 1.48 KB
/
build.js
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
import esbuild from "esbuild";
import CssModulesPlugin from "esbuild-css-modules-plugin";
import fs from "fs/promises";
esbuild
.build({
entryPoints: ["src/index.ts"],
outfile: "dist/bundle.js",
format: "esm",
tsconfig: "./tsconfig.json",
sourcemap: false,
bundle: true,
platform: "browser",
target: "esnext",
minify: false,
plugins: [
CssModulesPlugin({
force: true,
emitDeclarationFile: false,
namedExports: true,
}),
],
loader: {
".ts": "ts",
".css": "css",
},
})
.then(async () => {
try {
const cssOutputPath = "./dist/bundle.css";
// Read the generated CSS file content
const cssContent = await fs.readFile(cssOutputPath, "utf-8");
// Create a JavaScript snippet to inject the CSS into the <head>
const cssInjectionCode = `
(function() {
const style = document.createElement('style');
style.textContent = ${JSON.stringify(cssContent)};
document.head.appendChild(style);
})();
`;
// Append the injection code to the end of the bundle.js
const bundlePath = "./dist/bundle.js";
const bundleContent = await fs.readFile(bundlePath, "utf-8");
await fs.writeFile(bundlePath, `${bundleContent}\n${cssInjectionCode}`);
console.log("CSS injected successfully!");
} catch (error) {
console.error("Error injecting CSS:", error);
}
})
.catch(() => process.exit(1));