From 91df516884a2eccf7c6bb839f7a1994b22d305e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Wed, 14 Dec 2022 22:18:07 +0100 Subject: [PATCH] chore: typecheck create-vite --- package.json | 3 +- packages/create-vite/__tests__/cli.spec.ts | 2 +- packages/create-vite/build.config.ts | 10 +++--- packages/create-vite/package.json | 1 + packages/create-vite/tsconfig.json | 3 +- packages/vite/package.json | 2 ++ packages/vite/rollup.config.ts | 6 ++-- .../vite/rollupLicensePlugin.ts | 34 ++++++++----------- packages/vite/scripts/tsconfig.json | 6 ---- .../vite/src/node/__tests__/build.spec.ts | 2 +- .../src/node/__tests__/plugins/define.spec.ts | 4 ++- packages/vite/src/node/__tests__/scan.spec.ts | 14 +++++--- packages/vite/tsconfig.json | 11 ++++-- playground/package.json | 3 ++ pnpm-lock.yaml | 4 +-- 15 files changed, 54 insertions(+), 51 deletions(-) rename scripts/rollupLicensePlugin.mjs => packages/vite/rollupLicensePlugin.ts (84%) delete mode 100644 packages/vite/scripts/tsconfig.json diff --git a/package.json b/package.json index 78245b974c08e8..859b71d5a8a50b 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "postinstall": "simple-git-hooks", "format": "prettier --write --cache .", "lint": "eslint --cache .", - "typecheck": "tsc -p scripts --noEmit && tsc -p playground --noEmit", + "typecheck": "tsc -p scripts --noEmit && pnpm -r --parallel run typecheck", "test": "run-s test-unit test-serve test-build", "test-serve": "vitest run -c vitest.config.e2e.ts", "test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts", @@ -81,7 +81,6 @@ "resolve": "^1.22.1", "rimraf": "^3.0.2", "rollup": "^3.7.0", - "rollup-plugin-license": "^3.0.1", "semver": "^7.3.8", "simple-git-hooks": "^2.8.1", "tslib": "^2.4.1", diff --git a/packages/create-vite/__tests__/cli.spec.ts b/packages/create-vite/__tests__/cli.spec.ts index 35239f2707c2c3..6f47f6e25aaa66 100644 --- a/packages/create-vite/__tests__/cli.spec.ts +++ b/packages/create-vite/__tests__/cli.spec.ts @@ -36,7 +36,7 @@ beforeAll(() => remove(genPath)) afterEach(() => remove(genPath)) test('prompts for the project name if none supplied', () => { - const { stdout, exitCode } = run([]) + const { stdout } = run([]) expect(stdout).toContain('Project name:') }) diff --git a/packages/create-vite/build.config.ts b/packages/create-vite/build.config.ts index 3ec4f99bab2213..435c39bc937a6a 100644 --- a/packages/create-vite/build.config.ts +++ b/packages/create-vite/build.config.ts @@ -1,7 +1,7 @@ import path from 'node:path' import url from 'node:url' import { defineBuildConfig } from 'unbuild' -import licensePlugin from '../../scripts/rollupLicensePlugin.mjs' +import licensePlugin from '../vite/rollupLicensePlugin' const __dirname = path.dirname(url.fileURLToPath(import.meta.url)) @@ -20,16 +20,14 @@ export default defineBuildConfig({ }, hooks: { 'rollup:options'(ctx, options) { - if (!options.plugins) { - options.plugins = [] - } - options.plugins.push( + options.plugins = [ + options.plugins, licensePlugin( path.resolve(__dirname, './LICENSE'), 'create-vite license', 'create-vite', ), - ) + ] }, }, }) diff --git a/packages/create-vite/package.json b/packages/create-vite/package.json index 63d1581acabd68..2e971c5d44496d 100644 --- a/packages/create-vite/package.json +++ b/packages/create-vite/package.json @@ -17,6 +17,7 @@ "scripts": { "dev": "unbuild --stub", "build": "unbuild", + "typecheck": "tsc --noEmit", "prepublishOnly": "npm run build" }, "engines": { diff --git a/packages/create-vite/tsconfig.json b/packages/create-vite/tsconfig.json index 0ec39bdf6a1404..3a66e6179b5f97 100644 --- a/packages/create-vite/tsconfig.json +++ b/packages/create-vite/tsconfig.json @@ -1,11 +1,12 @@ { - "include": ["src", "__tests__"], + "include": ["build.config.ts", "src", "__tests__"], "compilerOptions": { "outDir": "dist", "target": "ES2020", "module": "ES2020", "moduleResolution": "Node", "strict": true, + "skipLibCheck": true, "declaration": false, "sourceMap": false, "noUnusedLocals": true, diff --git a/packages/vite/package.json b/packages/vite/package.json index ed92ab8314862f..a9e6a6bfb5e15d 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -52,6 +52,7 @@ "build-types-roll": "api-extractor run && rimraf temp", "build-types-post-patch": "tsx scripts/postPatchTypes.ts", "build-types-check": "tsx scripts/checkBuiltTypes.ts && tsc --project tsconfig.check.json", + "typecheck": "tsc --noEmit", "lint": "eslint --cache --ext .ts src/**", "format": "prettier --write --cache --parser typescript \"src/**/*.ts\"", "prepublishOnly": "npm run build" @@ -111,6 +112,7 @@ "postcss-load-config": "^4.0.1", "postcss-modules": "^6.0.0", "resolve.exports": "^1.1.0", + "rollup-plugin-license": "^3.0.1", "sirv": "^2.0.2", "source-map-js": "^1.0.2", "source-map-support": "^0.5.21", diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index 80b00b80822ac4..8c59ac28f26799 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -8,7 +8,7 @@ import json from '@rollup/plugin-json' import MagicString from 'magic-string' import type { Plugin, RollupOptions } from 'rollup' import { defineConfig } from 'rollup' -import licensePlugin from '../../scripts/rollupLicensePlugin.mjs' +import licensePlugin from './rollupLicensePlugin' const pkg = JSON.parse( readFileSync(new URL('./package.json', import.meta.url)).toString(), @@ -78,7 +78,7 @@ function createNodePlugins( isProduction: boolean, sourceMap: boolean, declarationDir: string | false, -): Plugin[] { +): (Plugin | false)[] { return [ nodeResolve({ preferBuiltins: true }), typescript({ @@ -284,7 +284,7 @@ const __require = require; if (!chunk.fileName.includes('chunks/dep-')) return const match = code.match(/^(?:import[\s\S]*?;\s*)+/) - const index = match ? match.index + match[0].length : 0 + const index = match ? match.index! + match[0].length : 0 const s = new MagicString(code) // inject after the last `import` s.appendRight(index, cjsPatch) diff --git a/scripts/rollupLicensePlugin.mjs b/packages/vite/rollupLicensePlugin.ts similarity index 84% rename from scripts/rollupLicensePlugin.mjs rename to packages/vite/rollupLicensePlugin.ts index 3d527afb0129f7..f1d24f4b7c8e82 100644 --- a/scripts/rollupLicensePlugin.mjs +++ b/packages/vite/rollupLicensePlugin.ts @@ -1,28 +1,26 @@ -// @ts-check - import fs from 'node:fs' import path from 'node:path' import license from 'rollup-plugin-license' import colors from 'picocolors' import fg from 'fast-glob' import resolve from 'resolve' +import type { Plugin } from 'rollup' -/** - * @param {string} licenseFilePath - * @param {string} licenseTitle - * @param {string} packageName - */ -function licensePlugin(licenseFilePath, licenseTitle, packageName) { +export default function licensePlugin( + licenseFilePath: string, + licenseTitle: string, + packageName: string, +): Plugin { return license({ thirdParty(dependencies) { // https://github.com/rollup/rollup/blob/master/build-plugins/generate-license-file.js // MIT Licensed https://github.com/rollup/rollup/blob/master/LICENSE-CORE.md const coreLicense = fs.readFileSync( - new URL('../LICENSE', import.meta.url), + new URL('../../LICENSE', import.meta.url), ) - function sortLicenses(licenses) { - let withParenthesis = [] - let noParenthesis = [] + function sortLicenses(licenses: Set) { + let withParenthesis: string[] = [] + let noParenthesis: string[] = [] licenses.forEach((license) => { if (/^\(/.test(license)) { withParenthesis.push(license) @@ -34,12 +32,10 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) { noParenthesis = noParenthesis.sort() return [...noParenthesis, ...withParenthesis] } - const licenses = new Set() + const licenses = new Set() const dependencyLicenseTexts = dependencies - .sort(({ name: _nameA }, { name: _nameB }) => { - const nameA = /** @type {string} */ (_nameA) - const nameB = /** @type {string} */ (_nameB) - return nameA > nameB ? 1 : nameB > nameA ? -1 : 0 + .sort(({ name: nameA }, { name: nameB }) => { + return nameA! > nameB! ? 1 : nameB! > nameA! ? -1 : 0 }) .map( ({ @@ -96,7 +92,7 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) { .join('\n') + '\n' } - licenses.add(license) + licenses.add(license!) return text }, ) @@ -122,5 +118,3 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) { }, }) } - -export default licensePlugin diff --git a/packages/vite/scripts/tsconfig.json b/packages/vite/scripts/tsconfig.json deleted file mode 100644 index af11a209690527..00000000000000 --- a/packages/vite/scripts/tsconfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": "../tsconfig.base.json", - "compilerOptions": { - "module": "esnext" - } -} diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts index a77ef0d67b410b..4e65a2848602d3 100644 --- a/packages/vite/src/node/__tests__/build.spec.ts +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -421,7 +421,7 @@ describe('resolveBuildOutputs', () => { }) test('array outputs: should ignore build.lib.formats', () => { - const log = { warn: vi.fn() } as Logger + const log = { warn: vi.fn() } as unknown as Logger expect( resolveBuildOutputs( [{ name: 'A' }], diff --git a/packages/vite/src/node/__tests__/plugins/define.spec.ts b/packages/vite/src/node/__tests__/plugins/define.spec.ts index b1634ef9adcd1a..b85ccaf70575c0 100644 --- a/packages/vite/src/node/__tests__/plugins/define.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/define.spec.ts @@ -10,7 +10,9 @@ async function createDefinePluginTransform( const config = await resolveConfig({ define }, build ? 'build' : 'serve') const instance = definePlugin(config) return async (code: string) => { - const result = await instance.transform.call({}, code, 'foo.ts', { ssr }) + const result = await (instance.transform as any).call({}, code, 'foo.ts', { + ssr, + }) return result?.code || result } } diff --git a/packages/vite/src/node/__tests__/scan.spec.ts b/packages/vite/src/node/__tests__/scan.spec.ts index 0a417f37a49b0a..b461f447e609d7 100644 --- a/packages/vite/src/node/__tests__/scan.spec.ts +++ b/packages/vite/src/node/__tests__/scan.spec.ts @@ -15,14 +15,14 @@ describe('optimizer-scan:script-test', () => { scriptRE.lastIndex = 0 const [, tsOpenTag, tsContent] = scriptRE.exec( ``, - ) + )! expect(tsOpenTag).toEqual('`, - ) + )! expect(openTag).toEqual('`) + const [, tag, content] = scriptRE.exec( + ``, + )! expect(tag).toEqual('`) + const [, tag1, content1] = scriptRE.exec( + ``, + )! expect(tag1).toEqual('