Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default defineConfig(
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022,
isolatedDeclarations: true,
project: shouldTypeCheck
? [
'./packages/*/tsconfig.json',
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ function wrapIIFEBabelPlugin(): BabelPlugin {
}
}

export const cspHashes = [
export const cspHashes: string[] = [
safari10NoModuleFix,
systemJSInlineCode,
detectModernBrowserCode,
Expand Down
16 changes: 8 additions & 8 deletions packages/plugin-legacy/src/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc
// DO NOT ALTER THIS CONTENT
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()}}();`
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()}}();`

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

const detectModernBrowserVarName = '__vite_is_modern_browser'
export const detectModernBrowserDetector = `import.meta.url;import("_").catch(()=>1);(async function*(){})().next()`
export const detectModernBrowserCode = `${detectModernBrowserDetector};window.${detectModernBrowserVarName}=true`
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)}();`
export const detectModernBrowserDetector: string = `import.meta.url;import("_").catch(()=>1);(async function*(){})().next()`
export const detectModernBrowserCode: string = `${detectModernBrowserDetector};window.${detectModernBrowserVarName}=true`
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)}();`

export const modernChunkLegacyGuard = `export function __vite_legacy_guard(){${detectModernBrowserDetector}};`
export const modernChunkLegacyGuard: string = `export function __vite_legacy_guard(){${detectModernBrowserDetector}};`
5 changes: 3 additions & 2 deletions packages/plugin-legacy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"include": ["tsdown.config.ts", "src"],
"include": ["src"],
"exclude": ["**/*.spec.ts"],
"compilerOptions": {
"outDir": "dist",
"target": "ES2023",
"module": "Preserve",
"moduleResolution": "bundler",
"strict": true,
"isolatedDeclarations": true,
"declaration": true,
"strict": true,
"sourceMap": true,
"noUnusedLocals": true,
"esModuleInterop": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"build-types": "pnpm build-types-roll && pnpm build-types-check",
"build-types-roll": "rolldown --config rolldown.dts.config.ts",
"build-types-check": "tsc --project tsconfig.check.json",
"typecheck": "tsc && tsc -p src/node",
"typecheck": "tsc && tsc -p src/node && tsc -p src/module-runner && tsc -p src/shared && tsc -p src/node/__tests_dts__",
"lint": "eslint --cache --ext .ts src/**",
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
"generate-target": "tsx scripts/generateTarget.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"types": [],
"target": "ES2020",
"lib": ["ESNext", "DOM"],
"isolatedDeclarations": false,
"declaration": false
}
}
3 changes: 2 additions & 1 deletion packages/vite/src/module-runner/esmEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
import type { ModuleEvaluator, ModuleRunnerContext } from './types'

