@sparknest/ui-build-vue 1.4.2 → 1.4.3
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/package.json +5 -5
- package/src/dual-declarations.mjs +5 -2
- package/src/index.mjs +131 -94
- package/src/module-entries.mjs +5 -1
- package/src/vue-declarations.mjs +126 -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.3",
|
|
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": {
|
|
@@ -30,6 +30,9 @@
|
|
|
30
30
|
"vite": "^8.3.8",
|
|
31
31
|
"vue-compat-legacy": "npm:vue@3.2.0"
|
|
32
32
|
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "~5.6.3"
|
|
35
|
+
},
|
|
33
36
|
"peerDependencies": {
|
|
34
37
|
"typescript": ">=5",
|
|
35
38
|
"vue": ">=3.2",
|
|
@@ -41,8 +44,5 @@
|
|
|
41
44
|
"publishConfig": {
|
|
42
45
|
"access": "public",
|
|
43
46
|
"registry": "https://registry.npmjs.org/"
|
|
44
|
-
},
|
|
45
|
-
"devDependencies": {
|
|
46
|
-
"typescript": "~5.6.3"
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
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.
|
|
@@ -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,17 @@
|
|
|
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 { createRequire } from 'node:module'
|
|
5
5
|
import { basename, dirname, relative, resolve } from 'node:path'
|
|
6
6
|
import vue from '@vitejs/plugin-vue'
|
|
7
7
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
|
8
8
|
import less from 'less'
|
|
9
9
|
import { build } from 'vite'
|
|
10
|
-
import { collectModuleEntries } from './module-entries.mjs'
|
|
11
|
-
import { libraryDefines } from './environment.mjs'
|
|
12
10
|
import { browserTargets } from './browser-targets.mjs'
|
|
11
|
+
import { libraryDefines } from './environment.mjs'
|
|
12
|
+
import { collectModuleEntries } from './module-entries.mjs'
|
|
13
13
|
import { downlevelVueDeclarations } from './vue-declarations.mjs'
|
|
14
|
+
|
|
14
15
|
export { browserTargets } from './browser-targets.mjs'
|
|
15
16
|
export { downlevelVueDeclarations } from './vue-declarations.mjs'
|
|
16
17
|
|
|
@@ -20,19 +21,23 @@ export function defineConfig(config) {
|
|
|
20
21
|
|
|
21
22
|
async function walk(root) {
|
|
22
23
|
const entries = await readdir(root, { withFileTypes: true })
|
|
23
|
-
return (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
return (
|
|
25
|
+
await Promise.all(
|
|
26
|
+
entries.map(async (entry) => {
|
|
27
|
+
const path = resolve(root, entry.name)
|
|
28
|
+
return entry.isDirectory() ? walk(path) : [path]
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
).flat()
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
function run(binary, args, cwd) {
|
|
30
35
|
return new Promise((resolvePromise, reject) => {
|
|
31
36
|
const child = spawn(binary, args, { cwd, stdio: 'inherit' })
|
|
32
37
|
child.on('error', reject)
|
|
33
|
-
child.on('exit', (code) =>
|
|
34
|
-
? resolvePromise()
|
|
35
|
-
|
|
38
|
+
child.on('exit', (code) =>
|
|
39
|
+
code === 0 ? resolvePromise() : reject(new Error(`${binary} exited with code ${code}`)),
|
|
40
|
+
)
|
|
36
41
|
})
|
|
37
42
|
}
|
|
38
43
|
|
|
@@ -85,7 +90,9 @@ function extractVueStyleBlocks(source) {
|
|
|
85
90
|
|
|
86
91
|
async function compileVueSfcStyles({ root, sourceDir, outputs, cssVariables }) {
|
|
87
92
|
const sourceRoot = resolve(root, sourceDir)
|
|
88
|
-
const sourceFiles = (await walk(sourceRoot)).filter(
|
|
93
|
+
const sourceFiles = (await walk(sourceRoot)).filter(
|
|
94
|
+
(path) => path.endsWith('.vue') && !isSkippedSource(relative(sourceRoot, path)),
|
|
95
|
+
)
|
|
89
96
|
|
|
90
97
|
for (const sourcePath of sourceFiles) {
|
|
91
98
|
const source = await readFile(sourcePath, 'utf8')
|
|
@@ -106,10 +113,12 @@ async function compileVueSfcStyles({ root, sourceDir, outputs, cssVariables }) {
|
|
|
106
113
|
})
|
|
107
114
|
css = rendered.css
|
|
108
115
|
}
|
|
109
|
-
await Promise.all(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
await Promise.all(
|
|
117
|
+
outputs.map(async (output) => {
|
|
118
|
+
await mkdir(dirname(resolve(root, output, target)), { recursive: true })
|
|
119
|
+
await writeFile(resolve(root, output, target), css)
|
|
120
|
+
}),
|
|
121
|
+
)
|
|
113
122
|
}
|
|
114
123
|
}
|
|
115
124
|
}
|
|
@@ -120,32 +129,35 @@ async function rewriteComponentStyleEntries({ root, cssVariables }) {
|
|
|
120
129
|
if (!existsSync(esRoot)) {
|
|
121
130
|
return
|
|
122
131
|
}
|
|
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
|
-
|
|
132
|
+
const dirs = (await readdir(esRoot, { withFileTypes: true })).filter(
|
|
133
|
+
(entry) => entry.isDirectory() && !skip.has(entry.name),
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
await Promise.all(
|
|
137
|
+
dirs.map(async (entry) => {
|
|
138
|
+
const dirRoot = resolve(esRoot, entry.name)
|
|
139
|
+
const cssFiles = (await walk(dirRoot)).filter((path) => {
|
|
140
|
+
const rel = relative(dirRoot, path).replaceAll('\\', '/')
|
|
141
|
+
return path.endsWith('.css') && !rel.startsWith('style/')
|
|
142
|
+
})
|
|
143
|
+
if (!cssFiles.length) {
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
const specs = cssFiles.map((path) => `../${relative(dirRoot, path).replaceAll('\\', '/')}`)
|
|
147
|
+
const lines = specs.map((spec) => `import '${spec}'`)
|
|
148
|
+
if (cssVariables) {
|
|
149
|
+
lines.unshift(`import '../../${cssVariables.replace(/\.less$/, '.css')}'`)
|
|
150
|
+
}
|
|
151
|
+
const esCode = `${lines.join('\n')}\n`
|
|
152
|
+
const cjsCode = esCode.replace(/^import '([^']+)'$/gm, "require('$1')")
|
|
153
|
+
await Promise.all([
|
|
154
|
+
mkdir(resolve(root, 'es', entry.name, 'style'), { recursive: true }),
|
|
155
|
+
mkdir(resolve(root, 'lib', entry.name, 'style'), { recursive: true }),
|
|
156
|
+
])
|
|
157
|
+
await writeFile(resolve(root, 'es', entry.name, 'style', 'index.mjs'), esCode)
|
|
158
|
+
await writeFile(resolve(root, 'lib', entry.name, 'style', 'index.js'), cjsCode)
|
|
159
|
+
}),
|
|
160
|
+
)
|
|
149
161
|
}
|
|
150
162
|
|
|
151
163
|
/**
|
|
@@ -180,9 +192,7 @@ async function renderAggregatedLibraryCss(sourceRoot) {
|
|
|
180
192
|
const source = await readFile(path, 'utf8')
|
|
181
193
|
for (const match of source.matchAll(/import\s+['"]([^'"]+)['"]/g)) {
|
|
182
194
|
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])
|
|
195
|
+
const target = match[1].startsWith('.') ? resolve(dirname(path), match[1]) : createRequire(path).resolve(match[1])
|
|
186
196
|
await collect(/\.(?:less|css|ts)$/.test(target) ? target : resolve(target, 'index.ts'))
|
|
187
197
|
}
|
|
188
198
|
}
|
|
@@ -190,7 +200,7 @@ async function renderAggregatedLibraryCss(sourceRoot) {
|
|
|
190
200
|
await collect(foundationPath)
|
|
191
201
|
}
|
|
192
202
|
// 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()) {
|
|
203
|
+
for (const entry of sourceFiles.filter((path) => /\/style\/index\.ts$/.test(path.replaceAll('\\', '/'))).sort()) {
|
|
194
204
|
await collect(entry)
|
|
195
205
|
}
|
|
196
206
|
for (const rel of styleLess) {
|
|
@@ -225,49 +235,64 @@ async function compileStyles(config) {
|
|
|
225
235
|
}
|
|
226
236
|
const styleEntries = sourceFiles.filter((path) => /\/style\/index\.ts$/.test(path.replaceAll('\\', '/')))
|
|
227
237
|
|
|
228
|
-
for (const sourcePath of sourceFiles.filter(
|
|
238
|
+
for (const sourcePath of sourceFiles.filter(
|
|
239
|
+
(path) => /\.(?:[cm]?[jt]sx?|vue)$/.test(path) && !isSkippedSource(relative(sourceRoot, path)),
|
|
240
|
+
)) {
|
|
229
241
|
const source = await readFile(sourcePath, 'utf8')
|
|
230
242
|
for (const match of source.matchAll(/(?:import|from)\s*['"]([^'"]+\.less)['"]/g)) {
|
|
231
243
|
files.add(resolve(dirname(sourcePath), match[1]))
|
|
232
244
|
}
|
|
233
245
|
}
|
|
234
246
|
|
|
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}`
|
|
247
|
+
await Promise.all(
|
|
248
|
+
[...files].map(async (path) => {
|
|
249
|
+
if (!existsSync(path)) {
|
|
250
|
+
return
|
|
257
251
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
252
|
+
const css = await renderLessFile(path)
|
|
253
|
+
const target = relative(sourceRoot, path).replace(/\.less$/, '.css')
|
|
254
|
+
await Promise.all(
|
|
255
|
+
outputs.map(async (output) => {
|
|
256
|
+
await mkdir(dirname(resolve(root, output, target)), { recursive: true })
|
|
257
|
+
await writeFile(resolve(root, output, target), css)
|
|
258
|
+
}),
|
|
259
|
+
)
|
|
260
|
+
}),
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
await Promise.all(
|
|
264
|
+
styleEntries.map(async (entry) => {
|
|
265
|
+
const styleDir = dirname(entry)
|
|
266
|
+
const targetDir = relative(sourceRoot, styleDir)
|
|
267
|
+
// Keep the source side-effect graph: shared CSS must remain separate modules.
|
|
268
|
+
const source = await readFile(entry, 'utf8')
|
|
269
|
+
const imports = [
|
|
270
|
+
...new Set(
|
|
271
|
+
[...source.matchAll(/import\s+['"]([^'"]+)['"]/g)].map((match) => match[1].replace(/\.less$/, '.css')),
|
|
272
|
+
),
|
|
273
|
+
]
|
|
274
|
+
const resolveImport = (specifier, extension) => {
|
|
275
|
+
if (specifier.startsWith('.') && existsSync(resolve(styleDir, specifier, 'index.ts'))) {
|
|
276
|
+
return `${specifier}/index.${extension}`
|
|
277
|
+
}
|
|
278
|
+
return specifier.replace(/\.ts$/, `.${extension}`)
|
|
279
|
+
}
|
|
280
|
+
const esCode = imports.length
|
|
281
|
+
? imports.map((s) => `import '${resolveImport(s, 'mjs')}'\n`).join('')
|
|
282
|
+
: 'export {}\n'
|
|
283
|
+
const cjsCode = imports.length
|
|
284
|
+
? imports.map((s) => `require('${resolveImport(s, 'js')}')\n`).join('')
|
|
285
|
+
: "'use strict'\n"
|
|
286
|
+
await Promise.all([
|
|
287
|
+
mkdir(resolve(root, 'es', targetDir), { recursive: true }),
|
|
288
|
+
mkdir(resolve(root, 'lib', targetDir), { recursive: true }),
|
|
289
|
+
])
|
|
290
|
+
await Promise.all([
|
|
291
|
+
writeFile(resolve(root, 'es', targetDir, 'index.mjs'), esCode),
|
|
292
|
+
writeFile(resolve(root, 'lib', targetDir, 'index.js'), cjsCode),
|
|
293
|
+
])
|
|
294
|
+
}),
|
|
295
|
+
)
|
|
271
296
|
|
|
272
297
|
if (vueSfcStyles) {
|
|
273
298
|
await compileVueSfcStyles(config)
|
|
@@ -281,14 +306,18 @@ async function compileStyles(config) {
|
|
|
281
306
|
bundleCss = await renderAggregatedLibraryCss(sourceRoot)
|
|
282
307
|
}
|
|
283
308
|
if (bundleCss == null) {
|
|
284
|
-
const cssFiles = (await walk(resolve(root, 'es'))).filter(
|
|
309
|
+
const cssFiles = (await walk(resolve(root, 'es'))).filter(
|
|
310
|
+
(path) => path.endsWith('.css') && !path.endsWith(`${bundleName}.css`),
|
|
311
|
+
)
|
|
285
312
|
bundleCss = (await Promise.all(cssFiles.map((path) => readFile(path, 'utf8')))).join('\n')
|
|
286
313
|
}
|
|
287
314
|
|
|
288
|
-
await Promise.all(
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
315
|
+
await Promise.all(
|
|
316
|
+
outputs.map(async (output) => {
|
|
317
|
+
await mkdir(resolve(root, output), { recursive: true })
|
|
318
|
+
await writeFile(resolve(root, output, `${bundleName}.css`), bundleCss)
|
|
319
|
+
}),
|
|
320
|
+
)
|
|
292
321
|
}
|
|
293
322
|
|
|
294
323
|
function vitePlugins(config) {
|
|
@@ -351,11 +380,13 @@ async function assertNoBundlerHelpers(config) {
|
|
|
351
380
|
}
|
|
352
381
|
}
|
|
353
382
|
if (offenders.length) {
|
|
354
|
-
throw new Error(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
383
|
+
throw new Error(
|
|
384
|
+
[
|
|
385
|
+
'Library output contains bundler-private preload helpers; consumers bundling with rolldown/vite 8 cannot parse them:',
|
|
386
|
+
...offenders.map((path) => ` ${path}`),
|
|
387
|
+
'Replace the dynamic import() in the matching source file with a static import and mark the module external.',
|
|
388
|
+
].join('\n'),
|
|
389
|
+
)
|
|
359
390
|
}
|
|
360
391
|
}
|
|
361
392
|
|
|
@@ -405,7 +436,11 @@ async function assertOutputs(config) {
|
|
|
405
436
|
}
|
|
406
437
|
const missing = []
|
|
407
438
|
for (const path of required) {
|
|
408
|
-
try {
|
|
439
|
+
try {
|
|
440
|
+
await readFile(resolve(root, path))
|
|
441
|
+
} catch {
|
|
442
|
+
missing.push(path)
|
|
443
|
+
}
|
|
409
444
|
}
|
|
410
445
|
if (missing.length) throw new Error(`Missing build outputs:\n${missing.join('\n')}`)
|
|
411
446
|
}
|
|
@@ -415,7 +450,9 @@ export async function buildLibrary(userConfig) {
|
|
|
415
450
|
// release. Reject it before cleaning any existing output, rather than silently
|
|
416
451
|
// mutating the environment of another build in the same process.
|
|
417
452
|
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'production') {
|
|
418
|
-
throw new Error(
|
|
453
|
+
throw new Error(
|
|
454
|
+
'buildLibrary requires NODE_ENV=production (or unset); custom compile/test modes are not release builds',
|
|
455
|
+
)
|
|
419
456
|
}
|
|
420
457
|
const config = {
|
|
421
458
|
sourceDir: 'src',
|
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
|
}
|
package/src/vue-declarations.mjs
CHANGED
|
@@ -14,20 +14,39 @@ 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
|
+
])
|
|
31
50
|
const convenienceSource = `import type { AllowedComponentProps, ComponentCustomProps, HTMLAttributes, Ref, VNodeProps } from 'vue'
|
|
32
51
|
export type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
|
|
33
52
|
export type SlotsType<T extends Record<string, any> = Record<string, any>> = { readonly __slots?: T }
|
|
@@ -61,65 +80,114 @@ export function downlevelVueDeclarations(packageDirectory, outputDirectory, ts)
|
|
|
61
80
|
const ast = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true)
|
|
62
81
|
const imports = new Map()
|
|
63
82
|
const namespaces = new Set()
|
|
64
|
-
for (const node of ast.statements)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
83
|
+
for (const node of ast.statements)
|
|
84
|
+
if (ts.isImportDeclaration(node) && node.moduleSpecifier.text === 'vue') {
|
|
85
|
+
const bindings = node.importClause?.namedBindings
|
|
86
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
87
|
+
for (const item of bindings.elements) imports.set(item.name.text, (item.propertyName ?? item.name).text)
|
|
88
|
+
if (bindings && ts.isNamespaceImport(bindings)) namespaces.add(bindings.name.text)
|
|
89
|
+
}
|
|
69
90
|
let helper = relative(dirname(file), join(root, 'vue-compat')).replaceAll('\\', '/')
|
|
70
91
|
if (!helper.startsWith('.')) helper = `./${helper}`
|
|
71
|
-
const importType = (module, name, args, isTypeOf = false) =>
|
|
92
|
+
const importType = (module, name, args, isTypeOf = false) =>
|
|
93
|
+
ts.factory.createImportTypeNode(
|
|
94
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(module)),
|
|
95
|
+
undefined,
|
|
96
|
+
ts.factory.createIdentifier(name),
|
|
97
|
+
args,
|
|
98
|
+
isTypeOf,
|
|
99
|
+
)
|
|
72
100
|
const nameOf = (node) => {
|
|
73
|
-
if (
|
|
101
|
+
if (
|
|
102
|
+
ts.isImportTypeNode(node) &&
|
|
103
|
+
ts.isLiteralTypeNode(node.argument) &&
|
|
104
|
+
node.argument.literal.text === 'vue' &&
|
|
105
|
+
node.qualifier &&
|
|
106
|
+
ts.isIdentifier(node.qualifier)
|
|
107
|
+
)
|
|
108
|
+
return node.qualifier.text
|
|
74
109
|
if (ts.isTypeReferenceNode(node)) {
|
|
75
110
|
if (ts.isIdentifier(node.typeName)) return imports.get(node.typeName.text)
|
|
76
|
-
if (
|
|
111
|
+
if (
|
|
112
|
+
ts.isQualifiedName(node.typeName) &&
|
|
113
|
+
ts.isIdentifier(node.typeName.left) &&
|
|
114
|
+
namespaces.has(node.typeName.left.text)
|
|
115
|
+
)
|
|
116
|
+
return node.typeName.right.text
|
|
77
117
|
}
|
|
78
118
|
}
|
|
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
|
-
|
|
119
|
+
const transformed = ts.transform(ast, [
|
|
120
|
+
(context) => {
|
|
121
|
+
const visit = (original) => {
|
|
122
|
+
const name = nameOf(original)
|
|
123
|
+
if (!name) return ts.visitEachChild(original, visit, context)
|
|
124
|
+
let target = name
|
|
125
|
+
let args = original.typeArguments ? [...original.typeArguments] : undefined
|
|
126
|
+
let node
|
|
127
|
+
let slots
|
|
128
|
+
if (name === 'CreateComponentPublicInstanceWithMixins') {
|
|
129
|
+
target = 'CreateComponentPublicInstance'
|
|
130
|
+
slots = original.typeArguments?.[12]
|
|
131
|
+
args = args?.slice(0, 11)
|
|
132
|
+
} else if (name === 'DefineComponent') slots = original.typeArguments?.[12]
|
|
133
|
+
if (exports.has(target)) {
|
|
134
|
+
if (args) args = args.slice(0, exports.get(target))
|
|
135
|
+
node = importType('vue', target, args?.length ? args : undefined, original.isTypeOf)
|
|
136
|
+
} else if (conveniences.has(target)) node = importType(helper, target, args)
|
|
137
|
+
else
|
|
138
|
+
throw new Error(`${file}: cannot downlevel unknown Vue type ${target}; choose a 3.2-compatible public type`)
|
|
139
|
+
node = ts.visitEachChild(node, visit, context)
|
|
140
|
+
// Vue 3.2 has no SlotsType parameter. Preserve consumer slot inference as
|
|
141
|
+
// an explicit instance intersection, rather than deleting the slot API.
|
|
142
|
+
if (slots && nameOf(slots) === 'SlotsType' && slots.typeArguments?.[0]) {
|
|
143
|
+
const slotType = ts.visitNode(slots.typeArguments[0], visit)
|
|
144
|
+
const members = [ts.factory.createPropertySignature(undefined, '$slots', undefined, slotType)]
|
|
145
|
+
const exposed = ts.factory.createTypeLiteralNode(members)
|
|
146
|
+
const extra =
|
|
147
|
+
name === 'DefineComponent'
|
|
148
|
+
? ts.factory.createTypeLiteralNode([ts.factory.createConstructSignature(undefined, [], exposed)])
|
|
149
|
+
: exposed
|
|
150
|
+
node = ts.factory.createIntersectionTypeNode([node, extra])
|
|
151
|
+
}
|
|
152
|
+
return node
|
|
108
153
|
}
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
154
|
+
return (source) => {
|
|
155
|
+
const transformed = ts.visitNode(source, visit)
|
|
156
|
+
// References to unavailable named convenience types were rewritten above;
|
|
157
|
+
// retaining their old imports would still break strict consumer checking.
|
|
158
|
+
return ts.factory.updateSourceFile(
|
|
159
|
+
transformed,
|
|
160
|
+
transformed.statements.flatMap((node) => {
|
|
161
|
+
if (
|
|
162
|
+
!ts.isImportDeclaration(node) ||
|
|
163
|
+
node.moduleSpecifier.text !== 'vue' ||
|
|
164
|
+
!node.importClause?.namedBindings ||
|
|
165
|
+
!ts.isNamedImports(node.importClause.namedBindings)
|
|
166
|
+
)
|
|
167
|
+
return [node]
|
|
168
|
+
const bindings = node.importClause.namedBindings.elements.filter((item) =>
|
|
169
|
+
exports.has((item.propertyName ?? item.name).text),
|
|
170
|
+
)
|
|
171
|
+
if (!bindings.length && !node.importClause.name) return []
|
|
172
|
+
return [
|
|
173
|
+
ts.factory.updateImportDeclaration(
|
|
174
|
+
node,
|
|
175
|
+
node.modifiers,
|
|
176
|
+
ts.factory.updateImportClause(
|
|
177
|
+
node.importClause,
|
|
178
|
+
node.importClause.isTypeOnly,
|
|
179
|
+
node.importClause.name,
|
|
180
|
+
ts.factory.updateNamedImports(node.importClause.namedBindings, bindings),
|
|
181
|
+
),
|
|
182
|
+
node.moduleSpecifier,
|
|
183
|
+
node.attributes,
|
|
184
|
+
),
|
|
185
|
+
]
|
|
186
|
+
}),
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
])
|
|
123
191
|
writeFileSync(file, printer.printFile(transformed.transformed[0]))
|
|
124
192
|
transformed.dispose()
|
|
125
193
|
}
|