@kayjs/kay 2.0.0-beta.2 → 2.0.0-beta.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.
Files changed (101) hide show
  1. package/llms.txt +42 -0
  2. package/package.json +24 -13
  3. package/src/build/bundle.ts +15 -53
  4. package/src/build/client-plugin.ts +108 -0
  5. package/src/build/compile.ts +9 -44
  6. package/src/build/graph.ts +16 -3
  7. package/src/build/index.ts +45 -25
  8. package/src/build/islands.ts +28 -30
  9. package/src/build/manifest.ts +9 -4
  10. package/src/build/prerender.ts +15 -7
  11. package/src/build/report.ts +82 -13
  12. package/src/build/resolve.ts +6 -7
  13. package/src/build/routes.ts +12 -21
  14. package/src/build/server-entry.ts +38 -0
  15. package/src/cli/check.ts +49 -43
  16. package/src/cli/commands.ts +224 -0
  17. package/src/cli/index.ts +46 -302
  18. package/src/cli/new.ts +15 -509
  19. package/src/cli/upgrade-plugins.ts +168 -0
  20. package/src/cli/upgrade.ts +220 -0
  21. package/src/compiler/analyze.ts +319 -565
  22. package/src/compiler/ast.ts +39 -0
  23. package/src/compiler/data-keys.ts +233 -0
  24. package/src/compiler/dom.ts +18 -15
  25. package/src/compiler/interactivity.ts +174 -0
  26. package/src/compiler/mistakes.ts +19 -1
  27. package/src/compiler/server-only.ts +4 -1
  28. package/src/compiler/shared.ts +1 -1
  29. package/src/compiler/ssr.ts +28 -28
  30. package/src/context.ts +1 -1
  31. package/src/dev/index.ts +19 -46
  32. package/src/dom/extras.ts +43 -12
  33. package/src/dom/index.ts +25 -1
  34. package/src/index.ts +0 -13
  35. package/src/jsx-runtime.ts +1 -1
  36. package/src/plugins/config.ts +49 -0
  37. package/src/plugins/context.ts +8 -28
  38. package/src/plugins/index.ts +10 -385
  39. package/src/plugins/load.ts +61 -0
  40. package/src/plugins/order.ts +107 -0
  41. package/src/plugins/public.ts +6 -0
  42. package/src/plugins/services.ts +35 -0
  43. package/src/plugins/types.ts +193 -0
  44. package/src/server/action-registry.ts +78 -0
  45. package/src/server/action.ts +8 -2
  46. package/src/server/app-entry.ts +23 -0
  47. package/src/server/assets.ts +182 -0
  48. package/src/server/document.ts +204 -0
  49. package/src/server/errors.ts +125 -0
  50. package/src/server/http.ts +14 -0
  51. package/src/server/index.ts +66 -766
  52. package/src/server/lifecycle.ts +22 -0
  53. package/src/server/loader.ts +60 -85
  54. package/src/server/page.ts +107 -0
  55. package/src/server/plugin-host.ts +165 -0
  56. package/src/server/public.ts +15 -0
  57. package/src/server/request-scope.ts +22 -0
  58. package/src/server/security.ts +40 -1
  59. package/src/server/static-dir.ts +22 -3
  60. package/src/server/stream.ts +95 -0
  61. package/src/ssr/context.ts +25 -0
  62. package/src/ssr/index.ts +10 -7
  63. package/src/test/browser.ts +180 -0
  64. package/src/test/index.ts +25 -6
  65. package/templates/_shared/AGENTS.md +5 -0
  66. package/templates/_shared/CLAUDE.md +1 -0
  67. package/templates/_shared/public/robots.txt +2 -0
  68. package/templates/_shared/tsconfig.json +19 -0
  69. package/templates/app/.env.example +4 -0
  70. package/templates/app/kay.config.ts +11 -0
  71. package/templates/app/package.json +20 -0
  72. package/templates/app/src/auth.ts +14 -0
  73. package/templates/app/src/routes/_layout.tsx +32 -0
  74. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  75. package/templates/app/src/routes/dashboard/index.tsx +13 -0
  76. package/templates/app/src/routes/index.tsx +15 -0
  77. package/templates/app/src/routes/login.tsx +34 -0
  78. package/templates/app/src/styles/app.css +7 -0
  79. package/templates/blog/content/posts/welcome.md +9 -0
  80. package/templates/blog/kay.config.ts +23 -0
  81. package/templates/blog/package.json +21 -0
  82. package/templates/blog/src/posts.ts +26 -0
  83. package/templates/blog/src/routes/_layout.tsx +20 -0
  84. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  85. package/templates/blog/src/routes/index.tsx +29 -0
  86. package/templates/blog/src/styles/app.css +10 -0
  87. package/templates/fullstack/kay.config.ts +6 -0
  88. package/templates/fullstack/package.json +18 -0
  89. package/templates/fullstack/src/actions/greet.ts +19 -0
  90. package/templates/fullstack/src/components/Counter.tsx +10 -0
  91. package/templates/fullstack/src/components/GreetForm.tsx +26 -0
  92. package/templates/fullstack/src/routes/_layout.tsx +14 -0
  93. package/templates/fullstack/src/routes/api/health.ts +5 -0
  94. package/templates/fullstack/src/routes/index.tsx +13 -0
  95. package/templates/fullstack/src/styles/app.css +29 -0
  96. package/templates/minimal/kay.config.ts +6 -0
  97. package/templates/minimal/package.json +18 -0
  98. package/templates/minimal/src/components/Counter.tsx +10 -0
  99. package/templates/minimal/src/routes/_layout.tsx +14 -0
  100. package/templates/minimal/src/routes/index.tsx +11 -0
  101. package/templates/minimal/src/styles/app.css +22 -0
