Files
next.js/test/unit/parse-page-static-info.test.ts
Luke SandbergandGitHub 13bf3d97bb [turbopack] Make it possible to synchronously access native bindings (#85787)
## Enable synchronous access to native bindings

## What?
This PR refactors the SWC bindings loading mechanism to support synchronous access to bindings after they've been loaded. It introduces new functions `installBindings()` and `getBindingsSync()` to properly manage the bindings lifecycle.

`installBindings` is called early by tools that rely on them.

There were of course  a few complexities:

* webpack loaders need to defensively install bindings to support being run by webpack
* the hacky `loadNativeSync` method used by `transformSync` remains required due to the jest-transformer API (i didn't see a clear initialization location).
* the `experimental.useWasmBinary` configuration is broken by design
   * if the config is `next.config.ts` when we load SWC to transpile it, but that requires the native bindings, so we might load native bindings even when you ask for wasm.  I added a warning message for this case and the workaround is to use the node native transpilation support.
   * this should probably be a command line flag if it is important
* we need to lazily load the `swc/index.ts` module since it transitively loads `react` which reads `NODE_ENV` and various next entrypoints might modify that.

## Why?

This makes it clearer that we are correctly applying the `useWasmBinary` option and can simplify some code paths that now don't need to be `async`.  This will be important in a future PR where i move some parts of issue formatting into rust/wasm.  Having those APIs be async will decrease their usability.
2025-11-08 14:42:42 -08:00

80 lines
2.5 KiB
TypeScript

import { getPagesPageStaticInfo } from 'next/dist/build/analysis/get-page-static-info'
import { installBindings } from 'next/dist/build/swc/install-bindings'
import { PAGE_TYPES } from 'next/dist/lib/page-types'
import { join } from 'path'
const fixtureDir = join(__dirname, 'fixtures')
function createNextConfig() {
return {}
}
describe('parse page static info', () => {
beforeAll(async () => {
await installBindings()
})
it('should parse nodejs runtime correctly', async () => {
const { runtime, getServerSideProps, getStaticProps } =
await getPagesPageStaticInfo({
page: 'nodejs-ssr',
pageFilePath: join(fixtureDir, 'page-runtime/nodejs-ssr.js'),
nextConfig: createNextConfig(),
pageType: PAGE_TYPES.PAGES,
isDev: false,
})
expect(runtime).toBe('nodejs')
expect(getServerSideProps).toBe(true)
expect(getStaticProps).toBe(false)
})
it('should parse static runtime correctly', async () => {
const { runtime, getServerSideProps, getStaticProps } =
await getPagesPageStaticInfo({
page: 'nodejs',
pageFilePath: join(fixtureDir, 'page-runtime/nodejs.js'),
nextConfig: createNextConfig(),
pageType: PAGE_TYPES.PAGES,
isDev: false,
})
expect(runtime).toBe('nodejs')
expect(getServerSideProps).toBe(false)
expect(getStaticProps).toBe(false)
})
it('should parse edge runtime correctly', async () => {
const { runtime } = await getPagesPageStaticInfo({
page: 'edge',
pageFilePath: join(fixtureDir, 'page-runtime/edge.js'),
nextConfig: createNextConfig(),
pageType: PAGE_TYPES.PAGES,
isDev: false,
})
expect(runtime).toBe('experimental-edge')
})
it('should return undefined if no runtime is specified', async () => {
const { runtime } = await getPagesPageStaticInfo({
page: 'static',
pageFilePath: join(fixtureDir, 'page-runtime/static.js'),
nextConfig: createNextConfig(),
pageType: PAGE_TYPES.PAGES,
isDev: false,
})
expect(runtime).toBe(undefined)
})
it('should parse ssr info with variable exported gSSP correctly', async () => {
const { getServerSideProps, getStaticProps } = await getPagesPageStaticInfo(
{
page: 'ssr-variable-gssp',
pageFilePath: join(fixtureDir, 'page-runtime/ssr-variable-gssp.js'),
nextConfig: createNextConfig(),
pageType: PAGE_TYPES.PAGES,
isDev: false,
}
)
expect(getStaticProps).toBe(false)
expect(getServerSideProps).toBe(true)
})
})