Skip to content

Commit d12cf1b

Browse files
authored
Merge pull request #33003 from storybookjs/yann/nextjs-wasm-changes
Next.js: Update SWC loader to support new wasm detection
2 parents 07e8036 + c5582fa commit d12cf1b

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

code/frameworks/nextjs/src/swc/next-swc-loader-patch.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE.
2929
import { isAbsolute, relative } from 'node:path';
3030

3131
import type { NextConfig } from 'next';
32-
import { isWasm, transform } from 'next/dist/build/swc/index.js';
32+
import * as nextSwcUtils from 'next/dist/build/swc/index.js';
3333
import { getLoaderSWCOptions } from 'next/dist/build/swc/options.js';
3434

3535
export interface SWCLoaderOptions {
@@ -136,7 +136,7 @@ async function loaderTransform(this: any, parentTrace: any, source?: string, inp
136136

137137
const swcSpan = parentTrace.traceChild('next-swc-transform');
138138
return swcSpan.traceAsyncFn(() =>
139-
transform(source as any, programmaticOptions).then((output) => {
139+
nextSwcUtils.transform(source as any, programmaticOptions).then((output) => {
140140
if (output.eliminatedPackages && this.eliminatedPackages) {
141141
for (const pkg of JSON.parse(output.eliminatedPackages)) {
142142
this.eliminatedPackages.add(pkg);
@@ -152,14 +152,25 @@ const EXCLUDED_PATHS = /[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual_
152152
export function pitch(this: any) {
153153
const callback = this.async();
154154
(async () => {
155+
let isWasm: boolean = false;
156+
157+
if (!!nextSwcUtils.isWasm) {
158+
isWasm = await nextSwcUtils.isWasm();
159+
// @ts-expect-error Relevant from Next.js >= 16.0.2-canary.12
160+
} else if (!!nextSwcUtils.getBindingsSync) {
161+
await nextSwcUtils.loadBindings();
162+
// @ts-expect-error Relevant from Next.js >= 16.0.2-canary.12
163+
isWasm = nextSwcUtils.getBindingsSync().isWasm;
164+
}
165+
155166
if (
156167
// TODO: Evaluate if this is correct after removing pnp compatibility code in SB11
157168
// TODO: investigate swc file reading in PnP mode?
158169
!process.versions.pnp &&
159170
!EXCLUDED_PATHS.test(this.resourcePath) &&
160171
this.loaders.length - 1 === this.loaderIndex &&
161172
isAbsolute(this.resourcePath) &&
162-
!(await isWasm())
173+
!isWasm
163174
) {
164175
const loaderSpan = mockCurrentTraceSpan.traceChild('next-swc-loader');
165176
this.addDependency(this.resourcePath);

0 commit comments

Comments
 (0)