Skip to content

Commit

Permalink
Skip postcss config location resolving in node_modules (#60697)
Browse files Browse the repository at this point in the history
## What?

Skip postcss config resolving in node_modules for Turbopack. This
matches current Next.js on webpack closer as that only resolves the
postcss config from the project root. The additional feature Turbopack
has is that it can find a postcss config relative to the css being
compiled. This is useful for e.g. reducing the amount of overhead
certain postcss plugins that only have to run against a single file take
(i.e. Tailwindcss).


Depends on github.com/vercel/turborepo/pull/7012 landing first.

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

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
  • Loading branch information
timneutkens and sokra authored Jan 17, 2024
1 parent c583528 commit 4548fed
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 66 deletions.
70 changes: 35 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ swc_core = { version = "0.87.16", features = [
testing = { version = "0.35.14" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240117.1" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240117.3" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240117.1" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240117.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240117.1" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240117.3" }

# General Deps

Expand Down
27 changes: 20 additions & 7 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ use turbopack_binding::{
},
dev::{react_refresh::assert_can_resolve_react_refresh, DevChunkingContext},
ecmascript::{chunk::EcmascriptChunkingContext, TreeShakingMode},
node::execution_context::ExecutionContext,
node::{
execution_context::ExecutionContext,
transforms::postcss::{PostCssConfigLocation, PostCssTransformOptions},
},
turbopack::{
condition::ContextCondition,
module_options::{
module_options_context::ModuleOptionsContext, CustomEcmascriptTransformPlugins,
JsxTransformOptions, MdxTransformModuleOptions, PostCssTransformOptions,
TypescriptTransformOptions, WebpackLoadersOptions,
JsxTransformOptions, MdxTransformModuleOptions, TypescriptTransformOptions,
WebpackLoadersOptions,
},
resolve_options_context::ResolveOptionsContext,
},
Expand Down Expand Up @@ -258,23 +261,34 @@ pub async fn get_client_module_options_context(
},
));

let postcss_transform_options = Some(PostCssTransformOptions {
let postcss_transform_options = PostCssTransformOptions {
postcss_package: Some(get_postcss_package_mapping(project_path)),
config_location: PostCssConfigLocation::ProjectPathOrLocalPath,
..Default::default()
});
};
let postcss_foreign_transform_options = PostCssTransformOptions {
// For node_modules we don't want to resolve postcss config relative to the file being
// compiled, instead it only uses the project root postcss config.
config_location: PostCssConfigLocation::ProjectPath,
..postcss_transform_options.clone()
};
let enable_postcss_transform = Some(postcss_transform_options.cell());
let enable_foreign_postcss_transform = Some(postcss_foreign_transform_options.cell());

let module_options_context = ModuleOptionsContext {
preset_env_versions: Some(env),
execution_context: Some(execution_context),
custom_ecma_transform_plugins,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
enable_postcss_transform,
..Default::default()
};

// node_modules context
let foreign_codes_options_context = ModuleOptionsContext {
enable_webpack_loaders: foreign_webpack_loaders,
enable_postcss_transform: enable_foreign_postcss_transform,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: postcss_transform_options.clone(),
..module_options_context.clone()
};

Expand All @@ -284,7 +298,6 @@ pub async fn get_client_module_options_context(
// the modules.
enable_jsx: Some(jsx_runtime_options),
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down
Loading

0 comments on commit 4548fed

Please sign in to comment.