Skip to content

Commit

Permalink
automatically copy over generated types when building the wasm/native…
Browse files Browse the repository at this point in the history
… binary
  • Loading branch information
ForsakenHarmony committed Sep 19, 2024
1 parent d9b0f83 commit 2003356
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"update-google-fonts": "node ./scripts/update-google-fonts.js",
"patch-next": "node scripts/patch-next.cjs",
"unpack-next": "node scripts/unpack-next.cjs",
"swc-build-native": "pnpm --filter=@next/swc build-native",
"swc-build-native": "node scripts/build-native.cjs",
"swc-build-wasm": "node scripts/build-wasm.cjs",
"sweep": "node scripts/sweep.cjs"
},
"devDependencies": {
Expand Down
10 changes: 8 additions & 2 deletions packages/next/src/build/swc/generated-native.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This was manually added
// Manual additions to make the generated types below work.

import type { TurbopackResult } from './types'

Expand All @@ -17,7 +17,9 @@ export function lightningCssTransformStyleAttribute(
args: object
): Promise<unknown>

// Below are the generated types from napi
// GENERATED-TYPES-BELOW
// DO NOT MANUALLY EDIT THESE TYPES
// You can regenerate this file by running `pnpm swc-build-native` in the root of the repo.

/* tslint:disable */
/* eslint-disable */
Expand Down Expand Up @@ -114,6 +116,8 @@ export interface NapiProjectOptions {
buildId: string
/** Options for draft mode. */
previewProps: NapiDraftModeOptions
/** The browserslist query to use for targeting browsers. */
browserslistQuery: string
}
/** [NapiProjectOptions] with all fields optional. */
export interface NapiPartialProjectOptions {
Expand Down Expand Up @@ -150,6 +154,8 @@ export interface NapiPartialProjectOptions {
buildId?: string
/** Options for draft mode. */
previewProps?: NapiDraftModeOptions
/** The browserslist query to use for targeting browsers. */
browserslistQuery?: string
}
export interface NapiDefineEnv {
client: Array<NapiEnvVar>
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/build/swc/generated-wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// DO NOT MANUALLY EDIT THESE TYPES
// You can regenerate this file by running `pnpm swc-build-wasm` in the root of the repo.

/* tslint:disable */
/* eslint-disable */
/**
Expand Down
44 changes: 39 additions & 5 deletions scripts/build-native.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const {
NEXT_DIR,
booleanArg,
execAsyncWithOutput,
execFn,
namedValueArg,
} = require('./pack-util.cjs')
const { rmSync } = require('fs')
const fs = require('fs')
const path = require('path')

const args = process.argv.slice(2)
Expand All @@ -16,14 +17,16 @@ booleanArg(args, '--no-build')
namedValueArg(args, '--project')

const targetDir = path.join(NEXT_DIR, 'target')
const nextSwcDir = path.join(NEXT_DIR, 'packages/next-swc')

module.exports = (async () => {
for (let i = 0; i < 2; i++) {
try {
await execAsyncWithOutput(
'Build native modules',
['pnpm', 'run', 'swc-build-native', ...args],
'Build native bindings',
['pnpm', 'run', 'build-native', ...args],
{
cwd: nextSwcDir,
shell: process.platform === 'win32' ? 'powershell.exe' : false,
env: {
CARGO_TERM_COLOR: 'always',
Expand All @@ -38,11 +41,11 @@ module.exports = (async () => {
.toString()
.includes('the compiler unexpectedly panicked. this is a bug.')
) {
rmSync(path.join(targetDir, 'release/incremental'), {
fs.rmSync(path.join(targetDir, 'release/incremental'), {
recursive: true,
force: true,
})
rmSync(path.join(targetDir, 'debug/incremental'), {
fs.rmSync(path.join(targetDir, 'debug/incremental'), {
recursive: true,
force: true,
})
Expand All @@ -54,4 +57,35 @@ module.exports = (async () => {
}
break
}

execFn(
'Copy generated types to `next/src/build/swc/generated-native.d.ts`',
() => writeTypes()
)
})()

function writeTypes() {
const generatedTypesPath = path.join(
NEXT_DIR,
'packages/next-swc/native/index.d.ts'
)
const vendoredTypesPath = path.join(
NEXT_DIR,
'packages/next/src/build/swc/generated-native.d.ts'
)
const generatedTypesMarker = '// GENERATED-TYPES-BELOW\n'
const generatedNotice =
'// DO NOT MANUALLY EDIT THESE TYPES\n// You can regenerate this file by running `pnpm swc-build-native` in the root of the repo.\n\n'

const generatedTypes = fs.readFileSync(generatedTypesPath, 'utf8')
let vendoredTypes = fs.readFileSync(vendoredTypesPath, 'utf8')

vendoredTypes = vendoredTypes.split(generatedTypesMarker)[0]
vendoredTypes =
vendoredTypes +
generatedTypesMarker +
generatedNotice +
generatedTypes

fs.writeFileSync(vendoredTypesPath, vendoredTypes)
}
82 changes: 82 additions & 0 deletions scripts/build-wasm.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env node

const {
NEXT_DIR,
booleanArg,
execAsyncWithOutput,
execFn,
namedValueArg,
} = require('./pack-util.cjs')
const fs = require('fs')
const path = require('path')

const args = process.argv.slice(2)

// strip --no-build and --project when called from pack-next.cjs
booleanArg(args, '--no-build')
namedValueArg(args, '--project')

const targetDir = path.join(NEXT_DIR, 'target')
const nextSwcDir = path.join(NEXT_DIR, 'packages/next-swc')

module.exports = (async () => {
for (let i = 0; i < 2; i++) {
try {
await execAsyncWithOutput(
'Build wasm bindings',
['pnpm', 'run', 'build-wasm', ...args],
{
cwd: nextSwcDir,
shell: process.platform === 'win32' ? 'powershell.exe' : false,
env: {
CARGO_TERM_COLOR: 'always',
TTY: '1',
...process.env,
},
}
)
} catch (e) {
if (
e.stderr
.toString()
.includes('the compiler unexpectedly panicked. this is a bug.')
) {
fs.rmSync(path.join(targetDir, 'release/incremental'), {
recursive: true,
force: true,
})
fs.rmSync(path.join(targetDir, 'debug/incremental'), {
recursive: true,
force: true,
})
continue
}
delete e.stdout
delete e.stderr
throw e
}
break
}

execFn(
'Copy generated types to `next/src/build/swc/generated-wasm.d.ts`',
() => writeTypes()
)
})()

function writeTypes() {
const generatedTypesPath = path.join(NEXT_DIR, 'crates/wasm/pkg/wasm.d.ts')
const vendoredTypesPath = path.join(
NEXT_DIR,
'packages/next/src/build/swc/generated-wasm.d.ts'
)

const generatedNotice =
'// DO NOT MANUALLY EDIT THESE TYPES\n// You can regenerate this file by running `pnpm swc-build-wasm` in the root of the repo.\n\n'

const generatedTypes = fs.readFileSync(generatedTypesPath, 'utf8')

const vendoredTypes = generatedNotice + generatedTypes

fs.writeFileSync(vendoredTypesPath, vendoredTypes)
}
13 changes: 13 additions & 0 deletions scripts/pack-util.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function execAsyncWithOutput(title, command, opts) {

exports.execAsyncWithOutput = execAsyncWithOutput

/**
* @template T
* @param {string} title
* @param {() => T} fn
* @returns {T}
*/
function execFn(title, fn) {
logCommand(title, fn.toString())
return fn()
}

exports.execFn = execFn

/**
* @param {string | string[]} command
*/
Expand Down
7 changes: 1 addition & 6 deletions scripts/patch-next.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const {
NEXT_DIR,
exec,
logCommand,
execFn,
booleanArg,
packageFiles,
} = require('./pack-util.cjs')
Expand All @@ -21,11 +21,6 @@ const noNativeBuild = booleanArg(args, '--no-native-build')

const PROJECT_DIR = path.resolve(args[0])

async function execFn(title, fn) {
logCommand(title, fn.toString())
return fn()
}

function realPathIfAny(path) {
try {
return fs.realpathSync(path)
Expand Down

0 comments on commit 2003356

Please sign in to comment.