Skip to content

Commit

Permalink
feat(next/config): enable experimental swcTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 11, 2022
1 parent ba78588 commit 2cb690e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/next-swc/crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ swc_common = { version = "0.17.19", features = ["concurrent", "sourcemap"] }
swc_ecma_loader = { version = "0.29.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.143.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_cached = "0.1.1"
tracing = { version = "0.1.32", features = ["release_max_level_off"] }

tracing = { version = "0.1.32", features = ["release_max_level_info"] }

[dev-dependencies]
swc_ecma_transforms_testing = "0.77.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/next-swc/crates/emotion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ sourcemap = "6.0.1"
swc_atoms = "0.2.11"
swc_common = { version = "0.17.19", features = ["concurrent", "sourcemap"] }
swc_ecmascript = { version = "0.143.0", features = ["codegen", "utils", "visit"] }
swc_trace_macro = "0.1.1"
tracing = { version = "0.1.32", features = ["release_max_level_info"] }

[dev-dependencies]
swc_ecma_transforms_testing = "0.77.0"
Expand Down
1 change: 1 addition & 0 deletions packages/next/build/swc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export function minify(src: string, options: any): Promise<string>
export function minifySync(src: string, options: any): string
export function bundle(options: any): Promise<any>
export function parse(src: string, options: any): any
export function initCustomTraceSubscriber(traceFileName?: string): void
34 changes: 34 additions & 0 deletions packages/next/build/swc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ function loadNative() {
},

getTargetTriple: bindings.getTargetTriple,
initCustomTraceSubscriber: bindings.initCustomTraceSubscriber,
teardownTraceSubscriber: bindings.teardownTraceSubscriber,
}
return nativeBindings
}
Expand Down Expand Up @@ -251,3 +253,35 @@ export function getBinaryMetadata() {
target: bindings?.getTargetTriple?.(),
}
}

/**
* Initialize trace subscriber to emit traces.
*
* Returns an internal object to guard async flush emission if subscriber is initialized, caller should manually
* tear it down via `teardownTraceSubscriber`.
*/
export const initCustomTraceSubscriber = (() => {
let guard

return (filename) => {
if (!guard) {
// Wasm binary doesn't support trace emission
let bindings = loadNative()
guard = bindings.initCustomTraceSubscriber(filename)
}

return guard
}
})()

export const teardownTraceSubscriber = (() => {
let bindings

return (guard) => {
if (!bindings && !!guard) {
// Wasm binary doesn't support trace emission
bindings = loadNative()
return bindings.teardownTraceSubscriber(guard)
}
}
})()
30 changes: 30 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ const devtoolRevertWarning = execOnce(

let loggedSwcDisabled = false
let loggedIgnoredCompilerOptions = false
let swcTraceFlushGuard: unknown = null

/**
* Teardown swc's trace subscriber if there's an initialized flush guard exists.
*
* This is workaround to amend behavior with process.exit
* (https://github.com/vercel/next.js/blob/4db8c49cc31e4fc182391fae6903fb5ef4e8c66e/packages/next/bin/next.ts#L134=)
* seems preventing napi's cleanup hook execution (https://github.com/swc-project/swc/blob/main/crates/node/src/util.rs#L48-L51=),
*
* instead parent process manually drops guard when process gets signal to exit.
*/
process.on('exit', () => {
if (swcTraceFlushGuard) {
require('./swc')?.teardownTraceSubscriber?.(swcTraceFlushGuard)
}
})

function getOptimizedAliases(): { [pkg: string]: string } {
const stubWindowFetch = path.join(__dirname, 'polyfills', 'fetch', 'index.js')
Expand Down Expand Up @@ -439,6 +455,20 @@ export default async function getBaseWebpackConfig(
}

const getBabelOrSwcLoader = (isMiddleware: boolean) => {
if (
useSWCLoader &&
config?.experimental?.swcTrace?.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
)
}

return useSWCLoader
? {
loader: 'next-swc-loader',
Expand Down
4 changes: 4 additions & 0 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export interface ExperimentalConfig {
skipDefaultConversion?: boolean
}
>
swcTrace?: {
enabled: boolean
traceFileName?: string
}
}

/**
Expand Down

0 comments on commit 2cb690e

Please sign in to comment.