-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathbuild.js
More file actions
243 lines (211 loc) · 7.18 KB
/
Copy pathbuild.js
File metadata and controls
243 lines (211 loc) · 7.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Build script: compiles Svelte components and bundles into dist/index.js (CJS)
*
* 1. Compile .svelte files → .js with svelte/compiler (server mode)
* 2. Transpile TypeScript from .ts files using the TypeScript compiler
* 3. Create ESM entry point
* 4. Bundle with esbuild into CJS
*/
const { compile } = require('svelte/compiler');
const ts = require('typescript');
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const SRC_DIR = path.join(__dirname, 'src');
const BUILD_DIR = path.join(__dirname, '.build');
const DIST_DIR = path.join(__dirname, 'dist');
const tsCompilerOptions = {
target: ts.ScriptTarget.ES2020,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.Bundler,
esModuleInterop: true,
strict: true,
skipLibCheck: true,
verbatimModuleSyntax: false,
};
/**
* Transpile TypeScript to JavaScript using the TypeScript compiler API.
* Handles all TS syntax properly (generics, Record<K,V>, interfaces, etc.)
*/
function transpileTS(code, fileName = 'file.ts') {
const result = ts.transpileModule(code, {
compilerOptions: tsCompilerOptions,
fileName,
reportDiagnostics: true,
});
if (result.diagnostics && result.diagnostics.length > 0) {
for (const diag of result.diagnostics) {
const msg = ts.flattenDiagnosticMessageText(diag.messageText, '\n');
console.warn(`TS warning in ${fileName}: ${msg}`);
}
}
return result.outputText;
}
function collectFiles(dir, exts, files = []) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) collectFiles(full, exts, files);
else if (exts.some(e => entry.name.endsWith(e))) files.push(full);
}
return files;
}
function ensureDir(dir) {
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
}
function build() {
// Clean build dir
if (fs.existsSync(BUILD_DIR)) fs.rmSync(BUILD_DIR, { recursive: true });
ensureDir(BUILD_DIR);
ensureDir(DIST_DIR);
// Compile Svelte files
const svelteFiles = collectFiles(SRC_DIR, ['.svelte']);
for (const file of svelteFiles) {
const source = fs.readFileSync(file, 'utf-8');
// Transpile TS in script blocks using the TypeScript compiler
const processed = source.replace(
/<script([^>]*)>([\s\S]*?)<\/script>/g,
(match, attrs, content) => {
if (attrs.includes('lang="ts"') || attrs.includes("lang='ts'")) {
const jsContent = transpileTS(content, file);
return `<script${attrs.replace(/\s*lang=["']ts["']/, '')}>${jsContent}</script>`;
}
return match;
}
);
const result = compile(processed, {
generate: 'server',
name: path.basename(file, '.svelte'),
filename: file,
css: 'injected',
});
const relPath = path.relative(SRC_DIR, file).replace('.svelte', '.js');
const outPath = path.join(BUILD_DIR, relPath);
ensureDir(path.dirname(outPath));
// Fix import paths: .svelte → .js, .ts → .js
let code = result.js.code;
code = code.replace(/(from\s+['"][^'"]*)\.(svelte|ts)(['"])/g, '$1.js$3');
fs.writeFileSync(outPath, code);
}
// Compile TS files
const tsFiles = collectFiles(SRC_DIR, ['.ts']);
for (const file of tsFiles) {
const source = fs.readFileSync(file, 'utf-8');
let code = transpileTS(source, file);
// Fix import paths
code = code.replace(/(from\s+['"][^'"]*)\.(ts)(['"])/g, '$1.js$3');
const relPath = path.relative(SRC_DIR, file).replace('.ts', '.js');
const outPath = path.join(BUILD_DIR, relPath);
ensureDir(path.dirname(outPath));
fs.writeFileSync(outPath, code);
}
// Create entry point
const entryCode = `
import { render as svelteRender } from 'svelte/server';
import Resume from './components/Resume.js';
import { setI18nLanguage } from './utils/helpers.js';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
let language = "en-gb";
export function changeLanguage(lang) {
let i18nLang = lang;
switch (lang) {
case "en-gb":
i18nLang = "en";
break;
}
setI18nLanguage(i18nLang);
language = lang;
}
function buildThemeOverrides(meta) {
if (!meta || !meta.theme) return '';
const theme = meta.theme;
const mapping = {
primaryColor: '--color-accent',
textColor: '--color-text',
textSecondaryColor: '--color-text-secondary',
headingColor: '--color-heading',
linkColor: '--color-link',
backgroundColor: '--color-background',
backgroundAltColor: '--color-background-alt',
borderColor: '--color-border',
keywordTextColor: '--color-keyword-text',
keywordBgColor: '--color-keyword-bg',
fontFamily: '--font-family',
};
const overrides = [];
for (const [key, cssVar] of Object.entries(mapping)) {
if (theme[key]) {
overrides.push(' ' + cssVar + ': ' + theme[key] + ';');
}
}
if (overrides.length === 0) return '';
return ':root {\\n' + overrides.join('\\n') + '\\n}';
}
export function render(resume, options) {
if (options && options.language) {
changeLanguage(options.language);
}
const stylePath = join(__dirname, '..', 'styles', 'global.css');
const css = readFileSync(stylePath, 'utf-8');
const result = svelteRender(Resume, {
props: { resume, language }
});
let body = result.body;
let head = result.head || '';
// Remove Svelte SSR comment markers
body = body.replace(/<!--\\[-->/g, '').replace(/<!--\\]-->/g, '');
const name = resume.basics?.name || '';
const themeOverrides = buildThemeOverrides(resume.meta);
return \`<!doctype html>
<html lang="__LANG__">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>\${name}</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer">
<link rel="stylesheet" href="./override.css">
<style>
\${css}
</style>
\${head}\${themeOverrides ? '\\n <style>\\n ' + themeOverrides + '\\n </style>' : ''}
</head>
<body>
\${body}
</body>
</html>\`.replace('__LANG__', language);
}
const marginValue = '0.8 cm';
export const pdfRenderOptions = {
margin: {
top: marginValue,
bottom: marginValue,
left: marginValue,
right: marginValue,
}
};
`;
fs.writeFileSync(path.join(BUILD_DIR, 'index.js'), entryCode);
// Bundle with esbuild
esbuild.buildSync({
entryPoints: [path.join(BUILD_DIR, 'index.js')],
bundle: true,
platform: 'node',
format: 'cjs',
outfile: path.join(DIST_DIR, 'index.js'),
external: ['fs', 'path', 'url'],
target: 'node18',
treeShaking: true,
minifySyntax: true,
minifyWhitespace: true,
define: {},
banner: {
js: '// Auto-generated by build.js — do not edit\n',
},
});
// Clean up build dir
fs.rmSync(BUILD_DIR, { recursive: true });
const stat = fs.statSync(path.join(DIST_DIR, 'index.js'));
console.log(`Built dist/index.js (${(stat.size / 1024).toFixed(1)}KB)`);
}
build();