Unverified Commit 1b0dad16 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] avoid creating many ModuleOptions (#76035)

<!-- 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 #

-->
parent 4abde4f1
......@@ -68,6 +68,55 @@ impl ModuleOptions {
path: Vc<FileSystemPath>,
module_options_context: Vc<ModuleOptionsContext>,
resolve_options_context: Vc<ResolveOptionsContext>,
) -> Result<Vc<ModuleOptions>> {
let ModuleOptionsContext {
css: CssOptionsContext { enable_raw_css, .. },
ref enable_postcss_transform,
ref enable_webpack_loaders,
ref rules,
..
} = *module_options_context.await?;
if !rules.is_empty() {
let path_value = path.await?;
for (condition, new_context) in rules.iter() {
if condition.matches(&path_value).await? {
return Ok(ModuleOptions::new(
path,
**new_context,
resolve_options_context,
));
}
}
}
let need_path = (!enable_raw_css
&& if let Some(options) = enable_postcss_transform {
let options = options.await?;
options.postcss_package.is_none()
} else {
false
})
|| if let Some(options) = enable_webpack_loaders {
let options = options.await?;
options.loader_runner_package.is_none()
} else {
false
};
Ok(Self::new_internal(
need_path.then_some(path),
module_options_context,
resolve_options_context,
))
}
#[turbo_tasks::function]
async fn new_internal(
path: Option<Vc<FileSystemPath>>,
module_options_context: Vc<ModuleOptionsContext>,
resolve_options_context: Vc<ResolveOptionsContext>,
) -> Result<Vc<ModuleOptions>> {
let ModuleOptionsContext {
ecmascript:
......@@ -96,26 +145,11 @@ impl ModuleOptions {
preset_env_versions,
ref module_rules,
execution_context,
ref rules,
tree_shaking_mode,
keep_last_successful_parse,
..
} = *module_options_context.await?;
if !rules.is_empty() {
let path_value = path.await?;
for (condition, new_context) in rules.iter() {
if condition.matches(&path_value).await? {
return Ok(ModuleOptions::new(
path,
**new_context,
resolve_options_context,
));
}
}
}
let mut refresh = false;
let mut transforms = vec![];
......@@ -416,7 +450,10 @@ impl ModuleOptions {
let import_map = if let Some(postcss_package) = options.postcss_package {
package_import_map_from_import_mapping("postcss".into(), *postcss_package)
} else {
package_import_map_from_context("postcss".into(), path)
package_import_map_from_context(
"postcss".into(),
path.context("need_path in ModuleOptions::new is incorrect")?,
)
};
rules.push(ModuleRule::new(
......@@ -548,7 +585,10 @@ impl ModuleOptions {
*loader_runner_package,
)
} else {
package_import_map_from_context("loader-runner".into(), path)
package_import_map_from_context(
"loader-runner".into(),
path.context("need_path in ModuleOptions::new is incorrect")?,
)
};
for (glob, rule) in webpack_loaders_options.rules.await?.iter() {
rules.push(ModuleRule::new(
......
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