Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerly load swc bindings for wasm fallback for jest #36784

Merged
merged 2 commits into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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