From 1168e5709765097f49d48d843015b7e9eabfcda9 Mon Sep 17 00:00:00 2001 From: bluwy Date: Sun, 5 Nov 2023 17:33:09 +0800 Subject: [PATCH] build: strip internal parameters --- packages/vite/package.json | 1 + packages/vite/rollup.dts.config.ts | 65 +++++++++++++++++++- packages/vite/src/node/server/moduleGraph.ts | 3 + pnpm-lock.yaml | 3 + 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/vite/package.json b/packages/vite/package.json index 0015355c94d0e0..671b38c8408441 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -83,6 +83,7 @@ }, "devDependencies": { "@ampproject/remapping": "^2.2.1", + "@babel/parser": "^7.23.0", "@jridgewell/trace-mapping": "^0.3.20", "@rollup/plugin-alias": "^5.0.1", "@rollup/plugin-commonjs": "^25.0.7", diff --git a/packages/vite/rollup.dts.config.ts b/packages/vite/rollup.dts.config.ts index cc1b20267c2d3f..cb767b2ab5bf02 100644 --- a/packages/vite/rollup.dts.config.ts +++ b/packages/vite/rollup.dts.config.ts @@ -4,6 +4,9 @@ import { findStaticImports } from 'mlly' import { defineConfig } from 'rollup' import type { Plugin, PluginContext, RenderedChunk } from 'rollup' import dts from 'rollup-plugin-dts' +import { parse } from '@babel/parser' +import { walk } from 'estree-walker' +import MagicString from 'magic-string' const depTypesDir = new URL('./src/types/', import.meta.url) const pkg = JSON.parse( @@ -58,7 +61,8 @@ const identifierReplacements: Record> = { * 1. Resolve `dep-types/*` and `types/*` imports * 2. Validate unallowed dependency imports * 3. Replace confusing type names - * 4. Clean unnecessary comments + * 4. Strip leftover internal types + * 5. Clean unnecessary comments */ function patchTypes(): Plugin { return { @@ -83,6 +87,7 @@ function patchTypes(): Plugin { renderChunk(code, chunk) { validateChunkImports.call(this, chunk) code = replaceConfusingTypeNames.call(this, code, chunk) + code = stripInternalTypes.call(this, code, chunk) code = cleanUnnecessaryComments(code) return code }, @@ -174,6 +179,64 @@ function replaceConfusingTypeNames( return code } +/** + * While we already enable `compilerOptions.stripInternal`, some internal comments + * like internal parameters are still not stripped by TypeScript, so we run another + * pass here. + */ +function stripInternalTypes( + this: PluginContext, + code: string, + chunk: RenderedChunk, +) { + if (code.includes('@internal')) { + const s = new MagicString(code) + const ast = parse(code, { + plugins: ['typescript'], + sourceType: 'module', + }) + + walk(ast as any, { + enter(node: any) { + if (removeInternal(s, node)) { + this.skip() + } + }, + }) + + code = s.toString() + + if (code.includes('@internal')) { + this.warn(`${chunk.fileName} has unhandled @internal declarations`) + process.exitCode = 1 + } + } + + return code +} + +/** + * Remove `@internal` comments not handled by `compilerOptions.stripInternal` + * Reference: https://github.com/vuejs/core/blob/main/rollup.dts.config.js + */ +function removeInternal(s: MagicString, node: any): boolean { + if ( + node.leadingComments && + node.leadingComments.some((c: any) => { + return c.type === 'CommentBlock' && c.value.includes('@internal') + }) + ) { + // Examples: + // function a(foo: string, /* @internal */ bar: number) + // ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + // strip trailing comma + const end = s.original[node.end] === ',' ? node.end + 1 : node.end + s.remove(node.leadingComments[0].start, end) + return true + } + return false +} + function cleanUnnecessaryComments(code: string) { return code .replace(singlelineCommentsRE, '') diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index 9266e2090e29cb..7985258187a469 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -152,7 +152,9 @@ export class ModuleGraph { seen: Set = new Set(), timestamp: number = Date.now(), isHmr: boolean = false, + /** @internal */ hmrBoundaries: ModuleNode[] = [], + /** @internal */ softInvalidate = false, ): void { const prevInvalidationState = mod.invalidationState @@ -249,6 +251,7 @@ export class ModuleGraph { acceptedExports: Set | null, isSelfAccepting: boolean, ssr?: boolean, + /** @internal */ staticImportedUrls?: Set, ): Promise | undefined> { mod.isSelfAccepting = isSelfAccepting diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2c6a5017e2d748..907924494bdd86 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -240,6 +240,9 @@ importers: '@ampproject/remapping': specifier: ^2.2.1 version: 2.2.1 + '@babel/parser': + specifier: ^7.23.0 + version: 7.23.0 '@jridgewell/trace-mapping': specifier: ^0.3.20 version: 0.3.20