Skip to content

Commit

Permalink
Rewrite references instead
Browse files Browse the repository at this point in the history
Combines misc.d.ts and webpack.d.ts into `$$compiled.internal.d.ts`.

`$$compiled.internal.d.ts` represents type declarations necessary to type the implementation while `compiled.d.ts`
represents declarations necessary to type the emitted declarations.

This internal/public split is really just a typechecking perf improvement.

We should just be able to include both public and internal ambient declarations in the declaratione emit program
and then just test the internal one isn't referenced.
  • Loading branch information
eps1lon committed Apr 15, 2024
1 parent fd771b7 commit 24914f3
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 56 deletions.
1 change: 0 additions & 1 deletion packages/next/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference types="./types/global" />
/// <reference types="./types/compiled" />
/// <reference path="./dist/styled-jsx/types/global.d.ts" />
/// <reference path="./amp.d.ts" />
/// <reference path="./app.d.ts" />
Expand Down
36 changes: 35 additions & 1 deletion packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,10 @@ export async function trace(task, opts) {
}

export async function build(task, opts) {
await task.serial(['precompile', 'compile', 'generate_types'], opts)
await task.serial(
['precompile', 'compile', 'generate_types', 'rewrite_compiled_references'],
opts
)
}

export async function generate_types(task, opts) {
Expand All @@ -2585,6 +2588,37 @@ export async function generate_types(task, opts) {
})
}

/**
* TypeScript will emit references to the compiled types used to type the implementation.
* The declarations however don't need such detailed types.
* We rewrite the references to reference a more lightweight solution instead.
* @param {import('taskr').Task} task
*/
export async function rewrite_compiled_references(task, opts) {
const declarationDirectory = join(__dirname, 'dist')
const declarationFiles = glob.sync('**/*.d.ts', { cwd: declarationDirectory })

for (const declarationFile of declarationFiles) {
const content = await fs.readFile(
join(declarationDirectory, declarationFile),
'utf8'
)
// Rewrite
// /// <reference path="../../../../types/$$compiled.internal.d.ts" />
// to
// /// <reference path="../../../../types/compiled.d.ts" />
if (content.indexOf('/types/$$compiled.internal.d.ts" />') !== -1) {
await fs.writeFile(
join(declarationDirectory, declarationFile),
content.replace(
/\/types\/\$\$compiled\.internal\.d\.ts" \/>/g,
'/types/compiled.d.ts" />'
)
)
}
}
}

export default async function (task) {
const opts = { dev: true }
await task.clear('dist')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,33 @@ declare module 'next/dist/compiled/zod' {
import * as m from 'zod'
export = m
}

declare module 'mini-css-extract-plugin'
declare module 'next/dist/compiled/loader-utils3'

declare module 'next/dist/compiled/webpack/webpack' {
import type webpackSources from 'webpack-sources1'
export function init(): void
export let BasicEvaluatedExpression: any
export let GraphHelpers: any
export let sources: typeof webpackSources
export let StringXor: any
export {
default as webpack,
Compiler,
Compilation,
Module,
Stats,
Template,
RuntimeModule,
RuntimeGlobals,
NormalModule,
ResolvePluginInstance,
ModuleFilenameHelpers,
} from 'webpack'
export type {
LoaderDefinitionFunction,
LoaderContext,
ModuleGraph,
} from 'webpack'
}
45 changes: 45 additions & 0 deletions packages/next/types/compiled.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,48 @@ declare module 'VAR_MODULE_GLOBAL_ERROR'
declare module 'VAR_USERLAND'
declare module 'VAR_MODULE_DOCUMENT'
declare module 'VAR_MODULE_APP'

declare module 'next/dist/compiled/webpack/webpack' {
export function init(): void
export let BasicEvaluatedExpression: any
export let GraphHelpers: any
export let sources: any
export let StringXor: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type LoaderDefinitionFunction<T> = any
namespace webpack {
export type Compiler = any
export type WebpackPluginInstance = any
export type Compilation = any
export type Module = any
export type Stats = any
export type Template = any
export type RuntimeModule = any
export type RuntimeGlobals = any
export type NormalModule = any
export type ResolvePluginInstance = any
export type Configuration = any
export type ResolveOptions = any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type LoaderContext<T> = any
export type RuleSetUseItem = any
export type EntryObject = any
export type Chunk = any
export type ChunkGroup = any
export type DefinePlugin = any
// eslint-disable-next-line @typescript-eslint/no-shadow
namespace sources {
export type RawSource = any
}
}
export var webpack: any
}

declare module 'next/dist/compiled/superstruct' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type Struct<T, S> = any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type Infer<T = any> = any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type Describe<T> = any
}
54 changes: 0 additions & 54 deletions packages/next/types/webpack.d.ts

This file was deleted.

0 comments on commit 24914f3

Please sign in to comment.