Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack dev/build: Initial browserslist passing #69781

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@

/// Options for draft mode.
pub preview_props: NapiDraftModeOptions,

/// The browserslist query to use for targeting browsers.
pub browserslist_query: String,
}

/// [NapiProjectOptions] with all fields optional.
Expand Down Expand Up @@ -161,6 +164,9 @@

/// Options for draft mode.
pub preview_props: Option<NapiDraftModeOptions>,

/// The browserslist query to use for targeting browsers.
pub browserslist_query: Option<String>,
}

#[napi(object)]
Expand Down Expand Up @@ -195,6 +201,7 @@
encryption_key: val.encryption_key.into(),
build_id: val.build_id.into(),
preview_props: val.preview_props.into(),
browserslist_query: val.browserslist_query.into(),
}
}
}
Expand Down Expand Up @@ -899,7 +906,7 @@
}
}

/// Subscribes to lifecycle events of the compilation.

Check warning on line 909 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`

Check warning on line 909 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 909 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 909 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`
/// Emits an [UpdateMessage::Start] event when any computation starts.
/// Emits an [UpdateMessage::End] event when there was no computation for the
/// specified time (`aggregation_ms`). The [UpdateMessage::End] event contains
Expand Down
10 changes: 7 additions & 3 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub struct ProjectOptions {

/// Options for draft mode.
pub preview_props: DraftModeOptions,

/// The browserslist query to use for targeting browsers.
pub browserslist_query: RcStr,
}

