Skip to content

Commit

Permalink
Eagerly load swc bindings for wasm fallback for jest (#36784)
Browse files Browse the repository at this point in the history
Follow-up to #36612 this updates to eagerly load the swc bindings unless babel is being used so that we don't wait for the transform calls to initialize swc. Eagerly loading in jest also allows us to fallback to the wasm bindings when previously we couldn't since they needed to wait for the import.
  • Loading branch information
ijjk committed May 12, 2022
1 parent 72f5c93 commit 8e401ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/next/build/jest/jest.ts
Expand Up @@ -5,6 +5,7 @@ import { PHASE_TEST } from '../../shared/lib/constants'
import loadJsConfig from '../load-jsconfig'
import * as Log from '../output/log'
import { findPagesDir } from '../../lib/find-pages-dir'
import { loadBindings, lockfilePatchPromise } from '../swc'

async function getConfig(dir: string) {
const conf = await loadConfig(PHASE_TEST, dir)
Expand Down Expand Up @@ -72,6 +73,13 @@ export default function nextJest(options: { dir?: string } = {}) {
? await customJestConfig()
: customJestConfig) ?? {}

// eagerly load swc bindings instead of waiting for transform calls
await loadBindings()

if (lockfilePatchPromise.cur) {
await lockfilePatchPromise.cur
}

return {
...resolvedJestConfig,

Expand Down
1 change: 1 addition & 0 deletions packages/next/build/swc/index.d.ts
Expand Up @@ -8,3 +8,4 @@ export function parse(src: string, options: any): any
export const lockfilePatchPromise: { cur?: Promise<void> }
export function initCustomTraceSubscriber(traceFileName?: string): void
export function teardownTraceSubscriber(): void
export function loadBindings(): Promise<void>
26 changes: 20 additions & 6 deletions packages/next/build/swc/index.js
Expand Up @@ -20,7 +20,7 @@ let pendingBindings
let swcTraceFlushGuard
export const lockfilePatchPromise = {}

async function loadBindings() {
export async function loadBindings() {
if (pendingBindings) {
return pendingBindings
}
Expand Down Expand Up @@ -87,6 +87,12 @@ function loadBindingsSync() {
attempts = attempts.concat(a)
}

// we can leverage the wasm bindings if they are already
// loaded
if (wasmBindings) {
return wasmBindings
}

logLoadFailure(attempts)
}

Expand Down Expand Up @@ -133,16 +139,24 @@ async function loadWasm(importPath = '') {
wasmBindings = {
isWasm: true,
transform(src, options) {
return Promise.resolve(
bindings.transformSync(src.toString(), options)
)
return bindings.transformSync(src.toString(), options)
},
transformSync(src, options) {
return bindings.transformSync(src.toString(), options)
},
minify(src, options) {
return Promise.resolve(bindings.minifySync(src.toString(), options))
return bindings.minifySync(src.toString(), options)
},
minifySync(src, options) {
return bindings.minifySync(src.toString(), options)
},
parse(src, options) {
const astStr = bindings.parseSync(src.toString(), options)
return Promise.resolve(astStr)
return astStr
},
parseSync(src, options) {
const astStr = bindings.parseSync(src.toString(), options)
return astStr
},
getTargetTriple() {
return undefined
Expand Down
6 changes: 6 additions & 0 deletions packages/next/build/webpack-config.ts
Expand Up @@ -54,6 +54,7 @@ import { withoutRSCExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist'
import loadJsConfig from './load-jsconfig'
import { getMiddlewareSourceMapPlugins } from './webpack/plugins/middleware-source-maps-plugin'
import { loadBindings } from './swc'

const watchOptions = Object.freeze({
aggregateTimeout: 5,
Expand Down Expand Up @@ -432,6 +433,11 @@ export default async function getBaseWebpackConfig(
loggedSwcDisabled = true
}

// eagerly load swc bindings instead of waiting for transform calls
if (!babelConfigFile && isClient) {
await loadBindings()
}

if (!loggedIgnoredCompilerOptions && !useSWCLoader && config.compiler) {
Log.info(
'`compiler` options in `next.config.js` will be ignored while using Babel https://nextjs.org/docs/messages/ignored-compiler-options'
Expand Down

0 comments on commit 8e401ae

Please sign in to comment.