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

Turbopack: Allow Pages entrypoint template to import user's .ts files #55296

Merged
merged 3 commits into from Sep 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-core/src/middleware.rs
Expand Up @@ -29,7 +29,7 @@ pub async fn get_middleware_module(
project_root: Vc<FileSystemPath>,
userland_module: Vc<Box<dyn Module>>,
) -> Result<Vc<Box<dyn Module>>> {
let template_file = "build/templates/middleware.js";
let template_file = "middleware.js";

// Load the file from the next.js codebase.
let file = load_next_js_template(project_root, template_file.to_string()).await?;
Expand Down
Expand Up @@ -70,7 +70,7 @@ pub async fn get_app_page_entry(
let original_name = page.to_string();
let pathname = AppPath::from(page.clone()).to_string();

let template_file = "build/templates/app-page.js";
let template_file = "app-page.js";

// Load the file from the next.js codebase.
let file = load_next_js_template(project_root, template_file.to_string()).await?;
Expand Down
Expand Up @@ -56,7 +56,7 @@ pub async fn get_app_route_entry(

let path = source.ident().path();

let template_file = "build/templates/app-route.js";
let template_file = "app-route.js";

// Load the file from the next.js codebase.
let file = load_next_js_template(project_root, template_file.to_string()).await?;
Expand Down
Expand Up @@ -159,7 +159,7 @@ pub async fn get_client_resolve_options_context(
enable_typescript: true,
enable_react: true,
rules: vec![(
foreign_code_context_condition(next_config).await?,
foreign_code_context_condition(next_config, project_path).await?,
module_options_context.clone().cell(),
)],
..module_options_context
Expand Down Expand Up @@ -256,7 +256,7 @@ pub async fn get_client_module_options_context(
decorators: Some(decorators_options),
rules: vec![
(
foreign_code_context_condition(next_config).await?,
foreign_code_context_condition(next_config, project_path).await?,
module_options_context.clone().cell(),
),
// If the module is an internal asset (i.e overlay, fallback) coming from the embedded
Expand Down
Expand Up @@ -125,7 +125,7 @@ pub async fn get_edge_resolve_options_context(
enable_typescript: true,
enable_react: true,
rules: vec![(
foreign_code_context_condition(next_config).await?,
foreign_code_context_condition(next_config, project_path).await?,
resolve_options_context.clone().cell(),
)],
..resolve_options_context
Expand Down
Expand Up @@ -46,15 +46,15 @@ pub async fn create_page_ssr_entry_module(
let template_file = match (&reference_type, runtime) {
(ReferenceType::Entry(EntryReferenceSubType::Page), _) => {
// Load the Page entry file.
"build/templates/pages.js"
"pages.js"
}
(ReferenceType::Entry(EntryReferenceSubType::PagesApi), NextRuntime::NodeJs) => {
// Load the Pages API entry file.
"build/templates/pages-api.js"
"pages-api.js"
}
(ReferenceType::Entry(EntryReferenceSubType::PagesApi), NextRuntime::Edge) => {
// Load the Pages API entry file.
"build/templates/pages-edge-api.js"
"pages-edge-api.js"
}
_ => bail!("Invalid path type"),
};
Expand Down
Expand Up @@ -98,7 +98,8 @@ pub async fn get_server_resolve_options_context(
) -> Result<Vc<ResolveOptionsContext>> {
let next_server_import_map =
get_next_server_import_map(project_path, ty, mode, next_config, execution_context);
let foreign_code_context_condition = foreign_code_context_condition(next_config).await?;
let foreign_code_context_condition =
foreign_code_context_condition(next_config, project_path).await?;
let root_dir = project_path.root().resolve().await?;
let module_feature_report_resolve_plugin = ModuleFeatureReportResolvePlugin::new(project_path);
let unsupported_modules_resolve_plugin = UnsupportedModulesResolvePlugin::new(project_path);
Expand Down Expand Up @@ -215,7 +216,8 @@ pub async fn get_server_module_options_context(
let custom_rules = get_next_server_transforms_rules(next_config, ty.into_value(), mode).await?;
let internal_custom_rules = get_next_server_internal_transforms_rules(ty.into_value()).await?;

let foreign_code_context_condition = foreign_code_context_condition(next_config).await?;
let foreign_code_context_condition =
foreign_code_context_condition(next_config, project_path).await?;
let enable_postcss_transform = Some(PostCssTransformOptions {
postcss_package: Some(get_postcss_package_mapping(project_path)),
..Default::default()
Expand Down
31 changes: 22 additions & 9 deletions packages/next-swc/crates/next-core/src/util.rs
Expand Up @@ -27,6 +27,8 @@ use crate::{
next_import_map::get_next_package,
};

const NEXT_TEMPLATE_PATH: &str = "dist/esm/build/templates";

#[derive(Debug, Clone, Copy, PartialEq, Eq, TaskInput)]
pub enum PathType {
PagesPage,
Expand Down Expand Up @@ -82,13 +84,27 @@ pub fn get_asset_path_from_pathname(pathname: &str, ext: &str) -> String {

pub async fn foreign_code_context_condition(
next_config: Vc<NextConfig>,
project_path: Vc<FileSystemPath>,
) -> Result<ContextCondition> {
let transpile_packages = next_config.transpile_packages().await?;

// The next template files are allowed to import the user's code via import
// mapping, and imports must use the project-level [ResolveOptions] instead
// of the `node_modules` specific resolve options (the template files are
// technically node module files).
let not_next_template_dir = ContextCondition::not(ContextCondition::InPath(
get_next_package(project_path).join(NEXT_TEMPLATE_PATH.to_string()),
));

let result = if transpile_packages.is_empty() {
ContextCondition::InDirectory("node_modules".to_string())
ContextCondition::all(vec![
ContextCondition::InDirectory("node_modules".to_string()),
not_next_template_dir,
])
} else {
ContextCondition::all(vec![
ContextCondition::InDirectory("node_modules".to_string()),
not_next_template_dir,
ContextCondition::not(ContextCondition::any(
transpile_packages
.iter()
Expand Down Expand Up @@ -334,11 +350,9 @@ fn parse_config_from_js_value(module: Vc<Box<dyn Module>>, value: &JsValue) -> N
#[turbo_tasks::function]
pub async fn load_next_js_template(
project_path: Vc<FileSystemPath>,
path: String,
file: String,
) -> Result<Vc<Rope>> {
let file_path = get_next_package(project_path)
.join("dist/esm".to_string())
.join(path);
let file_path = virtual_next_js_template_path(project_path, file);

let content = &*file_path.read().await?;

Expand All @@ -352,11 +366,10 @@ pub async fn load_next_js_template(
#[turbo_tasks::function]
pub fn virtual_next_js_template_path(
project_path: Vc<FileSystemPath>,
path: String,
file: String,
) -> Vc<FileSystemPath> {
get_next_package(project_path)
.join("dist/esm".to_string())
.join(path)
debug_assert!(!file.contains('/'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this intended assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so that we don't accidentally try to import esm/build/templates/build/templates/pages.ts

get_next_package(project_path).join(format!("{NEXT_TEMPLATE_PATH}/{file}"))
}

pub async fn load_next_js_templateon<T: DeserializeOwned>(
Expand Down