Unverified Commit 66b83f45 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

fix(next/swc): set cache dir explicitly (#38175)



This PR amends behavior of swc's cache by setting it explicitly under `distDir` from next.js config.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 23b8b982
...@@ -31,6 +31,7 @@ function getBaseSWCOptions({ ...@@ -31,6 +31,7 @@ function getBaseSWCOptions({
nextConfig, nextConfig,
resolvedBaseUrl, resolvedBaseUrl,
jsConfig, jsConfig,
swcCacheDir,
}) { }) {
const parserConfig = getParserOptions({ filename, jsConfig }) const parserConfig = getParserOptions({ filename, jsConfig })
const paths = jsConfig?.compilerOptions?.paths const paths = jsConfig?.compilerOptions?.paths
...@@ -46,6 +47,7 @@ function getBaseSWCOptions({ ...@@ -46,6 +47,7 @@ function getBaseSWCOptions({
const plugins = (nextConfig?.experimental?.swcPlugins ?? []) const plugins = (nextConfig?.experimental?.swcPlugins ?? [])
.filter(Array.isArray) .filter(Array.isArray)
.map(([name, options]) => [require.resolve(name), options]) .map(([name, options]) => [require.resolve(name), options])
return { return {
jsc: { jsc: {
...(resolvedBaseUrl && paths ...(resolvedBaseUrl && paths
...@@ -59,6 +61,7 @@ function getBaseSWCOptions({ ...@@ -59,6 +61,7 @@ function getBaseSWCOptions({
experimental: { experimental: {
keepImportAssertions: true, keepImportAssertions: true,
plugins, plugins,
cacheRoot: swcCacheDir,
}, },
transform: { transform: {
// Enables https://github.com/swc-project/swc/blob/0359deb4841be743d73db4536d4a22ac797d7f65/crates/swc_ecma_ext_transforms/src/jest.rs // Enables https://github.com/swc-project/swc/blob/0359deb4841be743d73db4536d4a22ac797d7f65/crates/swc_ecma_ext_transforms/src/jest.rs
...@@ -206,6 +209,7 @@ export function getLoaderSWCOptions({ ...@@ -206,6 +209,7 @@ export function getLoaderSWCOptions({
nextConfig, nextConfig,
jsConfig, jsConfig,
supportedBrowsers, supportedBrowsers,
swcCacheDir,
// 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,
}) { }) {
...@@ -217,6 +221,7 @@ export function getLoaderSWCOptions({ ...@@ -217,6 +221,7 @@ export function getLoaderSWCOptions({
nextConfig, nextConfig,
jsConfig, jsConfig,
// resolvedBaseUrl, // resolvedBaseUrl,
swcCacheDir,
}) })
const isNextDist = nextDistPath.test(filename) const isNextDist = nextDistPath.test(filename)
......
...@@ -480,6 +480,12 @@ export default async function getBaseWebpackConfig( ...@@ -480,6 +480,12 @@ export default async function getBaseWebpackConfig(
supportedBrowsers: config.experimental.browsersListForSwc supportedBrowsers: config.experimental.browsersListForSwc
? supportedBrowsers ? supportedBrowsers
: undefined, : undefined,
swcCacheDir: path.join(
dir,
config?.distDir ?? '.next',
'cache',
'swc'
),
}, },
} }
: { : {
......
...@@ -43,6 +43,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -43,6 +43,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
nextConfig, nextConfig,
jsConfig, jsConfig,
supportedBrowsers, supportedBrowsers,
swcCacheDir,
} = loaderOptions } = loaderOptions
const isPageFile = filename.startsWith(pagesDir) const isPageFile = filename.startsWith(pagesDir)
...@@ -56,6 +57,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -56,6 +57,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
nextConfig, nextConfig,
jsConfig, jsConfig,
supportedBrowsers, supportedBrowsers,
swcCacheDir,
}) })
const programmaticOptions = { const programmaticOptions = {
......
...@@ -7,6 +7,7 @@ import { nextBuild } from 'next-test-utils' ...@@ -7,6 +7,7 @@ import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const customFile = join(appDir, '.next/extra-file.txt') const customFile = join(appDir, '.next/extra-file.txt')
const cacheDir = join(appDir, '.next/cache') const cacheDir = join(appDir, '.next/cache')
const swcCacheDir = join(appDir, '.next/cache/swc')
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
let nextConfigContent let nextConfigContent
...@@ -18,6 +19,7 @@ async function checkFileWrite(existsAfterBuild) { ...@@ -18,6 +19,7 @@ async function checkFileWrite(existsAfterBuild) {
expect(fs.existsSync(customFile)).toBe(existsAfterBuild) expect(fs.existsSync(customFile)).toBe(existsAfterBuild)
// `.next/cache` should be preserved in all cases // `.next/cache` should be preserved in all cases
expect(fs.existsSync(cacheDir)).toBe(true) expect(fs.existsSync(cacheDir)).toBe(true)
expect(fs.existsSync(swcCacheDir)).toBe(true)
} }
const runTests = () => { const runTests = () => {
......
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