Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use postcss transforms consistently also on server side #57434

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/next-swc/crates/napi/src/next_api/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub async fn endpoint_write_to_disk(
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_server_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
issues: bool,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let turbo_tasks = endpoint.turbo_tasks().clone();
Expand All @@ -124,9 +125,13 @@ pub fn endpoint_server_changed_subscribe(
move || async move {
let changed = endpoint.server_changed();
changed.strongly_consistent().await?;
let issues = get_issues(changed).await?;
let diags = get_diagnostics(changed).await?;
Ok((issues, diags))
if issues {
let issues = get_issues(changed).await?;
let diags = get_diagnostics(changed).await?;
Ok((issues, diags))
} else {
Ok((vec![], vec![]))
}
},
|ctx| {
let (issues, diags) = ctx.value;
Expand Down
10 changes: 7 additions & 3 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,22 +812,23 @@ impl Project {
#[turbo_tasks::function]
pub fn server_changed(self: Vc<Self>, roots: Vc<OutputAssets>) -> Vc<Completion> {
let path = self.node_root();
any_output_changed(roots, path)
any_output_changed(roots, path, true)
}

/// Completion when client side changes are detected in output assets
/// referenced from the roots
#[turbo_tasks::function]
pub fn client_changed(self: Vc<Self>, roots: Vc<OutputAssets>) -> Vc<Completion> {
let path = self.client_root();
any_output_changed(roots, path)
any_output_changed(roots, path, false)
}
}

#[turbo_tasks::function]
async fn any_output_changed(
roots: Vc<OutputAssets>,
path: Vc<FileSystemPath>,
server: bool,
) -> Result<Vc<Completion>> {
let path = &path.await?;
let completions = AdjacencyMap::new()
Expand All @@ -839,7 +840,10 @@ async fn any_output_changed(
.into_reverse_topological()
.map(|m| async move {
let asset_path = m.ident().path().await?;
if !asset_path.path.ends_with(".map") && asset_path.is_inside_ref(path) {
if !asset_path.path.ends_with(".map")
&& (!server || !asset_path.path.ends_with(".css"))
&& asset_path.is_inside_ref(path)
{
Ok(Some(content_changed(Vc::upcast(m))))
} else {
Ok(None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ pub async fn get_client_module_options_context(
// we try resolve it once at the root and pass down a context to all
// the modules.
enable_jsx: Some(jsx_runtime_options),
enable_postcss_transform: postcss_transform_options,
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down
18 changes: 12 additions & 6 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub async fn get_server_module_options_context(

let foreign_code_context_condition =
foreign_code_context_condition(next_config, project_path).await?;
let enable_postcss_transform = Some(PostCssTransformOptions {
let postcss_transform_options = Some(PostCssTransformOptions {
postcss_package: Some(get_postcss_package_mapping(project_path)),
..Default::default()
});
Expand Down Expand Up @@ -351,6 +351,8 @@ pub async fn get_server_module_options_context(
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: postcss_transform_options.clone(),
..module_options_context.clone()
};

Expand All @@ -363,8 +365,8 @@ pub async fn get_server_module_options_context(

ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down Expand Up @@ -416,6 +418,8 @@ pub async fn get_server_module_options_context(
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: postcss_transform_options.clone(),
..module_options_context.clone()
};
let internal_module_options_context = ModuleOptionsContext {
Expand All @@ -436,8 +440,8 @@ pub async fn get_server_module_options_context(

ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down Expand Up @@ -498,6 +502,8 @@ pub async fn get_server_module_options_context(
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
// NOTE(WEB-1016) PostCSS transforms should also apply to foreign code.
enable_postcss_transform: postcss_transform_options.clone(),
..module_options_context.clone()
};
let internal_module_options_context = ModuleOptionsContext {
Expand All @@ -507,8 +513,8 @@ pub async fn get_server_module_options_context(
};
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down Expand Up @@ -538,8 +544,8 @@ pub async fn get_server_module_options_context(
..module_options_context.clone()
};
ModuleOptionsContext {
enable_postcss_transform,
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down Expand Up @@ -587,8 +593,8 @@ pub async fn get_server_module_options_context(
};
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_postcss_transform: postcss_transform_options,
enable_typescript_transform: Some(tsconfig),
enable_mdx_rs,
decorators: Some(decorators_options),
Expand Down
9 changes: 7 additions & 2 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,9 @@ export interface Endpoint {
* After serverChanged() has been awaited it will listen to changes.
* The async iterator will yield for each change.
*/
serverChanged(): Promise<AsyncIterableIterator<TurbopackResult>>
serverChanged(
includeIssues: boolean
): Promise<AsyncIterableIterator<TurbopackResult>>
}

interface EndpointConfig {
Expand Down Expand Up @@ -947,12 +949,15 @@ function bindingToApi(binding: any, _wasm: boolean) {
return clientSubscription
}

async serverChanged(): Promise<AsyncIterableIterator<TurbopackResult<{}>>> {
async serverChanged(
includeIssues: boolean
): Promise<AsyncIterableIterator<TurbopackResult<{}>>> {
const serverSubscription = subscribe<TurbopackResult>(
false,
async (callback) =>
binding.endpointServerChangedSubscribe(
await this._nativeEndpoint,
includeIssues,
callback
)
)
Expand Down
23 changes: 18 additions & 5 deletions packages/next/src/server/lib/router-utils/setup-dev-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ async function startWatcher(opts: SetupOpts) {
async function changeSubscription(
page: string,
type: 'client' | 'server',
includeIssues: boolean,
endpoint: Endpoint | undefined,
makePayload: (
page: string,
Expand All @@ -643,7 +644,7 @@ async function startWatcher(opts: SetupOpts) {
const key = `${page} (${type})`
if (!endpoint || changeSubscriptions.has(key)) return

const changedPromise = endpoint[`${type}Changed`]()
const changedPromise = endpoint[`${type}Changed`](includeIssues)
changeSubscriptions.set(key, changedPromise)
const changed = await changedPromise

Expand Down Expand Up @@ -1105,6 +1106,7 @@ async function startWatcher(opts: SetupOpts) {
changeSubscription(
'middleware',
'server',
false,
middleware.endpoint,
async () => {
const finishBuilding = startBuilding('middleware', true)
Expand Down Expand Up @@ -1327,6 +1329,7 @@ async function startWatcher(opts: SetupOpts) {
changeSubscription(
'_document',
'server',
false,
globalEntries.document,
() => {
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
Expand Down Expand Up @@ -1425,6 +1428,7 @@ async function startWatcher(opts: SetupOpts) {
changeSubscription(
'_document',
'server',
false,
globalEntries.document,
() => {
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
Expand Down Expand Up @@ -1461,19 +1465,27 @@ async function startWatcher(opts: SetupOpts) {
changeSubscription(
page,
'server',
false,
route.dataEndpoint,
(pageName) => {
console.log('server change', pageName)
return {
event: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ONLY_CHANGES,
pages: [pageName],
}
}
)
changeSubscription(page, 'client', route.htmlEndpoint, () => {
return {
event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
changeSubscription(
page,
'client',
false,
route.htmlEndpoint,
() => {
return {
event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
}
}
})
)
}

break
Expand Down Expand Up @@ -1517,6 +1529,7 @@ async function startWatcher(opts: SetupOpts) {
changeSubscription(
page,
'server',
true,
route.rscEndpoint,
(_page, change) => {
if (
Expand Down
8 changes: 6 additions & 2 deletions test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,16 @@ describe('next.rs api', () => {
switch (route.type) {
case 'page': {
await route.htmlEndpoint.writeToDisk()
serverSideSubscription = await route.dataEndpoint.serverChanged()
serverSideSubscription = await route.dataEndpoint.serverChanged(
false
)
break
}
case 'app-page': {
await route.htmlEndpoint.writeToDisk()
serverSideSubscription = await route.rscEndpoint.serverChanged()
serverSideSubscription = await route.rscEndpoint.serverChanged(
false
)
break
}
default: {
Expand Down
Loading