-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
110 lines (103 loc) · 3.84 KB
/
Copy pathtsdown.config.ts
File metadata and controls
110 lines (103 loc) · 3.84 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
import { readFile } from 'node:fs/promises'
import { dirname, relative, resolve as resolvePath } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { UserConfig } from 'tsdown'
import { transform } from 'lightningcss'
const PLUGIN_ID = 'dsh-mnemon'
const PROJECT_ROOT = dirname(fileURLToPath(import.meta.url))
const CLIENT_EXTERNALS = [
/^react(?:\/.*)?$/,
/^react-dom(?:\/.*)?$/,
/^cordis(?:\/.*)?$/,
/^@deepseek-ai\/dsh-client-ui-primitives(?:\/.*)?$/,
]
const CSS_VIRTUAL_PREFIX = '\0dsh-mnemon-css:'
const CSS_VIRTUAL_SUFFIX = '.mjs'
const host: UserConfig = {
name: PLUGIN_ID,
entry: {
index: 'src/index.ts', core: 'src/core/plugin.ts', contracts: 'src/core/contracts/index.ts',
'extension-sdk': 'src/sdk/index.ts', testing: 'src/sdk/testing.ts',
},
outDir: 'lib',
format: ['esm'],
platform: 'node',
target: 'es2024',
fixedExtension: false,
dts: false,
clean: true,
deps: { neverBundle: true },
}
const client: UserConfig = {
name: `${PLUGIN_ID}/client`,
entry: { client: 'src/client/index.ts' },
outDir: 'lib',
format: 'cjs',
fixedExtension: false,
outExtensions: () => ({ js: '.js' }),
platform: 'browser',
target: 'es2022',
dts: false,
sourcemap: false,
clean: false,
deps: {
neverBundle: CLIENT_EXTERNALS,
onlyBundle: [],
},
define: {
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV ?? 'production'),
},
plugins: [{
name: 'dsh-mnemon-css-modules-inline',
resolveId(source: string, importer: string | undefined) {
if (source === 'dsh-mnemon/client') return resolvePath(PROJECT_ROOT, 'src/client/extension-sdk.ts')
if (!source.endsWith('.module.css')) return null
const absolute = importer === undefined ? source : resolveAssetPath(source, importer)
return CSS_VIRTUAL_PREFIX + absolute + CSS_VIRTUAL_SUFFIX
},
async load(virtualId: string) {
if (!virtualId.startsWith(CSS_VIRTUAL_PREFIX)) return null
const fileId = virtualId.slice(CSS_VIRTUAL_PREFIX.length, -CSS_VIRTUAL_SUFFIX.length)
this.addWatchFile(fileId)
const source = await readFile(fileId)
const filename = portableRelativePath(PROJECT_ROOT, fileId)
const { code, exports: cssExports } = transform({
filename,
code: source,
cssModules: { pattern: '[hash]_[local]' },
minify: true,
})
const classMap = stableCssClassMap(cssExports)
const tagId = `${PLUGIN_ID}/${filename}`
return [
`const css = ${JSON.stringify(code.toString())};`,
`const tagId = ${JSON.stringify(tagId)};`,
'if (typeof document !== "undefined" && document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]") === null) {',
' const tag = document.createElement("style");',
` tag.dataset.plugin = ${JSON.stringify(PLUGIN_ID)};`,
' tag.dataset.pluginCss = tagId;',
' tag.textContent = css;',
' document.head.appendChild(tag);',
'}',
`export default ${JSON.stringify(classMap)};`,
].join('\n')
},
}],
outputOptions: {
banner: `window.__ModuleLoader__.load({ id: ${JSON.stringify(PLUGIN_ID)}, factory: (require) => {`,
footer: 'return module.exports; } });',
intro: 'var module = { exports: {} }; var exports = module.exports;',
},
}
function resolveAssetPath(source: string, importer: string): string {
return resolvePath(dirname(importer), source)
}
export function portableRelativePath(root: string, path: string): string {
return relative(root, path).replaceAll('\\', '/')
}
export function stableCssClassMap(cssExports: Record<string, { name: string }> | void): Record<string, string> {
return Object.fromEntries(Object.entries(cssExports ?? {})
.sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0)
.map(([local, exported]) => [local, exported.name]))
}
export default [host, client]