Skip to content

Commit 7218ab7

Browse files
authored
chore: improve typing for scripts (vuejs#9709)
1 parent 3071de4 commit 7218ab7

File tree

8 files changed

+138
-56
lines changed

8 files changed

+138
-56
lines changed

rollup.config.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-check
2+
import assert from 'node:assert/strict'
23
import { createRequire } from 'node:module'
34
import { fileURLToPath } from 'node:url'
45
import path from 'node:path'
@@ -14,6 +15,14 @@ import alias from '@rollup/plugin-alias'
1415
import { entries } from './scripts/aliases.js'
1516
import { inlineEnums } from './scripts/inline-enums.js'
1617

18+
/**
19+
* @template T
20+
* @template {keyof T} K
21+
* @typedef { Omit<T, K> & Required<Pick<T, K>> } MarkRequired
22+
*/
23+
/** @typedef {'cjs' | 'esm-bundler' | 'global' | 'global-runtime' | 'esm-browser' | 'esm-bundler-runtime' | 'esm-browser-runtime'} PackageFormat */
24+
/** @typedef {MarkRequired<import('rollup').OutputOptions, 'file' | 'format'>} OutputOptions */
25+
1726
if (!process.env.TARGET) {
1827
throw new Error('TARGET package must be specified via --environment flag.')
1928
}
@@ -27,34 +36,35 @@ const consolidatePkg = require('@vue/consolidate/package.json')
2736
const packagesDir = path.resolve(__dirname, 'packages')
2837
const packageDir = path.resolve(packagesDir, process.env.TARGET)
2938

30-
const resolve = p => path.resolve(packageDir, p)
39+
const resolve = (/** @type {string} */ p) => path.resolve(packageDir, p)
3140
const pkg = require(resolve(`package.json`))
3241
const packageOptions = pkg.buildOptions || {}
3342
const name = packageOptions.filename || path.basename(packageDir)
3443

3544
const [enumPlugin, enumDefines] = inlineEnums()
3645

46+
/** @type {Record<PackageFormat, OutputOptions>} */
3747
const outputConfigs = {
3848
'esm-bundler': {
3949
file: resolve(`dist/${name}.esm-bundler.js`),
40-
format: `es`
50+
format: 'es'
4151
},
4252
'esm-browser': {
4353
file: resolve(`dist/${name}.esm-browser.js`),
44-
format: `es`
54+
format: 'es'
4555
},
4656
cjs: {
4757
file: resolve(`dist/${name}.cjs.js`),
48-
format: `cjs`
58+
format: 'cjs'
4959
},
5060
global: {
5161
file: resolve(`dist/${name}.global.js`),
52-
format: `iife`
62+
format: 'iife'
5363
},
5464
// runtime-only builds, for main "vue" package only
5565
'esm-bundler-runtime': {
5666
file: resolve(`dist/${name}.runtime.esm-bundler.js`),
57-
format: `es`
67+
format: 'es'
5868
},
5969
'esm-browser-runtime': {
6070
file: resolve(`dist/${name}.runtime.esm-browser.js`),
@@ -66,8 +76,13 @@ const outputConfigs = {
6676
}
6777
}
6878

79+
/** @type {ReadonlyArray<PackageFormat>} */
6980
const defaultFormats = ['esm-bundler', 'cjs']
70-
const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
81+
/** @type {ReadonlyArray<PackageFormat>} */
82+
const inlineFormats = /** @type {any} */ (
83+
process.env.FORMATS && process.env.FORMATS.split(',')
84+
)
85+
/** @type {ReadonlyArray<PackageFormat>} */
7186
const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
7287
const packageConfigs = process.env.PROD_ONLY
7388
? []
@@ -89,6 +104,13 @@ if (process.env.NODE_ENV === 'production') {
89104

90105
export default packageConfigs
91106

107+
/**
108+
*
109+
* @param {PackageFormat} format
110+
* @param {OutputOptions} output
111+
* @param {ReadonlyArray<import('rollup').Plugin>} plugins
112+
* @returns {import('rollup').RollupOptions}
113+
*/
92114
function createConfig(format, output, plugins = []) {
93115
if (!output) {
94116
console.log(pico.yellow(`invalid format: "${format}"`))
@@ -132,6 +154,7 @@ function createConfig(format, output, plugins = []) {
132154
}
133155

134156
function resolveDefine() {
157+
/** @type {Record<string, string>} */
135158
const replacements = {
136159
__COMMIT__: `"${process.env.COMMIT}"`,
137160
__VERSION__: `"${masterVersion}"`,
@@ -162,15 +185,16 @@ function createConfig(format, output, plugins = []) {
162185

163186
if (!isBundlerESMBuild) {
164187
// hard coded dev/prod builds
165-
// @ts-ignore
166188
replacements.__DEV__ = String(!isProductionBuild)
167189
}
168190

169191
// allow inline overrides like
170192
//__RUNTIME_COMPILE__=true pnpm build runtime-core
171193
Object.keys(replacements).forEach(key => {
172194
if (key in process.env) {
173-
replacements[key] = process.env[key]
195+
const value = process.env[key]
196+
assert(typeof value === 'string')
197+
replacements[key] = value
174198
}
175199
})
176200
return replacements
@@ -207,7 +231,6 @@ function createConfig(format, output, plugins = []) {
207231
}
208232

209233
if (Object.keys(replacements).length) {
210-
// @ts-ignore
211234
return [replace({ values: replacements, preventAssignment: true })]
212235
} else {
213236
return []
@@ -245,6 +268,7 @@ function createConfig(format, output, plugins = []) {
245268
function resolveNodePlugins() {
246269
// we are bundling forked consolidate.js in compiler-sfc which dynamically
247270
// requires a ton of template engines which should be ignored.
271+
/** @type {ReadonlyArray<string>} */
248272
let cjsIgnores = []
249273
if (
250274
pkg.name === '@vue/compiler-sfc' ||
@@ -304,7 +328,7 @@ function createConfig(format, output, plugins = []) {
304328
],
305329
output,
306330
onwarn: (msg, warn) => {
307-
if (!/Circular/.test(msg)) {
331+
if (msg.code !== 'CIRCULAR_DEPENDENCY') {
308332
warn(msg)
309333
}
310334
},
@@ -314,14 +338,14 @@ function createConfig(format, output, plugins = []) {
314338
}
315339
}
316340

317-
function createProductionConfig(format) {
341+
function createProductionConfig(/** @type {PackageFormat} */ format) {
318342
return createConfig(format, {
319343
file: resolve(`dist/${name}.${format}.prod.js`),
320344
format: outputConfigs[format].format
321345
})
322346
}
323347

324-
function createMinifiedConfig(format) {
348+
function createMinifiedConfig(/** @type {PackageFormat} */ format) {
325349
return createConfig(
326350
format,
327351
{

rollup.dts.config.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
2+
import assert from 'node:assert/strict'
23
import { parse } from '@babel/parser'
3-
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'
4+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
45
import MagicString from 'magic-string'
56
import dts from 'rollup-plugin-dts'
67

@@ -70,15 +71,16 @@ function patchTypes(pkg) {
7071
if (!node.id) {
7172
return
7273
}
73-
// @ts-ignore
74+
assert(node.id.type === 'Identifier')
7475
const name = node.id.name
7576
if (name.startsWith('_')) {
7677
return
7778
}
7879
shouldRemoveExport.add(name)
7980
if (isExported.has(name)) {
80-
// @ts-ignore
81-
s.prependLeft((parentDecl || node).start, `export `)
81+
const start = (parentDecl || node).start
82+
assert(typeof start === 'number')
83+
s.prependLeft(start, `export `)
8284
}
8385
}
8486

@@ -102,9 +104,10 @@ function patchTypes(pkg) {
102104
if (node.type === 'VariableDeclaration') {
103105
processDeclaration(node.declarations[0], node)
104106
if (node.declarations.length > 1) {
107+
assert(typeof node.start === 'number')
108+
assert(typeof node.end === 'number')
105109
throw new Error(
106110
`unhandled declare const with more than one declarators:\n${code.slice(
107-
// @ts-ignore
108111
node.start,
109112
node.end
110113
)}`
@@ -131,7 +134,7 @@ function patchTypes(pkg) {
131134
spec.type === 'ExportSpecifier' &&
132135
shouldRemoveExport.has(spec.local.name)
133136
) {
134-
// @ts-ignore
137+
assert(spec.exported.type === 'Identifier')
135138
const exported = spec.exported.name
136139
if (exported !== spec.local.name) {
137140
// this only happens if we have something like
@@ -141,19 +144,27 @@ function patchTypes(pkg) {
141144
}
142145
const next = node.specifiers[i + 1]
143146
if (next) {
144-
// @ts-ignore
147+
assert(typeof spec.start === 'number')
148+
assert(typeof next.start === 'number')
145149
s.remove(spec.start, next.start)
146150
} else {
147151
// last one
148152
const prev = node.specifiers[i - 1]
149-
// @ts-ignore
150-
s.remove(prev ? prev.end : spec.start, spec.end)
153+
assert(typeof spec.start === 'number')
154+
assert(typeof spec.end === 'number')
155+
s.remove(
156+
prev
157+
? (assert(typeof prev.end === 'number'), prev.end)
158+
: spec.start,
159+
spec.end
160+
)
151161
}
152162
removed++
153163
}
154164
}
155165
if (removed === node.specifiers.length) {
156-
// @ts-ignore
166+
assert(typeof node.start === 'number')
167+
assert(typeof node.end === 'number')
157168
s.remove(node.start, node.end)
158169
}
159170
}
@@ -186,11 +197,8 @@ function copyMts() {
186197
return {
187198
name: 'copy-vue-mts',
188199
writeBundle(_, bundle) {
189-
writeFileSync(
190-
'packages/vue/dist/vue.d.mts',
191-
// @ts-ignore
192-
bundle['vue.d.ts'].code
193-
)
200+
assert('code' in bundle['vue.d.ts'])
201+
writeFileSync('packages/vue/dist/vue.d.mts', bundle['vue.d.ts'].code)
194202
}
195203
}
196204
}

scripts/aliases.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { readdirSync, statSync } from 'node:fs'
44
import path from 'node:path'
55
import { fileURLToPath } from 'node:url'
66

7-
const resolveEntryForPkg = p =>
7+
const resolveEntryForPkg = (/** @type {string} */ p) =>
88
path.resolve(
99
fileURLToPath(import.meta.url),
1010
`../../packages/${p}/src/index.ts`
1111
)
1212

1313
const dirs = readdirSync(new URL('../packages', import.meta.url))
1414

15+
/** @type {Record<string, string>} */
1516
const entries = {
1617
vue: resolveEntryForPkg('vue'),
1718
'vue/compiler-sfc': resolveEntryForPkg('compiler-sfc'),

scripts/build.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const prodOnly = !devOnly && (args.prodOnly || args.p)
3838
const buildTypes = args.withTypes || args.t
3939
const sourceMap = args.sourcemap || args.s
4040
const isRelease = args.release
41+
/** @type {boolean | undefined} */
4142
const buildAllMatching = args.all || args.a
4243
const writeSize = args.size
4344
const commit = execaSync('git', ['rev-parse', '--short=7', 'HEAD']).stdout
@@ -102,7 +103,9 @@ async function runParallel(maxConcurrency, source, iteratorFn) {
102103
ret.push(p)
103104

104105
if (maxConcurrency <= source.length) {
105-
const e = p.then(() => executing.splice(executing.indexOf(e), 1))
106+
const e = p.then(() => {
107+
executing.splice(executing.indexOf(e), 1)
108+
})
106109
executing.push(e)
107110
if (executing.length >= maxConcurrency) {
108111
await Promise.race(executing)

0 commit comments

Comments
 (0)