Unverified Commit 8325e3c1 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: fix passing project options from napi (#86256)

### What?

Make stuff more type safe. Use destructuring to avoid forgetting fields
parent 0dba1c69
...@@ -137,8 +137,8 @@ pub struct NapiProjectOptions { ...@@ -137,8 +137,8 @@ pub struct NapiProjectOptions {
/// Unix path. E.g. `apps/my-app` /// Unix path. E.g. `apps/my-app`
pub project_path: RcStr, pub project_path: RcStr,
/// A path where to emit the build outputs, relative to [`Project::project_path`], always Unix /// A path where tracing output will be written to and/or cache is read/written.
/// path. Corresponds to next.config.js's `distDir`. /// Usually equal to the `distDir` in next.config.js.
/// E.g. `.next` /// E.g. `.next`
pub dist_dir: RcStr, pub dist_dir: RcStr,
...@@ -192,11 +192,6 @@ pub struct NapiPartialProjectOptions { ...@@ -192,11 +192,6 @@ pub struct NapiPartialProjectOptions {
/// E.g. `apps/my-app` /// E.g. `apps/my-app`
pub project_path: Option<RcStr>, pub project_path: Option<RcStr>,
/// A path where to emit the build outputs, relative to [`Project::project_path`], always a
/// Unix path. Corresponds to next.config.js's `distDir`.
/// E.g. `.next`
pub dist_dir: Option<Option<RcStr>>,
/// Filesystem watcher options. /// Filesystem watcher options.
pub watch: Option<NapiWatchOptions>, pub watch: Option<NapiWatchOptions>,
...@@ -267,43 +262,70 @@ impl From<NapiWatchOptions> for WatchOptions { ...@@ -267,43 +262,70 @@ impl From<NapiWatchOptions> for WatchOptions {
impl From<NapiProjectOptions> for ProjectOptions { impl From<NapiProjectOptions> for ProjectOptions {
fn from(val: NapiProjectOptions) -> Self { fn from(val: NapiProjectOptions) -> Self {
let NapiProjectOptions {
root_path,
project_path,
// Only used for initializing cache and tracing
dist_dir: _,
watch,
next_config,
env,
define_env,
dev,
encryption_key,
build_id,
preview_props,
browserslist_query,
no_mangling,
current_node_js_version,
} = val;
ProjectOptions { ProjectOptions {
root_path: val.root_path, root_path,
project_path: val.project_path, project_path,
watch: val.watch.into(), watch: watch.into(),
next_config: val.next_config, next_config,
env: val env: env.into_iter().map(|var| (var.name, var.value)).collect(),
.env define_env: define_env.into(),
.into_iter() dev,
.map(|var| (var.name, var.value)) encryption_key,
.collect(), build_id,
define_env: val.define_env.into(), preview_props: preview_props.into(),
dev: val.dev, browserslist_query,
encryption_key: val.encryption_key, no_mangling,
build_id: val.build_id, current_node_js_version,
preview_props: val.preview_props.into(),
browserslist_query: val.browserslist_query,
no_mangling: val.no_mangling,
current_node_js_version: val.current_node_js_version,
} }
} }
} }
impl From<NapiPartialProjectOptions> for PartialProjectOptions { impl From<NapiPartialProjectOptions> for PartialProjectOptions {
fn from(val: NapiPartialProjectOptions) -> Self { fn from(val: NapiPartialProjectOptions) -> Self {
let NapiPartialProjectOptions {
root_path,
project_path,
watch,
next_config,
env,
define_env,
dev,
encryption_key,
build_id,
preview_props,
browserslist_query,
no_mangling,
} = val;
PartialProjectOptions { PartialProjectOptions {
root_path: val.root_path, root_path,
project_path: val.project_path, project_path,
watch: val.watch.map(From::from), watch: watch.map(From::from),
next_config: val.next_config, next_config,
env: val env: env.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
.env define_env: define_env.map(|env| env.into()),
.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()), dev,
define_env: val.define_env.map(|env| env.into()), encryption_key,
dev: val.dev, build_id,
encryption_key: val.encryption_key, preview_props: preview_props.map(|props| props.into()),
build_id: val.build_id, browserslist_query,
preview_props: val.preview_props.map(|props| props.into()), no_mangling,
} }
} }
} }
......
...@@ -229,6 +229,14 @@ pub struct PartialProjectOptions { ...@@ -229,6 +229,14 @@ pub struct PartialProjectOptions {
/// Options for draft mode. /// Options for draft mode.
pub preview_props: Option<DraftModeOptions>, pub preview_props: Option<DraftModeOptions>,
/// The browserslist query to use for targeting browsers.
pub browserslist_query: Option<RcStr>,
/// When the code is minified, this opts out of the default mangling of
/// local names for variables, functions etc., which can be useful for
/// debugging/profiling purposes.
pub no_mangling: Option<bool>,
} }
#[derive( #[derive(
...@@ -347,6 +355,8 @@ impl ProjectContainer { ...@@ -347,6 +355,8 @@ impl ProjectContainer {
encryption_key, encryption_key,
build_id, build_id,
preview_props, preview_props,
browserslist_query,
no_mangling,
} = options; } = options;
let resolved_self = self.to_resolved().await?; let resolved_self = self.to_resolved().await?;
...@@ -388,6 +398,12 @@ impl ProjectContainer { ...@@ -388,6 +398,12 @@ impl ProjectContainer {
if let Some(preview_props) = preview_props { if let Some(preview_props) = preview_props {
new_options.preview_props = preview_props; new_options.preview_props = preview_props;
} }
if let Some(browserslist_query) = browserslist_query {
new_options.browserslist_query = browserslist_query;
}
if let Some(no_mangling) = no_mangling {
new_options.no_mangling = no_mangling;
}
// TODO: Handle mode switch, should prevent mode being switched. // TODO: Handle mode switch, should prevent mode being switched.
let watch = new_options.watch; let watch = new_options.watch;
......
...@@ -123,8 +123,8 @@ export interface NapiProjectOptions { ...@@ -123,8 +123,8 @@ export interface NapiProjectOptions {
*/ */
projectPath: RcStr projectPath: RcStr
/** /**
* A path where to emit the build outputs, relative to [`Project::project_path`], always Unix * A path where tracing output will be written to and/or cache is read/written.
* path. Corresponds to next.config.js's `distDir`. * Usually equal to the `distDir` in next.config.js.
* E.g. `.next` * E.g. `.next`
*/ */
distDir: RcStr distDir: RcStr
...@@ -172,12 +172,6 @@ export interface NapiPartialProjectOptions { ...@@ -172,12 +172,6 @@ export interface NapiPartialProjectOptions {
* E.g. `apps/my-app` * E.g. `apps/my-app`
*/ */
projectPath?: RcStr projectPath?: RcStr
/**
* A path where to emit the build outputs, relative to [`Project::project_path`], always a
* Unix path. Corresponds to next.config.js's `distDir`.
* E.g. `.next`
*/
distDir?: RcStr | undefined | null
/** Filesystem watcher options. */ /** Filesystem watcher options. */
watch?: NapiWatchOptions watch?: NapiWatchOptions
/** The contents of next.config.js, serialized to JSON. */ /** The contents of next.config.js, serialized to JSON. */
......
...@@ -29,6 +29,7 @@ import type { ...@@ -29,6 +29,7 @@ import type {
Endpoint, Endpoint,
HmrIdentifiers, HmrIdentifiers,
Lockfile, Lockfile,
PartialProjectOptions,
Project, Project,
ProjectOptions, ProjectOptions,
RawEntrypoints, RawEntrypoints,
...@@ -651,7 +652,7 @@ function bindingToApi( ...@@ -651,7 +652,7 @@ function bindingToApi(
} }
async function rustifyPartialProjectOptions( async function rustifyPartialProjectOptions(
options: Partial<ProjectOptions> options: PartialProjectOptions
): Promise<NapiPartialProjectOptions> { ): Promise<NapiPartialProjectOptions> {
return { return {
...options, ...options,
...@@ -659,7 +660,7 @@ function bindingToApi( ...@@ -659,7 +660,7 @@ function bindingToApi(
options.nextConfig && options.nextConfig &&
(await serializeNextConfig( (await serializeNextConfig(
options.nextConfig, options.nextConfig,
path.join(options.rootPath!, options.projectPath!) path.join(options.rootPath, options.projectPath)
)), )),
env: options.env && rustifyEnv(options.env), env: options.env && rustifyEnv(options.env),
} }
...@@ -672,7 +673,7 @@ function bindingToApi( ...@@ -672,7 +673,7 @@ function bindingToApi(
this._nativeProject = nativeProject this._nativeProject = nativeProject
} }
async update(options: Partial<ProjectOptions>) { async update(options: PartialProjectOptions) {
await binding.projectUpdate( await binding.projectUpdate(
this._nativeProject, this._nativeProject,
await rustifyPartialProjectOptions(options) await rustifyPartialProjectOptions(options)
......
...@@ -5,6 +5,8 @@ import type { ...@@ -5,6 +5,8 @@ import type {
RefCell, RefCell,
NapiTurboEngineOptions, NapiTurboEngineOptions,
NapiSourceDiagnostic, NapiSourceDiagnostic,
NapiProjectOptions,
NapiPartialProjectOptions,
} from './generated-native' } from './generated-native'
export type { NapiTurboEngineOptions as TurboEngineOptions } export type { NapiTurboEngineOptions as TurboEngineOptions }
...@@ -367,27 +369,8 @@ export type WrittenEndpoint = ...@@ -367,27 +369,8 @@ export type WrittenEndpoint =
config: EndpointConfig config: EndpointConfig
} }
export interface ProjectOptions { export interface ProjectOptions
/** extends Omit<NapiProjectOptions, 'nextConfig' | 'env'> {
* An absolute root path (Unix or Windows path) from which all files must be nested under. Trying
* to access a file outside this root will fail, so think of this as a chroot.
* E.g. `/home/user/projects/my-repo`.
*/
rootPath: string
/**
* A path which contains the app/pages directories, relative to `root_path`, always a Unix path.
* E.g. `apps/my-app`
*/
projectPath: string
/**
* A path where to emit the build outputs, relative to [`Project::project_path`], always a Unix
* path. Corresponds to next.config.js's `distDir`.
* E.g. `.next`
*/
distDir: string
/** /**
* The next.config.js contents. * The next.config.js contents.
*/ */
...@@ -397,53 +380,21 @@ export interface ProjectOptions { ...@@ -397,53 +380,21 @@ export interface ProjectOptions {
* A map of environment variables to use when compiling code. * A map of environment variables to use when compiling code.
*/ */
env: Record<string, string> env: Record<string, string>
}
defineEnv: DefineEnv export interface PartialProjectOptions
extends Omit<NapiPartialProjectOptions, 'nextConfig' | 'env'> {
/** rootPath: NapiProjectOptions['rootPath']
* Whether to watch the filesystem for file changes. projectPath: NapiProjectOptions['projectPath']
*/
watch: {
enable: boolean
pollIntervalMs?: number
}
/**
* The mode in which Next.js is running.
*/
dev: boolean
/**
* The server actions encryption key.
*/
encryptionKey: string
/**
* The build id.
*/
buildId: string
/**
* Options for draft mode.
*/
previewProps: __ApiPreviewProps
/**
* The browserslist query to use for targeting browsers.
*/
browserslistQuery: string
/** /**
* When the code is minified, this opts out of the default mangling of local * The next.config.js contents.
* names for variables, functions etc., which can be useful for
* debugging/profiling purposes.
*/ */
noMangling: boolean nextConfig?: NextConfigComplete
/** /**
* The version of Node.js that is available/currently running. * A map of environment variables to use when compiling code.
*/ */
currentNodeJsVersion: string env?: Record<string, string>
} }
export interface DefineEnv { export interface DefineEnv {
......
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