@sparknest/ui-build-vue 1.1.10 → 1.1.12
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/package.json +6 -2
- package/src/dual-declarations.mjs +61 -0
- package/src/index.mjs +5 -0
- package/src/vue-declarations.mjs +127 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparknest/ui-build-vue",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.12",
|
|
4
4
|
"description": "Shared Vue component-library build core for Cedar and Maple",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"@vitejs/plugin-vue": "^5.2.4",
|
|
23
23
|
"@vitejs/plugin-vue-jsx": "^3.1.0",
|
|
24
24
|
"less": "^4.2.0",
|
|
25
|
-
"vite": "^5.4.21"
|
|
25
|
+
"vite": "^5.4.21",
|
|
26
|
+
"vue-compat-legacy": "npm:vue@3.2.0"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
29
|
"typescript": ">=5",
|
|
@@ -35,5 +36,8 @@
|
|
|
35
36
|
"publishConfig": {
|
|
36
37
|
"access": "public",
|
|
37
38
|
"registry": "https://registry.npmjs.org/"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "~5.6.3"
|
|
38
42
|
}
|
|
39
43
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { mkdirSync, readdirSync, readFileSync, statSync, existsSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
// Keep each module format's declaration graph in the corresponding TS module mode.
|
|
5
|
+
// The caller provides its TypeScript instance, matching the vue-tsc used to emit types.
|
|
6
|
+
export function writeDualDeclarations(root, ts) {
|
|
7
|
+
if (!ts || typeof ts.createSourceFile !== 'function') {
|
|
8
|
+
throw new Error('TypeScript compiler API is required to generate dual declarations')
|
|
9
|
+
}
|
|
10
|
+
root = resolve(root)
|
|
11
|
+
const files = []
|
|
12
|
+
function collect(directory) {
|
|
13
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
14
|
+
const file = join(directory, entry.name)
|
|
15
|
+
if (entry.isDirectory()) collect(file)
|
|
16
|
+
else if (entry.isFile() && entry.name.endsWith('.d.ts')) files.push(file)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
collect(root)
|
|
20
|
+
const contents = new Map(files.map((file) => [file, readFileSync(file, 'utf8')]))
|
|
21
|
+
const outputs = []
|
|
22
|
+
for (const [file, text] of contents) {
|
|
23
|
+
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
|
24
|
+
if (source.parseDiagnostics.length) throw new Error(`Cannot parse declaration: ${file}`)
|
|
25
|
+
const edits = []
|
|
26
|
+
function add(literal) {
|
|
27
|
+
if (!literal || !ts.isStringLiteral(literal) || !literal.text.startsWith('.')) return
|
|
28
|
+
let specifier = literal.text
|
|
29
|
+
let target = resolve(dirname(file), specifier)
|
|
30
|
+
if (existsSync(target) && statSync(target).isDirectory()) {
|
|
31
|
+
target = join(target, 'index')
|
|
32
|
+
specifier = specifier === '.' ? './index' : `${specifier}/index`
|
|
33
|
+
}
|
|
34
|
+
if (!contents.has(`${target}.d.ts`)) {
|
|
35
|
+
throw new Error(`Unresolved declaration ${literal.text} in ${relative(root, file)}`)
|
|
36
|
+
}
|
|
37
|
+
edits.push({ start: literal.getStart(source) + 1, end: literal.getEnd() - 1, specifier })
|
|
38
|
+
}
|
|
39
|
+
function visit(node) {
|
|
40
|
+
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) add(node.moduleSpecifier)
|
|
41
|
+
else if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument)) add(node.argument.literal)
|
|
42
|
+
else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
|
|
43
|
+
add(node.moduleReference.expression)
|
|
44
|
+
}
|
|
45
|
+
ts.forEachChild(node, visit)
|
|
46
|
+
}
|
|
47
|
+
visit(source)
|
|
48
|
+
edits.sort((a, b) => b.start - a.start)
|
|
49
|
+
for (const [suffix, extension] of [['.d.ts', 'js'], ['.d.cts', 'cjs']]) {
|
|
50
|
+
let result = text
|
|
51
|
+
for (const edit of edits) {
|
|
52
|
+
result = result.slice(0, edit.start) + `${edit.specifier}.${extension}` + result.slice(edit.end)
|
|
53
|
+
}
|
|
54
|
+
outputs.push([file.slice(0, -'.d.ts'.length) + suffix, result])
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const [file, text] of outputs) {
|
|
58
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
59
|
+
writeFileSync(file, text)
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -10,7 +10,9 @@ import { build } from 'vite'
|
|
|
10
10
|
import { collectModuleEntries } from './module-entries.mjs'
|
|
11
11
|
import { libraryDefines } from './environment.mjs'
|
|
12
12
|
import { browserTargets } from './browser-targets.mjs'
|
|
13
|
+
import { downlevelVueDeclarations } from './vue-declarations.mjs'
|
|
13
14
|
export { browserTargets } from './browser-targets.mjs'
|
|
15
|
+
export { downlevelVueDeclarations } from './vue-declarations.mjs'
|
|
14
16
|
|
|
15
17
|
export function defineConfig(config) {
|
|
16
18
|
return config
|
|
@@ -412,6 +414,9 @@ export async function buildLibrary(userConfig) {
|
|
|
412
414
|
await compileStyles({ ...config, outputs: ['es', 'lib', 'umd'] })
|
|
413
415
|
if (config.emitTypes !== false) {
|
|
414
416
|
await run('vue-tsc', ['-p', config.tsconfig], config.root)
|
|
417
|
+
if (existsSync(resolve(config.root, '../../node_modules/vue-compat-min/package.json'))) {
|
|
418
|
+
downlevelVueDeclarations(config.root, 'types', createRequire(resolve(config.root, 'package.json'))('typescript'))
|
|
419
|
+
}
|
|
415
420
|
}
|
|
416
421
|
|
|
417
422
|
await assertOutputs(config)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
4
|
+
|
|
5
|
+
const baselines = new Map()
|
|
6
|
+
function baseline(ts, packageDirectory) {
|
|
7
|
+
let root = resolve(packageDirectory)
|
|
8
|
+
while (!existsSync(join(root, 'node_modules/vue-compat-min/package.json')) && root !== dirname(root)) {
|
|
9
|
+
root = dirname(root)
|
|
10
|
+
}
|
|
11
|
+
// Workspaces may explicitly select their minimum; standalone projects use
|
|
12
|
+
// the tool's pinned Vue 3.2.0 baseline instead of relying on a sibling repo.
|
|
13
|
+
const hasWorkspaceBaseline = existsSync(join(root, 'node_modules/vue-compat-min/package.json'))
|
|
14
|
+
const require = hasWorkspaceBaseline ? createRequire(join(root, 'package.json')) : createRequire(import.meta.url)
|
|
15
|
+
const entry = require.resolve(`${hasWorkspaceBaseline ? 'vue-compat-min' : 'vue-compat-legacy'}/dist/vue.d.ts`)
|
|
16
|
+
if (baselines.has(entry)) return baselines.get(entry)
|
|
17
|
+
const program = ts.createProgram([entry], { moduleResolution: ts.ModuleResolutionKind.Node10, target: ts.ScriptTarget.ESNext, types: [], skipLibCheck: true })
|
|
18
|
+
const checker = program.getTypeChecker()
|
|
19
|
+
const exports = new Map(checker.getExportsOfModule(checker.getSymbolAtLocation(program.getSourceFile(entry))).map((symbol) => {
|
|
20
|
+
const name = symbol.name
|
|
21
|
+
if (symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol)
|
|
22
|
+
return [name, Math.max(0, ...(symbol.declarations ?? []).map((node) => node.typeParameters?.length ?? 0))]
|
|
23
|
+
}))
|
|
24
|
+
baselines.set(entry, exports)
|
|
25
|
+
return exports
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// These are type conveniences absent from 3.2.47, expressed using its public API.
|
|
29
|
+
// Runtime helpers are never shimmed by this declaration transform.
|
|
30
|
+
const conveniences = new Set(['PublicProps', 'SlotsType', 'AriaAttributes', 'ShallowRef', 'MaybeRef', 'MaybeRefOrGetter', 'ComponentProvideOptions', 'GlobalDirectives', 'Reactive', 'ClassValue', 'Attrs'])
|
|
31
|
+
const convenienceSource = `import type { AllowedComponentProps, ComponentCustomProps, HTMLAttributes, Ref, VNodeProps } from 'vue'
|
|
32
|
+
export type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
|
|
33
|
+
export type SlotsType<T extends Record<string, any> = Record<string, any>> = { readonly __slots?: T }
|
|
34
|
+
export type AriaAttributes = Pick<HTMLAttributes, Extract<keyof HTMLAttributes, \`aria-\${string}\`>>
|
|
35
|
+
export type ShallowRef<T = any> = Ref<T>
|
|
36
|
+
export type MaybeRef<T = any> = T | Ref<T>
|
|
37
|
+
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
|
|
38
|
+
export type Reactive<T> = T extends object ? ReturnType<typeof import('vue').reactive<T>> : T
|
|
39
|
+
export type ClassValue = false | null | undefined | string | Record<string, any> | Array<ClassValue>
|
|
40
|
+
export type Attrs = import('vue').ComponentPublicInstance['$attrs']
|
|
41
|
+
export type ComponentProvideOptions = Record<string | symbol, unknown> | (() => Record<string | symbol, unknown>)
|
|
42
|
+
export interface GlobalDirectives {}
|
|
43
|
+
`
|
|
44
|
+
|
|
45
|
+
export function downlevelVueDeclarations(packageDirectory, outputDirectory, ts) {
|
|
46
|
+
const root = resolve(packageDirectory, outputDirectory)
|
|
47
|
+
const exports = baseline(ts, packageDirectory)
|
|
48
|
+
const files = []
|
|
49
|
+
const collect = (dir) => {
|
|
50
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
51
|
+
const file = join(dir, entry.name)
|
|
52
|
+
if (entry.isDirectory()) collect(file)
|
|
53
|
+
else if (/\.d\.(?:ts|cts|mts)$/.test(entry.name) && entry.name !== 'vue-compat.d.ts') files.push(file)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
collect(root)
|
|
57
|
+
const printer = ts.createPrinter()
|
|
58
|
+
for (const file of files) {
|
|
59
|
+
const text = readFileSync(file, 'utf8')
|
|
60
|
+
if (!/['"]vue['"]/.test(text)) continue
|
|
61
|
+
const ast = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true)
|
|
62
|
+
const imports = new Map()
|
|
63
|
+
const namespaces = new Set()
|
|
64
|
+
for (const node of ast.statements) if (ts.isImportDeclaration(node) && node.moduleSpecifier.text === 'vue') {
|
|
65
|
+
const bindings = node.importClause?.namedBindings
|
|
66
|
+
if (bindings && ts.isNamedImports(bindings)) for (const item of bindings.elements) imports.set(item.name.text, (item.propertyName ?? item.name).text)
|
|
67
|
+
if (bindings && ts.isNamespaceImport(bindings)) namespaces.add(bindings.name.text)
|
|
68
|
+
}
|
|
69
|
+
let helper = relative(dirname(file), join(root, 'vue-compat')).replaceAll('\\', '/')
|
|
70
|
+
if (!helper.startsWith('.')) helper = `./${helper}`
|
|
71
|
+
const importType = (module, name, args, isTypeOf = false) => ts.factory.createImportTypeNode(ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(module)), undefined, ts.factory.createIdentifier(name), args, isTypeOf)
|
|
72
|
+
const nameOf = (node) => {
|
|
73
|
+
if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument) && node.argument.literal.text === 'vue' && node.qualifier && ts.isIdentifier(node.qualifier)) return node.qualifier.text
|
|
74
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
75
|
+
if (ts.isIdentifier(node.typeName)) return imports.get(node.typeName.text)
|
|
76
|
+
if (ts.isQualifiedName(node.typeName) && ts.isIdentifier(node.typeName.left) && namespaces.has(node.typeName.left.text)) return node.typeName.right.text
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const transformed = ts.transform(ast, [(context) => {
|
|
80
|
+
const visit = (original) => {
|
|
81
|
+
const name = nameOf(original)
|
|
82
|
+
if (!name) return ts.visitEachChild(original, visit, context)
|
|
83
|
+
let target = name
|
|
84
|
+
let args = original.typeArguments ? [...original.typeArguments] : undefined
|
|
85
|
+
let node
|
|
86
|
+
let slots
|
|
87
|
+
if (name === 'CreateComponentPublicInstanceWithMixins') {
|
|
88
|
+
target = 'CreateComponentPublicInstance'
|
|
89
|
+
slots = original.typeArguments?.[12]
|
|
90
|
+
args = args?.slice(0, 11)
|
|
91
|
+
} else if (name === 'DefineComponent') slots = original.typeArguments?.[12]
|
|
92
|
+
if (exports.has(target)) {
|
|
93
|
+
if (args) args = args.slice(0, exports.get(target))
|
|
94
|
+
node = importType('vue', target, args?.length ? args : undefined, original.isTypeOf)
|
|
95
|
+
} else if (conveniences.has(target)) node = importType(helper, target, args)
|
|
96
|
+
else throw new Error(`${file}: cannot downlevel unknown Vue type ${target}; choose a 3.2-compatible public type`)
|
|
97
|
+
node = ts.visitEachChild(node, visit, context)
|
|
98
|
+
// Vue 3.2 has no SlotsType parameter. Preserve consumer slot inference as
|
|
99
|
+
// an explicit instance intersection, rather than deleting the slot API.
|
|
100
|
+
if (slots && nameOf(slots) === 'SlotsType' && slots.typeArguments?.[0]) {
|
|
101
|
+
const slotType = ts.visitNode(slots.typeArguments[0], visit)
|
|
102
|
+
const members = [ts.factory.createPropertySignature(undefined, '$slots', undefined, slotType)]
|
|
103
|
+
const exposed = ts.factory.createTypeLiteralNode(members)
|
|
104
|
+
const extra = name === 'DefineComponent'
|
|
105
|
+
? ts.factory.createTypeLiteralNode([ts.factory.createConstructSignature(undefined, [], exposed)])
|
|
106
|
+
: exposed
|
|
107
|
+
node = ts.factory.createIntersectionTypeNode([node, extra])
|
|
108
|
+
}
|
|
109
|
+
return node
|
|
110
|
+
}
|
|
111
|
+
return (source) => {
|
|
112
|
+
const transformed = ts.visitNode(source, visit)
|
|
113
|
+
// References to unavailable named convenience types were rewritten above;
|
|
114
|
+
// retaining their old imports would still break strict consumer checking.
|
|
115
|
+
return ts.factory.updateSourceFile(transformed, transformed.statements.flatMap((node) => {
|
|
116
|
+
if (!ts.isImportDeclaration(node) || node.moduleSpecifier.text !== 'vue' || !node.importClause?.namedBindings || !ts.isNamedImports(node.importClause.namedBindings)) return [node]
|
|
117
|
+
const bindings = node.importClause.namedBindings.elements.filter((item) => exports.has((item.propertyName ?? item.name).text))
|
|
118
|
+
if (!bindings.length && !node.importClause.name) return []
|
|
119
|
+
return [ts.factory.updateImportDeclaration(node, node.modifiers, ts.factory.updateImportClause(node.importClause, node.importClause.isTypeOnly, node.importClause.name, ts.factory.updateNamedImports(node.importClause.namedBindings, bindings)), node.moduleSpecifier, node.attributes)]
|
|
120
|
+
}))
|
|
121
|
+
}
|
|
122
|
+
}])
|
|
123
|
+
writeFileSync(file, printer.printFile(transformed.transformed[0]))
|
|
124
|
+
transformed.dispose()
|
|
125
|
+
}
|
|
126
|
+
writeFileSync(join(root, 'vue-compat.d.ts'), convenienceSource)
|
|
127
|
+
}
|