Skip to content

Commit

Permalink
refactor(swc): use .next for the default trace output
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 20, 2022
1 parent 1a6103d commit 7560bd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/next-swc/crates/napi/src/util.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -108,6 +108,12 @@ pub fn init_custom_trace_subscriber(cx: CallContext) -> napi::Result<JsExternal>

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);
}

Expand Down
15 changes: 10 additions & 5 deletions packages/next/build/webpack-config.ts
Expand Up @@ -454,18 +454,23 @@ export default async function getBaseWebpackConfig(
loggedIgnoredCompilerOptions = true
}

const getBabelOrSwcLoader = (isMiddleware: boolean) => {
const getBabelOrSwcLoader = (isMiddleware: boolean, buildDir: string) => {
if (
useSWCLoader &&
config?.experimental?.swcTrace?.enabled &&
config?.experimental?.swcTraceProfiling?.enabled &&
!swcTraceFlushGuard
) {
// This will init subscribers once only in a single process lifecycle,
// even though it can be called multiple times.
// 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`
)
)
}

Expand Down Expand Up @@ -497,7 +502,7 @@ export default async function getBaseWebpackConfig(
}

const defaultLoaders = {
babel: getBabelOrSwcLoader(false),
babel: getBabelOrSwcLoader(false, dir),
}

const rawPageExtensions = hasServerComponents
Expand Down Expand Up @@ -1279,7 +1284,7 @@ export default async function getBaseWebpackConfig(
{
...codeCondition,
issuerLayer: 'middleware',
use: getBabelOrSwcLoader(true),
use: getBabelOrSwcLoader(true, dir),
},
{
...codeCondition,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/config-shared.ts
Expand Up @@ -128,7 +128,7 @@ export interface ExperimentalConfig {
skipDefaultConversion?: boolean
}
>
swcTrace?: {
swcTraceProfiling?: {
enabled: boolean
traceFileName?: string
}
Expand Down

0 comments on commit 7560bd4

Please sign in to comment.