Unverified Commit 8f7d24fa authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure next-swc-loader types are used (#55846)

Uses satisfies to check the next-swc-loader options passed. Found a few
mismatches and implicit default, made sure they match up the same
default values but set explicitly.

This will need a thorough double-check. 

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 5f4238d9
...@@ -5,7 +5,7 @@ import type { ...@@ -5,7 +5,7 @@ import type {
StyledComponentsConfig, StyledComponentsConfig,
} from '../../server/config-shared' } from '../../server/config-shared'
type BundleType = 'client' | 'server' | 'default' export type BundleType = 'client' | 'server' | 'default'
const nextDistPath = const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/ /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
...@@ -54,15 +54,15 @@ function getBaseSWCOptions({ ...@@ -54,15 +54,15 @@ function getBaseSWCOptions({
hasReactRefresh: boolean hasReactRefresh: boolean
globalWindow: boolean globalWindow: boolean
modularizeImports?: NextConfig['modularizeImports'] modularizeImports?: NextConfig['modularizeImports']
swcPlugins: ExperimentalConfig['swcPlugins']
compilerOptions: NextConfig['compiler'] compilerOptions: NextConfig['compiler']
swcPlugins: ExperimentalConfig['swcPlugins']
isServerActionsEnabled?: ExperimentalConfig['serverActions']
resolvedBaseUrl?: string resolvedBaseUrl?: string
jsConfig: any jsConfig: any
bundleTarget: BundleType bundleTarget: BundleType
swcCacheDir?: string swcCacheDir?: string
isServerLayer?: boolean isServerLayer?: boolean
hasServerComponents?: boolean hasServerComponents?: boolean
isServerActionsEnabled?: boolean
}) { }) {
const parserConfig = getParserOptions({ filename, jsConfig }) const parserConfig = getParserOptions({ filename, jsConfig })
const paths = jsConfig?.compilerOptions?.paths const paths = jsConfig?.compilerOptions?.paths
...@@ -322,7 +322,7 @@ export function getLoaderSWCOptions({ ...@@ -322,7 +322,7 @@ export function getLoaderSWCOptions({
isServerLayer, isServerLayer,
isServerActionsEnabled, isServerActionsEnabled,
optimizeBarrelExports, optimizeBarrelExports,
bundleTarget = 'client', bundleTarget,
}: // This is not passed yet as "paths" resolving is handled by webpack currently. }: // This is not passed yet as "paths" resolving is handled by webpack currently.
// resolvedBaseUrl, // resolvedBaseUrl,
{ {
...@@ -330,7 +330,7 @@ export function getLoaderSWCOptions({ ...@@ -330,7 +330,7 @@ export function getLoaderSWCOptions({
development: boolean development: boolean
isServer: boolean isServer: boolean
pagesDir?: string pagesDir?: string
appDir: string appDir?: string
isPageFile: boolean isPageFile: boolean
hasReactRefresh: boolean hasReactRefresh: boolean
optimizeServerReact?: boolean optimizeServerReact?: boolean
...@@ -341,14 +341,16 @@ export function getLoaderSWCOptions({ ...@@ -341,14 +341,16 @@ export function getLoaderSWCOptions({
swcPlugins: ExperimentalConfig['swcPlugins'] swcPlugins: ExperimentalConfig['swcPlugins']
compilerOptions: NextConfig['compiler'] compilerOptions: NextConfig['compiler']
jsConfig: any jsConfig: any
supportedBrowsers: string[] supportedBrowsers: string[] | undefined
swcCacheDir: string swcCacheDir: string
relativeFilePathFromRoot: string relativeFilePathFromRoot: string
bundleTarget: BundleType bundleTarget: BundleType
hasServerComponents?: boolean hasServerComponents?: boolean
isServerLayer: boolean isServerLayer: boolean
isServerActionsEnabled?: boolean isServerActionsEnabled?: boolean
optimizeBarrelExports?: string[] optimizeBarrelExports?: {
wildcard: boolean
}
}) { }) {
let baseOptions: any = getBaseSWCOptions({ let baseOptions: any = getBaseSWCOptions({
filename, filename,
......
...@@ -77,6 +77,7 @@ import { getBabelConfigFile } from './get-babel-config-file' ...@@ -77,6 +77,7 @@ import { getBabelConfigFile } from './get-babel-config-file'
import { defaultOverrides } from '../server/require-hook' import { defaultOverrides } from '../server/require-hook'
import { needsExperimentalReact } from '../lib/needs-experimental-react' import { needsExperimentalReact } from '../lib/needs-experimental-react'
import { getDefineEnvPlugin } from './webpack/plugins/define-env-plugin' import { getDefineEnvPlugin } from './webpack/plugins/define-env-plugin'
import { SWCLoaderOptions } from './webpack/loaders/next-swc-loader'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
type ClientEntries = { type ClientEntries = {
...@@ -680,7 +681,12 @@ export default async function getBaseWebpackConfig( ...@@ -680,7 +681,12 @@ export default async function getBaseWebpackConfig(
} }
let swcTraceProfilingInitialized = false let swcTraceProfilingInitialized = false
const getSwcLoader = (extraOptions?: any) => { const getSwcLoader = (
extraOptions: Partial<SWCLoaderOptions> & {
bundleTarget: SWCLoaderOptions['bundleTarget']
isServerLayer: SWCLoaderOptions['isServerLayer']
}
) => {
if ( if (
config?.experimental?.swcTraceProfiling && config?.experimental?.swcTraceProfiling &&
!swcTraceProfilingInitialized !swcTraceProfilingInitialized
...@@ -709,12 +715,14 @@ export default async function getBaseWebpackConfig( ...@@ -709,12 +715,14 @@ export default async function getBaseWebpackConfig(
supportedBrowsers, supportedBrowsers,
swcCacheDir: path.join(dir, config?.distDir ?? '.next', 'cache', 'swc'), swcCacheDir: path.join(dir, config?.distDir ?? '.next', 'cache', 'swc'),
...extraOptions, ...extraOptions,
}, } satisfies SWCLoaderOptions,
} }
} }
const defaultLoaders = { const defaultLoaders = {
babel: useSWCLoader ? getSwcLoader() : getBabelLoader(), babel: useSWCLoader
? getSwcLoader({ bundleTarget: 'client', isServerLayer: false })
: getBabelLoader(),
} }
const swcLoaderForServerLayer = hasServerComponents const swcLoaderForServerLayer = hasServerComponents
...@@ -731,13 +739,21 @@ export default async function getBaseWebpackConfig( ...@@ -731,13 +739,21 @@ export default async function getBaseWebpackConfig(
: [] : []
const swcLoaderForMiddlewareLayer = useSWCLoader const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({ hasServerComponents: false, bundleTarget: 'default' }) ? getSwcLoader({
isServerLayer: false,
hasServerComponents: false,
bundleTarget: 'default',
})
: // When using Babel, we will have to use SWC to do the optimization : // When using Babel, we will have to use SWC to do the optimization
// for middleware to tree shake the unused default optimized imports like "next/server". // for middleware to tree shake the unused default optimized imports like "next/server".
// This will cause some performance overhead but // This will cause some performance overhead but
// acceptable as Babel will not be recommended. // acceptable as Babel will not be recommended.
[ [
getSwcLoader({ hasServerComponents: false, bundleTarget: 'default' }), getSwcLoader({
isServerLayer: false,
hasServerComponents: false,
bundleTarget: 'default',
}),
getBabelLoader(), getBabelLoader(),
] ]
...@@ -761,6 +777,7 @@ export default async function getBaseWebpackConfig( ...@@ -761,6 +777,7 @@ export default async function getBaseWebpackConfig(
getSwcLoader({ getSwcLoader({
hasServerComponents, hasServerComponents,
isServerLayer: false, isServerLayer: false,
bundleTarget: 'client',
}), }),
] ]
: // When using Babel, we will have to add the SWC loader : // When using Babel, we will have to add the SWC loader
...@@ -770,6 +787,7 @@ export default async function getBaseWebpackConfig( ...@@ -770,6 +787,7 @@ export default async function getBaseWebpackConfig(
[ [
getSwcLoader({ getSwcLoader({
isServerLayer: false, isServerLayer: false,
bundleTarget: 'client',
}), }),
getBabelLoader(), getBabelLoader(),
] ]
...@@ -781,14 +799,11 @@ export default async function getBaseWebpackConfig( ...@@ -781,14 +799,11 @@ export default async function getBaseWebpackConfig(
// be performed. // be performed.
const loaderForAPIRoutes = const loaderForAPIRoutes =
hasServerComponents && useSWCLoader hasServerComponents && useSWCLoader
? { ? getSwcLoader({
loader: 'next-swc-loader', isServerLayer: false,
options: { bundleTarget: 'default',
...getSwcLoader().options, hasServerComponents: false,
bundleTarget: 'default', })
hasServerComponents: false,
},
}
: defaultLoaders.babel : defaultLoaders.babel
const pageExtensions = config.pageExtensions const pageExtensions = config.pageExtensions
...@@ -1806,6 +1821,8 @@ export default async function getBaseWebpackConfig( ...@@ -1806,6 +1821,8 @@ export default async function getBaseWebpackConfig(
return [ return [
getSwcLoader({ getSwcLoader({
isServerLayer: false,
bundleTarget: 'client',
hasServerComponents: false, hasServerComponents: false,
optimizeBarrelExports: { optimizeBarrelExports: {
wildcard: isFromWildcardExport, wildcard: isFromWildcardExport,
......
...@@ -26,10 +26,30 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ...@@ -26,10 +26,30 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
*/ */
import type { NextConfig } from '../../../../types'
import { isWasm, transform } from '../../swc' import { isWasm, transform } from '../../swc'
import { getLoaderSWCOptions } from '../../swc/options' import { type BundleType, getLoaderSWCOptions } from '../../swc/options'
import path, { isAbsolute } from 'path' import path, { isAbsolute } from 'path'
export interface SWCLoaderOptions {
rootDir: string
isServer: boolean
pagesDir?: string
appDir?: string
hasReactRefresh: boolean
optimizeServerReact?: boolean
nextConfig: NextConfig
jsConfig: any
supportedBrowsers: string[] | undefined
swcCacheDir: string
bundleTarget: BundleType
hasServerComponents?: boolean
isServerLayer: boolean
optimizeBarrelExports?: {
wildcard: boolean
}
}
async function loaderTransform( async function loaderTransform(
this: any, this: any,
parentTrace: any, parentTrace: any,
...@@ -39,7 +59,7 @@ async function loaderTransform( ...@@ -39,7 +59,7 @@ async function loaderTransform(
// Make the loader async // Make the loader async
const filename = this.resourcePath const filename = this.resourcePath
let loaderOptions = this.getOptions() || {} let loaderOptions: SWCLoaderOptions = this.getOptions() || {}
const { const {
isServer, isServer,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment