Unverified Commit 2150d1ae authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(next/build): send next-swc plugins telemetry (#38116)



* Requires prerequisite PR https://github.com/vercel/next-telemetry/pull/84

This PR adds telemetry to send list of the experimental swc plugins from next.config.js. Implementation largely mimics existing plugins telemetry for next_package (https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/events/plugins.ts), with small modification to read swc plugin package instead.

One notable difference is swc plugin telemetry can be sent without version. This is due to swc plugins can be specified with absolute path without any npm pkg resolutions.

## Bug

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

## Feature

- [x] 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 f21a9dd5
...@@ -111,6 +111,7 @@ import { injectedClientEntries } from './webpack/plugins/client-entry-plugin' ...@@ -111,6 +111,7 @@ import { injectedClientEntries } from './webpack/plugins/client-entry-plugin'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex' import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
import { flatReaddir } from '../lib/flat-readdir' import { flatReaddir } from '../lib/flat-readdir'
import { RemotePattern } from '../shared/lib/image-config' import { RemotePattern } from '../shared/lib/image-config'
import { eventSwcPlugins } from '../telemetry/events/swc-plugins'
export type SsgRoute = { export type SsgRoute = {
initialRevalidateSeconds: number | false initialRevalidateSeconds: number | false
...@@ -226,6 +227,10 @@ export default async function build( ...@@ -226,6 +227,10 @@ export default async function build(
telemetry.record(events) telemetry.record(events)
) )
eventSwcPlugins(path.resolve(dir), config).then((events) =>
telemetry.record(events)
)
const ignoreTypeScriptErrors = Boolean( const ignoreTypeScriptErrors = Boolean(
config.typescript.ignoreBuildErrors config.typescript.ignoreBuildErrors
) )
......
import findUp from 'next/dist/compiled/find-up'
import type { NextConfig } from '../../server/config-shared'
const EVENT_SWC_PLUGIN_PRESENT = 'NEXT_SWC_PLUGIN_DETECTED'
type SwcPluginsEvent = {
eventName: string
payload: {
packageName: string
packageVersion?: string
}
}
export async function eventSwcPlugins(
dir: string,
config: NextConfig
): Promise<Array<SwcPluginsEvent>> {
try {
const packageJsonPath = await findUp('package.json', { cwd: dir })
if (!packageJsonPath) {
return []
}
const { dependencies = {}, devDependencies = {} } = require(packageJsonPath)
const deps = { ...devDependencies, ...dependencies }
const swcPluginPackages =
config.experimental?.swcPlugins?.map(([name, _]) => name) ?? []
return swcPluginPackages.reduce(
(events: SwcPluginsEvent[], plugin: string): SwcPluginsEvent[] => {
// swc plugins can be non-npm pkgs with absolute path doesn't have version
const version = deps[plugin] ?? undefined
events.push({
eventName: EVENT_SWC_PLUGIN_PRESENT,
payload: {
packageName: plugin,
packageVersion: version,
},
})
return events
},
[]
)
} catch (_) {
return []
}
}
module.exports = {
experimental: {
swcPlugins: [
["swc-plugin-coverage-instrument", {}],
["@swc/plugin-relay", {}],
["/test/absolute_path/plugin.wasm", {}]
]
}
}
\ No newline at end of file
{
"dependencies": {
"swc-plugin-coverage-instrument": "0.0.6"
},
"devDependencies": {
"@swc/plugin-relay": "0.2.0"
}
}
\ No newline at end of file
...@@ -808,4 +808,54 @@ describe('Telemetry CLI', () => { ...@@ -808,4 +808,54 @@ describe('Telemetry CLI', () => {
const optimizedEvt = regex.exec(stderr).pop() const optimizedEvt = regex.exec(stderr).pop()
expect(optimizedEvt).toContain(`"middlewareCount": 1`) expect(optimizedEvt).toContain(`"middlewareCount": 1`)
}) })
it('emits telemetry for usage of swc plugins', async () => {
await fs.remove(path.join(appDir, 'next.config.js'))
await fs.remove(path.join(appDir, 'package.json'))
await fs.rename(
path.join(appDir, 'next.config.swc-plugins'),
path.join(appDir, 'next.config.js')
)
await fs.rename(
path.join(appDir, 'package.swc-plugins'),
path.join(appDir, 'package.json')
)
const { stderr } = await nextBuild(appDir, [], {
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
console.log(stderr)
await fs.rename(
path.join(appDir, 'next.config.js'),
path.join(appDir, 'next.config.swc-plugins')
)
await fs.rename(
path.join(appDir, 'package.json'),
path.join(appDir, 'package.swc-plugins')
)
const regex = /NEXT_SWC_PLUGIN_DETECTED[\s\S]+?{([\s\S]+?)}/g
const coverage = regex.exec(stderr).pop()
expect(coverage).toContain(
`"packageName": "swc-plugin-coverage-instrument"`
)
expect(coverage).toContain(`"packageVersion": "0.0.6"`)
const relay = regex.exec(stderr).pop()
expect(relay).toContain(`"packageName": "@swc/plugin-relay"`)
expect(relay).toContain(`"packageVersion": "0.2.0"`)
const absolute = regex.exec(stderr).pop()
expect(absolute).toContain(
`"packageName": "/test/absolute_path/plugin.wasm"`
)
expect(absolute).not.toContain(`packageVersion`)
})
}) })
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