export class ESModulesEvaluator implements ModuleEvaluator {
public readonly startOffset = getAsyncFunctionDeclarationPaddingLineCount()
public readonly startOffset: number =
getAsyncFunctionDeclarationPaddingLineCount()

async runInlinedModule(
context: ModuleRunnerContext,
Expand Down
11 changes: 6 additions & 5 deletions packages/vite/src/module-runner/evaluatedModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const MODULE_RUNNER_SOURCEMAPPING_REGEXP = new RegExp(
)

export class EvaluatedModuleNode {
public importers = new Set<string>()
public imports = new Set<string>()
public importers: Set<string> = new Set()
public imports: Set<string> = new Set()
public evaluated = false
public meta: ResolvedResult | undefined
public promise: Promise<any> | undefined
Expand All @@ -27,9 +27,10 @@ export class EvaluatedModuleNode {
}

export class EvaluatedModules {
public readonly idToModuleMap = new Map<string, EvaluatedModuleNode>()
public readonly fileToModulesMap = new Map<string, Set<EvaluatedModuleNode>>()
public readonly urlToIdModuleMap = new Map<string, EvaluatedModuleNode>()
public readonly idToModuleMap: Map<string, EvaluatedModuleNode> = new Map()
public readonly fileToModulesMap: Map<string, Set<EvaluatedModuleNode>> =
new Map()
public readonly urlToIdModuleMap: Map<string, EvaluatedModuleNode> = new Map()

/**
* Returns the module node by the resolved module ID. Usually, module ID is
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ModuleRunner {
constructor(
public options: ModuleRunnerOptions,
public evaluator: ModuleEvaluator = new ESModulesEvaluator(),
private debug?: ModuleRunnerDebugger,
private debug?: ModuleRunnerDebugger | undefined,
) {
this.evaluatedModules = options.evaluatedModules ?? new EvaluatedModules()
this.transport = normalizeModuleRunnerTransport(options.transport)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/module-runner/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"include": ["./", "../node", "../dep-types", "../types"],
"exclude": ["**/__tests__", "**/__tests_dts__"],
"include": ["./", "../node", "../types"],
"exclude": ["../**/__tests__", "../**/__tests_dts__"],
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"stripInternal": true
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/module-runner/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as pathe from 'pathe'
import { isWindows } from '../shared/utils'

export const decodeBase64 =
export const decodeBase64: typeof atob =
typeof atob !== 'undefined'
? atob
: (str: string) => Buffer.from(str, 'base64').toString('utf-8')
Expand Down Expand Up @@ -32,8 +32,8 @@ function encodePathChars(filepath: string) {
return filepath
}

export const posixDirname = pathe.dirname
export const posixResolve = pathe.resolve
export const posixDirname: (path: string) => string = pathe.dirname
export const posixResolve: (...paths: string[]) => string = pathe.resolve

export function posixPathToFileHref(posixPath: string): string {
let resolved = posixResolve(posixPath)
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/__tests_dts__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"isolatedDeclarations": false,
"declaration": false
},
"include": ["../", "../../types"],
"exclude": ["../**/__tests__"]
}
23 changes: 15 additions & 8 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export interface ResolvedBuildOptions
modulePreload: false | ResolvedModulePreloadOptions
}

export const buildEnvironmentOptionsDefaults = Object.freeze({
const _buildEnvironmentOptionsDefaults = Object.freeze({
target: 'baseline-widely-available',
/** @deprecated */
polyfillModulePreload: true,
Expand Down Expand Up @@ -390,7 +390,10 @@ export const buildEnvironmentOptionsDefaults = Object.freeze({
chunkSizeWarningLimit: 500,
watch: null,
// createEnvironment
})
} satisfies BuildEnvironmentOptions)
export const buildEnvironmentOptionsDefaults: Readonly<
Partial<BuildEnvironmentOptions>
> = _buildEnvironmentOptionsDefaults
Comment on lines +394 to +396
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other similar setups are kinda funky to handle with isolated declarations, and they all relate to configDefaults, since our source code depends on satisifies and the inferred type. But it is an issue if we one day export configDefaults and its type, so the hoops here are as if we're tackling that now as isolated declarations don't know if the exported type is public


export function resolveBuildEnvironmentOptions(
raw: BuildEnvironmentOptions,
Expand All @@ -414,7 +417,7 @@ export function resolveBuildEnvironmentOptions(

const merged = mergeWithDefaults(
{
...buildEnvironmentOptionsDefaults,
..._buildEnvironmentOptionsDefaults,
cssCodeSplit: !raw.lib,
minify: consumer === 'server' ? false : 'esbuild',
ssr: consumer === 'server',
Expand Down Expand Up @@ -1443,8 +1446,10 @@ export function toOutputFilePathWithoutRuntime(
}
}

export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime
export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime
export const toOutputFilePathInCss: typeof toOutputFilePathWithoutRuntime =
toOutputFilePathWithoutRuntime
export const toOutputFilePathInHtml: typeof toOutputFilePathWithoutRuntime =
toOutputFilePathWithoutRuntime

export class BuildEnvironment extends BaseEnvironment {
mode = 'build' as const
Expand Down Expand Up @@ -1505,18 +1510,20 @@ export interface BuilderOptions {
buildApp?: (builder: ViteBuilder) => Promise<void>
}

export const builderOptionsDefaults = Object.freeze({
const _builderOptionsDefaults = Object.freeze({
sharedConfigBuild: false,
sharedPlugins: false,
// buildApp
})
} satisfies BuilderOptions)
export const builderOptionsDefaults: Readonly<Partial<BuilderOptions>> =
_builderOptionsDefaults

export function resolveBuilderOptions(
options: BuilderOptions | undefined,
): ResolvedBuilderOptions | undefined {
if (!options) return
return mergeWithDefaults(
{ ...builderOptionsDefaults, buildApp: async () => {} },
{ ..._builderOptionsDefaults, buildApp: async () => {} },
options,
)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import {

const debug = createDebugger('vite:config', { depth: 10 })
const promisifiedRealpath = promisify(fs.realpath)
const SYMBOL_RESOLVED_CONFIG = Symbol('vite:resolved-config')
const SYMBOL_RESOLVED_CONFIG: unique symbol = Symbol('vite:resolved-config')

export interface ConfigEnv {
/**
Expand Down Expand Up @@ -641,7 +641,7 @@ export interface ResolvedConfig
> {}

// inferred ones are omitted
export const configDefaults = Object.freeze({
const configDefaults = Object.freeze({
define: {},
dev: {
warmup: [],
Expand Down Expand Up @@ -721,7 +721,7 @@ export const configDefaults = Object.freeze({
envPrefix: 'VITE_',
worker: {
format: 'iife',
plugins: () => [],
plugins: (): never[] => [],
// rollupOptions
},
optimizeDeps: {
Expand Down
Loading