@sparknest/ui-build-vue 1.4.2 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ui-build.mjs +0 -1
- package/bin/ui-compat-declarations.mjs +5 -0
- package/package.json +14 -12
- package/src/dual-declarations.mjs +6 -3
- package/src/index.mjs +149 -103
- package/src/module-entries.mjs +5 -1
- package/src/normalize-vue-type-files.mjs +12 -0
- package/src/vue-declarations.mjs +148 -58
package/bin/ui-build.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparknest/ui-build-vue",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
|
+
"description": "Shared Vue component-library build core for Cedar and Maple",
|
|
4
5
|
"repository": {
|
|
5
6
|
"type": "git",
|
|
6
7
|
"url": "git+https://github.com/sparknest-team/design-ui.git",
|
|
7
8
|
"directory": "packages/ui-build"
|
|
8
9
|
},
|
|
9
|
-
"description": "Shared Vue component-library build core for Cedar and Maple",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"type": "module",
|
|
12
12
|
"exports": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"bin": {
|
|
20
|
-
"ui-build": "./bin/ui-build.mjs"
|
|
20
|
+
"ui-build": "./bin/ui-build.mjs",
|
|
21
|
+
"ui-compat-declarations": "./bin/ui-compat-declarations.mjs"
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
24
|
"bin",
|
|
@@ -26,23 +27,24 @@
|
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@vitejs/plugin-vue": "^6.0.11",
|
|
28
29
|
"@vitejs/plugin-vue-jsx": "^5.1.6",
|
|
29
|
-
"less": "^4.
|
|
30
|
+
"less": "^4.9.1",
|
|
30
31
|
"vite": "^8.3.8",
|
|
31
|
-
"vue-compat-legacy": "npm:vue@3.2.0"
|
|
32
|
+
"vue-compat-legacy": "npm:vue@3.2.0",
|
|
33
|
+
"typescript-js": "npm:typescript@6.0.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"typescript": "7.1.0-beta",
|
|
37
|
+
"vue": "3.5.46"
|
|
32
38
|
},
|
|
33
39
|
"peerDependencies": {
|
|
34
|
-
"typescript": ">=
|
|
35
|
-
"vue": ">=3.2"
|
|
36
|
-
"vue-tsc": ">=2"
|
|
40
|
+
"typescript": ">=7.1.0-beta",
|
|
41
|
+
"vue": ">=3.2"
|
|
37
42
|
},
|
|
38
43
|
"engines": {
|
|
39
|
-
"node": ">=
|
|
44
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
40
45
|
},
|
|
41
46
|
"publishConfig": {
|
|
42
47
|
"access": "public",
|
|
43
48
|
"registry": "https://registry.npmjs.org/"
|
|
44
|
-
},
|
|
45
|
-
"devDependencies": {
|
|
46
|
-
"typescript": "~5.6.3"
|
|
47
49
|
}
|
|
48
50
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { mkdirSync, readdirSync, readFileSync, statSync,
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
2
2
|
import { dirname, join, relative, resolve } from 'node:path'
|
|
3
3
|
|
|
4
4
|
// Keep each module format's declaration graph in the corresponding TS module mode.
|
|
5
|
-
// The caller provides its TypeScript instance,
|
|
5
|
+
// The caller provides its TypeScript instance, for transforming the generated declarations.
|
|
6
6
|
export function writeDualDeclarations(root, ts) {
|
|
7
7
|
if (!ts || typeof ts.createSourceFile !== 'function') {
|
|
8
8
|
throw new Error('TypeScript compiler API is required to generate dual declarations')
|
|
@@ -46,7 +46,10 @@ export function writeDualDeclarations(root, ts) {
|
|
|
46
46
|
}
|
|
47
47
|
visit(source)
|
|
48
48
|
edits.sort((a, b) => b.start - a.start)
|
|
49
|
-
for (const [suffix, extension] of [
|
|
49
|
+
for (const [suffix, extension] of [
|
|
50
|
+
['.d.ts', 'js'],
|
|
51
|
+
['.d.cts', 'cjs'],
|
|
52
|
+
]) {
|
|
50
53
|
let result = text
|
|
51
54
|
for (const edit of edits) {
|
|
52
55
|
result = result.slice(0, edit.start) + `${edit.specifier}.${extension}` + result.slice(edit.end)
|
package/src/index.mjs
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process'
|
|
2
|
-
import { createRequire } from 'node:module'
|
|
3
2
|
import { existsSync } from 'node:fs'
|
|
4
|
-
import { mkdir,
|
|
3
|
+
import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { normalizeVueTypeFiles } from './normalize-vue-type-files.mjs'
|
|
5
|
+
import { createRequire } from 'node:module'
|
|
5
6
|
import { basename, dirname, relative, resolve } from 'node:path'
|
|
6
7
|
import vue from '@vitejs/plugin-vue'
|
|
7
8
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
|
8
9
|
import less from 'less'
|
|
9
10
|
import { build } from 'vite'
|
|
10
|
-
import { collectModuleEntries } from './module-entries.mjs'
|
|
11
|
-
import { libraryDefines } from './environment.mjs'
|
|
12
11
|
import { browserTargets } from './browser-targets.mjs'
|
|
12
|
+
import { libraryDefines } from './environment.mjs'
|
|
13
|
+
import { collectModuleEntries } from './module-entries.mjs'
|
|
13
14
|
import { downlevelVueDeclarations } from './vue-declarations.mjs'
|
|
15
|
+
|
|
16
|
+
export { writeDualDeclarations } from './dual-declarations.mjs'
|
|
14
17
|
export { browserTargets } from './browser-targets.mjs'
|
|
15
18
|
export { downlevelVueDeclarations } from './vue-declarations.mjs'
|
|
16
19
|
|
|
@@ -20,19 +23,26 @@ export function defineConfig(config) {
|
|
|
20
23
|
|
|
21
24
|
async function walk(root) {
|
|
22
25
|
const entries = await readdir(root, { withFileTypes: true })
|
|
23
|
-
return (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
return (
|
|
27
|
+
await Promise.all(
|
|
28
|
+
entries.map(async (entry) => {
|
|
29
|
+
const path = resolve(root, entry.name)
|
|
30
|
+
return entry.isDirectory() ? walk(path) : [path]
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
33
|
+
).flat()
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
function run(binary, args, cwd) {
|
|
30
37
|
return new Promise((resolvePromise, reject) => {
|
|
31
|
-
const child = spawn(binary, args, {
|
|
38
|
+
const child = spawn(binary, args, {
|
|
39
|
+
cwd, stdio: 'inherit',
|
|
40
|
+
env: { ...process.env, VUE_CONTENT_MAPPER_WORKERS: process.env.VUE_CONTENT_MAPPER_WORKERS ?? '1' },
|
|
41
|
+
})
|
|
32
42
|
child.on('error', reject)
|
|
33
|
-
child.on('exit', (code) =>
|
|
34
|
-
? resolvePromise()
|
|
35
|
-
|
|
43
|
+
child.on('exit', (code) =>
|
|
44
|
+
code === 0 ? resolvePromise() : reject(new Error(`${binary} exited with code ${code}`)),
|
|
45
|
+
)
|
|
36
46
|
})
|
|
37
47
|
}
|
|
38
48
|
|
|
@@ -85,7 +95,9 @@ function extractVueStyleBlocks(source) {
|
|
|
85
95
|
|
|
86
96
|
async function compileVueSfcStyles({ root, sourceDir, outputs, cssVariables }) {
|
|
87
97
|
const sourceRoot = resolve(root, sourceDir)
|
|
88
|
-
const sourceFiles = (await walk(sourceRoot)).filter(
|
|
98
|
+
const sourceFiles = (await walk(sourceRoot)).filter(
|
|
99
|
+
(path) => path.endsWith('.vue') && !isSkippedSource(relative(sourceRoot, path)),
|
|
100
|
+
)
|
|
89
101
|
|
|
90
102
|
for (const sourcePath of sourceFiles) {
|
|
91
103
|
const source = await readFile(sourcePath, 'utf8')
|
|
@@ -106,10 +118,12 @@ async function compileVueSfcStyles({ root, sourceDir, outputs, cssVariables }) {
|
|
|
106
118
|
})
|
|
107
119
|
css = rendered.css
|
|
108
120
|
}
|
|
109
|
-
await Promise.all(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
121
|
+
await Promise.all(
|
|
122
|
+
outputs.map(async (output) => {
|
|
123
|
+
await mkdir(dirname(resolve(root, output, target)), { recursive: true })
|
|
124
|
+
await writeFile(resolve(root, output, target), css)
|
|
125
|
+
}),
|
|
126
|
+
)
|
|
113
127
|
}
|
|
114
128
|
}
|
|
115
129
|
}
|
|
@@ -120,32 +134,35 @@ async function rewriteComponentStyleEntries({ root, cssVariables }) {
|
|
|
120
134
|
if (!existsSync(esRoot)) {
|
|
121
135
|
return
|
|
122
136
|
}
|
|
123
|
-
const dirs = (await readdir(esRoot, { withFileTypes: true }))
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
lines.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
137
|
+
const dirs = (await readdir(esRoot, { withFileTypes: true })).filter(
|
|
138
|
+
(entry) => entry.isDirectory() && !skip.has(entry.name),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
await Promise.all(
|
|
142
|
+
dirs.map(async (entry) => {
|
|
143
|
+
const dirRoot = resolve(esRoot, entry.name)
|
|
144
|
+
const cssFiles = (await walk(dirRoot)).filter((path) => {
|
|
145
|
+
const rel = relative(dirRoot, path).replaceAll('\\', '/')
|
|
146
|
+
return path.endsWith('.css') && !rel.startsWith('style/')
|
|
147
|
+
})
|
|
148
|
+
if (!cssFiles.length) {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
const specs = cssFiles.map((path) => `../${relative(dirRoot, path).replaceAll('\\', '/')}`)
|
|
152
|
+
const lines = specs.map((spec) => `import '${spec}'`)
|
|
153
|
+
if (cssVariables) {
|
|
154
|
+
lines.unshift(`import '../../${cssVariables.replace(/\.less$/, '.css')}'`)
|
|
155
|
+
}
|
|
156
|
+
const esCode = `${lines.join('\n')}\n`
|
|
157
|
+
const cjsCode = esCode.replace(/^import '([^']+)'$/gm, "require('$1')")
|
|
158
|
+
await Promise.all([
|
|
159
|
+
mkdir(resolve(root, 'es', entry.name, 'style'), { recursive: true }),
|
|
160
|
+
mkdir(resolve(root, 'lib', entry.name, 'style'), { recursive: true }),
|
|
161
|
+
])
|
|
162
|
+
await writeFile(resolve(root, 'es', entry.name, 'style', 'index.mjs'), esCode)
|
|
163
|
+
await writeFile(resolve(root, 'lib', entry.name, 'style', 'index.js'), cjsCode)
|
|
164
|
+
}),
|
|
165
|
+
)
|
|
149
166
|
}
|
|
150
167
|
|
|
151
168
|
/**
|
|
@@ -180,9 +197,7 @@ async function renderAggregatedLibraryCss(sourceRoot) {
|
|
|
180
197
|
const source = await readFile(path, 'utf8')
|
|
181
198
|
for (const match of source.matchAll(/import\s+['"]([^'"]+)['"]/g)) {
|
|
182
199
|
if (!match[1].startsWith('.') && !match[1].endsWith('.css')) continue
|
|
183
|
-
const target = match[1].startsWith('.')
|
|
184
|
-
? resolve(dirname(path), match[1])
|
|
185
|
-
: createRequire(path).resolve(match[1])
|
|
200
|
+
const target = match[1].startsWith('.') ? resolve(dirname(path), match[1]) : createRequire(path).resolve(match[1])
|
|
186
201
|
await collect(/\.(?:less|css|ts)$/.test(target) ? target : resolve(target, 'index.ts'))
|
|
187
202
|
}
|
|
188
203
|
}
|
|
@@ -190,7 +205,7 @@ async function renderAggregatedLibraryCss(sourceRoot) {
|
|
|
190
205
|
await collect(foundationPath)
|
|
191
206
|
}
|
|
192
207
|
// Match on-demand dependency order in the full-library entry as well.
|
|
193
|
-
for (const entry of sourceFiles.filter(path => /\/style\/index\.ts$/.test(path.replaceAll('\\', '/'))).sort()) {
|
|
208
|
+
for (const entry of sourceFiles.filter((path) => /\/style\/index\.ts$/.test(path.replaceAll('\\', '/'))).sort()) {
|
|
194
209
|
await collect(entry)
|
|
195
210
|
}
|
|
196
211
|
for (const rel of styleLess) {
|
|
@@ -225,49 +240,64 @@ async function compileStyles(config) {
|
|
|
225
240
|
}
|
|
226
241
|
const styleEntries = sourceFiles.filter((path) => /\/style\/index\.ts$/.test(path.replaceAll('\\', '/')))
|
|
227
242
|
|
|
228
|
-
for (const sourcePath of sourceFiles.filter(
|
|
243
|
+
for (const sourcePath of sourceFiles.filter(
|
|
244
|
+
(path) => /\.(?:[cm]?[jt]sx?|vue)$/.test(path) && !isSkippedSource(relative(sourceRoot, path)),
|
|
245
|
+
)) {
|
|
229
246
|
const source = await readFile(sourcePath, 'utf8')
|
|
230
247
|
for (const match of source.matchAll(/(?:import|from)\s*['"]([^'"]+\.less)['"]/g)) {
|
|
231
248
|
files.add(resolve(dirname(sourcePath), match[1]))
|
|
232
249
|
}
|
|
233
250
|
}
|
|
234
251
|
|
|
235
|
-
await Promise.all(
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const css = await renderLessFile(path)
|
|
240
|
-
const target = relative(sourceRoot, path).replace(/\.less$/, '.css')
|
|
241
|
-
await Promise.all(outputs.map(async (output) => {
|
|
242
|
-
await mkdir(dirname(resolve(root, output, target)), { recursive: true })
|
|
243
|
-
await writeFile(resolve(root, output, target), css)
|
|
244
|
-
}))
|
|
245
|
-
}))
|
|
246
|
-
|
|
247
|
-
await Promise.all(styleEntries.map(async (entry) => {
|
|
248
|
-
const styleDir = dirname(entry)
|
|
249
|
-
const targetDir = relative(sourceRoot, styleDir)
|
|
250
|
-
// Keep the source side-effect graph: shared CSS must remain separate modules.
|
|
251
|
-
const source = await readFile(entry, 'utf8')
|
|
252
|
-
const imports = [...new Set([...source.matchAll(/import\s+['"]([^'"]+)['"]/g)]
|
|
253
|
-
.map((match) => match[1].replace(/\.less$/, '.css')))]
|
|
254
|
-
const resolveImport = (specifier, extension) => {
|
|
255
|
-
if (specifier.startsWith('.') && existsSync(resolve(styleDir, specifier, 'index.ts'))) {
|
|
256
|
-
return `${specifier}/index.${extension}`
|
|
252
|
+
await Promise.all(
|
|
253
|
+
[...files].map(async (path) => {
|
|
254
|
+
if (!existsSync(path)) {
|
|
255
|
+
return
|
|
257
256
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
257
|
+
const css = await renderLessFile(path)
|
|
258
|
+
const target = relative(sourceRoot, path).replace(/\.less$/, '.css')
|
|
259
|
+
await Promise.all(
|
|
260
|
+
outputs.map(async (output) => {
|
|
261
|
+
await mkdir(dirname(resolve(root, output, target)), { recursive: true })
|
|
262
|
+
await writeFile(resolve(root, output, target), css)
|
|
263
|
+
}),
|
|
264
|
+
)
|
|
265
|
+
}),
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
await Promise.all(
|
|
269
|
+
styleEntries.map(async (entry) => {
|
|
270
|
+
const styleDir = dirname(entry)
|
|
271
|
+
const targetDir = relative(sourceRoot, styleDir)
|
|
272
|
+
// Keep the source side-effect graph: shared CSS must remain separate modules.
|
|
273
|
+
const source = await readFile(entry, 'utf8')
|
|
274
|
+
const imports = [
|
|
275
|
+
...new Set(
|
|
276
|
+
[...source.matchAll(/import\s+['"]([^'"]+)['"]/g)].map((match) => match[1].replace(/\.less$/, '.css')),
|
|
277
|
+
),
|
|
278
|
+
]
|
|
279
|
+
const resolveImport = (specifier, extension) => {
|
|
280
|
+
if (specifier.startsWith('.') && existsSync(resolve(styleDir, specifier, 'index.ts'))) {
|
|
281
|
+
return `${specifier}/index.${extension}`
|
|
282
|
+
}
|
|
283
|
+
return specifier.replace(/\.ts$/, `.${extension}`)
|
|
284
|
+
}
|
|
285
|
+
const esCode = imports.length
|
|
286
|
+
? imports.map((s) => `import '${resolveImport(s, 'mjs')}'\n`).join('')
|
|
287
|
+
: 'export {}\n'
|
|
288
|
+
const cjsCode = imports.length
|
|
289
|
+
? imports.map((s) => `require('${resolveImport(s, 'js')}')\n`).join('')
|
|
290
|
+
: "'use strict'\n"
|
|
291
|
+
await Promise.all([
|
|
292
|
+
mkdir(resolve(root, 'es', targetDir), { recursive: true }),
|
|
293
|
+
mkdir(resolve(root, 'lib', targetDir), { recursive: true }),
|
|
294
|
+
])
|
|
295
|
+
await Promise.all([
|
|
296
|
+
writeFile(resolve(root, 'es', targetDir, 'index.mjs'), esCode),
|
|
297
|
+
writeFile(resolve(root, 'lib', targetDir, 'index.js'), cjsCode),
|
|
298
|
+
])
|
|
299
|
+
}),
|
|
300
|
+
)
|
|
271
301
|
|
|
272
302
|
if (vueSfcStyles) {
|
|
273
303
|
await compileVueSfcStyles(config)
|
|
@@ -281,21 +311,25 @@ async function compileStyles(config) {
|
|
|
281
311
|
bundleCss = await renderAggregatedLibraryCss(sourceRoot)
|
|
282
312
|
}
|
|
283
313
|
if (bundleCss == null) {
|
|
284
|
-
const cssFiles = (await walk(resolve(root, 'es'))).filter(
|
|
314
|
+
const cssFiles = (await walk(resolve(root, 'es'))).filter(
|
|
315
|
+
(path) => path.endsWith('.css') && !path.endsWith(`${bundleName}.css`),
|
|
316
|
+
)
|
|
285
317
|
bundleCss = (await Promise.all(cssFiles.map((path) => readFile(path, 'utf8')))).join('\n')
|
|
286
318
|
}
|
|
287
319
|
|
|
288
|
-
await Promise.all(
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
320
|
+
await Promise.all(
|
|
321
|
+
outputs.map(async (output) => {
|
|
322
|
+
await mkdir(resolve(root, output), { recursive: true })
|
|
323
|
+
await writeFile(resolve(root, output, `${bundleName}.css`), bundleCss)
|
|
324
|
+
}),
|
|
325
|
+
)
|
|
292
326
|
}
|
|
293
327
|
|
|
294
328
|
function vitePlugins(config) {
|
|
295
329
|
return [vue(), vueJsx(), styleImportPlugin(), ...(config.vueSfcStyles ? [ignoreVueStylePlugin()] : [])]
|
|
296
330
|
}
|
|
297
331
|
|
|
298
|
-
async function buildModules(config, format, outDir, extension) {
|
|
332
|
+
async function buildModules(config, entries, format, outDir, extension) {
|
|
299
333
|
const root = resolve(config.root)
|
|
300
334
|
await build({
|
|
301
335
|
root,
|
|
@@ -310,8 +344,8 @@ async function buildModules(config, format, outDir, extension) {
|
|
|
310
344
|
emptyOutDir: false,
|
|
311
345
|
minify: false,
|
|
312
346
|
outDir,
|
|
313
|
-
|
|
314
|
-
input:
|
|
347
|
+
rolldownOptions: {
|
|
348
|
+
input: entries,
|
|
315
349
|
external: externalMatcher(config.external),
|
|
316
350
|
preserveEntrySignatures: 'strict',
|
|
317
351
|
output: {
|
|
@@ -351,11 +385,13 @@ async function assertNoBundlerHelpers(config) {
|
|
|
351
385
|
}
|
|
352
386
|
}
|
|
353
387
|
if (offenders.length) {
|
|
354
|
-
throw new Error(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
388
|
+
throw new Error(
|
|
389
|
+
[
|
|
390
|
+
'Library output contains bundler-private preload helpers; consumers bundling with rolldown/vite 8 cannot parse them:',
|
|
391
|
+
...offenders.map((path) => ` ${path}`),
|
|
392
|
+
'Replace the dynamic import() in the matching source file with a static import and mark the module external.',
|
|
393
|
+
].join('\n'),
|
|
394
|
+
)
|
|
359
395
|
}
|
|
360
396
|
}
|
|
361
397
|
|
|
@@ -383,7 +419,7 @@ async function buildBundle(config, format, outDir, fileName) {
|
|
|
383
419
|
formats: [format],
|
|
384
420
|
fileName: () => fileName,
|
|
385
421
|
},
|
|
386
|
-
|
|
422
|
+
rolldownOptions: {
|
|
387
423
|
external: externalMatcher(format === 'umd' ? config.umdExternal : config.external),
|
|
388
424
|
output: { exports: 'named', globals: config.globals },
|
|
389
425
|
},
|
|
@@ -405,7 +441,11 @@ async function assertOutputs(config) {
|
|
|
405
441
|
}
|
|
406
442
|
const missing = []
|
|
407
443
|
for (const path of required) {
|
|
408
|
-
try {
|
|
444
|
+
try {
|
|
445
|
+
await readFile(resolve(root, path))
|
|
446
|
+
} catch {
|
|
447
|
+
missing.push(path)
|
|
448
|
+
}
|
|
409
449
|
}
|
|
410
450
|
if (missing.length) throw new Error(`Missing build outputs:\n${missing.join('\n')}`)
|
|
411
451
|
}
|
|
@@ -415,7 +455,9 @@ export async function buildLibrary(userConfig) {
|
|
|
415
455
|
// release. Reject it before cleaning any existing output, rather than silently
|
|
416
456
|
// mutating the environment of another build in the same process.
|
|
417
457
|
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'production') {
|
|
418
|
-
throw new Error(
|
|
458
|
+
throw new Error(
|
|
459
|
+
'buildLibrary requires NODE_ENV=production (or unset); custom compile/test modes are not release builds',
|
|
460
|
+
)
|
|
419
461
|
}
|
|
420
462
|
const config = {
|
|
421
463
|
sourceDir: 'src',
|
|
@@ -442,19 +484,23 @@ export async function buildLibrary(userConfig) {
|
|
|
442
484
|
})
|
|
443
485
|
}
|
|
444
486
|
|
|
445
|
-
await
|
|
446
|
-
await buildModules(config, '
|
|
487
|
+
const entries = await collectModuleEntries(config.root, config.sourceDir, config.entry)
|
|
488
|
+
await buildModules(config, entries, 'es', 'es', 'mjs')
|
|
489
|
+
await buildModules(config, entries, 'cjs', 'lib', 'js')
|
|
447
490
|
await assertNoBundlerHelpers(config)
|
|
448
491
|
await buildBundle(config, 'es', 'es', `${config.bundleName}.esm.js`)
|
|
449
492
|
await buildBundle(config, 'cjs', 'lib', `${config.bundleName}.cjs.js`)
|
|
450
493
|
await buildBundle(config, 'umd', 'umd', `${config.bundleName}.js`)
|
|
451
494
|
await compileStyles({ ...config, outputs: ['es', 'lib', 'umd'] })
|
|
452
495
|
if (config.emitTypes !== false) {
|
|
453
|
-
await run('
|
|
496
|
+
await run('tsc', ['--runExternalCode', '-p', config.tsconfig], config.root)
|
|
497
|
+
await normalizeVueTypeFiles(resolve(config.root, 'types'))
|
|
454
498
|
if (existsSync(resolve(config.root, '../../node_modules/vue-compat-min/package.json'))) {
|
|
455
|
-
downlevelVueDeclarations(config.root, 'types', createRequire(
|
|
499
|
+
downlevelVueDeclarations(config.root, 'types', createRequire(import.meta.url)('typescript-js'))
|
|
456
500
|
}
|
|
457
501
|
}
|
|
458
502
|
|
|
459
503
|
await assertOutputs(config)
|
|
460
504
|
}
|
|
505
|
+
|
|
506
|
+
export { normalizeVueTypeFiles }
|
package/src/module-entries.mjs
CHANGED
|
@@ -13,7 +13,11 @@ export async function collectModuleEntries(root, sourceDir, entry) {
|
|
|
13
13
|
const full = resolve(directory, item.name)
|
|
14
14
|
if (item.isDirectory()) await visit(full)
|
|
15
15
|
else if (/^index\.(?:ts|tsx|js|mjs)$/.test(item.name) && full !== entries.index) {
|
|
16
|
-
entries[
|
|
16
|
+
entries[
|
|
17
|
+
relative(sourceRoot, full)
|
|
18
|
+
.replaceAll('\\', '/')
|
|
19
|
+
.replace(/\.[^.]+$/, '')
|
|
20
|
+
] = full
|
|
17
21
|
}
|
|
18
22
|
}
|
|
19
23
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { readdir, rename } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
// TypeScript 7 emits Component.d.vue.ts for arbitrary extensions. Keep the
|
|
5
|
+
// package's existing Component.vue.d.ts paths for older TypeScript consumers.
|
|
6
|
+
export async function normalizeVueTypeFiles(directory) {
|
|
7
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
8
|
+
const path = join(directory, entry.name)
|
|
9
|
+
if (entry.isDirectory()) await normalizeVueTypeFiles(path)
|
|
10
|
+
else if (entry.name.endsWith('.d.vue.ts')) await rename(path, path.replace(/\.d\.vue\.ts$/, '.vue.d.ts'))
|
|
11
|
+
}
|
|
12
|
+
}
|
package/src/vue-declarations.mjs
CHANGED
|
@@ -14,20 +14,42 @@ function baseline(ts, packageDirectory) {
|
|
|
14
14
|
const require = hasWorkspaceBaseline ? createRequire(join(root, 'package.json')) : createRequire(import.meta.url)
|
|
15
15
|
const entry = require.resolve(`${hasWorkspaceBaseline ? 'vue-compat-min' : 'vue-compat-legacy'}/dist/vue.d.ts`)
|
|
16
16
|
if (baselines.has(entry)) return baselines.get(entry)
|
|
17
|
-
const program = ts.createProgram([entry], {
|
|
17
|
+
const program = ts.createProgram([entry], {
|
|
18
|
+
moduleResolution: ts.ModuleResolutionKind.Node10,
|
|
19
|
+
target: ts.ScriptTarget.ESNext,
|
|
20
|
+
types: [],
|
|
21
|
+
skipLibCheck: true,
|
|
22
|
+
})
|
|
18
23
|
const checker = program.getTypeChecker()
|
|
19
|
-
const exports = new Map(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
const exports = new Map(
|
|
25
|
+
checker.getExportsOfModule(checker.getSymbolAtLocation(program.getSourceFile(entry))).map((symbol) => {
|
|
26
|
+
const name = symbol.name
|
|
27
|
+
if (symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol)
|
|
28
|
+
return [name, Math.max(0, ...(symbol.declarations ?? []).map((node) => node.typeParameters?.length ?? 0))]
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
24
31
|
baselines.set(entry, exports)
|
|
25
32
|
return exports
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
// These are type conveniences absent from 3.2.47, expressed using its public API.
|
|
29
36
|
// Runtime helpers are never shimmed by this declaration transform.
|
|
30
|
-
const conveniences = new Set([
|
|
37
|
+
const conveniences = new Set([
|
|
38
|
+
'PublicProps',
|
|
39
|
+
'SlotsType',
|
|
40
|
+
'AriaAttributes',
|
|
41
|
+
'ShallowRef',
|
|
42
|
+
'MaybeRef',
|
|
43
|
+
'MaybeRefOrGetter',
|
|
44
|
+
'ComponentProvideOptions',
|
|
45
|
+
'GlobalDirectives',
|
|
46
|
+
'Reactive',
|
|
47
|
+
'ClassValue',
|
|
48
|
+
'Attrs',
|
|
49
|
+
'InputAutoCompleteAttribute',
|
|
50
|
+
'InputTypeHTMLAttribute',
|
|
51
|
+
'ReservedProps',
|
|
52
|
+
])
|
|
31
53
|
const convenienceSource = `import type { AllowedComponentProps, ComponentCustomProps, HTMLAttributes, Ref, VNodeProps } from 'vue'
|
|
32
54
|
export type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
|
|
33
55
|
export type SlotsType<T extends Record<string, any> = Record<string, any>> = { readonly __slots?: T }
|
|
@@ -37,6 +59,9 @@ export type MaybeRef<T = any> = T | Ref<T>
|
|
|
37
59
|
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
|
|
38
60
|
export type Reactive<T> = T extends object ? ReturnType<typeof import('vue').reactive<T>> : T
|
|
39
61
|
export type ClassValue = false | null | undefined | string | Record<string, any> | Array<ClassValue>
|
|
62
|
+
export type InputAutoCompleteAttribute = NonNullable<import('vue').InputHTMLAttributes['autocomplete']>
|
|
63
|
+
export type InputTypeHTMLAttribute = NonNullable<import('vue').InputHTMLAttributes['type']>
|
|
64
|
+
export type ReservedProps = Pick<VNodeProps, 'key' | 'ref'> & { ref_for?: boolean; ref_key?: string }
|
|
40
65
|
export type Attrs = import('vue').ComponentPublicInstance['$attrs']
|
|
41
66
|
export type ComponentProvideOptions = Record<string | symbol, unknown> | (() => Record<string | symbol, unknown>)
|
|
42
67
|
export interface GlobalDirectives {}
|
|
@@ -59,67 +84,132 @@ export function downlevelVueDeclarations(packageDirectory, outputDirectory, ts)
|
|
|
59
84
|
const text = readFileSync(file, 'utf8')
|
|
60
85
|
if (!/['"]vue['"]/.test(text)) continue
|
|
61
86
|
const ast = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true)
|
|
87
|
+
// Vue language tools can append an empty synthetic component to a
|
|
88
|
+
// script-only SFC which already forwards its implementation's default.
|
|
89
|
+
// The forwarded declaration is the component's full public contract.
|
|
90
|
+
if (file.endsWith('.vue.d.ts') && ast.statements.length === 4) {
|
|
91
|
+
const [forward, component, alias, assignment] = ast.statements
|
|
92
|
+
if (
|
|
93
|
+
ts.isExportDeclaration(forward) && forward.moduleSpecifier && forward.exportClause &&
|
|
94
|
+
ts.isNamedExports(forward.exportClause) && forward.exportClause.elements.some(item => item.name.text === 'default') &&
|
|
95
|
+
ts.isVariableStatement(component) && component.declarationList.declarations[0]?.name.getText(ast) === '__VLS_export' &&
|
|
96
|
+
ts.isVariableStatement(alias) && alias.declarationList.declarations[0]?.type?.getText(ast) === 'typeof __VLS_export' &&
|
|
97
|
+
ts.isExportAssignment(assignment) && assignment.expression.getText(ast) === alias.declarationList.declarations[0].name.getText(ast)
|
|
98
|
+
) {
|
|
99
|
+
writeFileSync(file, forward.getText(ast) + '\n')
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
}
|
|
62
103
|
const imports = new Map()
|
|
63
104
|
const namespaces = new Set()
|
|
64
|
-
for (const node of ast.statements)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
105
|
+
for (const node of ast.statements)
|
|
106
|
+
if (ts.isImportDeclaration(node) && node.moduleSpecifier.text === 'vue') {
|
|
107
|
+
const bindings = node.importClause?.namedBindings
|
|
108
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
109
|
+
for (const item of bindings.elements) imports.set(item.name.text, (item.propertyName ?? item.name).text)
|
|
110
|
+
if (bindings && ts.isNamespaceImport(bindings)) namespaces.add(bindings.name.text)
|
|
111
|
+
}
|
|
69
112
|
let helper = relative(dirname(file), join(root, 'vue-compat')).replaceAll('\\', '/')
|
|
70
113
|
if (!helper.startsWith('.')) helper = `./${helper}`
|
|
71
|
-
const importType = (module, name, args, isTypeOf = false) =>
|
|
114
|
+
const importType = (module, name, args, isTypeOf = false) =>
|
|
115
|
+
ts.factory.createImportTypeNode(
|
|
116
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(module)),
|
|
117
|
+
undefined,
|
|
118
|
+
ts.factory.createIdentifier(name),
|
|
119
|
+
args,
|
|
120
|
+
isTypeOf,
|
|
121
|
+
)
|
|
72
122
|
const nameOf = (node) => {
|
|
73
|
-
if (
|
|
123
|
+
if (
|
|
124
|
+
ts.isImportTypeNode(node) &&
|
|
125
|
+
ts.isLiteralTypeNode(node.argument) &&
|
|
126
|
+
node.argument.literal.text === 'vue' &&
|
|
127
|
+
node.qualifier &&
|
|
128
|
+
ts.isIdentifier(node.qualifier)
|
|
129
|
+
)
|
|
130
|
+
return node.qualifier.text
|
|
74
131
|
if (ts.isTypeReferenceNode(node)) {
|
|
75
132
|
if (ts.isIdentifier(node.typeName)) return imports.get(node.typeName.text)
|
|
76
|
-
if (
|
|
133
|
+
if (
|
|
134
|
+
ts.isQualifiedName(node.typeName) &&
|
|
135
|
+
ts.isIdentifier(node.typeName.left) &&
|
|
136
|
+
namespaces.has(node.typeName.left.text)
|
|
137
|
+
)
|
|
138
|
+
return node.typeName.right.text
|
|
77
139
|
}
|
|
78
140
|
}
|
|
79
|
-
const transformed = ts.transform(ast, [
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
141
|
+
const transformed = ts.transform(ast, [
|
|
142
|
+
(context) => {
|
|
143
|
+
const visit = (original) => {
|
|
144
|
+
const name = nameOf(original)
|
|
145
|
+
if (!name) return ts.visitEachChild(original, visit, context)
|
|
146
|
+
let target = name
|
|
147
|
+
let args = original.typeArguments ? [...original.typeArguments] : undefined
|
|
148
|
+
let node
|
|
149
|
+
let slots
|
|
150
|
+
if (name === 'CreateComponentPublicInstanceWithMixins') {
|
|
151
|
+
target = 'CreateComponentPublicInstance'
|
|
152
|
+
slots = original.typeArguments?.[12]
|
|
153
|
+
args = args?.slice(0, 11)
|
|
154
|
+
} else if (name === 'DefineComponent') slots = original.typeArguments?.[12]
|
|
155
|
+
if (exports.has(target)) {
|
|
156
|
+
if (args) args = args.slice(0, exports.get(target))
|
|
157
|
+
node = importType('vue', target, args?.length ? args : undefined, original.isTypeOf)
|
|
158
|
+
} else if (conveniences.has(target)) node = importType(helper, target, args)
|
|
159
|
+
else
|
|
160
|
+
throw new Error(`${file}: cannot downlevel unknown Vue type ${target}; choose a 3.2-compatible public type`)
|
|
161
|
+
node = ts.visitEachChild(node, visit, context)
|
|
162
|
+
// Vue 3.2 has no SlotsType parameter. Preserve consumer slot inference as
|
|
163
|
+
// an explicit instance intersection, rather than deleting the slot API.
|
|
164
|
+
if (slots && nameOf(slots) === 'SlotsType' && slots.typeArguments?.[0]) {
|
|
165
|
+
const slotType = ts.visitNode(slots.typeArguments[0], visit)
|
|
166
|
+
const members = [ts.factory.createPropertySignature(undefined, '$slots', undefined, slotType)]
|
|
167
|
+
const exposed = ts.factory.createTypeLiteralNode(members)
|
|
168
|
+
const extra =
|
|
169
|
+
name === 'DefineComponent'
|
|
170
|
+
? ts.factory.createTypeLiteralNode([ts.factory.createConstructSignature(undefined, [], exposed)])
|
|
171
|
+
: exposed
|
|
172
|
+
node = ts.factory.createIntersectionTypeNode([node, extra])
|
|
173
|
+
}
|
|
174
|
+
return node
|
|
108
175
|
}
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
176
|
+
return (source) => {
|
|
177
|
+
const transformed = ts.visitNode(source, visit)
|
|
178
|
+
// References to unavailable named convenience types were rewritten above;
|
|
179
|
+
// retaining their old imports would still break strict consumer checking.
|
|
180
|
+
return ts.factory.updateSourceFile(
|
|
181
|
+
transformed,
|
|
182
|
+
transformed.statements.flatMap((node) => {
|
|
183
|
+
if (
|
|
184
|
+
!ts.isImportDeclaration(node) ||
|
|
185
|
+
node.moduleSpecifier.text !== 'vue' ||
|
|
186
|
+
!node.importClause?.namedBindings ||
|
|
187
|
+
!ts.isNamedImports(node.importClause.namedBindings)
|
|
188
|
+
)
|
|
189
|
+
return [node]
|
|
190
|
+
const bindings = node.importClause.namedBindings.elements.filter((item) =>
|
|
191
|
+
exports.has((item.propertyName ?? item.name).text),
|
|
192
|
+
)
|
|
193
|
+
if (!bindings.length && !node.importClause.name) return []
|
|
194
|
+
return [
|
|
195
|
+
ts.factory.updateImportDeclaration(
|
|
196
|
+
node,
|
|
197
|
+
node.modifiers,
|
|
198
|
+
ts.factory.updateImportClause(
|
|
199
|
+
node.importClause,
|
|
200
|
+
node.importClause.isTypeOnly,
|
|
201
|
+
node.importClause.name,
|
|
202
|
+
ts.factory.updateNamedImports(node.importClause.namedBindings, bindings),
|
|
203
|
+
),
|
|
204
|
+
node.moduleSpecifier,
|
|
205
|
+
node.attributes,
|
|
206
|
+
),
|
|
207
|
+
]
|
|
208
|
+
}),
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
])
|
|
123
213
|
writeFileSync(file, printer.printFile(transformed.transformed[0]))
|
|
124
214
|
transformed.dispose()
|
|
125
215
|
}
|