From 3b650615dfd1a8c9b1853681c3414a5368577575 Mon Sep 17 00:00:00 2001 From: Sean Parmelee Date: Fri, 10 May 2024 16:47:26 -0500 Subject: [PATCH 1/2] Next.js: avoid conflicts with the raw loader --- code/frameworks/nextjs/src/swc/loader.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/code/frameworks/nextjs/src/swc/loader.ts b/code/frameworks/nextjs/src/swc/loader.ts index d1b5c6e7ab98..7cf2d05a65eb 100644 --- a/code/frameworks/nextjs/src/swc/loader.ts +++ b/code/frameworks/nextjs/src/swc/loader.ts @@ -18,6 +18,14 @@ export const configureSWCLoader = async ( const { jsConfig } = await loadJsConfig(dir, nextConfig as any); + const rawRule = baseConfig.module?.rules?.find( + (rule) => typeof rule === 'object' && rule?.resourceQuery?.toString() === '/raw/' + ); + + if (rawRule) { + rawRule.test = /^(?!__barrel_optimize__)/; + } + baseConfig.module.rules = [ ...baseConfig.module.rules, { From a36c95a4d4a93b6b201400193908d1909d1ddd02 Mon Sep 17 00:00:00 2001 From: Sean Parmelee Date: Mon, 13 May 2024 14:08:53 -0500 Subject: [PATCH 2/2] address TS errors --- code/frameworks/nextjs/src/swc/loader.ts | 56 ++++++++++++------------ 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/code/frameworks/nextjs/src/swc/loader.ts b/code/frameworks/nextjs/src/swc/loader.ts index 7cf2d05a65eb..c9ca896d533b 100644 --- a/code/frameworks/nextjs/src/swc/loader.ts +++ b/code/frameworks/nextjs/src/swc/loader.ts @@ -4,9 +4,10 @@ import type { Options } from '@storybook/types'; import type { NextConfig } from 'next'; import path from 'path'; import loadJsConfig from 'next/dist/build/load-jsconfig'; +import type { Configuration as WebpackConfig } from 'webpack'; export const configureSWCLoader = async ( - baseConfig: any, + baseConfig: WebpackConfig, options: Options, nextConfig: NextConfig ) => { @@ -22,37 +23,34 @@ export const configureSWCLoader = async ( (rule) => typeof rule === 'object' && rule?.resourceQuery?.toString() === '/raw/' ); - if (rawRule) { + if (rawRule && typeof rawRule === 'object') { rawRule.test = /^(?!__barrel_optimize__)/; } - baseConfig.module.rules = [ - ...baseConfig.module.rules, - { - test: /\.((c|m)?(j|t)sx?)$/, - include: [getProjectRoot()], - exclude: [/(node_modules)/, ...Object.keys(virtualModules)], - enforce: 'post', - use: { - // we use our own patch because we need to remove tracing from the original code - // which is not possible otherwise - loader: require.resolve('./swc/next-swc-loader-patch.js'), - options: { - isServer: false, - rootDir: dir, - pagesDir: `${dir}/pages`, - appDir: `${dir}/apps`, - hasReactRefresh: isDevelopment, - jsConfig, - nextConfig, - supportedBrowsers: require('next/dist/build/utils').getSupportedBrowsers( - dir, - isDevelopment - ), - swcCacheDir: path.join(dir, nextConfig?.distDir ?? '.next', 'cache', 'swc'), - bundleTarget: 'default', - }, + baseConfig.module?.rules?.push({ + test: /\.((c|m)?(j|t)sx?)$/, + include: [getProjectRoot()], + exclude: [/(node_modules)/, ...Object.keys(virtualModules)], + enforce: 'post', + use: { + // we use our own patch because we need to remove tracing from the original code + // which is not possible otherwise + loader: require.resolve('./swc/next-swc-loader-patch.js'), + options: { + isServer: false, + rootDir: dir, + pagesDir: `${dir}/pages`, + appDir: `${dir}/apps`, + hasReactRefresh: isDevelopment, + jsConfig, + nextConfig, + supportedBrowsers: require('next/dist/build/utils').getSupportedBrowsers( + dir, + isDevelopment + ), + swcCacheDir: path.join(dir, nextConfig?.distDir ?? '.next', 'cache', 'swc'), + bundleTarget: 'default', }, }, - ]; + }); };