#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, Hash, TraceRawVcs)]
Expand Down Expand Up @@ -308,6 +311,7 @@ impl ProjectContainer {
let encryption_key;
let build_id;
let preview_props;
let browserslist_query;
{
let options = this.options_state.get();
let options = options
Expand All @@ -329,6 +333,7 @@ impl ProjectContainer {
encryption_key = options.encryption_key.clone();
build_id = options.build_id.clone();
preview_props = options.preview_props.clone();
browserslist_query = options.browserslist_query.clone();
}

let dist_dir = next_config
Expand All @@ -346,9 +351,7 @@ impl ProjectContainer {
dist_dir,
env: Vc::upcast(env_map),
define_env,
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions"
.into(),
browserslist_query,
mode: if dev {
NextMode::Development.cell()
} else {
Expand Down Expand Up @@ -418,6 +421,7 @@ pub struct Project {
/// time.
define_env: Vc<ProjectDefineEnv>,

/// The browserslist query to use for targeting browsers.
browserslist_query: RcStr,

mode: Vc<NextMode>,
Expand Down
3 changes: 3 additions & 0 deletions crates/next-build-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ fn main() {
project_path: canonical_path.to_string_lossy().into(),
root_path: "/".into(),
watch: false,
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 \
Safari versions, last 1 Edge versions"
.into(),
};

let json = serde_json::to_string_pretty(&options).unwrap();
Expand Down
8 changes: 8 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import {
isAppBuiltinNotFoundPage,
collectRoutesUsingEdgeRuntime,
collectMeta,
// getSupportedBrowsers,
} from './utils'
import type { PageInfo, PageInfos, AppConfig, PrerenderedRoute } from './utils'
import { writeBuildId } from './write-build-id'
Expand Down Expand Up @@ -1330,6 +1331,12 @@ export default async function build(
const startTime = process.hrtime()
const bindings = await loadBindings(config?.experimental?.useWasmBinary)
const dev = false

// const supportedBrowsers = await getSupportedBrowsers(dir, dev)
const supportedBrowsers = [
'last 1 Chrome versions, last 1 Firefox versions, last 1 Safari versions, last 1 Edge versions',
]

const project = await bindings.turbo.createProject(
{
projectPath: dir,
Expand All @@ -1353,6 +1360,7 @@ export default async function build(
buildId: NextBuildContext.buildId!,
encryptionKey: NextBuildContext.encryptionKey!,
previewProps: NextBuildContext.previewProps!,
browserslistQuery: supportedBrowsers.join(', '),
},
{
memoryLimit: config.experimental.turbo?.memoryLimit,
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ export interface ProjectOptions {
* Options for draft mode.
*/
previewProps: __ApiPreviewProps

/**
* The browserslist query to use for targeting browsers.
*/
browserslistQuery: string
}

type RustifiedEnv = { name: string; value: string }[]
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2329,7 +2329,7 @@ export class NestedMiddlewareError extends Error {
export function getSupportedBrowsers(
dir: string,
isDevelopment: boolean
): string[] | undefined {
): string[] {
let browsers: any
try {
const browsersListConfig = browserslist.loadConfig({
Expand Down
18 changes: 13 additions & 5 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { generateEncryptionKeyBase64 } from '../app-render/encryption-utils'
import { isAppPageRouteDefinition } from '../route-definitions/app-page-route-definition'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import { getNodeDebugType } from '../lib/utils'
// import { getSupportedBrowsers } from '../../build/utils'

const wsServer = new ws.Server({ noServer: true })
const isTestMode = !!(
Expand All @@ -96,6 +97,7 @@ export async function createHotReloaderTurbopack(
distDir: string,
resetFetch: () => void
): Promise<NextJsHotReloaderInterface> {
const dev = true
const buildId = 'development'
const { nextConfig, dir } = opts

Expand Down Expand Up @@ -125,28 +127,33 @@ export async function createHotReloaderTurbopack(
// of the current `next dev` invocation.
hotReloaderSpan.stop()

const encryptionKey = await generateEncryptionKeyBase64(true)
const encryptionKey = await generateEncryptionKeyBase64(dev)

// TODO: Implement
let clientRouterFilters: any
if (nextConfig.experimental.clientRouterFilter) {
// TODO this need to be set correctly for persistent caching to work
}

// const supportedBrowsers = await getSupportedBrowsers(dir, dev)
const supportedBrowsers = [
'last 1 Chrome versions, last 1 Firefox versions, last 1 Safari versions, last 1 Edge versions',
]

const project = await bindings.turbo.createProject(
{
projectPath: dir,
rootPath: opts.nextConfig.outputFileTracingRoot || dir,
nextConfig: opts.nextConfig,
jsConfig: await getTurbopackJsConfig(dir, nextConfig),
watch: true,
dev: true,
watch: dev,
dev,
env: process.env as Record<string, string>,
defineEnv: createDefineEnv({
isTurbopack: true,
clientRouterFilters,
config: nextConfig,
dev: true,
dev,
distDir,
fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix,
hasRewrites,
Expand All @@ -156,6 +163,7 @@ export async function createHotReloaderTurbopack(
buildId,
encryptionKey,
previewProps: opts.fsChecker.prerenderManifest.preview,
browserslistQuery: supportedBrowsers.join(', '),
},
{
memoryLimit: opts.nextConfig.experimental.turbo?.memoryLimit,
Expand Down Expand Up @@ -912,7 +920,7 @@ export async function createHotReloaderTurbopack(
const finishBuilding = startBuilding(pathname, requestUrl, false)
try {
await handleRouteType({
dev: true,
dev,
page,
pathname,
route,
Expand Down
1 change: 1 addition & 0 deletions test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('next.rs api', () => {
previewModeEncryptionKey: '12345',
previewModeSigningKey: '12345',
},
browserslistQuery: 'last 2 versions',
})
projectUpdateSubscription = filterMapAsyncIterator(
project.updateInfoSubscribe(1000),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub fn get_runtime_asset_context(environment: Vc<Environment>) -> Vc<Box<dyn Ass
enable_typescript_transform: Some(TypescriptTransformOptions::default().cell()),
..Default::default()
},
// TODO: Somehow this fails to compile when enabled.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was investigating in the first place, the Turbopack runtime doesn't downlevel to the minimum esmodules browsers, i.e. it doesn't pass es2020 when running npx es-check es2020 test/e2e/app-dir/hello-world/.next/static/**/*.js. We'll have to figure out why it fails during minification with this enable.

// preset_env_versions: Some(environment),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
..Default::default()
}
Expand Down
Loading