Skip to content

Commit

Permalink
feat(telemetry): report swc target triple to telemetry (vercel#35420)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Mar 22, 2022
1 parent af95b0c commit c261924
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 20 deletions.
15 changes: 13 additions & 2 deletions packages/next/build/swc/index.js
Expand Up @@ -76,6 +76,9 @@ async function loadWasm() {
parse(src, options) {
return Promise.resolve(bindings.parse(src.toString(), options))
},
getTargetTriple() {
return undefined
},
}
return wasmBindings
} catch (e) {
Expand Down Expand Up @@ -187,6 +190,8 @@ function loadNative() {
parse(src, options) {
return bindings.parse(src, toBuffer(options ?? {}))
},

getTargetTriple: bindings.getTargetTriple,
}
return nativeBindings
}
Expand Down Expand Up @@ -235,8 +240,14 @@ export async function parse(src, options) {
}

export function getBinaryMetadata() {
let bindings = loadBindingsSync()
let bindings
try {
bindings = loadNative()
} catch (e) {
// Suppress exceptions, this fn allows to fail to load native bindings
}

return {
target: bindings.getTargetTriple(),
target: bindings?.getTargetTriple?.(),
}
}
50 changes: 33 additions & 17 deletions packages/next/build/webpack-config.ts
Expand Up @@ -43,7 +43,11 @@ import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin
import { regexLikeCss } from './webpack/config/blocks/css'
import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin'
import { FlightManifestPlugin } from './webpack/plugins/flight-manifest-plugin'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
import {
Feature,
SWC_TARGET_TRIPLE,
TelemetryPlugin,
} from './webpack/plugins/telemetry-plugin'
import type { Span } from '../trace'
import { getRawPageExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist'
Expand Down Expand Up @@ -406,6 +410,15 @@ export default async function getBaseWebpackConfig(
const distDir = path.join(dir, config.distDir)

let useSWCLoader = !babelConfigFile
let SWCBinaryTarget: [Feature, boolean] | undefined = undefined
if (useSWCLoader) {
// TODO: we do not collect wasm target yet
const binaryTarget = require('./swc')?.getBinaryMetadata?.()
?.target as SWC_TARGET_TRIPLE
SWCBinaryTarget = binaryTarget
? [`swc/target/${binaryTarget}` as const, true]
: undefined
}

if (!loggedSwcDisabled && !useSWCLoader && babelConfigFile) {
Log.info(
Expand Down Expand Up @@ -1492,23 +1505,26 @@ export default async function getBaseWebpackConfig(
!dev &&
!isServer &&
new TelemetryPlugin(
new Map([
['swcLoader', useSWCLoader],
['swcMinify', config.swcMinify],
['swcRelay', !!config.compiler?.relay],
['swcStyledComponents', !!config.compiler?.styledComponents],
[
'swcReactRemoveProperties',
!!config.compiler?.reactRemoveProperties,
],
new Map(
[
'swcExperimentalDecorators',
!!jsConfig?.compilerOptions?.experimentalDecorators,
],
['swcRemoveConsole', !!config.compiler?.removeConsole],
['swcImportSource', !!jsConfig?.compilerOptions?.jsxImportSource],
['swcEmotion', !!config.experimental.emotion],
])
['swcLoader', useSWCLoader],
['swcMinify', config.swcMinify],
['swcRelay', !!config.compiler?.relay],
['swcStyledComponents', !!config.compiler?.styledComponents],
[
'swcReactRemoveProperties',
!!config.compiler?.reactRemoveProperties,
],
[
'swcExperimentalDecorators',
!!jsConfig?.compilerOptions?.experimentalDecorators,
],
['swcRemoveConsole', !!config.compiler?.removeConsole],
['swcImportSource', !!jsConfig?.compilerOptions?.jsxImportSource],
['swcEmotion', !!config.experimental.emotion],
SWCBinaryTarget,
].filter<[Feature, boolean]>(Boolean as any)
)
),
].filter(Boolean as any as ExcludesFalse),
}
Expand Down
34 changes: 33 additions & 1 deletion packages/next/build/webpack/plugins/telemetry-plugin.ts
@@ -1,6 +1,24 @@
import type { webpack5 as webpack } from 'next/dist/compiled/webpack/webpack'

type Feature =
/**
* List of target triples next-swc native binary supports.
*/
export type SWC_TARGET_TRIPLE =
| 'x86_64-apple-darwin'
| 'x86_64-unknown-linux-gnu'
| 'x86_64-pc-windows-msvc'
| 'i686-pc-windows-msvc'
| 'aarch64-unknown-linux-gnu'
| 'armv7-unknown-linux-gnueabihf'
| 'aarch64-apple-darwin'
| 'aarch64-linux-android'
| 'arm-linux-androideabi'
| 'x86_64-unknown-freebsd'
| 'x86_64-unknown-linux-musl'
| 'aarch64-unknown-linux-musl'
| 'aarch64-pc-windows-msvc'

export type Feature =
| 'next/image'
| 'next/script'
| 'next/dynamic'
Expand All @@ -13,6 +31,7 @@ type Feature =
| 'swcRemoveConsole'
| 'swcImportSource'
| 'swcEmotion'
| `swc/target/${SWC_TARGET_TRIPLE}`

interface FeatureUsage {
featureName: Feature
Expand Down Expand Up @@ -52,6 +71,19 @@ const BUILD_FEATURES: Array<Feature> = [
'swcRemoveConsole',
'swcImportSource',
'swcEmotion',
'swc/target/x86_64-apple-darwin',
'swc/target/x86_64-unknown-linux-gnu',
'swc/target/x86_64-pc-windows-msvc',
'swc/target/i686-pc-windows-msvc',
'swc/target/aarch64-unknown-linux-gnu',
'swc/target/armv7-unknown-linux-gnueabihf',
'swc/target/aarch64-apple-darwin',
'swc/target/aarch64-linux-android',
'swc/target/arm-linux-androideabi',
'swc/target/x86_64-unknown-freebsd',
'swc/target/x86_64-unknown-linux-musl',
'swc/target/aarch64-unknown-linux-musl',
'swc/target/aarch64-pc-windows-msvc',
]

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/next/telemetry/events/build.ts
@@ -1,4 +1,5 @@
import { TelemetryPlugin } from '../../build/webpack/plugins/telemetry-plugin'
import type { SWC_TARGET_TRIPLE } from '../../build/webpack/plugins/telemetry-plugin'

const REGEXP_DIRECTORY_DUNDER =
/[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/]/i
Expand Down Expand Up @@ -144,6 +145,7 @@ export type EventBuildFeatureUsage = {
| 'swcRemoveConsole'
| 'swcImportSource'
| 'swcEmotion'
| `swc/target/${SWC_TARGET_TRIPLE}`
| 'build-lint'
invocationCount: number
}
Expand Down
13 changes: 13 additions & 0 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -632,6 +632,19 @@ describe('Telemetry CLI', () => {
regex.exec(stderr).pop() // swcRemoveConsole
regex.exec(stderr).pop() // swcImportSource
regex.exec(stderr).pop() // swcEmotion
regex.exec(stderr).pop() // swc/targets/*
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
regex.exec(stderr).pop()
const image = regex.exec(stderr).pop()
expect(image).toContain(`"featureName": "next/image"`)
expect(image).toContain(`"invocationCount": 1`)
Expand Down

0 comments on commit c261924

Please sign in to comment.