Skip to content

Commit af86e5b

Browse files
authored
chore: typecheck create-vite (#11295)
1 parent 27362cb commit af86e5b

15 files changed

+54
-51
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"postinstall": "simple-git-hooks",
1818
"format": "prettier --write --cache .",
1919
"lint": "eslint --cache .",
20-
"typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit",
20+
"typecheck": "tsc -p scripts --noEmit && pnpm -r --parallel run typecheck",
2121
"test": "run-s test-unit test-serve test-build",
2222
"test-serve": "vitest run -c vitest.config.e2e.ts",
2323
"test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
@@ -81,7 +81,6 @@
8181
"resolve": "^1.22.1",
8282
"rimraf": "^3.0.2",
8383
"rollup": "^3.7.0",
84-
"rollup-plugin-license": "^3.0.1",
8584
"semver": "^7.3.8",
8685
"simple-git-hooks": "^2.8.1",
8786
"tslib": "^2.4.1",

packages/create-vite/__tests__/cli.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ beforeAll(() => remove(genPath))
3636
afterEach(() => remove(genPath))
3737

3838
test('prompts for the project name if none supplied', () => {
39-
const { stdout, exitCode } = run([])
39+
const { stdout } = run([])
4040
expect(stdout).toContain('Project name:')
4141
})
4242

packages/create-vite/build.config.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
22
import url from 'node:url'
33
import { defineBuildConfig } from 'unbuild'
4-
import licensePlugin from '../../scripts/rollupLicensePlugin.mjs'
4+
import licensePlugin from '../vite/rollupLicensePlugin'
55

66
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
77

@@ -20,16 +20,14 @@ export default defineBuildConfig({
2020
},
2121
hooks: {
2222
'rollup:options'(ctx, options) {
23-
if (!options.plugins) {
24-
options.plugins = []
25-
}
26-
options.plugins.push(
23+
options.plugins = [
24+
options.plugins,
2725
licensePlugin(
2826
path.resolve(__dirname, './LICENSE'),
2927
'create-vite license',
3028
'create-vite',
3129
),
32-
)
30+
]
3331
},
3432
},
3533
})

