Skip to content

Commit

Permalink
build: strip internal parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and patak-dev committed Nov 6, 2023
1 parent 5684382 commit 1168e57
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 64 additions & 1 deletion packages/vite/rollup.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -58,7 +61,8 @@ const identifierReplacements: Record<string, Record<string, string>> = {
* 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 {
Expand All @@ -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
},
Expand Down Expand Up @@ -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, '')
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ export class ModuleGraph {
seen: Set<ModuleNode> = new Set(),
timestamp: number = Date.now(),
isHmr: boolean = false,
/** @internal */
hmrBoundaries: ModuleNode[] = [],
/** @internal */
softInvalidate = false,
): void {
const prevInvalidationState = mod.invalidationState
Expand Down Expand Up @@ -249,6 +251,7 @@ export class ModuleGraph {
acceptedExports: Set<string> | null,
isSelfAccepting: boolean,
ssr?: boolean,
/** @internal */
staticImportedUrls?: Set<string>,
): Promise<Set<ModuleNode> | undefined> {
mod.isSelfAccepting = isSelfAccepting
Expand Down
3 changes: 3 additions & 0 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 1168e57

Please sign in to comment.