Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cli/scripts/generate-api-surface.js at version-1 · sveltejs/cli · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
sveltejs
/
cli
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
105
Star
502
Code
Issues
24
Pull requests
12
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Files
Expand file tree
version-1
Breadcrumbs
cli
/
scripts
/
generate-api-surface.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
234 lines (209 loc) · 6.75 KB
version-1
Breadcrumbs
cli
/
scripts
/
generate-api-surface.js
Copy path
Top
File metadata and controls
Code
Blame
234 lines (209 loc) · 6.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Reads generated `.d.mts` files and writes compact `api-surface.md` snapshots per package.
*
* Strips region/source-map directives, non-deprecated block comments, import-only lines,
* and excess blank lines. Blocks mentioning `@deprecated` are kept verbatim.
*
*
@remarks
* Run: `node scripts/generate-api-surface.js` — or via root `postbuild` after `pnpm build`.
* Output is formatted with Oxfmt using the repo root `oxfmt.config.ts` so it matches `pnpm format`.
*/
import
fs
from
'node:fs'
;
import
path
from
'node:path'
;
import
process
from
'node:process'
;
import
{
format
}
from
'oxfmt'
;
import
oxfmtConfig
from
'../oxfmt.config.ts'
;
const
ROOT
=
path
.
resolve
(
import
.
meta
.
dirname
,
'..'
)
;
const
packages
=
[
{
// Self-contained DTS from the single-entry api-surface build (see tsdown.config.ts).
// The published `dist/src/index.d.mts` only re-exports names from a shared chunk,
// so it would not capture signatures for `defineAddon`, `officialAddons`, etc.
name
:
'sv'
,
dts
:
'packages/sv/dts-api-surface/index/index.d.mts'
,
out
:
'packages/sv/api-surface.md'
}
,
{
name
:
'sv (testing)'
,
dts
:
'packages/sv/dts-api-surface/testing/testing.d.mts'
,
out
:
'packages/sv/api-surface-testing.md'
}
,
{
name
:
'@sveltejs/sv-utils'
,
dts
:
'packages/sv-utils/dist/index.d.mts'
,
out
:
'packages/sv-utils/api-surface.md'
}
]
;
/**
* Remove `//#region` / `//#endregion` lines emitted by the DTS bundler.
*
@param
{
string
} source
*
@returns
{
string
}
*/
function
stripRegionDirectives
(
source
)
{
return
source
.
split
(
'\n'
)
.
filter
(
(
line
)
=>
!
/
^
\s
*
\/
\/
#
(
r
e
g
i
o
n
|
e
n
d
r
e
g
i
o
n
)
\b
/
.
test
(
line
)
)
.
join
(
'\n'
)
;
}
/**
* Remove `//# sourceMappingURL=...` lines from declaration emit.
*
@param
{
string
} source
*
@returns
{
string
}
*/
function
stripSourceMappingUrl
(
source
)
{
return
source
.
split
(
'\n'
)
.
filter
(
(
line
)
=>
!
/
^
\s
*
\/
\/
#
\s
*
s
o
u
r
c
e
M
a
p
p
i
n
g
U
R
L
=
/
.
test
(
line
)
)
.
join
(
'\n'
)
;
}
/**
* Remove slash-star-star block comments unless they contain `@deprecated` (full block kept).
*
@param
{
string
} source
*
@returns
{
string
}
*/
function
stripJsDoc
(
source
)
{
return
source
.
replace
(
/
\/
\*
\*
[
\s
\S
]
*
?
\*
\/
/
g
,
(
match
)
=>
{
if
(
/
@
d
e
p
r
e
c
a
t
e
d
\b
/
.
test
(
match
)
)
{
return
match
;
}
return
''
;
}
)
;
}
/**
* Drop top-level `import …` lines (types-only noise in the snapshot).
*
@param
{
string
} source
*
@returns
{
string
}
*/
function
stripImportLines
(
source
)
{
return
source
.
split
(
'\n'
)
.
filter
(
(
line
)
=>
!
line
.
match
(
/
^
i
m
p
o
r
t
\s
/
)
)
.
join
(
'\n'
)
;
}
/**
* Collapse three or more consecutive newlines to two.
*
@param
{
string
} source
*
@returns
{
string
}
*/
function
collapseBlankLines
(
source
)
{
return
source
.
replace
(
/
\n
{
3
,
}
/
g
,
'\n\n'
)
;
}
/**
* Apply all cleaning steps to declaration text.
*
@param
{
string
} source
*
@returns
{
string
}
*/
function
clean
(
source
)
{
let
result
=
stripRegionDirectives
(
source
)
;
result
=
stripSourceMappingUrl
(
result
)
;
result
=
stripJsDoc
(
result
)
;
result
=
stripImportLines
(
result
)
;
result
=
collapseBlankLines
(
result
)
;
return
result
.
trim
(
)
+
'\n'
;
}
/**
* Format a file with repo Oxfmt options.
*
@param
{
string
} absPath absolute path to the markdown file
*
@returns
{
Promise<void>
}
*/
async
function
formatWithOxfmt
(
absPath
)
{
const
raw
=
fs
.
readFileSync
(
absPath
,
'utf8'
)
;
const
formatted
=
(
await
format
(
absPath
,
raw
,
oxfmtConfig
)
)
.
code
;
fs
.
writeFileSync
(
absPath
,
formatted
,
'utf8'
)
;
}
/**
* Collect names marked `@deprecated` in chunk files imported by the entry .d.mts.
* Returns a Set of exported names whose declarations carry `@deprecated`.
*
@param
{
string
} entrySource
*
@param
{
string
} entryDir
*
@returns
{
Set<string>
}
*/
function
collectDeprecatedFromChunks
(
entrySource
,
entryDir
)
{
/**
@type
{
Set<string>
} */
const
deprecated
=
new
Set
(
)
;
// match: import { A as Foo, B as Bar } from "../chunk.mjs";
const
importRe
=
/
^
i
m
p
o
r
t
\s
+
\{
(
[
^
}
]
+
)
\}
\s
+
f
r
o
m
\s
+
"
(
[
^
"
]
+
)
"
/
gm
;
let
m
;
while
(
(
m
=
importRe
.
exec
(
entrySource
)
)
)
{
const
specifiers
=
m
[
1
]
;
const
chunkRel
=
m
[
2
]
;
const
chunkPath
=
path
.
resolve
(
entryDir
,
chunkRel
.
replace
(
/
\.
m
j
s
$
/
,
'.d.mts'
)
)
;
if
(
!
fs
.
existsSync
(
chunkPath
)
)
continue
;
const
chunkSrc
=
fs
.
readFileSync
(
chunkPath
,
'utf8'
)
;
// collect full type names marked
@deprecated
in the chunk
const
deprecatedNames
=
new
Set
(
)
;
// Match /**
@deprecated
... */ directly followed by a top-level type declaration
// The [^{}]* ensures we don't cross type body boundaries
const
depRe
=
/
\/
\*
\*
\s
*
@
d
e
p
r
e
c
a
t
e
d
[
^
{
}
]
*
?
\*
\/
\s
*
(?:
e
x
p
o
r
t
\s
+
)
?
t
y
p
e
\s
+
(
\w
+
)
/
g
;
let
dm
;
while
(
(
dm
=
depRe
.
exec
(
chunkSrc
)
)
)
{
deprecatedNames
.
add
(
dm
[
1
]
)
;
}
// map import specifiers (alias as ExportedName) back to chunk names
// entry: "d as ConditionDefinition" -> chunk exports "ConditionDefinition as d"
// so we check if ExportedName is deprecated in the chunk
for
(
const
spec
of
specifiers
.
split
(
','
)
)
{
const
parts
=
spec
.
trim
(
)
.
split
(
/
\s
+
a
s
\s
+
/
)
;
if
(
parts
.
length
!==
2
)
continue
;
const
exportedName
=
parts
[
1
]
;
if
(
deprecatedNames
.
has
(
exportedName
)
)
{
deprecated
.
add
(
exportedName
)
;
}
}
}
return
deprecated
;
}
/**
* Annotate `type Foo` entries in the export block with `/** @deprecated *\/` if
* the underlying declaration was marked deprecated in a chunk file.
*
@param
{
string
} cleaned
*
@param
{
Set<string>
} deprecated
*
@returns
{
string
}
*/
function
annotateDeprecatedExports
(
cleaned
,
deprecated
)
{
if
(
deprecated
.
size
===
0
)
return
cleaned
;
// match "type Name" inside export { ... } blocks
return
cleaned
.
replace
(
/
\b
t
y
p
e
\s
+
(
\w
+
)
/
g
,
(
match
,
name
)
=>
{
if
(
deprecated
.
has
(
name
)
)
{
return
`/** @deprecated */ type
${
name
}
`
;
}
return
match
;
}
)
;
}
/**
@returns
{
Promise<number>
} number of api-surface files written */
export
async
function
generateApiSurface
(
)
{
let
generated
=
0
;
for
(
const
pkg
of
packages
)
{
const
dtsPath
=
path
.
resolve
(
ROOT
,
pkg
.
dts
)
;
if
(
!
fs
.
existsSync
(
dtsPath
)
)
{
console
.
warn
(
` skipped
${
pkg
.
name
}
-
${
pkg
.
dts
}
not found (run build first)`
)
;
continue
;
}
const
raw
=
fs
.
readFileSync
(
dtsPath
,
'utf8'
)
;
const
deprecated
=
collectDeprecatedFromChunks
(
raw
,
path
.
dirname
(
dtsPath
)
)
;
let
cleaned
=
clean
(
raw
)
;
cleaned
=
annotateDeprecatedExports
(
cleaned
,
deprecated
)
;
const
header
=
`#
${
pkg
.
name
}
- Public API Surface\n\n`
+
`<!-- auto-generated by scripts/generate-api-surface.js - do not edit -->\n\n`
+
'```ts\n'
;
const
footer
=
'```\n'
;
const
outPath
=
path
.
resolve
(
ROOT
,
pkg
.
out
)
;
fs
.
writeFileSync
(
outPath
,
header
+
cleaned
+
footer
,
'utf8'
)
;
await
formatWithOxfmt
(
outPath
)
;
generated
++
;
console
.
log
(
`
${
pkg
.
name
}
->
${
pkg
.
out
}
`
)
;
}
if
(
generated
===
0
)
{
console
.
warn
(
'No .d.mts files found - run `pnpm build` first.'
)
;
process
.
exit
(
1
)
;
}
return
generated
;
}
const
isMain
=
process
.
argv
[
1
]
&&
import
.
meta
.
filename
===
path
.
resolve
(
process
.
argv
[
1
]
)
;
if
(
isMain
)
{
generateApiSurface
(
)
.
catch
(
(
err
)
=>
{
console
.
error
(
err
)
;
process
.
exit
(
1
)
;
}
)
;
}
You can’t perform that action at this time.