Skip to content

Commit

Permalink
PostCSS support in next-dev (vercel/turbo#3065)
Browse files Browse the repository at this point in the history
This adds the ability to add Source transforms on assets, which modify
the source code of an asset before parsing.

This adds postcss via node.js as first source transform.

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
  • Loading branch information
Brooooooklyn and sokra committed Dec 19, 2022
1 parent df7fa75 commit 78abb7e
Show file tree
Hide file tree
Showing 25 changed files with 414 additions and 101 deletions.
10 changes: 6 additions & 4 deletions packages/next-swc/crates/next-core/js/src/entry/config/next.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const loadConfig = require("next/dist/server/config").default;
const { PHASE_DEVELOPMENT_SERVER } = require("next/dist/shared/lib/constants");
import loadConfig from "next/dist/server/config";
import { PHASE_DEVELOPMENT_SERVER } from "next/dist/shared/lib/constants";

module.exports = (async () => {
const loadNextConfig = async () => {
const nextConfig = await loadConfig(PHASE_DEVELOPMENT_SERVER, process.cwd());
nextConfig.rewrites = await nextConfig.rewrites?.();
nextConfig.redirects = await nextConfig.redirects?.();
return nextConfig;
})();
};

export { loadNextConfig as default };
93 changes: 63 additions & 30 deletions packages/next-swc/crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ use turbopack_ecmascript::{
};
use turbopack_env::ProcessEnvAssetVc;
use turbopack_node::{
create_node_rendered_source,
node_entry::{NodeRenderingEntry, NodeRenderingEntryVc},
NodeEntry, NodeEntryVc,
execution_context::ExecutionContextVc, render::rendered_source::create_node_rendered_source,
NodeEntry, NodeEntryVc, NodeRenderingEntry, NodeRenderingEntryVc,
};

use crate::{
Expand Down Expand Up @@ -69,21 +68,22 @@ use crate::{

#[turbo_tasks::function]
fn next_client_chunks_transition(
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
app_dir: FileSystemPathVc,
server_root: FileSystemPathVc,
browserslist_query: &str,
) -> TransitionVc {
let ty = Value::new(ContextType::App { app_dir });
let client_chunking_context = get_client_chunking_context(project_root, server_root, ty);
let client_chunking_context = get_client_chunking_context(project_path, server_root, ty);
let client_environment = get_client_environment(browserslist_query);

let client_module_options_context =
get_client_module_options_context(project_root, client_environment, ty);
get_client_module_options_context(project_path, execution_context, client_environment, ty);
NextClientChunksTransition {
client_chunking_context,
client_module_options_context,
client_resolve_options_context: get_client_resolve_options_context(project_root, ty),
client_resolve_options_context: get_client_resolve_options_context(project_path, ty),
client_environment,
server_root,
}
Expand All @@ -93,20 +93,21 @@ fn next_client_chunks_transition(

#[turbo_tasks::function]
async fn next_client_transition(
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
server_root: FileSystemPathVc,
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
browserslist_query: &str,
next_config: NextConfigVc,
) -> Result<TransitionVc> {
let ty = Value::new(ContextType::App { app_dir });
let client_chunking_context = get_client_chunking_context(project_root, server_root, ty);
let client_chunking_context = get_client_chunking_context(project_path, server_root, ty);
let client_environment = get_client_environment(browserslist_query);
let client_module_options_context =
get_client_module_options_context(project_root, client_environment, ty);
let client_runtime_entries = get_client_runtime_entries(project_root, env, ty, next_config);
let client_resolve_options_context = get_client_resolve_options_context(project_root, ty);
get_client_module_options_context(project_path, execution_context, client_environment, ty);
let client_runtime_entries = get_client_runtime_entries(project_path, env, ty, next_config);
let client_resolve_options_context = get_client_resolve_options_context(project_path, ty);

Ok(NextClientTransition {
is_app: true,
Expand All @@ -123,16 +124,21 @@ async fn next_client_transition(

#[turbo_tasks::function]
fn next_ssr_client_module_transition(
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
app_dir: FileSystemPathVc,
process_env: ProcessEnvVc,
next_config: NextConfigVc,
) -> TransitionVc {
let ty = Value::new(ServerContextType::AppSSR { app_dir });
NextSSRClientModuleTransition {
ssr_module_options_context: get_server_module_options_context(ty),
ssr_module_options_context: get_server_module_options_context(
project_path,
execution_context,
ty,
),
ssr_resolve_options_context: get_server_resolve_options_context(
project_root,
project_path,
ty,
next_config,
),
Expand All @@ -144,7 +150,8 @@ fn next_ssr_client_module_transition(

#[turbo_tasks::function]
fn next_layout_entry_transition(
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
app_dir: FileSystemPathVc,
server_root: FileSystemPathVc,
process_env: ProcessEnvVc,
Expand All @@ -153,8 +160,9 @@ fn next_layout_entry_transition(
let ty = Value::new(ServerContextType::AppRSC { app_dir });
let rsc_environment = get_server_environment(ty, process_env);
let rsc_resolve_options_context =
get_server_resolve_options_context(project_root, ty, next_config);
let rsc_module_options_context = get_server_module_options_context(ty);
get_server_resolve_options_context(project_path, ty, next_config);
let rsc_module_options_context =
get_server_module_options_context(project_path, execution_context, ty);

NextLayoutEntryTransition {
rsc_environment,
Expand All @@ -168,7 +176,8 @@ fn next_layout_entry_transition(

#[turbo_tasks::function]
fn app_context(
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
server_root: FileSystemPathVc,
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
Expand All @@ -181,7 +190,14 @@ fn app_context(
let mut transitions = HashMap::new();
transitions.insert(
"next-layout-entry".to_string(),
next_layout_entry_transition(project_root, app_dir, server_root, env, next_config),
next_layout_entry_transition(
project_path,
execution_context,
app_dir,
server_root,
env,
next_config,
),
);
transitions.insert(
"server-to-client".to_string(),
Expand All @@ -190,7 +206,8 @@ fn app_context(
transitions.insert(
"next-client".to_string(),
next_client_transition(
project_root,
project_path,
execution_context,
server_root,
app_dir,
env,
Expand All @@ -200,19 +217,31 @@ fn app_context(
);
transitions.insert(
"next-client-chunks".to_string(),
next_client_chunks_transition(project_root, app_dir, server_root, browserslist_query),
next_client_chunks_transition(
project_path,
execution_context,
app_dir,
server_root,
browserslist_query,
),
);
transitions.insert(
"next-ssr-client-module".to_string(),
next_ssr_client_module_transition(project_root, app_dir, env, next_config),
next_ssr_client_module_transition(
project_path,
execution_context,
app_dir,
env,
next_config,
),
);

let ssr_ty = Value::new(ServerContextType::AppSSR { app_dir });
ModuleAssetContextVc::new(
TransitionsByNameVc::cell(transitions),
get_server_environment(ssr_ty, env),
get_server_module_options_context(ssr_ty),
get_server_resolve_options_context(project_root, ssr_ty, next_config),
get_server_module_options_context(project_path, execution_context, ssr_ty),
get_server_resolve_options_context(project_path, ssr_ty, next_config),
)
.into()
}
Expand All @@ -222,6 +251,7 @@ fn app_context(
#[turbo_tasks::function]
pub async fn create_app_source(
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
output_path: FileSystemPathVc,
server_root: FileSystemPathVc,
env: ProcessEnvVc,
Expand All @@ -246,6 +276,7 @@ pub async fn create_app_source(

let context_ssr = app_context(
project_path,
execution_context,
server_root,
app_dir,
env,
Expand All @@ -255,6 +286,7 @@ pub async fn create_app_source(
);
let context = app_context(
project_path,
execution_context,
server_root,
app_dir,
env,
Expand All @@ -271,6 +303,7 @@ pub async fn create_app_source(

let fallback_page = get_fallback_page(
project_path,
execution_context,
server_root,
env,
browserslist_query,
Expand Down Expand Up @@ -298,7 +331,7 @@ pub async fn create_app_source(
async fn create_app_source_for_directory(
context_ssr: AssetContextVc,
context: AssetContextVc,
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
specificity: SpecificityVc,
position: u32,
input_dir: FileSystemPathVc,
Expand Down Expand Up @@ -392,7 +425,7 @@ async fn create_app_source_for_directory(
layout_path: layouts,
page_path,
target,
project_root,
project_path,
intermediate_output_path,
}
.cell()
Expand Down Expand Up @@ -422,7 +455,7 @@ async fn create_app_source_for_directory(
create_app_source_for_directory(
context_ssr,
context,
project_root,
project_path,
specificity,
position,
*dir,
Expand All @@ -449,7 +482,7 @@ struct AppRenderer {
layout_path: LayoutSegmentsVc,
page_path: FileSystemPathVc,
target: FileSystemPathVc,
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
intermediate_output_path: FileSystemPathVc,
}

Expand Down Expand Up @@ -580,7 +613,7 @@ import BOOTSTRAP from {};
};

let chunking_context = DevChunkingContextVc::builder(
self.project_root,
self.project_path,
intermediate_output_path,
intermediate_output_path.join("chunks"),
self.server_root.join("_next/static/assets"),
Expand Down
17 changes: 10 additions & 7 deletions packages/next-swc/crates/next-core/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use turbopack_core::{
resolve::{options::ImportMap, origin::PlainResolveOriginVc},
};
use turbopack_dev_server::html::DevHtmlAssetVc;
use turbopack_node::execution_context::ExecutionContextVc;

use crate::{
next_client::context::{
Expand All @@ -26,21 +27,23 @@ use crate::{

#[turbo_tasks::function]
pub async fn get_fallback_page(
project_root: FileSystemPathVc,
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
dev_server_root: FileSystemPathVc,
env: ProcessEnvVc,
browserslist_query: &str,
next_config: NextConfigVc,
) -> Result<DevHtmlAssetVc> {
let ty = Value::new(ContextType::Fallback);
let environment = get_client_environment(browserslist_query);
let resolve_options_context = get_client_resolve_options_context(project_root, ty);
let module_options_context = get_client_module_options_context(project_root, environment, ty);
let chunking_context = get_client_chunking_context(project_root, dev_server_root, ty);
let entries = get_client_runtime_entries(project_root, env, ty, next_config);
let resolve_options_context = get_client_resolve_options_context(project_path, ty);
let module_options_context =
get_client_module_options_context(project_path, execution_context, environment, ty);
let chunking_context = get_client_chunking_context(project_path, dev_server_root, ty);
let entries = get_client_runtime_entries(project_path, env, ty, next_config);

let mut import_map = ImportMap::empty();
insert_next_shared_aliases(&mut import_map, project_root);
insert_next_shared_aliases(&mut import_map, project_path);

let context: AssetContextVc = ModuleAssetContextVc::new(
TransitionsByNameVc::cell(HashMap::new()),
Expand All @@ -53,7 +56,7 @@ pub async fn get_fallback_page(
let runtime_entries = entries.resolve_entries(context);

let fallback_chunk = resolve_runtime_request(
PlainResolveOriginVc::new(context, project_root).into(),
PlainResolveOriginVc::new(context, project_path).into(),
"entry/fallback",
);

Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod embed_js;
pub mod env;
mod fallback;
pub mod manifest;
mod next_build;
pub mod next_client;
mod next_client_component;
pub mod next_config;
Expand All @@ -23,15 +24,14 @@ mod web_entry_source;

pub use app_source::create_app_source;
pub use server_rendered_source::create_server_rendered_source;
pub use turbopack_node::source_map;
pub use web_entry_source::create_web_entry_source;

pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
turbo_tasks_fetch::register();
turbopack_dev_server::register();
turbopack::register();
turbopack_node::register();
turbopack::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}
5 changes: 3 additions & 2 deletions packages/next-swc/crates/next-core/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use turbopack_core::asset::AssetContentVc;
use turbopack_dev_server::source::{
ContentSource, ContentSourceContent, ContentSourceData, ContentSourceResultVc, ContentSourceVc,
};
use turbopack_node::{
node_api_source::NodeApiContentSourceVc, node_rendered_source::NodeRenderContentSourceVc,
use turbopack_node::render::{
node_api_source::NodeApiContentSourceVc, rendered_source::NodeRenderContentSourceVc,
};

/// A content source which creates the next.js `_devPagesManifest.json` and
Expand All @@ -29,6 +29,7 @@ impl DevManifestContentSourceVc {
while let Some(content_source) = queue.pop() {
queue.extend(content_source.get_children().await?.iter());

// TODO This shouldn't use casts but an public api instead
if let Some(api_source) = NodeApiContentSourceVc::resolve_from(content_source).await? {
routes.insert(format!("/{}", api_source.get_pathname().await?));

Expand Down

0 comments on commit 78abb7e

Please sign in to comment.