Skip to content

Commit a0d1b3e

Browse files
Merge pull request storybookjs#31501 from storybookjs/valentin/fix-nextjs-module-transpilation
Next.js: Fix module transpilation
2 parents 96e1c1b + 599144c commit a0d1b3e

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

code/frameworks/nextjs/src/babel/loader.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import type { Options } from 'storybook/internal/types';
33

44
import { getVirtualModules } from '@storybook/builder-webpack5';
55

6-
export const configureBabelLoader = async (baseConfig: any, options: Options) => {
6+
import type { NextConfig } from 'next';
7+
8+
import { getNodeModulesExcludeRegex } from '../utils';
9+
10+
export const configureBabelLoader = async (
11+
baseConfig: any,
12+
options: Options,
13+
nextConfig: NextConfig
14+
) => {
715
const { virtualModules } = await getVirtualModules(options);
816

917
const babelOptions = await options.presets.apply('babel', {}, options);
@@ -23,7 +31,10 @@ export const configureBabelLoader = async (baseConfig: any, options: Options) =>
2331
},
2432
],
2533
include: [getProjectRoot()],
26-
exclude: [/node_modules/, ...Object.keys(virtualModules)],
34+
exclude: [
35+
getNodeModulesExcludeRegex(nextConfig.transpilePackages ?? []),
36+
...Object.keys(virtualModules),
37+
],
2738
},
2839
];
2940
};

code/frameworks/nextjs/src/preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (baseConfig,
190190
await configureSWCLoader(baseConfig, options, nextConfig);
191191
} else {
192192
logger.info('=> Using Babel as compiler');
193-
await configureBabelLoader(baseConfig, options);
193+
await configureBabelLoader(baseConfig, options, nextConfig);
194194
}
195195

196196
return baseConfig;

code/frameworks/nextjs/src/swc/loader.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type { NextConfig } from 'next';
99
import loadJsConfig from 'next/dist/build/load-jsconfig';
1010
import type { Configuration as WebpackConfig } from 'webpack';
1111

12+
import { getNodeModulesExcludeRegex } from '../utils';
13+
1214
export const configureSWCLoader = async (
1315
baseConfig: WebpackConfig,
1416
options: Options,
@@ -30,10 +32,12 @@ export const configureSWCLoader = async (
3032
rawRule.exclude = /^__barrel_optimize__/;
3133
}
3234

35+
const transpilePackages = nextConfig.transpilePackages ?? [];
36+
3337
baseConfig.module?.rules?.push({
3438
test: /\.((c|m)?(j|t)sx?)$/,
3539
include: [getProjectRoot()],
36-
exclude: [/(node_modules)/, ...Object.keys(virtualModules)],
40+
exclude: [getNodeModulesExcludeRegex(transpilePackages), ...Object.keys(virtualModules)],
3741
use: {
3842
// we use our own patch because we need to remove tracing from the original code
3943
// which is not possible otherwise

code/frameworks/nextjs/src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,20 @@ export const scopedResolve = (id: string): string => {
9696
const beginningOfMainScriptPath = moduleFolderStrPosition + id.length;
9797
return scopedModulePath.substring(0, beginningOfMainScriptPath);
9898
};
99+
100+
/**
101+
* Returns a RegExp that matches node_modules except for the given transpilePackages.
102+
*
103+
* @param transpilePackages Array of package names to NOT exclude (i.e., to include for
104+
* transpilation)
105+
* @returns RegExp for use in Webpack's exclude
106+
*/
107+
export function getNodeModulesExcludeRegex(transpilePackages: string[]): RegExp {
108+
if (!transpilePackages || transpilePackages.length === 0) {
109+
return /node_modules/;
110+
}
111+
const escaped = transpilePackages
112+
.map((pkg) => pkg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
113+
.join('|');
114+
return new RegExp(`node_modules/(?!(${escaped})/)`);
115+
}

0 commit comments

Comments
 (0)