From 7ac15daf5cdd31dc5df92fecd41c08eb498fb4ae Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sun, 30 Oct 2022 19:13:28 -0700 Subject: [PATCH 1/2] Ensure we detect config correctly with turbo flag --- packages/next/build/index.ts | 1 + packages/next/cli/next-dev.ts | 40 ++++++++++++++++--- packages/next/export/index.ts | 1 + packages/next/server/dev/next-dev-server.ts | 1 + packages/next/telemetry/events/version.ts | 2 + test/integration/telemetry/test/index.test.js | 27 +++++++++++++ 6 files changed, 66 insertions(+), 6 deletions(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 2fc36d6eacff57e..ac2ef9bd60cf49f 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -340,6 +340,7 @@ export default async function build( (!!appDir && path.relative(dir, appDir).startsWith('src')), hasNowJson: !!(await findUp('now.json', { cwd: dir })), isCustomServer: null, + turboFlag: false, }) ) diff --git a/packages/next/cli/next-dev.ts b/packages/next/cli/next-dev.ts index 22d3de3ca945f87..708875fc9f1ac0d 100755 --- a/packages/next/cli/next-dev.ts +++ b/packages/next/cli/next-dev.ts @@ -12,6 +12,8 @@ import { CONFIG_FILES } from '../shared/lib/constants' import path from 'path' import type { NextConfig } from '../types' import { interopDefault } from '../lib/interop-default' +import { Telemetry } from '../telemetry/storage' +import { NextConfigComplete } from '../server/config-shared' const nextDev: cliCommand = (argv) => { const validArgs: arg.Spec = { @@ -121,6 +123,10 @@ const nextDev: cliCommand = (argv) => { require('../shared/lib/constants') as typeof import('../shared/lib/constants') const chalk = require('next/dist/compiled/chalk') as typeof import('next/dist/compiled/chalk') + const { eventCliSession } = + require('../telemetry/events/version') as typeof import('../telemetry/events/version') + const { findPagesDir } = + require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir') // To regenerate the TURBOPACK gradient require('gradient-string')('blue', 'red')('>>> TURBOPACK') const isTTY = process.stdout.isTTY @@ -153,7 +159,8 @@ const nextDev: cliCommand = (argv) => { configKey === 'appDir' || configKey === 'transpilePackages' || configKey === 'reactStrictMode' || - configKey === 'swcMinify' + configKey === 'swcMinify' || + configKey == 'configFileName' ) { return false } @@ -163,7 +170,7 @@ const nextDev: cliCommand = (argv) => { if (typeof defaultValue !== 'object') { return defaultValue !== userValue } - return Object.keys(userValue).some((key: string) => { + return Object.keys(userValue || {}).some((key: string) => { return checkUnsupportedCustomConfig(key, userValue, defaultValue) }) } @@ -277,10 +284,29 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit console.log(feedbackMessage) return loadBindings() - .then((bindings: any) => { - // eslint-disable-next-line no-shadow - const findUp = - require('next/dist/compiled/find-up') as typeof import('next/dist/compiled/find-up') + .then(async (bindings: any) => { + const distDir = path.join(dir, rawNextConfig.distDir || '.next') + const { pagesDir, appDir } = findPagesDir( + dir, + !!rawNextConfig.experimental?.appDir + ) + const telemetry = new Telemetry({ + distDir, + }) + + telemetry.record( + eventCliSession(distDir, rawNextConfig as NextConfigComplete, { + webpackVersion: 5, + cliCommand: 'dev', + isSrcDir: + (!!pagesDir && + path.relative(dir, pagesDir).startsWith('src')) || + (!!appDir && path.relative(dir, appDir).startsWith('src')), + hasNowJson: !!(await findUp('now.json', { cwd: dir })), + isCustomServer: false, + turboFlag: true, + }) + ) const turboJson = findUp.sync('turbo.json', { cwd: dir }) // eslint-disable-next-line no-shadow const packagePath = findUp.sync('package.json', { cwd: dir }) @@ -300,6 +326,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit }) // Start preflight after server is listening and ignore errors: preflight().catch(() => {}) + + await telemetry.flush() return server }) .then( diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index a77140e8306f040..2f78047fd963fc7 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -182,6 +182,7 @@ export default async function exportApp( isSrcDir: null, hasNowJson: !!(await findUp('now.json', { cwd: dir })), isCustomServer: null, + turboFlag: false, }) ) } diff --git a/packages/next/server/dev/next-dev-server.ts b/packages/next/server/dev/next-dev-server.ts index fa5159b9f3d10d5..1626bb276ee537b 100644 --- a/packages/next/server/dev/next-dev-server.ts +++ b/packages/next/server/dev/next-dev-server.ts @@ -728,6 +728,7 @@ export default class DevServer extends Server { (!!this.appDir && relative(this.dir, this.appDir).startsWith('src')), hasNowJson: !!(await findUp('now.json', { cwd: this.dir })), isCustomServer: this.isCustomServer, + turboFlag: false, }) ) // This is required by the tracing subsystem. diff --git a/packages/next/telemetry/events/version.ts b/packages/next/telemetry/events/version.ts index 759574cee4492ba..e3dd9ef98ba5738 100644 --- a/packages/next/telemetry/events/version.ts +++ b/packages/next/telemetry/events/version.ts @@ -29,6 +29,7 @@ type EventCliSessionStarted = { trailingSlashEnabled: boolean reactStrictMode: boolean webpackVersion: number | null + turboFlag: boolean } function hasBabelConfig(dir: string): boolean { @@ -111,6 +112,7 @@ export function eventCliSession( trailingSlashEnabled: !!nextConfig?.trailingSlash, reactStrictMode: !!nextConfig?.reactStrictMode, webpackVersion: event.webpackVersion || null, + turboFlag: event.turboFlag || false, } return [{ eventName: EVENT_VERSION, payload }] } diff --git a/test/integration/telemetry/test/index.test.js b/test/integration/telemetry/test/index.test.js index 749f1a5c3f63e93..930ec2af18fd4a0 100644 --- a/test/integration/telemetry/test/index.test.js +++ b/test/integration/telemetry/test/index.test.js @@ -368,6 +368,32 @@ describe('Telemetry CLI', () => { expect(stderr).toMatch(/isSrcDir.*?true/) }) + it('detects --turbo correctly for `next dev`', async () => { + let port = await findPort() + let stderr = '' + + const handleStderr = (msg) => { + stderr += msg + } + let app = await launchApp(appDir, port, { + onStderr: handleStderr, + env: { + NEXT_TELEMETRY_DEBUG: 1, + }, + turbo: true, + }) + await waitFor(1000) + + if (app) { + await killApp(app) + } + const event1 = /NEXT_CLI_SESSION_STARTED[\s\S]+?{([\s\S]+?)}/ + .exec(stderr) + .pop() + + expect(event1).toMatch(/"turboFlag": true/) + }) + it('detect reportWebVitals correctly for `next build`', async () => { // Case 1: When _app.js does not exist. let build = await nextBuild(appDir, [], { @@ -484,6 +510,7 @@ describe('Telemetry CLI', () => { expect(event1).toMatch(/"imageFormats": "image\/avif,image\/webp"/) expect(event1).toMatch(/"trailingSlashEnabled": false/) expect(event1).toMatch(/"reactStrictMode": false/) + expect(event1).toMatch(/"turboFlag": false/) await fs.rename( path.join(appDir, 'next.config.i18n-images'), From 91a5b6b54ff0c568f06be1bd01cbec98986f89dd Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sun, 30 Oct 2022 19:25:05 -0700 Subject: [PATCH 2/2] lint-fix --- packages/next/cli/next-dev.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/cli/next-dev.ts b/packages/next/cli/next-dev.ts index 708875fc9f1ac0d..a26ea893914c99d 100755 --- a/packages/next/cli/next-dev.ts +++ b/packages/next/cli/next-dev.ts @@ -160,7 +160,7 @@ const nextDev: cliCommand = (argv) => { configKey === 'transpilePackages' || configKey === 'reactStrictMode' || configKey === 'swcMinify' || - configKey == 'configFileName' + configKey === 'configFileName' ) { return false }