-
-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathbuild.ts
More file actions
151 lines (138 loc) · 4.32 KB
/
build.ts
File metadata and controls
151 lines (138 loc) · 4.32 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
import esbuild from "esbuild";
import * as sass from "sass-embedded";
import path from "path";
import fs from "fs";
import chokidar from "chokidar";
import postcss from "postcss";
import autoprefixer from "autoprefixer";
import cssnano from "cssnano";
import settings from "../../settings.local.json";
import postcssImport from "postcss-import";
import postcssBanner from "postcss-banner";
// Define input and output file types
interface FileConfig {
input: string;
output: string;
}
const mode = process.argv.includes("--watch") ? "watch" : "build";
// List of files to process
const files: FileConfig[] = [
{
input: "src/styles/default-css/10.0.0/default.scss",
output:
mode === "build"
? path.relative(process.cwd(), path.resolve("../../Website/Resources/Shared/stylesheets/dnndefault/10.0.0/default.css"))
: path.resolve(settings.WebsitePath, "Resources/Shared/stylesheets/dnndefault/10.0.0/default.css"),
},
{
input: "src/styles/install/install.scss",
output:
mode === "build"
? path.relative(process.cwd(), path.resolve("../../Website/Install/Install.css"))
: path.resolve(settings.WebsitePath, "Install/Install.css"),
}
// { input: "src/scripts/test.ts", output: "dist/scripts/test.js" },
];
// Helper function to ensure directories exist
function ensureDirectoryExists(filePath: string): void {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
function normalizePath(filePath: string): string {
return path.resolve(filePath).replace(/\\/g, "/");
}
// Compile SCSS to CSS with sourcemaps
async function buildScss(input: string, output: string): Promise<void> {
try {
const result = await sass.compileAsync(
input,
{
sourceMap: true,
sourceMapIncludeSources: true,
}
);
const cssWithBanner = result.css;
const postcssResult = await postcss([
postcssImport(),
autoprefixer,
cssnano,
postcssBanner({
banner: "This file is generated by the Dnn.ClientSide project.\nDo not edit it directly.\nChanges will be overwritten upon upgrades.\n",
important: true,
})
])
.process(cssWithBanner, {
from: normalizePath(input),
to: normalizePath(output),
map: {
inline: false,
annotation: `${path.basename(output)}.map`,
prev: result.sourceMap,
sourcesContent: true,
},
});
ensureDirectoryExists(output);
console.log(`Writing CSS to: ${output}`);
fs.writeFileSync(output, postcssResult.css);
console.log(`CSS written to: ${output}`);
if (postcssResult.map) {
const sourcemapPath = `${output}.map`;
fs.writeFileSync(sourcemapPath, JSON.stringify(postcssResult.map));
console.log(`Sourcemap written to: ${sourcemapPath}`);
}
} catch (error) {
console.error(`Error compiling SCSS for ${input}:`, error);
}
}
// Bundle TypeScript/JavaScript with esbuild
async function buildJs(input: string, output: string): Promise<void> {
try {
ensureDirectoryExists(output);
await esbuild.build({
entryPoints: [input],
outfile: output,
bundle: true,
sourcemap: true,
format: "iife",
target: "es2018",
minify: true,
});
console.log(`JS written to: ${output}`);
} catch (error) {
console.error(`Error bundling JS for ${input}:`, error);
}
}
// Process all files
async function buildAll(): Promise<void> {
for (const { input, output } of files) {
if (input.endsWith(".scss")) {
await buildScss(input, output);
} else if (input.endsWith(".ts") || input.endsWith(".js")) {
await buildJs(input, output);
}
}
}
// Watch for changes (optional)
function watchFiles(): void {
const watcher = chokidar.watch(
"./src",
{
ignored: /(^|[\/\\])\../, // Ignore dotfiles
persistent: true,
ignoreInitial: true,
});
buildAll();
watcher.on("all", async (event, filePath) => {
await buildAll();
});
}
// Entry point
const args = process.argv.slice(2);
if (args.includes("--watch")) {
console.log("Watching for file changes...");
watchFiles();
} else {
buildAll();
}