@@ -0,0 +1,168 @@
1
+ // `kay upgrade` for #3579: a stateful plugin is registered as the handle its `createX()` returns, and two factories took the `createX()`/`x()` names.
2
+
3
+ import type { AstNode } from '../compiler/ast'
4
+ import { parseTsx } from '../compiler/analyze'
5
+ import { walk } from '../compiler/ast'
6
+ import { SourceEditor } from '../compiler/patch'
7
+
8
+ const RENAMED_FACTORIES = new Map([
9
+ ['@kayjs/mail', new Map([['mailer', 'createMailer']])],
10
+ ['@kayjs/content', new Map([['contentPlugin', 'content']])],
11
+ ])
12
+ // `mailPlugin(mail)` registered the mailer, which is now the plugin itself.
13
+ const UNWRAPPED = { module: '@kayjs/mail', name: 'mailPlugin' }
14
+
15
+ type Import = Extract<AstNode, { type: 'ImportDeclaration' }>
16
+ type Specifier = Extract<AstNode, { type: 'ImportSpecifier' }>
17
+
18
+ interface Binding {
19
+ spec: Specifier
20
+ imported: string
21
+ /** What `imported` is called now: a new name, or null for an export that is gone. */
22
+ renamed: string | null
23
+ refs: AstNode[]
24
+ /** Used where a rename would change more than the binding: a shorthand property or an export specifier. */
25
+ pinned: boolean
26
+ }
27
+
28
+ export function lineOf(code: string, offset: number): number {
29
+ let line = 1
30
+ for (let i = 0; i < offset; i++) {
31
+ if (code.charCodeAt(i) === 10)
32
+ line++
33
+ }
34
+ return line
35
+ }
36
+
37
+ const nameOf = (node: { type: string, name?: string, value?: unknown }): string => node.type === 'Identifier' ? node.name! : String(node.value)
38
+
39
+ /** Rewrites one source file; each registration it cannot rewrite is pushed to `manual` as `file:line: what`. */
40
+ export function upgradePluginHandles(file: string, code: string, manual: string[]): string {
41
+ if (!code.includes('@kayjs/') && !code.includes('.plugin'))
42
+ return code
43
+ const program = parseTsx(file, code)
44
+ const bindings = new Map<string, Binding>()
45
+ for (const stmt of program.body) {
46
+ if (stmt.type !== 'ImportDeclaration')
47
+ continue
48
+ for (const spec of stmt.specifiers) {
49
+ if (spec.type !== 'ImportSpecifier')
50
+ continue
51
+ const imported = nameOf(spec.imported)
52
+ const renamed = RENAMED_FACTORIES.get(stmt.source.value)?.get(imported)
53
+ const gone = stmt.source.value === UNWRAPPED.module && imported === UNWRAPPED.name
54
+ if (renamed !== undefined || gone)
55
+ bindings.set(spec.local.name, { spec, imported, renamed: renamed ?? null, refs: [], pinned: false })
56
+ }
57
+ }
58
+
59
+ const editor = new SourceEditor(code)
60
+ const names = new Set<string>()
61
+ const skipped = new Set<number>()
62
+ const reports: [number, string][] = []
63
+ const pin = (name: string): void => {
64
+ const binding = bindings.get(name)
65
+ if (binding !== undefined)
66
+ binding.pinned = true
67
+ }
68
+ walk(program.body, (node) => {
69
+ if (node.type === 'ImportDeclaration')
70
+ return false
71
+ // Every keyed node (property, method, class field, type member) names its key, which is no reference.
72
+ const keyed = node as AstNode & { key?: AstNode & { type: string, name?: string }, computed?: boolean, shorthand?: boolean }
73
+ if (keyed.key !== undefined && keyed.computed === false) {
74
+ if (keyed.shorthand === true)
75
+ pin(nameOf(keyed.key))
76
+ skipped.add(keyed.key.start)
77
+ }
78
+ if (node.type === 'MemberExpression' && !node.computed)
79
+ skipped.add(node.property.start)
80
+ if (node.type === 'ExportSpecifier')
81
+ pin(nameOf(node.local))
82
+ if (node.type === 'Property' && node.key.type === 'Identifier' && node.key.name === 'plugins' && node.value.type === 'ArrayExpression')
83
+ registerHandles(code, node.value.elements, bindings, editor, skipped, reports)
84
+ if (node.type === 'Identifier') {
85
+ names.add(node.name)
86
+ if (!skipped.has(node.start))
87
+ bindings.get(node.name)?.refs.push(node)
88
+ }
89
+ return true
90
+ })
91
+
92
+ const removedLines: number[] = []
93
+ for (const stmt of program.body) {
94
+ if (stmt.type === 'ImportDeclaration')
95
+ rewriteImport(code, stmt, bindings, names, editor, removedLines)
96
+ }
97
+ for (const [offset, what] of reports)
98
+ manual.push(`${file}:${lineOf(code, offset) - removedLines.filter(start => start < offset).length}: ${what}`)
99
+ return editor.toString()
100
+ }
101
+
102
+ // In a `plugins` array, `x.plugin` and `x.plugin()` become `x`, and so does `mailPlugin(x)`.
103
+ function registerHandles(code: string, elements: readonly (AstNode | null)[], bindings: Map<string, Binding>, editor: SourceEditor, skipped: Set<number>, reports: [number, string][]): void {
104
+ for (const el of elements) {
105
+ if (el === null)
106
+ continue
107
+ const call = el.type === 'CallExpression' ? el : undefined
108
+ const member = call?.callee ?? el
109
+ if (member.type === 'MemberExpression' && member.property.type === 'Identifier' && member.property.name === 'plugin' && !member.computed && member.object.type === 'Identifier') {
110
+ const handle = member.object.name
111
+ if (call === undefined || call.arguments.length === 0)
112
+ editor.replace(el.start, el.end, handle)
113
+ else
114
+ reports.push([el.start, `register ${handle} itself and move the options of ${handle}.plugin(...) into the createX() call that made it.`])
115
+ continue
116
+ }
117
+ const unwrap = call !== undefined && call.callee.type === 'Identifier' ? bindings.get(call.callee.name) : undefined
118
+ if (call === undefined || unwrap === undefined || unwrap.renamed !== null)
119
+ continue
120
+ const mailer = call.arguments[0]
121
+ if (call.arguments.length === 1 && mailer !== undefined) {
122
+ editor.replace(call.start, call.end, code.slice(mailer.start, mailer.end))
123
+ skipped.add(call.callee.start)
124
+ }
125
+ else if (mailer !== undefined) {
126
+ reports.push([call.start, `register ${code.slice(mailer.start, mailer.end)} itself and move the options of ${unwrap.imported}(...) into its createMailer().`])
127
+ }
128
+ }
129
+ }
130
+
131
+ // Renames the specifiers of `stmt` and their uses; a name already taken keeps the old one as an alias, and an unused export that is gone is dropped.
132
+ function rewriteImport(code: string, stmt: Import, bindings: Map<string, Binding>, names: Set<string>, editor: SourceEditor, removedLines: number[]): void {
133
+ let changed = false
134
+ const specs: string[] = []
135
+ for (const spec of stmt.specifiers) {
136
+ const binding = spec.type === 'ImportSpecifier' ? bindings.get(spec.local.name) : undefined
137
+ if (binding === undefined || binding.spec !== spec) {
138
+ specs.push(code.slice(spec.start, spec.end))
139
+ continue
140
+ }
141
+ changed = true
142
+ const local = spec.local.name
143
+ if (binding.renamed === null) {
144
+ if (binding.refs.length > 0)
145
+ specs.push(code.slice(spec.start, spec.end))
146
+ continue
147
+ }
148
+ if (local !== binding.imported || binding.pinned || names.has(binding.renamed)) {
149
+ specs.push(`${binding.renamed} as ${local}`)
150
+ continue
151
+ }
152
+ specs.push(binding.renamed)
153
+ for (const ref of binding.refs)
154
+ editor.replace(ref.start, ref.end, binding.renamed)
155
+ }
156
+ if (!changed)
157
+ return
158
+ if (specs.length === 0) {
159
+ const end = code[stmt.end] === '\n' ? stmt.end + 1 : stmt.end
160
+ editor.remove(stmt.start, end)
161
+ removedLines.push(stmt.start)
162
+ return
163
+ }
164
+ const head = stmt.importKind === 'type' ? 'import type' : 'import'
165
+ const quote = code[stmt.source.start]!
166
+ const semi = code[stmt.end - 1] === ';' ? ';' : ''
167
+ editor.replace(stmt.start, stmt.end, `${head} { ${specs.join(', ')} } from ${quote}${stmt.source.value}${quote}${semi}`)
168
+ }
@@ -0,0 +1,220 @@
1
+ // `kay upgrade`: moves an app written for the bare `kay` package onto `@kayjs/kay`, its dependency, JSX source and imports.
2
+
3
+ import type { Program } from 'oxc-parser'
4
+ import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import pkg from '../../package.json' with { type: 'json' }
7
+ import { relPosix } from '../build/files'
8
+ import { resolveKaySpecifier } from '../build/resolve'
9
+ import { parseTsx } from '../compiler/analyze'
10
+ import { SourceEditor } from '../compiler/patch'
11
+ import { lineOf, upgradePluginHandles } from './upgrade-plugins'
12
+
13
+ export interface UpgradeResult {
14
+ /** Root-relative files rewritten. */
15
+ changed: string[]
16
+ /** `file:line: what` for each import left for the developer. */
17
+ manual: string[]
18
+ }
19
+
20
+ const BARE_KAY_RE = /^kay(\/[\w-]+)?$/
21
+ const BARE_SPECIFIER_RE = /(['"])kay(\/[\w-]+)?\1/g
22
+ const DYNAMIC_IMPORT_RE = /\bimport\(\s*(['"])kay(\/[\w-]+)?\1\s*\)/g
23
+ const JSX_PRAGMA_RE = /@jsxImportSource\s+kay\b(?!\/)/g
24
+ const TSCONFIG_JSX_RE = /("jsxImportSource"\s*:\s*)"kay"/g
25
+ const SOURCE_FILE_RE = /\.(?:tsx?|mts)$/
26
+ const TSCONFIG_FILE_RE = /^tsconfig(?:\.[\w-]+)?\.json$/
27
+ const SKIPPED_DIRS = new Set(['node_modules', 'dist'])
28
+ const PUBLIC_ENTRIES = new Set(['', '/server', '/config', '/test', '/build', '/jsx-runtime', '/jsx-dev-runtime'])
29
+ // kay v1 served `raw`, `esc` and `escAttr` from `kay/ssr`; v2 exports them from `/server`.
30
+ const RENAMED_ENTRIES = new Map([['/ssr', '/server']])
31
+ // Names a package moved to another of its entries, so that its root stays importable from an island.
32
+ const MOVED_EXPORTS = new Map([['@kayjs/icons', { to: '@kayjs/icons/plugin', names: new Set(['icons', 'IconsPluginOptions']) }]])
33
+
34
+ /** Rewrites the app at `root` in place; installing the renamed dependency is the caller's job. */
35
+ export function upgradeProject(root: string): UpgradeResult {
36
+ const changed: string[] = []
37
+ const manual: string[] = []
38
+ const write = (file: string, before: string, after: string): void => {
39
+ if (after === before)
40
+ return
41
+ writeFileSync(join(root, file), after)
42
+ changed.push(file)
43
+ }
44
+ for (const file of rootFiles(root)) {
45
+ const text = readFileSync(join(root, file), 'utf8')
46
+ if (file === 'package.json')
47
+ write(file, text, upgradePackageJson(text))
48
+ else if (TSCONFIG_FILE_RE.test(file))
49
+ write(file, text, text.replace(TSCONFIG_JSX_RE, '$1"@kayjs/kay"'))
50
+ }
51
+ for (const file of sourceFiles(root)) {
52
+ const text = readFileSync(join(root, file), 'utf8')
53
+ if (text.includes('kay'))
54
+ write(file, text, upgradePluginHandles(file, upgradeSource(file, text, manual), manual))
55
+ }
56
+ return { changed: changed.sort(), manual }
57
+ }
58
+
59
+ function rootFiles(root: string): string[] {
60
+ return readdirSync(root, { withFileTypes: true }).filter(e => e.isFile()).map(e => e.name).sort()
61
+ }
62
+
63
+ // A folder holding its own package.json is another package, upgraded on its own.
64
+ function sourceFiles(root: string, dir = root, out: string[] = []): string[] {
65
+ if (dir !== root && existsSync(join(dir, 'package.json')))
66
+ return out
67
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
68
+ if (entry.name.startsWith('.') || SKIPPED_DIRS.has(entry.name))
69
+ continue
70
+ const path = join(dir, entry.name)
71
+ if (entry.isDirectory())
72
+ sourceFiles(root, path, out)
73
+ else if (SOURCE_FILE_RE.test(entry.name) && !entry.name.endsWith('.d.ts'))
74
+ out.push(relPosix(root, path))
75
+ }
76
+ return out.sort()
77
+ }
78
+
79
+ // Every `@kayjs/*` package is released at kay's version, so they all move with it.
80
+ function upgradePackageJson(text: string): string {
81
+ const json = JSON.parse(text) as Record<string, unknown>
82
+ const range = `^${pkg.version}`
83
+ let touched = false
84
+ for (const field of ['dependencies', 'devDependencies', 'peerDependencies']) {
85
+ const deps = json[field] as Record<string, string> | undefined
86
+ if (deps === undefined || !Object.keys(deps).some(name => name === 'kay' || name.startsWith('@kayjs/')))
87
+ continue
88
+ json[field] = Object.fromEntries(Object.entries(deps).map(([name, version]) => {
89
+ if (name === 'kay' || name.startsWith('@kayjs/')) {
90
+ touched = true
91
+ return [name === 'kay' ? '@kayjs/kay' : name, range]
92
+ }
93
+ return [name, version]
94
+ }))
95
+ }
96
+ if (!touched)
97
+ return text
98
+ const indent = /^\{\n([ \t]+)"/.exec(text)?.[1] ?? ' '
99
+ return `${JSON.stringify(json, null, indent)}\n`
100
+ }
101
+
102
+ const exportCache = new Map<string, Set<string>>()
103
+
104
+ /** The names an entry of `@kayjs/kay` exports, read from its source. */
105
+ function exportsOf(entry: string): Set<string> {
106
+ let names = exportCache.get(entry)
107
+ if (names === undefined) {
108
+ names = new Set()
109
+ const file = resolveKaySpecifier(`@kayjs/kay${entry}`)!
110
+ for (const stmt of parseTsx(file, readFileSync(file, 'utf8')).body) {
111
+ if (stmt.type !== 'ExportNamedDeclaration')
112
+ continue
113
+ for (const spec of stmt.specifiers)
114
+ names.add(spec.exported.type === 'Identifier' ? spec.exported.name : String(spec.exported.value))
115
+ const decl = stmt.declaration
116
+ if (decl === null)
117
+ continue
118
+ if (decl.type === 'VariableDeclaration') {
119
+ for (const d of decl.declarations) {
120
+ if (d.id.type === 'Identifier')
121
+ names.add(d.id.name)
122
+ }
123
+ }
124
+ else if ('id' in decl && decl.id !== null && decl.id.type === 'Identifier') {
125
+ names.add(decl.id.name)
126
+ }
127
+ }
128
+ exportCache.set(entry, names)
129
+ }
130
+ return names
131
+ }
132
+
133
+
134
+ type Statement = Program['body'][number]
135
+ type ImportStatement = Extract<Statement, { type: 'ImportDeclaration' }>
136
+
137
+ function upgradeSource(file: string, code: string, manual: string[]): string {
138
+ const editor = new SourceEditor(code)
139
+ const scoped = (quote: string, entry: string): string => `${quote}@kayjs/kay${RENAMED_ENTRIES.get(entry) ?? entry}${quote}`
140
+ for (const stmt of parseTsx(file, code).body) {
141
+ const source = moduleSource(stmt)
142
+ const moves = stmt.type === 'ImportDeclaration' ? MOVED_EXPORTS.get(stmt.source.value) : undefined
143
+ if (moves !== undefined && stmt.type === 'ImportDeclaration') {
144
+ editor.replace(stmt.start, stmt.end, splitImport(code, stmt, code[stmt.source.start]!, moves.to, moves.names))
145
+ continue
146
+ }
147
+ const match = source === undefined ? null : BARE_KAY_RE.exec(source.value)
148
+ if (source === undefined || match === null)
149
+ continue
150
+ const entry = match[1] ?? ''
151
+ const quote = code[source.start]!
152
+ if (!PUBLIC_ENTRIES.has(RENAMED_ENTRIES.get(entry) ?? entry)) {
153
+ manual.push(`${file}:${lineOf(code, stmt.start)}: kay${entry} has no public @kayjs/kay entry: import from @kayjs/kay, /server, /config or /test.`)
154
+ continue
155
+ }
156
+ if (stmt.type === 'ImportDeclaration' && (entry === '/server' || entry === '/ssr'))
157
+ editor.replace(stmt.start, stmt.end, splitServerImport(file, code, stmt, quote, manual))
158
+ else
159
+ editor.replace(source.start, source.end, scoped(quote, entry))
160
+ }
161
+ let out = editor.toString()
162
+ out = out.replace(DYNAMIC_IMPORT_RE, (whole: string, quote: string, entry = '') => whole.replace(BARE_SPECIFIER_RE, scoped(quote, entry)))
163
+ return out.replace(JSX_PRAGMA_RE, '@jsxImportSource @kayjs/kay')
164
+ }
165
+
166
+ function moduleSource(stmt: Statement): { value: string, start: number, end: number } | undefined {
167
+ if (stmt.type === 'ImportDeclaration' || stmt.type === 'ExportAllDeclaration')
168
+ return stmt.source
169
+ if (stmt.type === 'ExportNamedDeclaration')
170
+ return stmt.source ?? undefined
171
+ if (stmt.type === 'TSModuleDeclaration' && stmt.id.type === 'Literal')
172
+ return { value: String(stmt.id.value), start: stmt.id.start, end: stmt.id.end }
173
+ return undefined
174
+ }
175
+
176
+ // `/config` names leave a `kay/server` import for one of their own; a name neither entry exports stays and is reported.
177
+ function splitServerImport(file: string, code: string, stmt: ImportStatement, quote: string, manual: string[]): string {
178
+ const specifier = `${quote}@kayjs/kay/server${quote}`
179
+ if (stmt.specifiers.length === 0 || stmt.specifiers.some(s => s.type !== 'ImportSpecifier'))
180
+ return code.slice(stmt.start, stmt.source.start) + specifier + code.slice(stmt.source.end, stmt.end)
181
+ const server = exportsOf('/server')
182
+ const config = exportsOf('/config')
183
+ const keep: string[] = []
184
+ const moved: string[] = []
185
+ for (const spec of stmt.specifiers) {
186
+ if (spec.type !== 'ImportSpecifier')
187
+ continue
188
+ const text = code.slice(spec.start, spec.end)
189
+ const name = spec.imported.type === 'Identifier' ? spec.imported.name : String(spec.imported.value)
190
+ if (!server.has(name) && config.has(name))
191
+ moved.push(text)
192
+ else
193
+ keep.push(text)
194
+ if (!server.has(name) && !config.has(name))
195
+ manual.push(`${file}:${lineOf(code, stmt.start)}: ${name} is not exported by @kayjs/kay/server or @kayjs/kay/config: it is kay's internal API, outside semver.`)
196
+ }
197
+ return importLines(code, stmt, quote, [keep, '@kayjs/kay/server'], [moved, '@kayjs/kay/config'])
198
+ }
199
+
200
+ // The named imports of `stmt` listed in `names` move to the module `to`; the others stay.
201
+ function splitImport(code: string, stmt: ImportStatement, quote: string, to: string, names: ReadonlySet<string>): string {
202
+ if (stmt.specifiers.some(s => s.type !== 'ImportSpecifier'))
203
+ return code.slice(stmt.start, stmt.end)
204
+ const keep: string[] = []
205
+ const moved: string[] = []
206
+ for (const spec of stmt.specifiers) {
207
+ const named = spec.type === 'ImportSpecifier' ? (spec.imported.type === 'Identifier' ? spec.imported.name : String(spec.imported.value)) : undefined
208
+ ;(named !== undefined && names.has(named) ? moved : keep).push(code.slice(spec.start, spec.end))
209
+ }
210
+ if (moved.length === 0)
211
+ return code.slice(stmt.start, stmt.end)
212
+ return importLines(code, stmt, quote, [keep, stmt.source.value], [moved, to])
213
+ }
214
+
215
+ // One import statement per non-empty group, each keeping the kind, quote and semicolon of `stmt`.
216
+ function importLines(code: string, stmt: ImportStatement, quote: string, ...groups: [string[], string][]): string {
217
+ const head = stmt.importKind === 'type' ? 'import type' : 'import'
218
+ const semi = code[stmt.end - 1] === ';' ? ';' : ''
219
+ return groups.filter(([names]) => names.length > 0).map(([names, from]) => `${head} { ${names.join(', ')} } from ${quote}${from}${quote}${semi}`).join('\n')
220
+ }