-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsup.config.ts
More file actions
148 lines (127 loc) · 4.83 KB
/
Copy pathtsup.config.ts
File metadata and controls
148 lines (127 loc) · 4.83 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
import { defineConfig } from 'tsup';
import { copyFileSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
import { join, dirname, relative } from 'path';
import { preprocess } from 'svelte/compiler';
import { sveltePreprocess } from 'svelte-preprocess';
import { createLogger } from './src/lib/utils/logger.ts';
const logger = createLogger('tsup-build');
/**
* Recursively copy files matching a pattern from src to dist
*/
function copyFiles(srcDir: string, destDir: string, pattern: RegExp): void {
const items = readdirSync(srcDir);
for (const item of items) {
const srcPath = join(srcDir, item);
const destPath = join(destDir, item);
const stat = statSync(srcPath);
if (stat.isDirectory()) {
mkdirSync(destPath, { recursive: true });
copyFiles(srcPath, destPath, pattern);
} else if (pattern.test(item)) {
mkdirSync(dirname(destPath), { recursive: true });
copyFileSync(srcPath, destPath);
logger.debug({ file: relative(process.cwd(), destPath) }, 'Copied file');
}
}
}
/**
* Recursively preprocess and copy .svelte files from src to dist.
*
* Components are authored with `<script lang="ts">`, but consumers should not
* have to preprocess TypeScript inside a dependency's Svelte files (modern
* Vite/Svelte toolchains no longer do this). We strip the TypeScript here so
* the shipped components contain plain-JS `<script>` blocks that compile
* anywhere — the same thing `svelte-package` does.
*/
async function preprocessSvelteFiles(srcDir: string, destDir: string): Promise<void> {
const items = readdirSync(srcDir);
for (const item of items) {
const srcPath = join(srcDir, item);
const destPath = join(destDir, item);
const stat = statSync(srcPath);
if (stat.isDirectory()) {
mkdirSync(destPath, { recursive: true });
await preprocessSvelteFiles(srcPath, destPath);
} else if (/\.svelte$/.test(item)) {
const source = readFileSync(srcPath, 'utf-8');
const { code } = await preprocess(source, sveltePreprocess(), { filename: srcPath });
// Drop the now-redundant `lang="ts"` so the Svelte compiler treats the
// already-transpiled script as plain JS with no preprocessor required.
const jsCode = code.replace(/(<script[^>]*?)\s+lang=(["'])(?:ts|typescript)\2/g, '$1');
mkdirSync(dirname(destPath), { recursive: true });
writeFileSync(destPath, jsCode);
logger.debug({ file: relative(process.cwd(), destPath) }, 'Preprocessed svelte file');
}
}
}
export default defineConfig({
// Entry points for all public APIs
entry: [
'src/lib/index.ts',
'src/lib/server/index.ts',
'src/lib/server/markdownRenderer.ts',
'src/lib/server/screenshot-service.ts',
'src/lib/plugins/index.ts',
'src/lib/components/index.ts',
'src/lib/utils/index.ts',
'src/lib/config/index.ts',
],
// Output format: ESM only (modern SvelteKit standard)
format: ['esm'],
// Generate TypeScript declaration files
// Note: components/index.ts is excluded from DTS generation as it exports
// Svelte components (.svelte files) which are handled separately by SvelteKit
dts: {
entry: [
'src/lib/index.ts',
'src/lib/server/index.ts',
'src/lib/server/markdownRenderer.ts',
'src/lib/server/screenshot-service.ts',
'src/lib/plugins/index.ts',
'src/lib/utils/index.ts',
'src/lib/config/index.ts',
],
},
// Enable code splitting for better tree-shaking
splitting: true,
// Generate sourcemaps for debugging
sourcemap: true,
// Clean dist/ before each build
clean: true,
// External dependencies (peer deps + runtime deps that should not be bundled)
external: [
'svelte',
'@sveltejs/kit',
'$app/environment',
'$app/navigation',
'mermaid',
'@lucide/svelte',
'playwright',
// Also externalize our own runtime dependencies
'shiki',
'unist-util-visit',
'yaml',
// Server-only dependencies (should not be bundled)
'ts-morph',
'glob',
'chokidar',
],
// esbuild options
esbuildOptions(options) {
// Mark .svelte files as external so they're not processed
options.external = [...(options.external || []), '*.svelte'];
},
// Post-build: Preprocess .svelte files and copy .css/.json files to dist/
onSuccess: async () => {
logger.info('Preprocessing .svelte files and copying .css/.json files to dist/');
const srcLib = join(process.cwd(), 'src/lib');
const distLib = join(process.cwd(), 'dist');
// Preprocess .svelte files (strip TypeScript so consumers don't have to)
await preprocessSvelteFiles(srcLib, distLib);
// Copy .css files (modern CSS with native nesting)
copyFiles(srcLib, distLib, /\.css$/);
// Copy .json files (for grammars, etc.)
copyFiles(srcLib, distLib, /\.json$/);
logger.info('Non-TS files copied successfully');
},
});