Skip to content

Commit 0018336

Browse files
committed
Turbopack: fix passing project options from napi
1 parent 6c9b878 commit 0018336

File tree

5 files changed

+101
-109
lines changed

5 files changed

+101
-109
lines changed

crates/napi/src/next_api/project.rs

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use next_api::{
2121
ProjectOptions, WatchOptions,
2222
},
2323
route::Endpoint,
24+
routes_hashes_manifest::routes_hashes_manifest_asset_if_enabled,
2425
};
2526
use next_core::tracing_presets::{
2627
TRACING_NEXT_OVERVIEW_TARGETS, TRACING_NEXT_TARGETS, TRACING_NEXT_TURBO_TASKS_TARGETS,
@@ -137,8 +138,8 @@ pub struct NapiProjectOptions {
137138
/// Unix path. E.g. `apps/my-app`
138139
pub project_path: RcStr,
139140

140-
/// A path where to emit the build outputs, relative to [`Project::project_path`], always Unix
141-
/// path. Corresponds to next.config.js's `distDir`.
141+
/// A path where tracing output will be written to and/or cache is read/written.
142+
/// Usually equal to the `distDir` in next.config.js.
142143
/// E.g. `.next`
143144
pub dist_dir: RcStr,
144145

@@ -192,11 +193,6 @@ pub struct NapiPartialProjectOptions {
192193
/// E.g. `apps/my-app`
193194
pub project_path: Option<RcStr>,
194195

195-
/// A path where to emit the build outputs, relative to [`Project::project_path`], always a
196-
/// Unix path. Corresponds to next.config.js's `distDir`.
197-
/// E.g. `.next`
198-
pub dist_dir: Option<Option<RcStr>>,
199-
200196
/// Filesystem watcher options.
201197
pub watch: Option<NapiWatchOptions>,
202198

@@ -267,43 +263,70 @@ impl From<NapiWatchOptions> for WatchOptions {
267263

268264
impl From<NapiProjectOptions> for ProjectOptions {
269265
fn from(val: NapiProjectOptions) -> Self {
266+
let NapiProjectOptions {
267+
root_path,
268+
project_path,
269+
// Only used for initializing cache and tracing
270+
dist_dir: _,
271+
watch,
272+
next_config,
273+
env,
274+
define_env,
275+
dev,
276+
encryption_key,
277+
build_id,
278+
preview_props,
279+
browserslist_query,
280+
no_mangling,
281+
current_node_js_version,
282+
} = val;
270283
ProjectOptions {
271-
root_path: val.root_path,
272-
project_path: val.project_path,
273-
watch: val.watch.into(),
274-
next_config: val.next_config,
275-
env: val
276-
.env
277-
.into_iter()
278-
.map(|var| (var.name, var.value))
279-
.collect(),
280-
define_env: val.define_env.into(),
281-
dev: val.dev,
282-
encryption_key: val.encryption_key,
283-
build_id: val.build_id,
284-
preview_props: val.preview_props.into(),
285-
browserslist_query: val.browserslist_query,
286-
no_mangling: val.no_mangling,
287-
current_node_js_version: val.current_node_js_version,
284+
root_path,
285+
project_path,
286+
watch: watch.into(),
287+
next_config,
288+
env: env.into_iter().map(|var| (var.name, var.value)).collect(),
289+
define_env: define_env.into(),
290+
dev,
291+
encryption_key,
292+
build_id,
293+
preview_props: preview_props.into(),
294+
browserslist_query,
295+
no_mangling,
296+
current_node_js_version,
288297
}
289298
}
290299
}
291300

292301
impl From<NapiPartialProjectOptions> for PartialProjectOptions {
293302
fn from(val: NapiPartialProjectOptions) -> Self {
303+
let NapiPartialProjectOptions {
304+
root_path,
305+
project_path,
306+
watch,
307+
next_config,
308+
env,
309+
define_env,
310+
dev,
311+
encryption_key,
312+
build_id,
313+
preview_props,
314+
browserslist_query,
315+
no_mangling,
316+
} = val;
294317
PartialProjectOptions {
295-
root_path: val.root_path,
296-
project_path: val.project_path,
297-
watch: val.watch.map(From::from),
298-
next_config: val.next_config,
299-
env: val
300-
.env
301-
.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
302-
define_env: val.define_env.map(|env| env.into()),
303-
dev: val.dev,
304-
encryption_key: val.encryption_key,
305-
build_id: val.build_id,
306-
preview_props: val.preview_props.map(|props| props.into()),
318+
root_path,
319+
project_path,
320+
watch: watch.map(From::from),
321+
next_config,
322+
env: env.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
323+
define_env: define_env.map(|env| env.into()),
324+
dev,
325+
encryption_key,
326+
build_id,
327+
preview_props: preview_props.map(|props| props.into()),
328+
browserslist_query,
329+
no_mangling,
307330
}
308331
}
309332
}
@@ -1035,12 +1058,15 @@ async fn output_assets_operation(
10351058

10361059
let nft = next_server_nft_assets(project).await?;
10371060

1061+
let routes_hashes_manifest = routes_hashes_manifest_asset_if_enabled(project).await?;
1062+
10381063
whole_app_module_graphs.as_side_effect().await?;
10391064

10401065
Ok(Vc::cell(
10411066
output_assets
10421067
.into_iter()
10431068
.chain(nft.iter().copied())
1069+
.chain(routes_hashes_manifest.iter().copied())
10441070
.collect(),
10451071
))
10461072
}

crates/next-api/src/project.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ pub struct PartialProjectOptions {
229229

230230
/// Options for draft mode.
231231
pub preview_props: Option<DraftModeOptions>,
232+
233+
/// The browserslist query to use for targeting browsers.
234+
pub browserslist_query: Option<RcStr>,
235+
236+
/// When the code is minified, this opts out of the default mangling of
237+
/// local names for variables, functions etc., which can be useful for
238+
/// debugging/profiling purposes.
239+
pub no_mangling: Option<bool>,
232240
}
233241

234242
#[derive(
@@ -347,6 +355,8 @@ impl ProjectContainer {
347355
encryption_key,
348356
build_id,
349357
preview_props,
358+
browserslist_query,
359+
no_mangling,
350360
} = options;
351361

352362
let resolved_self = self.to_resolved().await?;
@@ -388,6 +398,12 @@ impl ProjectContainer {
388398
if let Some(preview_props) = preview_props {
389399
new_options.preview_props = preview_props;
390400
}
401+
if let Some(browserslist_query) = browserslist_query {
402+
new_options.browserslist_query = browserslist_query;
403+
}
404+
if let Some(no_mangling) = no_mangling {
405+
new_options.no_mangling = no_mangling;
406+
}
391407

392408
// TODO: Handle mode switch, should prevent mode being switched.
393409
let watch = new_options.watch;

packages/next/src/build/swc/generated-native.d.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ export interface NapiProjectOptions {
123123
*/
124124
projectPath: RcStr
125125
/**
126-
* A path where to emit the build outputs, relative to [`Project::project_path`], always Unix
127-
* path. Corresponds to next.config.js's `distDir`.
126+
* A path where tracing output will be written to and/or cache is read/written.
127+
* Usually equal to the `distDir` in next.config.js.
128128
* E.g. `.next`
129129
*/
130130
distDir: RcStr
@@ -155,6 +155,8 @@ export interface NapiProjectOptions {
155155
* debugging/profiling purposes.
156156
*/
157157
noMangling: boolean
158+
/** Whether to write the route hashes manifest. */
159+
writeRoutesHashesManifest: boolean
158160
/** The version of Node.js that is available/currently running. */
159161
currentNodeJsVersion: RcStr
160162
}
@@ -172,12 +174,6 @@ export interface NapiPartialProjectOptions {
172174
* E.g. `apps/my-app`
173175
*/
174176
projectPath?: RcStr
175-
/**
176-
* A path where to emit the build outputs, relative to [`Project::project_path`], always a
177-
* Unix path. Corresponds to next.config.js's `distDir`.
178-
* E.g. `.next`
179-
*/
180-
distDir?: RcStr | undefined | null
181177
/** Filesystem watcher options. */
182178
watch?: NapiWatchOptions
183179
/** The contents of next.config.js, serialized to JSON. */
@@ -199,6 +195,8 @@ export interface NapiPartialProjectOptions {
199195
previewProps?: NapiDraftModeOptions
200196
/** The browserslist query to use for targeting browsers. */
201197
browserslistQuery?: RcStr
198+
/** Whether to write the route hashes manifest. */
199+
writeRoutesHashesManifest?: boolean
202200
/**
203201
* When the code is minified, this opts out of the default mangling of
204202
* local names for variables, functions etc., which can be useful for

packages/next/src/build/swc/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type {
2929
Endpoint,
3030
HmrIdentifiers,
3131
Lockfile,
32+
PartialProjectOptions,
3233
Project,
3334
ProjectOptions,
3435
RawEntrypoints,
@@ -651,15 +652,15 @@ function bindingToApi(
651652
}
652653

653654
async function rustifyPartialProjectOptions(
654-
options: Partial<ProjectOptions>
655+
options: PartialProjectOptions
655656
): Promise<NapiPartialProjectOptions> {
656657
return {
657658
...options,
658659
nextConfig:
659660
options.nextConfig &&
660661
(await serializeNextConfig(
661662
options.nextConfig,
662-
path.join(options.rootPath!, options.projectPath!)
663+
path.join(options.rootPath, options.projectPath)
663664
)),
664665
env: options.env && rustifyEnv(options.env),
665666
}
@@ -672,7 +673,7 @@ function bindingToApi(
672673
this._nativeProject = nativeProject
673674
}
674675

675-
async update(options: Partial<ProjectOptions>) {
676+
async update(options: PartialProjectOptions) {
676677
await binding.projectUpdate(
677678
this._nativeProject,
678679
await rustifyPartialProjectOptions(options)

packages/next/src/build/swc/types.ts

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type {
55
RefCell,
66
NapiTurboEngineOptions,
77
NapiSourceDiagnostic,
8+
NapiProjectOptions,
9+
NapiPartialProjectOptions,
810
} from './generated-native'
911

1012
export type { NapiTurboEngineOptions as TurboEngineOptions }
@@ -367,27 +369,8 @@ export type WrittenEndpoint =
367369
config: EndpointConfig
368370
}
369371

370-
export interface ProjectOptions {
371-
/**
372-
* An absolute root path (Unix or Windows path) from which all files must be nested under. Trying
373-
* to access a file outside this root will fail, so think of this as a chroot.
374-
* E.g. `/home/user/projects/my-repo`.
375-
*/
376-
rootPath: string
377-
378-
/**
379-
* A path which contains the app/pages directories, relative to `root_path`, always a Unix path.
380-
* E.g. `apps/my-app`
381-
*/
382-
projectPath: string
383-
384-
/**
385-
* A path where to emit the build outputs, relative to [`Project::project_path`], always a Unix
386-
* path. Corresponds to next.config.js's `distDir`.
387-
* E.g. `.next`
388-
*/
389-
distDir: string
390-
372+
export interface ProjectOptions
373+
extends Omit<NapiProjectOptions, 'nextConfig' | 'env'> {
391374
/**
392375
* The next.config.js contents.
393376
*/
@@ -397,53 +380,21 @@ export interface ProjectOptions {
397380
* A map of environment variables to use when compiling code.
398381
*/
399382
env: Record<string, string>
383+
}
400384

401-
defineEnv: DefineEnv
402-
403-
/**
404-
* Whether to watch the filesystem for file changes.
405-
*/
406-
watch: {
407-
enable: boolean
408-
pollIntervalMs?: number
409-
}
410-
411-
/**
412-
* The mode in which Next.js is running.
413-
*/
414-
dev: boolean
415-
416-
/**
417-
* The server actions encryption key.
418-
*/
419-
encryptionKey: string
420-
421-
/**
422-
* The build id.
423-
*/
424-
buildId: string
425-
426-
/**
427-
* Options for draft mode.
428-
*/
429-
previewProps: __ApiPreviewProps
430-
431-
/**
432-
* The browserslist query to use for targeting browsers.
433-
*/
434-
browserslistQuery: string
435-
385+
export interface PartialProjectOptions
386+
extends Omit<NapiPartialProjectOptions, 'nextConfig' | 'env'> {
387+
rootPath: NapiProjectOptions['rootPath']
388+
projectPath: NapiProjectOptions['projectPath']
436389
/**
437-
* When the code is minified, this opts out of the default mangling of local
438-
* names for variables, functions etc., which can be useful for
439-
* debugging/profiling purposes.
390+
* The next.config.js contents.
440391
*/
441-
noMangling: boolean
392+
nextConfig?: NextConfigComplete
442393

443394
/**
444-
* The version of Node.js that is available/currently running.
395+
* A map of environment variables to use when compiling code.
445396
*/
446-
currentNodeJsVersion: string
397+
env?: Record<string, string>
447398
}
448399

449400
export interface DefineEnv {

0 commit comments

Comments
 (0)