Skip to content

Commit

Permalink
Merge pull request #27093 from seanparmelee/raw-conflicts
Browse files Browse the repository at this point in the history
Next.js: Avoid conflicts with the raw loader

(cherry picked from commit f5fc402)
  • Loading branch information
valentinpalkovic authored and storybook-bot committed May 14, 2024
1 parent c52a272 commit 9864d79
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions code/frameworks/nextjs/src/swc/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) => {
Expand All @@ -18,33 +19,38 @@ export const configureSWCLoader = async (

const { jsConfig } = await loadJsConfig(dir, nextConfig as any);

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',
},
const rawRule = baseConfig.module?.rules?.find(
(rule) => typeof rule === 'object' && rule?.resourceQuery?.toString() === '/raw/'
);

if (rawRule && typeof rawRule === 'object') {
rawRule.test = /^(?!__barrel_optimize__)/;
}

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',
},
},
];
});
};

0 comments on commit 9864d79

Please sign in to comment.