Skip to content

Commit

Permalink
fix(custom-transform): allow to assert empty program for rsc
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Feb 12, 2024
1 parent 5437fda commit c321197
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, path::PathBuf};

use regex::Regex;
use serde::Deserialize;
use swc_core::ecma::visit::VisitWith;
use swc_core::{common::util::take::Take, ecma::visit::VisitWith};
use turbopack_binding::swc::core::{
common::{
comments::{Comment, CommentKind, Comments},
Expand Down Expand Up @@ -576,6 +576,7 @@ impl ReactServerComponentValidator {
let is_error_file = Regex::new(r"[\\/]error\.(ts|js)x?$")
.unwrap()
.is_match(&self.filepath);

if is_error_file {
if let Some(app_dir) = &self.app_dir {
if let Some(app_dir) = app_dir.to_str() {
Expand Down Expand Up @@ -727,6 +728,15 @@ impl ReactServerComponentValidator {
impl Visit for ReactServerComponentValidator {
noop_visit_type!();

// Workaround for swc's parse_program, where it detects empty file as a script.
// we may fix upstream to autodetect empty file as a module
fn visit_script(&mut self, script: &swc_core::ecma::ast::Script) {
if script.body.is_empty() {
// run validation for the context, which is still a valid when file is empty
self.visit_module(&Module::dummy());
}
}

fn visit_module(&mut self, module: &Module) {
let (is_client_entry, is_action_file, imports, export_names) =
collect_top_level_directives_and_imports(&self.app_dir, &self.filepath, module);
Expand Down

0 comments on commit c321197

Please sign in to comment.