Skip to content

Commit

Permalink
move all binding types to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Sep 4, 2024
1 parent 1995df1 commit 7a9fae7
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 403 deletions.
402 changes: 14 additions & 388 deletions packages/next/src/build/swc/index.ts

Large diffs are not rendered by default.

329 changes: 329 additions & 0 deletions packages/next/src/build/swc/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
import type { NextConfigComplete } from '../../server/config-shared'
import type { __ApiPreviewProps } from '../../server/api-utils'
import type { ExternalObject, RefCell, TurboTasks } from './generated'
import type { PageExtensions } from '../page-extensions-type'

export interface Binding {
isWasm: boolean
turbo: {
startTrace(options: any, turboTasks: ExternalObject<TurboTasks>): any
createTurboTasks(memoryLimit?: number): ExternalObject<TurboTasks>
entrypoints: {
stream(
turboTasks: ExternalObject<TurboTasks>,
rootDir: string,
applicationDir: string,
pageExtensions: PageExtensions,
fn: (entrypoints: any) => void
): any
get(
turboTasks: ExternalObject<TurboTasks>,
rootDir: string,
applicationDir: string,
pageExtensions: PageExtensions
): any
}
createProject(
options: ProjectOptions,
turboEngineOptions?: TurboEngineOptions
): Promise<Project>
startTurbopackTraceServer(traceFilePath: string): void

nextBuild?: any
}
mdx: {
compile(src: string, options: any): any
compileSync(src: string, options: any): any
}
minify(src: string, options: any): Promise<any>
minifySync(src: string, options: any): any
transform(src: string, options: any): Promise<any>
transformSync(src: string, options: any): any
parse(src: string, options: any): Promise<string>

getTargetTriple(): string | undefined

initCustomTraceSubscriber?(traceOutFilePath?: string): ExternalObject<RefCell>
teardownTraceSubscriber?(guardExternal: ExternalObject<RefCell>): void
initHeapProfiler?(): ExternalObject<RefCell>
teardownHeapProfiler?(guardExternal: ExternalObject<RefCell>): void
css: {
lightning: {
transform(transformOptions: any): Promise<any>
transformStyleAttr(transformAttrOptions: any): Promise<any>
}
}
}

