Unverified Commit b3cd381d authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add dev option to Turbopack createProject() (#62083)

## What?

Adds the `dev` option to switch between development and production
output flags for Turbopack.
Note: This only adds the option, the `false` value (production) doesn't
work yet.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2499
parent 96484750
...@@ -90,6 +90,9 @@ pub struct NapiProjectOptions { ...@@ -90,6 +90,9 @@ pub struct NapiProjectOptions {
/// A map of environment variables which should get injected at compile /// A map of environment variables which should get injected at compile
/// time. /// time.
pub define_env: NapiDefineEnv, pub define_env: NapiDefineEnv,
/// The mode in which Next.js is running.
pub dev: bool,
} }
/// [NapiProjectOptions] with all fields optional. /// [NapiProjectOptions] with all fields optional.
...@@ -121,6 +124,9 @@ pub struct NapiPartialProjectOptions { ...@@ -121,6 +124,9 @@ pub struct NapiPartialProjectOptions {
/// A map of environment variables which should get injected at compile /// A map of environment variables which should get injected at compile
/// time. /// time.
pub define_env: Option<NapiDefineEnv>, pub define_env: Option<NapiDefineEnv>,
/// The mode in which Next.js is running.
pub dev: Option<bool>,
} }
#[napi(object)] #[napi(object)]
...@@ -151,6 +157,7 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -151,6 +157,7 @@ impl From<NapiProjectOptions> for ProjectOptions {
.map(|var| (var.name, var.value)) .map(|var| (var.name, var.value))
.collect(), .collect(),
define_env: val.define_env.into(), define_env: val.define_env.into(),
dev: val.dev,
} }
} }
} }
...@@ -167,6 +174,7 @@ impl From<NapiPartialProjectOptions> for PartialProjectOptions { ...@@ -167,6 +174,7 @@ impl From<NapiPartialProjectOptions> for PartialProjectOptions {
.env .env
.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()), .map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
define_env: val.define_env.map(|env| env.into()), define_env: val.define_env.map(|env| env.into()),
dev: val.dev,
} }
} }
} }
......
...@@ -90,6 +90,9 @@ pub struct ProjectOptions { ...@@ -90,6 +90,9 @@ pub struct ProjectOptions {
/// Whether to watch the filesystem for file changes. /// Whether to watch the filesystem for file changes.
pub watch: bool, pub watch: bool,
/// The mode in which Next.js is running.
pub dev: bool,
} }
#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)] #[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
...@@ -117,6 +120,9 @@ pub struct PartialProjectOptions { ...@@ -117,6 +120,9 @@ pub struct PartialProjectOptions {
/// Whether to watch the filesystem for file changes. /// Whether to watch the filesystem for file changes.
pub watch: Option<bool>, pub watch: Option<bool>,
/// The mode in which Next.js is running.
pub dev: Option<bool>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)] #[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
...@@ -181,6 +187,8 @@ impl ProjectContainer { ...@@ -181,6 +187,8 @@ impl ProjectContainer {
new_options.watch = watch; new_options.watch = watch;
} }
// TODO: Handle mode switch, should prevent mode being switched.
self.options_state.set(new_options); self.options_state.set(new_options);
Default::default() Default::default()
...@@ -190,7 +198,7 @@ impl ProjectContainer { ...@@ -190,7 +198,7 @@ impl ProjectContainer {
pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> { pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
let this = self.await?; let this = self.await?;
let (env, define_env, next_config, js_config, root_path, project_path, watch) = { let (env, define_env, next_config, js_config, root_path, project_path, watch, dev) = {
let options = this.options_state.get(); let options = this.options_state.get();
let env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect()); let env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect());
let define_env: Vc<ProjectDefineEnv> = ProjectDefineEnv { let define_env: Vc<ProjectDefineEnv> = ProjectDefineEnv {
...@@ -204,6 +212,7 @@ impl ProjectContainer { ...@@ -204,6 +212,7 @@ impl ProjectContainer {
let root_path = options.root_path.clone(); let root_path = options.root_path.clone();
let project_path = options.project_path.clone(); let project_path = options.project_path.clone();
let watch = options.watch; let watch = options.watch;
let dev = options.dev;
( (
env, env,
define_env, define_env,
...@@ -212,6 +221,7 @@ impl ProjectContainer { ...@@ -212,6 +221,7 @@ impl ProjectContainer {
root_path, root_path,
project_path, project_path,
watch, watch,
dev,
) )
}; };
...@@ -233,7 +243,11 @@ impl ProjectContainer { ...@@ -233,7 +243,11 @@ impl ProjectContainer {
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \ browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions" versions, last 1 Edge versions"
.to_string(), .to_string(),
mode: NextMode::Development, mode: if dev {
NextMode::Development
} else {
NextMode::Build
},
versioned_content_map: this.versioned_content_map, versioned_content_map: this.versioned_content_map,
} }
.cell()) .cell())
......
...@@ -1342,12 +1342,14 @@ export default async function build( ...@@ -1342,12 +1342,14 @@ export default async function build(
process.env.NODE_ENV = 'development' process.env.NODE_ENV = 'development'
const startTime = process.hrtime() const startTime = process.hrtime()
const bindings = await loadBindings(config?.experimental?.useWasmBinary) const bindings = await loadBindings(config?.experimental?.useWasmBinary)
const dev = true
const project = await bindings.turbo.createProject({ const project = await bindings.turbo.createProject({
projectPath: dir, projectPath: dir,
rootPath: config.experimental.outputFileTracingRoot || dir, rootPath: config.experimental.outputFileTracingRoot || dir,
nextConfig: config, nextConfig: config,
jsConfig: await getTurbopackJsConfig(dir, config), jsConfig: await getTurbopackJsConfig(dir, config),
watch: false, watch: false,
dev,
env: process.env as Record<string, string>, env: process.env as Record<string, string>,
defineEnv: createDefineEnv({ defineEnv: createDefineEnv({
isTurbopack: true, isTurbopack: true,
...@@ -1355,7 +1357,7 @@ export default async function build( ...@@ -1355,7 +1357,7 @@ export default async function build(
clientRouterFilters: undefined, clientRouterFilters: undefined,
config, config,
// When passing `false` you get `react.jsxDEV is not a function` // When passing `false` you get `react.jsxDEV is not a function`
dev: true, dev,
distDir, distDir,
fetchCacheKeyPrefix: undefined, fetchCacheKeyPrefix: undefined,
hasRewrites, hasRewrites,
......
...@@ -410,6 +410,11 @@ export interface ProjectOptions { ...@@ -410,6 +410,11 @@ export interface ProjectOptions {
* Whether to watch the filesystem for file changes. * Whether to watch the filesystem for file changes.
*/ */
watch: boolean watch: boolean
/**
* The mode in which Next.js is running.
*/
dev: boolean
} }
type RustifiedEnv = { name: string; value: string }[] type RustifiedEnv = { name: string; value: string }[]
......
...@@ -119,6 +119,7 @@ export async function createHotReloaderTurbopack( ...@@ -119,6 +119,7 @@ export async function createHotReloaderTurbopack(
nextConfig: opts.nextConfig, nextConfig: opts.nextConfig,
jsConfig: await getTurbopackJsConfig(dir, nextConfig), jsConfig: await getTurbopackJsConfig(dir, nextConfig),
watch: true, watch: true,
dev: true,
env: process.env as Record<string, string>, env: process.env as Record<string, string>,
defineEnv: createDefineEnv({ defineEnv: createDefineEnv({
isTurbopack: true, isTurbopack: true,
......
...@@ -202,6 +202,7 @@ describe('next.rs api', () => { ...@@ -202,6 +202,7 @@ describe('next.rs api', () => {
? path.resolve(__dirname, '../../..') ? path.resolve(__dirname, '../../..')
: next.testDir, : next.testDir,
watch: true, watch: true,
dev: true,
defineEnv: createDefineEnv({ defineEnv: createDefineEnv({
isTurbopack: true, isTurbopack: true,
allowedRevalidateHeaderKeys: undefined, allowedRevalidateHeaderKeys: undefined,
......
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