packages/create-vite/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"scripts": {
1818
"dev": "unbuild --stub",
1919
"build": "unbuild",
20+
"typecheck": "tsc --noEmit",
2021
"prepublishOnly": "npm run build"
2122
},
2223
"engines": {

packages/create-vite/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"include": ["src", "__tests__"],
2+
"include": ["build.config.ts", "src", "__tests__"],
33
"compilerOptions": {
44
"outDir": "dist",
55
"target": "ES2020",
66
"module": "ES2020",
77
"moduleResolution": "Node",
88
"strict": true,
9+
"skipLibCheck": true,
910
"declaration": false,
1011
"sourceMap": false,
1112
"noUnusedLocals": true,

packages/vite/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"build-types-roll": "api-extractor run && rimraf temp",
5353
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
5454
"build-types-check": "tsx scripts/checkBuiltTypes.ts && tsc --project tsconfig.check.json",
55+
"typecheck": "tsc --noEmit",
5556
"lint": "eslint --cache --ext .ts src/**",
5657
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
5758
"prepublishOnly": "npm run build"
@@ -111,6 +112,7 @@
111112
"postcss-load-config": "^4.0.1",
112113
"postcss-modules": "^6.0.0",
113114
"resolve.exports": "^1.1.0",
115+
"rollup-plugin-license": "^3.0.1",
114116
"sirv": "^2.0.2",
115117
"source-map-js": "^1.0.2",
116118
"source-map-support": "^0.5.21",

packages/vite/rollup.config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import json from '@rollup/plugin-json'
88
import MagicString from 'magic-string'
99
import type { Plugin, RollupOptions } from 'rollup'
1010
import { defineConfig } from 'rollup'
11-
import licensePlugin from '../../scripts/rollupLicensePlugin.mjs'
11+
import licensePlugin from './rollupLicensePlugin'
1212

1313
const pkg = JSON.parse(
1414
readFileSync(new URL('./package.json', import.meta.url)).toString(),
@@ -78,7 +78,7 @@ function createNodePlugins(
7878
isProduction: boolean,
7979
sourceMap: boolean,
8080
declarationDir: string | false,
81-
): Plugin[] {
81+
): (Plugin | false)[] {
8282
return [
8383
nodeResolve({ preferBuiltins: true }),
8484
typescript({
@@ -284,7 +284,7 @@ const __require = require;
284284
if (!chunk.fileName.includes('chunks/dep-')) return
285285

286286
const match = code.match(/^(?:import[\s\S]*?;\s*)+/)
287-
const index = match ? match.index + match[0].length : 0
287+
const index = match ? match.index! + match[0].length : 0
288288
const s = new MagicString(code)
289289
// inject after the last `import`
290290
s.appendRight(index, cjsPatch)

scripts/rollupLicensePlugin.mjs packages/vite/rollupLicensePlugin.ts

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
// @ts-check
2-
31
import fs from 'node:fs'
42
import path from 'node:path'
53
import license from 'rollup-plugin-license'
64
import colors from 'picocolors'
75
import fg from 'fast-glob'
86
import resolve from 'resolve'
7+
import type { Plugin } from 'rollup'
98

10-
/**
11-
* @param {string} licenseFilePath
12-
* @param {string} licenseTitle
13-
* @param {string} packageName
14-
*/
15-
function licensePlugin(licenseFilePath, licenseTitle, packageName) {
9+
export default function licensePlugin(
10+
licenseFilePath: string,
11+
licenseTitle: string,
12+
packageName: string,
13+
): Plugin {
1614
return license({
1715
thirdParty(dependencies) {
1816
// https://github.com/rollup/rollup/blob/master/build-plugins/generate-license-file.js
1917
// MIT Licensed https://github.com/rollup/rollup/blob/master/LICENSE-CORE.md
2018
const coreLicense = fs.readFileSync(
21-
new URL('../LICENSE', import.meta.url),
19+
new URL('../../LICENSE', import.meta.url),
2220
)
23-
function sortLicenses(licenses) {
24-
let withParenthesis = []
25-
let noParenthesis = []
21+
function sortLicenses(licenses: Set<string>) {
22+
let withParenthesis: string[] = []
23+
let noParenthesis: string[] = []
2624
licenses.forEach((license) => {
2725
if (/^\(/.test(license)) {
2826
withParenthesis.push(license)
@@ -34,12 +32,10 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) {
3432
noParenthesis = noParenthesis.sort()
3533
return [...noParenthesis, ...withParenthesis]
3634
}
37-
const licenses = new Set()
35+
const licenses = new Set<string>()
3836
const dependencyLicenseTexts = dependencies
39-
.sort(({ name: _nameA }, { name: _nameB }) => {
40-
const nameA = /** @type {string} */ (_nameA)
41-
const nameB = /** @type {string} */ (_nameB)
42-
return nameA > nameB ? 1 : nameB > nameA ? -1 : 0
37+
.sort(({ name: nameA }, { name: nameB }) => {
38+
return nameA! > nameB! ? 1 : nameB! > nameA! ? -1 : 0
4339
})
4440
.map(
4541
({
@@ -96,7 +92,7 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) {
9692
.join('\n') +
9793
'\n'
9894
}
99-
licenses.add(license)
95+
licenses.add(license!)
10096
return text
10197
},
10298
)
@@ -122,5 +118,3 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) {
122118
},
123119
})
124120
}
125-
126-
export default licensePlugin

packages/vite/scripts/tsconfig.json

-6
This file was deleted.

packages/vite/src/node/__tests__/build.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ describe('resolveBuildOutputs', () => {
421421
})
422422

423423
test('array outputs: should ignore build.lib.formats', () => {
424-
const log = { warn: vi.fn() } as Logger
424+
const log = { warn: vi.fn() } as unknown as Logger
425425
expect(
426426
resolveBuildOutputs(
427427
[{ name: 'A' }],

packages/vite/src/node/__tests__/plugins/define.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ async function createDefinePluginTransform(
1010
const config = await resolveConfig({ define }, build ? 'build' : 'serve')
1111
const instance = definePlugin(config)
1212
return async (code: string) => {
13-
const result = await instance.transform.call({}, code, 'foo.ts', { ssr })
13+
const result = await (instance.transform as any).call({}, code, 'foo.ts', {
14+
ssr,
15+
})
1416
return result?.code || result
1517
}
1618
}

packages/vite/src/node/__tests__/scan.spec.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ describe('optimizer-scan:script-test', () => {
1515
scriptRE.lastIndex = 0
1616
const [, tsOpenTag, tsContent] = scriptRE.exec(
1717
`<script lang="ts">${scriptContent}</script>`,
18-
)
18+
)!
1919
expect(tsOpenTag).toEqual('<script lang="ts">')
2020
expect(tsContent).toEqual(scriptContent)
2121

2222
scriptRE.lastIndex = 0
2323
const [, openTag, content] = scriptRE.exec(
2424
`<script>${scriptContent}</script>`,
25-
)
25+
)!
2626
expect(openTag).toEqual('<script>')
2727
expect(content).toEqual(scriptContent)
2828
})
@@ -58,12 +58,16 @@ describe('optimizer-scan:script-test', () => {
5858

5959
test('ordinary script tag test', () => {
6060
scriptRE.lastIndex = 0
61-
const [, tag, content] = scriptRE.exec(`<script >var test = null</script>`)
61+
const [, tag, content] = scriptRE.exec(
62+
`<script >var test = null</script>`,
63+
)!
6264
expect(tag).toEqual('<script >')
6365
expect(content).toEqual('var test = null')
6466

6567
scriptRE.lastIndex = 0
66-
const [, tag1, content1] = scriptRE.exec(`<script>var test = null</script>`)
68+
const [, tag1, content1] = scriptRE.exec(
69+
`<script>var test = null</script>`,
70+
)!
6771
expect(tag1).toEqual('<script>')
6872
expect(content1).toEqual('var test = null')
6973
})
@@ -90,7 +94,7 @@ describe('optimizer-scan:script-test', () => {
9094

9195
shouldMatchArray.forEach((str) => {
9296
importsRE.lastIndex = 0
93-
expect(importsRE.exec(str)[1]).toEqual("'vue'")
97+
expect(importsRE.exec(str)![1]).toEqual("'vue'")
9498
})
9599

96100
const shouldFailArray = [

packages/vite/tsconfig.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
22
"extends": "./tsconfig.base.json",
3+
"include": [
4+
"./rollup.config.ts",
5+
"scripts",
6+
"src/node/__tests__",
7+
"src/types/shims.d.ts"
8+
],
39
"compilerOptions": {
4-
"strict": false,
10+
"esModuleInterop": true,
511
"declaration": false,
612
"resolveJsonModule": true
7-
},
8-
"include": ["./rollup.config.ts"]
13+
}
914
}

playground/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "@vitejs/vite-playground",
33
"private": true,
44
"version": "1.0.0",
5+
"scripts": {
6+
"typecheck": "tsc --noEmit"
7+
},
58
"devDependencies": {
69
"convert-source-map": "^2.0.0",
710
"css-color-names": "^1.0.1",

pnpm-lock.yaml

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)