Skip to content

Commit 13a952c

Browse files
committed
Turbopack: fix passing project options from napi
1 parent 8478e37 commit 13a952c

File tree

5 files changed

+93
-109
lines changed

5 files changed

+93
-109
lines changed

crates/napi/src/next_api/project.rs

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ pub struct NapiProjectOptions {
137137
/// Unix path. E.g. `apps/my-app`
138138
pub project_path: RcStr,
139139

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`.
140+
/// A path where tracing output will be written to and/or cache is read/written.
141+
/// Usually equal to the `distDir` in next.config.js.
142142
/// E.g. `.next`
143143
pub dist_dir: RcStr,
144144

@@ -192,11 +192,6 @@ pub struct NapiPartialProjectOptions {
192192
/// E.g. `apps/my-app`
193193
pub project_path: Option<RcStr>,
194194

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-
200195
/// Filesystem watcher options.
201196
pub watch: Option<NapiWatchOptions>,
202197

@@ -267,43 +262,70 @@ impl From<NapiWatchOptions> for WatchOptions {
267262

268263
impl From<NapiProjectOptions> for ProjectOptions {
269264
fn from(val: NapiProjectOptions) -> Self {
265+
let NapiProjectOptions {
266+
root_path,
267+
project_path,
268+
// Only used for initializing cache and tracing
269+
dist_dir: _,
270+
watch,
271+
next_config,
272+
env,
273+
define_env,
274+
dev,
275+
encryption_key,
276+
build_id,
277+
preview_props,
278+
browserslist_query,
279+
no_mangling,
280+
current_node_js_version,
281+
} = val;
270282
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,
283+
root_path,
284+
project_path,
285+
watch: watch.into(),
286+
next_config,
287+
env: env.into_iter().map(|var| (var.name, var.value)).collect(),
288+
define_env: define_env.into(),
289+
dev,
290+
encryption_key,
291+
build_id,
292+
preview_props: preview_props.into(),
293+
browserslist_query,
294+
no_mangling,
295+
current_node_js_version,
288296
}
289297
}
290298
}
291299

292300
impl From<NapiPartialProjectOptions> for PartialProjectOptions {
293301
fn from(val: NapiPartialProjectOptions) -> Self {
302+
let NapiPartialProjectOptions {
303+
root_path,
304+
project_path,
305+
watch,
306+
next_config,
307+
env,
308+
define_env,
309+
dev,
310+
encryption_key,
311+
build_id,
312+
preview_props,
313+
browserslist_query,
314+
no_mangling,
315+
} = val;
294316
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()),
317+
root_path,
318+
project_path,
319+
watch: watch.map(From::from),
320+
next_config,
321+
env: env.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
322+
define_env: define_env.map(|env| env.into()),
323+
dev,
324+
encryption_key,
325+
build_id,
326+
preview_props: preview_props.map(|props| props.into()),
327+
browserslist_query,
328+
no_mangling,
307329
}
308330
}
309331
}

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: 2 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
@@ -172,12 +172,6 @@ export interface NapiPartialProjectOptions {
172172
* E.g. `apps/my-app`
173173
*/
174174
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
181175
/** Filesystem watcher options. */
182176
watch?: NapiWatchOptions
183177
/** The contents of next.config.js, serialized to JSON. */

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)