Skip to content

Commit f730162

Browse files
committed
Rewrite references instead
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.
1 parent e363f46 commit f730162

File tree

5 files changed

+110
-56
lines changed

5 files changed

+110
-56
lines changed

packages/next/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/// <reference types="./types/global" />
2-
/// <reference types="./types/compiled" />
32
/// <reference path="./dist/styled-jsx/types/global.d.ts" />
43
/// <reference path="./amp.d.ts" />
54
/// <reference path="./app.d.ts" />

packages/next/taskfile.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,10 @@ export async function trace(task, opts) {
25762576
}
25772577

25782578
export async function build(task, opts) {
2579-
await task.serial(['precompile', 'compile', 'generate_types'], opts)
2579+
await task.serial(
2580+
['precompile', 'compile', 'generate_types', 'rewrite_compiled_references'],
2581+
opts
2582+
)
25802583
}
25812584

25822585
export async function generate_types(task, opts) {
@@ -2585,6 +2588,37 @@ export async function generate_types(task, opts) {
25852588
})
25862589
}
25872590

2591+
/**
2592+
* TypeScript will emit references to the compiled types used to type the implementation.
2593+
* The declarations however don't need such detailed types.
2594+
* We rewrite the references to reference a more lightweight solution instead.
2595+
* @param {import('taskr').Task} task
2596+
*/
2597+
export async function rewrite_compiled_references(task, opts) {
2598+
const declarationDirectory = join(__dirname, 'dist')
2599+
const declarationFiles = glob.sync('**/*.d.ts', { cwd: declarationDirectory })
2600+
2601+
for (const declarationFile of declarationFiles) {
2602+
const content = await fs.readFile(
2603+
join(declarationDirectory, declarationFile),
2604+
'utf8'
2605+
)
2606+
// Rewrite
2607+
// /// <reference path="../../../../types/$$compiled.internal.d.ts" />
2608+
// to
2609+
// /// <reference path="../../../../types/compiled.d.ts" />
2610+
if (content.indexOf('/types/$$compiled.internal.d.ts" />') !== -1) {
2611+
await fs.writeFile(
2612+
join(declarationDirectory, declarationFile),
2613+
content.replace(
2614+
/\/types\/\$\$compiled\.internal\.d\.ts" \/>/g,
2615+
'/types/compiled.d.ts" />'
2616+
)
2617+
)
2618+
}
2619+
}
2620+
}
2621+
25882622
export default async function (task) {
25892623
const opts = { dev: true }
25902624
await task.clear('dist')

packages/next/types/misc.d.ts renamed to packages/next/types/$$compiled.internal.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,33 @@ declare module 'next/dist/compiled/zod' {
446446
import * as m from 'zod'
447447
export = m
448448
}
449+
450+
declare module 'mini-css-extract-plugin'
451+
declare module 'next/dist/compiled/loader-utils3'
452+
453+
declare module 'next/dist/compiled/webpack/webpack' {
454+
import type webpackSources from 'webpack-sources1'
455+
export function init(): void
456+
export let BasicEvaluatedExpression: any
457+
export let GraphHelpers: any
458+
export let sources: typeof webpackSources
459+
export let StringXor: any
460+
export {
461+
default as webpack,
462+
Compiler,
463+
Compilation,
464+
Module,
465+
Stats,
466+
Template,
467+
RuntimeModule,
468+
RuntimeGlobals,
469+
NormalModule,
470+
ResolvePluginInstance,
471+
ModuleFilenameHelpers,
472+
} from 'webpack'
473+
export type {
474+
LoaderDefinitionFunction,
475+
LoaderContext,
476+
ModuleGraph,
477+
} from 'webpack'
478+
}

packages/next/types/compiled.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,48 @@ declare module 'VAR_MODULE_GLOBAL_ERROR'
88
declare module 'VAR_USERLAND'
99
declare module 'VAR_MODULE_DOCUMENT'
1010
declare module 'VAR_MODULE_APP'
11+
12+
declare module 'next/dist/compiled/webpack/webpack' {
13+
export function init(): void
14+
export let BasicEvaluatedExpression: any
15+
export let GraphHelpers: any
16+
export let sources: any
17+
export let StringXor: any
18+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
19+
export type LoaderDefinitionFunction<T> = any
20+
namespace webpack {
21+
export type Compiler = any
22+
export type WebpackPluginInstance = any
23+
export type Compilation = any
24+
export type Module = any
25+
export type Stats = any
26+
export type Template = any
27+
export type RuntimeModule = any
28+
export type RuntimeGlobals = any
29+
export type NormalModule = any
30+
export type ResolvePluginInstance = any
31+
export type Configuration = any
32+
export type ResolveOptions = any
33+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
34+
export type LoaderContext<T> = any
35+
export type RuleSetUseItem = any
36+
export type EntryObject = any
37+
export type Chunk = any
38+
export type ChunkGroup = any
39+
export type DefinePlugin = any
40+
// eslint-disable-next-line @typescript-eslint/no-shadow
41+
namespace sources {
42+
export type RawSource = any
43+
}
44+
}
45+
export var webpack: any
46+
}
47+
48+
declare module 'next/dist/compiled/superstruct' {
49+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
50+
export type Struct<T, S> = any
51+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
52+
export type Infer<T = any> = any
53+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
54+
export type Describe<T> = any
55+
}

packages/next/types/webpack.d.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)