-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Next.js: Add experimental SWC support
- Loading branch information
1 parent
1ecc591
commit 554ad41
Showing
14 changed files
with
474 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { getProjectRoot } from '@storybook/core-common'; | ||
import { getVirtualModuleMapping } from '@storybook/core-webpack'; | ||
import type { Options } from '@storybook/types'; | ||
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'; | ||
import type { NextConfig } from 'next'; | ||
import { getSupportedBrowsers } from 'next/dist/build/utils'; | ||
import path from 'path'; | ||
import type { RuleSetRule } from 'webpack'; | ||
|
||
export const configureSWCLoader = async ( | ||
baseConfig: any, | ||
options: Options, | ||
nextConfig: NextConfig | ||
) => { | ||
const isDevelopment = options.configType !== 'PRODUCTION'; | ||
|
||
const dir = getProjectRoot(); | ||
|
||
baseConfig.plugins = [ | ||
...baseConfig.plugins, | ||
new ReactRefreshWebpackPlugin({ | ||
overlay: { | ||
sockIntegration: 'whm', | ||
}, | ||
}), | ||
]; | ||
|
||
const virtualModules = await getVirtualModuleMapping(options); | ||
|
||
baseConfig.module.rules = [ | ||
// TODO: Remove filtering in Storybook 8.0 | ||
...baseConfig.module.rules.filter( | ||
(r: RuleSetRule) => | ||
!(typeof r.use === 'object' && 'loader' in r.use && r.use.loader?.includes('swc-loader')) | ||
), | ||
{ | ||
test: /\.(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, | ||
hasServerComponents: true, | ||
nextConfig, | ||
supportedBrowsers: getSupportedBrowsers(dir, isDevelopment), | ||
swcCacheDir: path.join(dir, nextConfig?.distDir ?? '.next', 'cache', 'swc'), | ||
isServerLayer: false, | ||
bundleTarget: 'default', | ||
}, | ||
}, | ||
}, | ||
]; | ||
}; |
Oops, something went wrong.