Skip to content

Commit dad7643

Browse files
bluwysapphi-red
andauthored
build: use isolated declarations (#20928)
Co-authored-by: 翠 <green@sapphi.red>
1 parent c4f6039 commit dad7643

38 files changed

+197
-143
lines changed

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default defineConfig(
3939
parserOptions: {
4040
sourceType: 'module',
4141
ecmaVersion: 2022,
42+
isolatedDeclarations: true,
4243
project: shouldTypeCheck
4344
? [
4445
'./packages/*/tsconfig.json',

packages/plugin-legacy/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ function wrapIIFEBabelPlugin(): BabelPlugin {
10041004
}
10051005
}
10061006

1007-
export const cspHashes = [
1007+
export const cspHashes: string[] = [
10081008
safari10NoModuleFix,
10091009
systemJSInlineCode,
10101010
detectModernBrowserCode,
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc
22
// DO NOT ALTER THIS CONTENT
3-
export const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();`
3+
export const safari10NoModuleFix: string = `!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();`
44

5-
export const legacyPolyfillId = 'vite-legacy-polyfill'
6-
export const legacyEntryId = 'vite-legacy-entry'
7-
export const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`
5+
export const legacyPolyfillId: string = 'vite-legacy-polyfill'
6+
export const legacyEntryId: string = 'vite-legacy-entry'
7+
export const systemJSInlineCode: string = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`
88

99
const detectModernBrowserVarName = '__vite_is_modern_browser'
10-
export const detectModernBrowserDetector = `import.meta.url;import("_").catch(()=>1);(async function*(){})().next()`
11-
export const detectModernBrowserCode = `${detectModernBrowserDetector};window.${detectModernBrowserVarName}=true`
12-
export const dynamicFallbackInlineCode = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`
10+
export const detectModernBrowserDetector: string = `import.meta.url;import("_").catch(()=>1);(async function*(){})().next()`
11+
export const detectModernBrowserCode: string = `${detectModernBrowserDetector};window.${detectModernBrowserVarName}=true`
12+
export const dynamicFallbackInlineCode: string = `!function(){if(window.${detectModernBrowserVarName})return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("${legacyPolyfillId}"),n=document.createElement("script");n.src=e.src,n.onload=function(){${systemJSInlineCode}},document.body.appendChild(n)}();`
1313

14-
export const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectModernBrowserDetector}};`
14+
export const modernChunkLegacyGuard: string = `export function __vite_legacy_guard(){${detectModernBrowserDetector}};`

packages/plugin-legacy/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
2-
"include": ["tsdown.config.ts", "src"],
2+
"include": ["src"],
33
"exclude": ["**/*.spec.ts"],
44
"compilerOptions": {
55
"outDir": "dist",
66
"target": "ES2023",
77
"module": "Preserve",
88
"moduleResolution": "bundler",
9-
"strict": true,
9+
"isolatedDeclarations": true,
1010
"declaration": true,
11+
"strict": true,
1112
"sourceMap": true,
1213
"noUnusedLocals": true,
1314
"esModuleInterop": true,

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"build-types": "pnpm build-types-roll && pnpm build-types-check",
7474
"build-types-roll": "rolldown --config rolldown.dts.config.ts",
7575
"build-types-check": "tsc --project tsconfig.check.json",
76-
"typecheck": "tsc && tsc -p src/node",
76+
"typecheck": "tsc && tsc -p src/node && tsc -p src/module-runner && tsc -p src/shared && tsc -p src/node/__tests_dts__",
7777
"lint": "eslint --cache --ext .ts src/**",
7878
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
7979
"generate-target": "tsx scripts/generateTarget.ts",

packages/vite/src/client/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"types": [],
66
"target": "ES2020",
77
"lib": ["ESNext", "DOM"],
8+
"isolatedDeclarations": false,
89
"declaration": false
910
}
1011
}

packages/vite/src/module-runner/esmEvaluator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
import type { ModuleEvaluator, ModuleRunnerContext } from './types'
1414

1515
export class ESModulesEvaluator implements ModuleEvaluator {
16-
public readonly startOffset = getAsyncFunctionDeclarationPaddingLineCount()
16+
public readonly startOffset: number =
17+
getAsyncFunctionDeclarationPaddingLineCount()
1718

1819
async runInlinedModule(
1920
context: ModuleRunnerContext,

packages/vite/src/module-runner/evaluatedModules.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const MODULE_RUNNER_SOURCEMAPPING_REGEXP = new RegExp(
99
)
1010

1111
export class EvaluatedModuleNode {
12-
public importers = new Set<string>()
13-
public imports = new Set<string>()
12+
public importers: Set<string> = new Set()
13+
public imports: Set<string> = new Set()
1414
public evaluated = false
1515
public meta: ResolvedResult | undefined
1616
public promise: Promise<any> | undefined
@@ -27,9 +27,10 @@ export class EvaluatedModuleNode {
2727
}
2828

2929
export class EvaluatedModules {
30-
public readonly idToModuleMap = new Map<string, EvaluatedModuleNode>()
31-
public readonly fileToModulesMap = new Map<string, Set<EvaluatedModuleNode>>()
32-
public readonly urlToIdModuleMap = new Map<string, EvaluatedModuleNode>()
30+
public readonly idToModuleMap: Map<string, EvaluatedModuleNode> = new Map()
31+
public readonly fileToModulesMap: Map<string, Set<EvaluatedModuleNode>> =
32+
new Map()
33+
public readonly urlToIdModuleMap: Map<string, EvaluatedModuleNode> = new Map()
3334

3435
/**
3536
* Returns the module node by the resolved module ID. Usually, module ID is

packages/vite/src/module-runner/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class ModuleRunner {
5050
constructor(
5151
public options: ModuleRunnerOptions,
5252
public evaluator: ModuleEvaluator = new ESModulesEvaluator(),
53-
private debug?: ModuleRunnerDebugger,
53+
private debug?: ModuleRunnerDebugger | undefined,
5454
) {
5555
this.evaluatedModules = options.evaluatedModules ?? new EvaluatedModules()
5656
this.transport = normalizeModuleRunnerTransport(options.transport)

packages/vite/src/module-runner/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../tsconfig.base.json",
3-
"include": ["./", "../node", "../dep-types", "../types"],
4-
"exclude": ["**/__tests__", "**/__tests_dts__"],
3+
"include": ["./", "../node", "../types"],
4+
"exclude": ["../**/__tests__", "../**/__tests_dts__"],
55
"compilerOptions": {
66
"lib": ["ESNext", "DOM"],
77
"stripInternal": true

0 commit comments

Comments
 (0)