From 81a2271fb71f2f3fd55ab24fc5633a89783bd4d1 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 20 Apr 2022 10:22:09 -0700 Subject: [PATCH] refactor(swc): use .next for the default trace output --- packages/next-swc/crates/napi/src/util.rs | 8 +++++++- packages/next/build/webpack-config.ts | 15 ++++++++++----- packages/next/server/config-shared.ts | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/next-swc/crates/napi/src/util.rs b/packages/next-swc/crates/napi/src/util.rs index df8ed75e7557b1..dbb5509951c233 100644 --- a/packages/next-swc/crates/napi/src/util.rs +++ b/packages/next-swc/crates/napi/src/util.rs @@ -26,7 +26,7 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -use anyhow::{Context, Error}; +use anyhow::{anyhow, Context, Error}; use napi::{CallContext, Env, JsBuffer, JsExternal, JsString, JsUndefined, JsUnknown, Status}; use serde::de::DeserializeOwned; use std::{any::type_name, convert::TryFrom, path::PathBuf}; @@ -108,6 +108,12 @@ pub fn init_custom_trace_subscriber(cx: CallContext) -> napi::Result let mut layer = ChromeLayerBuilder::new().include_args(true); if let Some(trace_out_file) = trace_out_file_path { + let dir = trace_out_file + .parent() + .ok_or_else(|| anyhow!("Not able to find path to the trace output")) + .convert_err()?; + std::fs::create_dir_all(dir)?; + layer = layer.file(trace_out_file); } diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index c8c3d1089ffd6a..e59ec524a586f6 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -451,10 +451,10 @@ export default async function getBaseWebpackConfig( loggedIgnoredCompilerOptions = true } - const getBabelOrSwcLoader = () => { + const getBabelOrSwcLoader = (buildDir: string) => { if ( useSWCLoader && - config?.experimental?.swcTrace?.enabled && + config?.experimental?.swcTraceProfiling?.enabled && !swcTraceFlushGuard ) { // This will init subscribers once only in a single process lifecycle, @@ -462,7 +462,12 @@ export default async function getBaseWebpackConfig( // Subscriber need to be initialized _before_ any actual swc's call (transform, etcs) // to collect correct trace spans when they are called. swcTraceFlushGuard = require('./swc')?.initCustomTraceSubscriber?.( - config?.experimental?.swcTrace?.traceFileName + config?.experimental?.swcTraceProfiling?.traceFileName ?? + path.join( + buildDir, + config.distDir, + `swc-trace-profile-${Date.now()}.json` + ) ) } @@ -494,7 +499,7 @@ export default async function getBaseWebpackConfig( } const defaultLoaders = { - babel: getBabelOrSwcLoader(), + babel: getBabelOrSwcLoader(false), } const rawPageExtensions = hasServerComponents @@ -1247,7 +1252,7 @@ export default async function getBaseWebpackConfig( { ...codeCondition, issuerLayer: 'middleware', - use: getBabelOrSwcLoader(), + use: getBabelOrSwcLoader(true), }, { ...codeCondition, diff --git a/packages/next/server/config-shared.ts b/packages/next/server/config-shared.ts index 9694bfae849e69..9530014ae92d40 100644 --- a/packages/next/server/config-shared.ts +++ b/packages/next/server/config-shared.ts @@ -131,7 +131,7 @@ export interface ExperimentalConfig { skipDefaultConversion?: boolean } > - swcTrace?: { + swcTraceProfiling?: { enabled: boolean traceFileName?: string }