Skip to content

Commit

Permalink
chore: typecheck create-vite (vitejs#11295)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre authored and futurGH committed Feb 26, 2023
1 parent dd65dd4 commit 98a84af
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 51 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:')
})

Expand Down
10 changes: 4 additions & 6 deletions packages/create-vite/build.config.ts
Original file line number Diff line number Diff line change
@@ -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))

Expand All @@ -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',
),
)
]
},
},
})
1 change: 1 addition & 0 deletions packages/create-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"scripts": {
"dev": "unbuild --stub",
"build": "unbuild",
"typecheck": "tsc --noEmit",
"prepublishOnly": "npm run build"
},
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion packages/create-vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -78,7 +78,7 @@ function createNodePlugins(
isProduction: boolean,
sourceMap: boolean,
declarationDir: string | false,
): Plugin[] {
): (Plugin | false)[] {
return [
nodeResolve({ preferBuiltins: true }),
typescript({
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string>) {
let withParenthesis: string[] = []
let noParenthesis: string[] = []
licenses.forEach((license) => {
if (/^\(/.test(license)) {
withParenthesis.push(license)
Expand All @@ -34,12 +32,10 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) {
noParenthesis = noParenthesis.sort()
return [...noParenthesis, ...withParenthesis]
}
const licenses = new Set()
const licenses = new Set<string>()
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(
({
Expand Down Expand Up @@ -96,7 +92,7 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) {
.join('\n') +
'\n'
}
licenses.add(license)
licenses.add(license!)
return text
},
)
Expand All @@ -122,5 +118,3 @@ function licensePlugin(licenseFilePath, licenseTitle, packageName) {
},
})
}

export default licensePlugin
6 changes: 0 additions & 6 deletions packages/vite/scripts/tsconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/__tests__/plugins/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
14 changes: 9 additions & 5 deletions packages/vite/src/node/__tests__/scan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ describe('optimizer-scan:script-test', () => {
scriptRE.lastIndex = 0
const [, tsOpenTag, tsContent] = scriptRE.exec(
`<script lang="ts">${scriptContent}</script>`,
)
)!
expect(tsOpenTag).toEqual('<script lang="ts">')
expect(tsContent).toEqual(scriptContent)

scriptRE.lastIndex = 0
const [, openTag, content] = scriptRE.exec(
`<script>${scriptContent}</script>`,
)
)!
expect(openTag).toEqual('<script>')
expect(content).toEqual(scriptContent)
})
Expand Down Expand Up @@ -58,12 +58,16 @@ describe('optimizer-scan:script-test', () => {

test('ordinary script tag test', () => {
scriptRE.lastIndex = 0
const [, tag, content] = scriptRE.exec(`<script >var test = null</script>`)
const [, tag, content] = scriptRE.exec(
`<script >var test = null</script>`,
)!
expect(tag).toEqual('<script >')
expect(content).toEqual('var test = null')

scriptRE.lastIndex = 0
const [, tag1, content1] = scriptRE.exec(`<script>var test = null</script>`)
const [, tag1, content1] = scriptRE.exec(
`<script>var test = null</script>`,
)!
expect(tag1).toEqual('<script>')
expect(content1).toEqual('var test = null')
})
Expand All @@ -90,7 +94,7 @@ describe('optimizer-scan:script-test', () => {

shouldMatchArray.forEach((str) => {
importsRE.lastIndex = 0
expect(importsRE.exec(str)[1]).toEqual("'vue'")
expect(importsRE.exec(str)![1]).toEqual("'vue'")
})

const shouldFailArray = [
Expand Down
11 changes: 8 additions & 3 deletions packages/vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"extends": "./tsconfig.base.json",
"include": [
"./rollup.config.ts",
"scripts",
"src/node/__tests__",
"src/types/shims.d.ts"
],
"compilerOptions": {
"strict": false,
"esModuleInterop": true,
"declaration": false,
"resolveJsonModule": true
},
"include": ["./rollup.config.ts"]
}
}
3 changes: 3 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "@vitejs/vite-playground",
"private": true,
"version": "1.0.0",
"scripts": {
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"convert-source-map": "^2.0.0",
"css-color-names": "^1.0.1",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98a84af

Please sign in to comment.