Skip to content

Commit

Permalink
add typings to bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 11, 2023
1 parent 1d775ab commit e22ac8f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ export default async function build(
ignore: [] as string[],
}))

let binding = (await loadBindings()) as any
let binding = await loadBindings()

async function turbopackBuild() {
const turboNextBuildStart = process.hrtime()
Expand Down
66 changes: 55 additions & 11 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure'
import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadWasmSwc } from '../../lib/download-wasm-swc'
import { spawn } from 'child_process'
import { string } from 'zod'

const nextVersion = process.env.__NEXT_VERSION as string

Expand Down Expand Up @@ -81,7 +80,38 @@ let swcHeapProfilerFlushGuard: any
let swcCrashReporterFlushGuard: any
export const lockfilePatchPromise: { cur?: Promise<void> } = {}

export async function loadBindings(): Promise<any> {
export interface Binding {
isWasm: boolean
turbo: {
startDev: any
startTrace: any
nextBuild?: any
createTurboTasks?: any
entrypoints: {
stream: any
get: any
}
mdx: {
compile: any
compileSync: any
}
createProject: (options: ProjectOptions) => Promise<Project>
}
minify: any
minifySync: any
transform: any
transformSync: any
parse: any
parseSync: any
getTargetTriple(): string | undefined
initCustomTraceSubscriber?: any
teardownTraceSubscriber?: any
initHeapProfiler?: any
teardownHeapProfiler?: any
teardownCrashReporter?: any
}

export async function loadBindings(): Promise<Binding> {
if (pendingBindings) {
return pendingBindings
}
Expand Down Expand Up @@ -219,6 +249,29 @@ function logLoadFailure(attempts: any, triedWasm = false) {
})
}

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

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

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

interface RoutesOptions {
/** File extensions to scan inside our project */
pageExtensions: string[]
Expand Down Expand Up @@ -549,9 +602,6 @@ async function loadWasm(importPath = '', isCustomTurbopack: boolean) {
startTrace: () => {
Log.error('Wasm binding does not support trace yet')
},
experimentalTurbo: () => {
Log.error('Wasm binding does not support this interface')
},
entrypoints: {
stream: (
turboTasks: any,
Expand Down Expand Up @@ -811,12 +861,6 @@ function loadNative(isCustomTurbopack = false) {
)
return ret
},
experimentalTurbo: () => {
initHeapProfiler()

const ret = bindings.experimentalTurbo()
return ret
},
createTurboTasks: (memoryLimit?: number): unknown =>
bindings.createTurboTasks(memoryLimit),
entrypoints: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {

// startTrace existed and callable
if (this.turbotrace) {
let binding = (await loadBindings()) as any
let binding = await loadBindings()
if (
!binding?.isWasm &&
typeof binding.turbo.startTrace === 'function'
Expand Down Expand Up @@ -475,7 +475,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
})
// startTrace existed and callable
if (this.turbotrace) {
let binding = (await loadBindings()) as any
let binding = await loadBindings()
if (
!binding?.isWasm &&
typeof binding.turbo.startTrace === 'function'
Expand Down Expand Up @@ -825,7 +825,7 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {

if (this.turbotrace) {
compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async () => {
let binding = (await loadBindings()) as any
let binding = await loadBindings()
if (
!binding?.isWasm &&
typeof binding.turbo.startTrace === 'function'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ const nextDev: CliCommand = async (argv) => {
// Turbopack need to be in control over reading the .env files and watching them.
// So we need to start with a initial env to know which env vars are coming from the user.
resetEnv()
let bindings: any = await loadBindings()
let bindings = await loadBindings()

const project = await bindings.turbo.createProject({
projectPath: '/workspaces/nextpack/test-app',
Expand Down

0 comments on commit e22ac8f

Please sign in to comment.