Skip to content

Commit 218b899

Browse files
authored
feat(turbo-tracing): implement next plugin (vercel#267)
1 parent daf46fd commit 218b899

31 files changed

+275
-127
lines changed

.eslintrc.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ignorePatterns:
1313
- dist
1414
- node_modules
1515
- crates
16-
- packages/webpack-nmt/test/with-mongodb-mongoose
16+
- packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose
1717

1818
env:
1919
browser: false

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ crates/turbopack-ecmascript/tests
55
crates/turbopack/tests/node-file-trace/integration
66
crates/turbopack/bench.json
77
packages/node-module-trace/npm
8-
packages/webpack-nmt/test/with-mongodb-mongoose
8+
packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose
99
dist

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"private": true,
44
"workspaces": [
55
"packages/*",
6-
"packages/webpack-nmt/test/with-mongodb-mongoose",
6+
"packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose",
77
"crates/turbopack/tests/node-file-trace",
88
"crates/next-dev/tests"
99
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# `@vercel/turbo-tracing-next-plugin`
2+
3+
## Installation
4+
5+
- yarn add -D `@vercel/turbo-tracing-next-plugin`
6+
- npm install -D `@vercel/turbo-tracing-next-plugin`
7+
- pnpm install -D `@vercel/turbo-tracing-next-plugin`
8+
9+
## Usage
10+
11+
```js
12+
// next.config.js
13+
14+
const { withTurboTracing } = require('@vercel/turbo-tracing-next-plugin')
15+
16+
module.exports = withTurboTracing({
17+
// turbo tracing options
18+
log: {
19+
all: true,
20+
},
21+
})({
22+
// next config
23+
})
24+
```
25+
26+
### turbo tracing options
27+
28+
> **Note**
29+
>
30+
> The default options should work fine.
31+
32+
- `cwd?: string`, default is `process.cwd()`, you can override it to specify another directory to run turbo tracing.
33+
- `contextDirectory?: string`, relative to cwd, default is `.`.
34+
- `path?: string`, additional path which will be appended into the `PATH` environment variable.
35+
- `log?.all?: boolean`, default is `false`, whether to show all logs.
36+
- `log?.level?: string`, default is `error`, the log level.
37+
- `log?.detail?: boolean`, default is `false`, whether to expand the log details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@vercel/turbo-tracing-next-plugin",
3+
"version": "0.0.0",
4+
"license": "UNLICENSED",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"files": [
8+
"dist/**/*"
9+
],
10+
"publishConfig": {
11+
"access": "restricted"
12+
},
13+
"dependencies": {
14+
"@vercel/webpack-node-module-trace": "0.0.3"
15+
},
16+
"peerDependencies": {
17+
"next": ">= 12"
18+
},
19+
"devDependencies": {
20+
"next": "^12.2.5"
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
NodeModuleTracePlugin,
3+
NodeModuleTracePluginOptions,
4+
} from '@vercel/webpack-node-module-trace'
5+
import type { NextConfig } from 'next'
6+
7+
export function withTurboTracing(options?: NodeModuleTracePluginOptions) {
8+
return function createTurboTracingConfig(config: NextConfig = {}) {
9+
const createWebpackConfig = config.webpack
10+
config.outputFileTracing = false
11+
config.webpack = (webpackConfig, context) => {
12+
const config =
13+
createWebpackConfig?.(webpackConfig, context) ?? webpackConfig
14+
const plugin = new NodeModuleTracePlugin(options)
15+
if (config.plugins) {
16+
config.plugins.push(plugin)
17+
} else {
18+
config.plugins = [plugin]
19+
}
20+
21+
return config
22+
}
23+
return config
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { join } = require('path')
2+
3+
const { withTurboTracing } = require('../..')
4+
5+
module.exports = withTurboTracing({
6+
path: join(__dirname, '..', '..', '..', '..', 'target', 'debug'),
7+
})({
8+
reactStrictMode: true,
9+
eslint: {
10+
ignoreDuringBuilds: true,
11+
},
12+
})

packages/webpack-nmt/test/with-mongodb-mongoose/package.json packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@vercel/nmt-test-app",
2+
"name": "@vercel/turbo-tracing-test-app",
33
"private": true,
44
"scripts": {
55
"dev": "next dev",
@@ -14,7 +14,7 @@
1414
"swr": "^1.3.0"
1515
},
1616
"devDependencies": {
17-
"@vercel/node-module-trace": "0.0.1",
17+
"@vercel/turbo-tracing-next-plugin": "0.0.0",
1818
"cross-env": "^7.0.3"
1919
}
2020
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"outDir": "./dist",
6+
"rootDir": "./src"
7+
},
8+
"include": ["src"],
9+
"references": [
10+
{
11+
"path": "../webpack-nmt"
12+
}
13+
]
14+
}

packages/webpack-nmt/src/index.ts

+63-19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ export interface NodeModuleTracePluginOptions {
99
contextDirectory?: string
1010
// additional PATH environment variable to use for spawning the `node-file-trace` process
1111
path?: string
12+
// log options
13+
log?: {
14+
all?: boolean
15+
detail?: boolean
16+
// Default is `error`
17+
level?:
18+
| 'bug'
19+
| 'fatal'
20+
| 'error'
21+
| 'warning'
22+
| 'hint'
23+
| 'note'
24+
| 'suggestions'
25+
| 'info'
26+
}
1227
}
1328

1429
export class NodeModuleTracePlugin implements WebpackPluginInstance {
@@ -63,25 +78,54 @@ export class NodeModuleTracePlugin implements WebpackPluginInstance {
6378
}
6479

6580
private runTrace() {
66-
spawnSync(
67-
'node-file-trace',
68-
[
69-
'annotate',
70-
'--context-directory',
71-
this.options?.contextDirectory ?? '.',
72-
'--exact',
73-
...this.chunksToTrace,
74-
],
75-
{
76-
stdio: 'inherit',
77-
env: {
78-
...process.env,
79-
PATH: `${this.options?.path ?? ''}${
80-
process.platform === 'win32' ? ';' : ':'
81-
}${process.env.PATH}`,
82-
},
83-
cwd: this.options?.cwd ?? process.cwd(),
81+
process.stdout.write('\n')
82+
const args = [
83+
'annotate',
84+
'--context-directory',
85+
this.options?.contextDirectory ?? '.',
86+
'--exact',
87+
]
88+
if (this.options?.log?.detail) {
89+
args.push('--log-detail')
90+
}
91+
if (this.options?.log?.all) {
92+
args.push('--show-all')
93+
}
94+
const logLevel = this.options?.log?.level
95+
if (logLevel) {
96+
args.push(`--log-level ${logLevel}`)
97+
}
98+
let turboTracingPackagePath = ''
99+
let turboTracingBinPath = ''
100+
try {
101+
turboTracingPackagePath = require.resolve('@vercel/node-module-trace')
102+
} catch (e) {
103+
// ignore
104+
}
105+
if (turboTracingPackagePath) {
106+
try {
107+
turboTracingBinPath = require.resolve(
108+
`@vercel/node-module-trace-${process.platform}-${process.arch}`,
109+
{
110+
paths: [turboTracingPackagePath],
111+
},
112+
)
113+
} catch (e) {
114+
// ignore
115+
}
116+
}
117+
const pathSep = process.platform === 'win32' ? ';' : ':'
118+
let paths = `${this.options?.path ?? ''}${pathSep}${process.env.PATH}`
119+
if (turboTracingBinPath) {
120+
paths = `${turboTracingBinPath}${pathSep}${paths}`
121+
}
122+
spawnSync('node-file-trace', [...args, ...this.chunksToTrace], {
123+
stdio: 'inherit',
124+
env: {
125+
...process.env,
126+
PATH: paths,
84127
},
85-
)
128+
cwd: this.options?.cwd ?? process.cwd(),
129+
})
86130
}
87131
}

packages/webpack-nmt/test/with-mongodb-mongoose/next.config.js

-24
This file was deleted.

tsconfig.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
"importHelpers": false,
88
"noEmitHelpers": false,
99
"skipLibCheck": true,
10-
"outDir": "./dist"
10+
"outDir": "./dist",
11+
"baseUrl": "./packages",
12+
"paths": {
13+
"@vercel/webpack-node-module-trace": ["webpack-nmt/src/index.ts"]
14+
}
1115
},
1216
"exclude": ["node_modules", "crates", ".yarn", "target"]
1317
}

tsconfig.project.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"extends": "./tsconfig.json",
33
"include": [],
4-
"references": [{ "path": "./packages/webpack-nmt" }]
4+
"references": [
5+
{ "path": "./packages/webpack-nmt" },
6+
{ "path": "./packages/turbo-tracing-next-plugin" }
7+
]
58
}

0 commit comments

Comments
 (0)