export type StyledString =
| {
type: 'text'
Expand Down Expand Up @@ -61,3 +118,275 @@ export type TurbopackResult<T = {}> = T & {
issues: Issue[]
diagnostics: Diagnostics[]
}

export interface TurboEngineOptions {
/**
* An upper bound of memory that turbopack will attempt to stay under.
*/
memoryLimit?: number
}

export interface Middleware {
endpoint: Endpoint
}

export interface Instrumentation {
nodeJs: Endpoint
edge: Endpoint
}

export interface Entrypoints {
routes: Map<string, Route>
middleware?: Middleware
instrumentation?: Instrumentation
pagesDocumentEndpoint: Endpoint
pagesAppEndpoint: Endpoint
pagesErrorEndpoint: Endpoint
}

interface BaseUpdate {
resource: {
headers: unknown
path: string
}
diagnostics: unknown[]
issues: Issue[]
}

interface IssuesUpdate extends BaseUpdate {
type: 'issues'
}

interface EcmascriptMergedUpdate {
type: 'EcmascriptMergedUpdate'
chunks: { [moduleName: string]: { type: 'partial' } }
entries: { [moduleName: string]: { code: string; map: string; url: string } }
}

interface PartialUpdate extends BaseUpdate {
type: 'partial'
instruction: {
type: 'ChunkListUpdate'
merged: EcmascriptMergedUpdate[] | undefined
}
}

export type Update = IssuesUpdate | PartialUpdate

export interface HmrIdentifiers {
identifiers: string[]
}

/** @see https://github.com/vercel/next.js/blob/415cd74b9a220b6f50da64da68c13043e9b02995/packages/next-swc/crates/napi/src/next_api/project.rs#L824-L833 */
export interface TurbopackStackFrame {
isServer: boolean
isInternal?: boolean
file: string
/** 1-indexed, unlike source map tokens */
line?: number
/** 1-indexed, unlike source map tokens */
column?: number
methodName?: string
}

export type UpdateMessage =
| {
updateType: 'start'
}
| {
updateType: 'end'
value: UpdateInfo
}

export interface UpdateInfo {
duration: number
tasks: number
}

export interface Project {
update(options: Partial<ProjectOptions>): Promise<void>

entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>>

hmrEvents(identifier: string): AsyncIterableIterator<TurbopackResult<Update>>

hmrIdentifiersSubscribe(): AsyncIterableIterator<
TurbopackResult<HmrIdentifiers>
>

getSourceForAsset(filePath: string): Promise<string | null>

traceSource(
stackFrame: TurbopackStackFrame
): Promise<TurbopackStackFrame | null>

updateInfoSubscribe(
aggregationMs: number
): AsyncIterableIterator<TurbopackResult<UpdateMessage>>

onExit(): Promise<void>
}

export type Route =
| {
type: 'conflict'
}
| {
type: 'app-page'
pages: {
originalName: string
htmlEndpoint: Endpoint
rscEndpoint: Endpoint
}[]
}
| {
type: 'app-route'
originalName: string
endpoint: Endpoint
}
| {
type: 'page'
htmlEndpoint: Endpoint
dataEndpoint: Endpoint
}
| {
type: 'page-api'
endpoint: Endpoint
}

export interface Endpoint {
/** Write files for the endpoint to disk. */
writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>>

/**
* Listen to client-side changes to the endpoint.
* After clientChanged() has been awaited it will listen to changes.
* The async iterator will yield for each change.
*/
clientChanged(): Promise<AsyncIterableIterator<TurbopackResult>>

/**
* Listen to server-side changes to the endpoint.
* After serverChanged() has been awaited it will listen to changes.
* The async iterator will yield for each change.
*/
serverChanged(
includeIssues: boolean
): Promise<AsyncIterableIterator<TurbopackResult>>
}

interface EndpointConfig {
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
dynamicParams?: boolean
revalidate?: 'never' | 'force-cache' | number
fetchCache?:
| 'auto'
| 'default-cache'
| 'only-cache'
| 'force-cache'
| 'default-no-store'
| 'only-no-store'
| 'force-no-store'
runtime?: 'nodejs' | 'edge'
preferredRegion?: string
}

export type ServerPath = {
path: string
contentHash: string
}

export type WrittenEndpoint =
| {
type: 'nodejs'
/** The entry path for the endpoint. */
entryPath: string
/** All client paths that have been written for the endpoint. */
clientPaths: string[]
/** All server paths that have been written for the endpoint. */
serverPaths: ServerPath[]
config: EndpointConfig
}
| {
type: 'edge'
/** All client paths that have been written for the endpoint. */
clientPaths: string[]
/** All server paths that have been written for the endpoint. */
serverPaths: ServerPath[]
config: EndpointConfig
}
| {
type: 'none'
clientPaths: []
serverPaths: []
config: EndpointConfig
}

export interface ProjectOptions {
/**
* A root path from which all files must be nested under. Trying to access
* a file outside this root will fail. Think of this as a chroot.
*/
rootPath: string

/**
* A path inside the root_path which contains the app/pages directories.
*/
projectPath: string

/**
* The next.config.js contents.
*/
nextConfig: NextConfigComplete

/**
* Jsconfig, or tsconfig contents.
*
* Next.js implicitly requires to read it to support few options
* https://nextjs.org/docs/architecture/nextjs-compiler#legacy-decorators
* https://nextjs.org/docs/architecture/nextjs-compiler#importsource
*/
jsConfig: {
compilerOptions: object
}

/**
* A map of environment variables to use when compiling code.
*/
env: Record<string, string>

defineEnv: DefineEnv

/**
* Whether to watch the filesystem for file changes.
*/
watch: boolean

/**
* The mode in which Next.js is running.
*/
dev: boolean

/**
* The server actions encryption key.
*/
encryptionKey: string

/**
* The build id.
*/
buildId: string

/**
* Options for draft mode.
*/
previewProps: __ApiPreviewProps
}

export interface DefineEnv {
client: RustifiedEnv
edge: RustifiedEnv
nodejs: RustifiedEnv
}

export type RustifiedEnv = { name: string; value: string }[]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import fs, { constants as FS } from 'fs/promises'
import { launchEditor } from '../internal/helpers/launchEditor'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import type { Project, TurbopackStackFrame } from '../../../../build/swc'
import type { Project, TurbopackStackFrame } from '../../../../build/swc/types'

const currentSourcesByFile: Map<string, Promise<string | null>> = new Map()
export async function batchedTraceSource(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Update as TurbopackUpdate } from '../../build/swc'
import type { Update as TurbopackUpdate } from '../../build/swc/types'

export function extractModulesFromTurbopackMessage(
data: TurbopackUpdate | TurbopackUpdate[]
Expand Down
14 changes: 7 additions & 7 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import type {
TurbopackConnectedAction,
} from './hot-reloader-types'
import { HMR_ACTIONS_SENT_TO_BROWSER } from './hot-reloader-types'
import type { Update as TurbopackUpdate } from '../../build/swc'
import {
createDefineEnv,
type Endpoint,
type TurbopackResult,
type WrittenEndpoint,
} from '../../build/swc'
import type {
Update as TurbopackUpdate,
Endpoint,
WrittenEndpoint,
TurbopackResult,
} from '../../build/swc/types'
import { createDefineEnv } from '../../build/swc'
import * as Log from '../../build/output/log'
import {
getVersionInfo,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/hot-reloader-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Duplex } from 'stream'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type getBaseWebpackConfig from '../../build/webpack-config'
import type { RouteDefinition } from '../route-definitions/route-definition'
import type { Project, Update as TurbopackUpdate } from '../../build/swc'
import type { Project, Update as TurbopackUpdate } from '../../build/swc/types'
import type { VersionInfo } from './parse-version-info'
import type { DebugInfo } from '../../client/components/react-dev-overlay/types'

Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/dev/turbopack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import type {
SetupOpts,
} from '../lib/router-utils/setup-dev-bundler'
import type {
Endpoint,
Entrypoints as RawEntrypoints,
Issue,
StyledString,
TurbopackResult,
Endpoint,
Entrypoints as RawEntrypoints,
Update as TurbopackUpdate,
WrittenEndpoint,
} from '../../build/swc'
} from '../../build/swc/types'
import {
decodeMagicIdentifier,
MAGIC_IDENTIFIER_REGEX,
Expand Down
Loading

0 comments on commit 7a9fae7

Please sign in to comment.