-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathloader.ts
58 lines (48 loc) · 1.78 KB
/
loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { join } from 'node:path';
import { getProjectRoot } from 'storybook/internal/common';
import type { Options } from 'storybook/internal/types';
import { getVirtualModules } from '@storybook/builder-webpack5';
import type { NextConfig } from 'next';
import loadJsConfig from 'next/dist/build/load-jsconfig';
import type { Configuration as WebpackConfig } from 'webpack';
export const configureSWCLoader = async (
baseConfig: WebpackConfig,
options: Options,
nextConfig: NextConfig
) => {
const isDevelopment = options.configType !== 'PRODUCTION';
const dir = getProjectRoot();
const { virtualModules } = await getVirtualModules(options);
const { jsConfig } = await loadJsConfig(dir, nextConfig as any);
const rawRule = baseConfig.module?.rules?.find(
(rule) => typeof rule === 'object' && rule?.resourceQuery?.toString() === '/raw/'
);
if (rawRule && typeof rawRule === 'object') {
rawRule.exclude = /^__barrel_optimize__/;
}
baseConfig.module?.rules?.push({
test: /\.((c|m)?(j|t)sx?)$/,
include: [getProjectRoot()],
exclude: [/(node_modules)/, ...Object.keys(virtualModules)],
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: join(dir, nextConfig?.distDir ?? '.next', 'cache', 'swc'),
bundleTarget: 'default',
},
},
});
};