Unverified Commit 8ee776dd authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add support for esmExternals in pages (#61983)

### What?

* add support for esmExternals in pages
* fix default of esmExternals to true
* fix serverComponentExternalPackages support
* add test case

fixes PACK-2311
fixes PACK-1916

Fixes https://github.com/vercel/turbo/issues/4544
parent 895e7df8
This diff is collapsed.
...@@ -37,11 +37,11 @@ swc_core = { version = "0.90.8", features = [ ...@@ -37,11 +37,11 @@ swc_core = { version = "0.90.8", features = [
testing = { version = "0.35.18" } testing = { version = "0.35.18" }
# Turbo crates # Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240215.5" } turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240216.2" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros.. # [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-240215.5" } turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240216.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core # [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240215.5" } turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240216.2" }
# General Deps # General Deps
......
use anyhow::Result; use anyhow::Result;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::FileSystemPath, turbopack::core::resolve::options::ImportMapping, turbo::tasks_fs::FileSystemPath,
turbopack::core::resolve::{options::ImportMapping, ExternalType},
}; };
use crate::next_import_map::get_next_package; use crate::next_import_map::get_next_package;
...@@ -26,12 +27,10 @@ pub async fn get_postcss_package_mapping( ...@@ -26,12 +27,10 @@ pub async fn get_postcss_package_mapping(
pub async fn get_external_next_compiled_package_mapping( pub async fn get_external_next_compiled_package_mapping(
package_name: Vc<String>, package_name: Vc<String>,
) -> Result<Vc<ImportMapping>> { ) -> Result<Vc<ImportMapping>> {
Ok( Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
ImportMapping::Alternatives(vec![ImportMapping::External(Some(format!( Some(format!("next/dist/compiled/{}", &*package_name.await?)),
"next/dist/compiled/{}", ExternalType::CommonJs,
&*package_name.await?
)))
.into()])
.cell(),
) )
.into()])
.cell())
} }
use anyhow::{Context, Result}; use anyhow::{bail, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
...@@ -20,7 +20,7 @@ use turbopack_binding::{ ...@@ -20,7 +20,7 @@ use turbopack_binding::{
resolve::{ resolve::{
find_context_file, find_context_file,
options::{ImportMap, ImportMapping}, options::{ImportMap, ImportMapping},
FindContextFileResult, ResolveAliasMap, ExternalType, FindContextFileResult, ResolveAliasMap,
}, },
source::Source, source::Source,
}, },
...@@ -480,7 +480,7 @@ pub struct ExperimentalConfig { ...@@ -480,7 +480,7 @@ pub struct ExperimentalConfig {
cra_compat: Option<bool>, cra_compat: Option<bool>,
disable_optimized_loading: Option<bool>, disable_optimized_loading: Option<bool>,
disable_postcss_preset_env: Option<bool>, disable_postcss_preset_env: Option<bool>,
esm_externals: Option<serde_json::Value>, esm_externals: Option<EsmExternals>,
extension_alias: Option<serde_json::Value>, extension_alias: Option<serde_json::Value>,
external_dir: Option<bool>, external_dir: Option<bool>,
/// If set to `false`, webpack won't fall back to polyfill Node.js modules /// If set to `false`, webpack won't fall back to polyfill Node.js modules
...@@ -546,6 +546,19 @@ pub enum ServerActionsOrLegacyBool { ...@@ -546,6 +546,19 @@ pub enum ServerActionsOrLegacyBool {
LegacyBool(bool), LegacyBool(bool),
} }
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, TraceRawVcs)]
#[serde(rename_all = "kebab-case")]
pub enum EsmExternalsValue {
Loose,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, TraceRawVcs)]
#[serde(untagged)]
pub enum EsmExternals {
Loose(EsmExternalsValue),
Bool(bool),
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, TraceRawVcs)] #[derive(Clone, Debug, PartialEq, Deserialize, Serialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ServerActions { pub struct ServerActions {
...@@ -757,6 +770,15 @@ impl NextConfig { ...@@ -757,6 +770,15 @@ impl NextConfig {
Ok(alias_map.cell()) Ok(alias_map.cell())
} }
#[turbo_tasks::function]
pub async fn import_externals(self: Vc<Self>) -> Result<Vc<bool>> {
Ok(Vc::cell(match self.await?.experimental.esm_externals {
Some(EsmExternals::Bool(b)) => b,
Some(EsmExternals::Loose(_)) => bail!("esmExternals = \"loose\" is not supported"),
None => true,
}))
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn mdx_rs(self: Vc<Self>) -> Result<Vc<bool>> { pub async fn mdx_rs(self: Vc<Self>) -> Result<Vc<bool>> {
Ok(Vc::cell(self.await?.experimental.mdx_rs.unwrap_or(false))) Ok(Vc::cell(self.await?.experimental.mdx_rs.unwrap_or(false)))
...@@ -878,14 +900,30 @@ async fn load_next_config_and_custom_routes_internal( ...@@ -878,14 +900,30 @@ async fn load_next_config_and_custom_routes_internal(
} = *execution_context.await?; } = *execution_context.await?;
let mut import_map = ImportMap::default(); let mut import_map = ImportMap::default();
import_map.insert_exact_alias("next", ImportMapping::External(None).into()); import_map.insert_exact_alias(
import_map.insert_wildcard_alias("next/", ImportMapping::External(None).into()); "next",
import_map.insert_exact_alias("styled-jsx", ImportMapping::External(None).into()); ImportMapping::External(None, ExternalType::CommonJs).into(),
);
import_map.insert_wildcard_alias(
"next/",
ImportMapping::External(None, ExternalType::CommonJs).into(),
);
import_map.insert_exact_alias(
"styled-jsx",
ImportMapping::External(None, ExternalType::CommonJs).into(),
);
import_map.insert_exact_alias( import_map.insert_exact_alias(
"styled-jsx/style", "styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".to_string())).cell(), ImportMapping::External(
Some("styled-jsx/style.js".to_string()),
ExternalType::CommonJs,
)
.cell(),
);
import_map.insert_wildcard_alias(
"styled-jsx/",
ImportMapping::External(None, ExternalType::CommonJs).into(),
); );
import_map.insert_wildcard_alias("styled-jsx/", ImportMapping::External(None).into());
let context = node_evaluate_asset_context( let context = node_evaluate_asset_context(
execution_context, execution_context,
......
...@@ -12,7 +12,7 @@ use turbopack_binding::{ ...@@ -12,7 +12,7 @@ use turbopack_binding::{
options::{ConditionValue, ImportMap, ImportMapping, ResolveOptions, ResolvedMap}, options::{ConditionValue, ImportMap, ImportMapping, ResolveOptions, ResolvedMap},
parse::Request, parse::Request,
pattern::Pattern, pattern::Pattern,
resolve, AliasPattern, ResolveAliasMap, SubpathValue, resolve, AliasPattern, ExternalType, ResolveAliasMap, SubpathValue,
}, },
source::Source, source::Source,
}, },
...@@ -193,14 +193,18 @@ pub fn get_next_build_import_map() -> Vc<ImportMap> { ...@@ -193,14 +193,18 @@ pub fn get_next_build_import_map() -> Vc<ImportMap> {
next_js_fs().root(), next_js_fs().root(),
); );
let external = ImportMapping::External(None).cell(); let external = ImportMapping::External(None, ExternalType::CommonJs).cell();
import_map.insert_exact_alias("next", external); import_map.insert_exact_alias("next", external);
import_map.insert_wildcard_alias("next/", external); import_map.insert_wildcard_alias("next/", external);
import_map.insert_exact_alias("styled-jsx", external); import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias( import_map.insert_exact_alias(
"styled-jsx/style", "styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".to_string())).cell(), ImportMapping::External(
Some("styled-jsx/style.js".to_string()),
ExternalType::CommonJs,
)
.cell(),
); );
import_map.insert_wildcard_alias("styled-jsx/", external); import_map.insert_wildcard_alias("styled-jsx/", external);
...@@ -263,7 +267,7 @@ pub async fn get_next_server_import_map( ...@@ -263,7 +267,7 @@ pub async fn get_next_server_import_map(
let ty = ty.into_value(); let ty = ty.into_value();
let external: Vc<ImportMapping> = ImportMapping::External(None).cell(); let external: Vc<ImportMapping> = ImportMapping::External(None, ExternalType::CommonJs).cell();
import_map.insert_exact_alias("next/dist/server/require-hook", external); import_map.insert_exact_alias("next/dist/server/require-hook", external);
match ty { match ty {
...@@ -277,7 +281,11 @@ pub async fn get_next_server_import_map( ...@@ -277,7 +281,11 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("styled-jsx", external); import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias( import_map.insert_exact_alias(
"styled-jsx/style", "styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".to_string())).cell(), ImportMapping::External(
Some("styled-jsx/style.js".to_string()),
ExternalType::CommonJs,
)
.cell(),
); );
import_map.insert_wildcard_alias("styled-jsx/", external); import_map.insert_wildcard_alias("styled-jsx/", external);
// TODO: we should not bundle next/dist/build/utils in the pages renderer at all // TODO: we should not bundle next/dist/build/utils in the pages renderer at all
...@@ -964,5 +972,5 @@ fn request_to_import_mapping(context_path: Vc<FileSystemPath>, request: &str) -> ...@@ -964,5 +972,5 @@ fn request_to_import_mapping(context_path: Vc<FileSystemPath>, request: &str) ->
/// Creates a direct import mapping to the result of resolving an external /// Creates a direct import mapping to the result of resolving an external
/// request. /// request.
fn external_request_to_import_mapping(request: &str) -> Vc<ImportMapping> { fn external_request_to_import_mapping(request: &str) -> Vc<ImportMapping> {
ImportMapping::External(Some(request.to_string())).into() ImportMapping::External(Some(request.to_string()), ExternalType::CommonJs).into()
} }
...@@ -156,8 +156,7 @@ pub async fn get_server_resolve_options_context( ...@@ -156,8 +156,7 @@ pub async fn get_server_resolve_options_context(
project_path, project_path,
project_path.root(), project_path.root(),
ExternalPredicate::AllExcept(next_config.transpile_packages()).cell(), ExternalPredicate::AllExcept(next_config.transpile_packages()).cell(),
// TODO(sokra) esmExternals support *next_config.import_externals().await?,
false,
); );
let next_external_plugin = NextExternalResolvePlugin::new(project_path); let next_external_plugin = NextExternalResolvePlugin::new(project_path);
...@@ -441,6 +440,7 @@ pub async fn get_server_module_options_context( ...@@ -441,6 +440,7 @@ pub async fn get_server_module_options_context(
esm_url_rewrite_behavior: url_rewrite_behavior, esm_url_rewrite_behavior: url_rewrite_behavior,
use_lightningcss, use_lightningcss,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default() ..Default::default()
}; };
...@@ -502,6 +502,7 @@ pub async fn get_server_module_options_context( ...@@ -502,6 +502,7 @@ pub async fn get_server_module_options_context(
execution_context: Some(execution_context), execution_context: Some(execution_context),
use_lightningcss, use_lightningcss,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default() ..Default::default()
}; };
...@@ -577,6 +578,7 @@ pub async fn get_server_module_options_context( ...@@ -577,6 +578,7 @@ pub async fn get_server_module_options_context(
execution_context: Some(execution_context), execution_context: Some(execution_context),
use_lightningcss, use_lightningcss,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default() ..Default::default()
}; };
...@@ -620,6 +622,7 @@ pub async fn get_server_module_options_context( ...@@ -620,6 +622,7 @@ pub async fn get_server_module_options_context(
let module_options_context = ModuleOptionsContext { let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context), execution_context: Some(execution_context),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default() ..Default::default()
}; };
let foreign_code_module_options_context = ModuleOptionsContext { let foreign_code_module_options_context = ModuleOptionsContext {
...@@ -667,6 +670,7 @@ pub async fn get_server_module_options_context( ...@@ -667,6 +670,7 @@ pub async fn get_server_module_options_context(
let module_options_context = ModuleOptionsContext { let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context), execution_context: Some(execution_context),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default() ..Default::default()
}; };
let foreign_code_module_options_context = ModuleOptionsContext { let foreign_code_module_options_context = ModuleOptionsContext {
...@@ -708,16 +712,6 @@ pub async fn get_server_module_options_context( ...@@ -708,16 +712,6 @@ pub async fn get_server_module_options_context(
Ok(module_options_context) Ok(module_options_context)
} }
#[turbo_tasks::function]
pub fn get_build_module_options_context() -> Vc<ModuleOptionsContext> {
ModuleOptionsContext {
enable_typescript_transform: Some(Default::default()),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
..Default::default()
}
.cell()
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn get_server_runtime_entries( pub fn get_server_runtime_entries(
_ty: Value<ServerContextType>, _ty: Value<ServerContextType>,
......
use anyhow::Result; use anyhow::Result;
use turbo_tasks::{Value, Vc}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, Value, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{glob::Glob, FileJsonContent, FileSystemPath}, turbo::tasks_fs::{glob::Glob, FileJsonContent, FileSystemPath},
turbopack::core::{ turbopack::core::{
...@@ -10,7 +11,8 @@ use turbopack_binding::{ ...@@ -10,7 +11,8 @@ use turbopack_binding::{
package_json, package_json,
parse::Request, parse::Request,
plugin::{ResolvePlugin, ResolvePluginCondition}, plugin::{ResolvePlugin, ResolvePluginCondition},
resolve, FindContextFileResult, ResolveResult, ResolveResultItem, ResolveResultOption, resolve, ExternalType, FindContextFileResult, ResolveResult, ResolveResultItem,
ResolveResultOption,
}, },
source::Source, source::Source,
}, },
...@@ -88,9 +90,12 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -88,9 +90,12 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
// from https://github.com/vercel/next.js/blob/8d1c619ad650f5d147207f267441caf12acd91d1/packages/next/src/build/handle-externals.ts#L188 // from https://github.com/vercel/next.js/blob/8d1c619ad650f5d147207f267441caf12acd91d1/packages/next/src/build/handle-externals.ts#L188
let never_external_regex = lazy_regex::regex!("^(?:private-next-pages\\/|next\\/(?:dist\\/pages\\/|(?:app|document|link|image|legacy\\/image|constants|dynamic|script|navigation|headers|router)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference$)"); let never_external_regex = lazy_regex::regex!("^(?:private-next-pages\\/|next\\/(?:dist\\/pages\\/|(?:app|document|link|image|legacy\\/image|constants|dynamic|script|navigation|headers|router)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference$)");
if never_external_regex.is_match(&request_value.request().unwrap_or_default()) { let request_str = request_value.request();
if let Some(request_str) = &request_str {
if never_external_regex.is_match(request_str) {
return Ok(ResolveResultOption::none()); return Ok(ResolveResultOption::none());
} }
}
let raw_fs_path = &*fs_path.await?; let raw_fs_path = &*fs_path.await?;
...@@ -99,8 +104,18 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -99,8 +104,18 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
ExternalPredicate::AllExcept(exceptions) => { ExternalPredicate::AllExcept(exceptions) => {
let exception_glob = packages_glob(*exceptions).await?; let exception_glob = packages_glob(*exceptions).await?;
if let Some(exception_glob) = *exception_glob { if let Some(PackagesGlobs {
if exception_glob.await?.execute(&raw_fs_path.path) { path_glob,
request_glob,
}) = *exception_glob
{
let path_match = path_glob.await?.execute(&raw_fs_path.path);
let request_match = if let Some(request_str) = &request_str {
request_glob.await?.execute(request_str)
} else {
false
};
if path_match || request_match {
return Ok(ResolveResultOption::none()); return Ok(ResolveResultOption::none());
} }
} }
...@@ -108,8 +123,19 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -108,8 +123,19 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
ExternalPredicate::Only(externals) => { ExternalPredicate::Only(externals) => {
let external_glob = packages_glob(*externals).await?; let external_glob = packages_glob(*externals).await?;
if let Some(external_glob) = *external_glob { if let Some(PackagesGlobs {
if !external_glob.await?.execute(&raw_fs_path.path) { path_glob,
request_glob,
}) = *external_glob
{
let path_match = path_glob.await?.execute(&raw_fs_path.path);
let request_match = if let Some(request_str) = &request_str {
request_glob.await?.execute(request_str)
} else {
false
};
if !path_match && !request_match {
return Ok(ResolveResultOption::none()); return Ok(ResolveResultOption::none());
} }
} else { } else {
...@@ -122,6 +148,7 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -122,6 +148,7 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
&& ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::Undefined) && ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::Undefined)
.includes(&reference_type); .includes(&reference_type);
#[derive(Debug, Copy, Clone)]
enum FileType { enum FileType {
CommonJs, CommonJs,
EcmaScriptModule, EcmaScriptModule,
...@@ -187,12 +214,28 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -187,12 +214,28 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
// unsupported file type, bundle it // unsupported file type, bundle it
Ok(ResolveResultOption::none()) Ok(ResolveResultOption::none())
} }
(FileType::CommonJs, _) | (FileType::EcmaScriptModule, true) => { (FileType::CommonJs, _) => {
if let Some(request) = request.await?.request() {
// mark as external
Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::External(
request,
ExternalType::CommonJs,
))
.cell(),
))
} else {
// unsupported request, bundle it
Ok(ResolveResultOption::none())
}
}
(FileType::EcmaScriptModule, true) => {
if let Some(request) = request.await?.request() { if let Some(request) = request.await?.request() {
// mark as external // mark as external
Ok(ResolveResultOption::some( Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::OriginalReferenceTypeExternal( ResolveResult::primary(ResolveResultItem::External(
request, request,
ExternalType::EcmaScriptModule,
)) ))
.cell(), .cell(),
)) ))
...@@ -210,19 +253,30 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -210,19 +253,30 @@ impl ResolvePlugin for ExternalCjsModulesResolvePlugin {
} }
} }
#[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, Debug)]
pub struct PackagesGlobs {
path_glob: Vc<Glob>,
request_glob: Vc<Glob>,
}
// TODO move that to turbo // TODO move that to turbo
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
pub struct OptionGlob(Option<Vc<Glob>>); pub struct OptionPackagesGlobs(Option<PackagesGlobs>);
#[turbo_tasks::function] #[turbo_tasks::function]
async fn packages_glob(packages: Vc<Vec<String>>) -> Result<Vc<OptionGlob>> { async fn packages_glob(packages: Vc<Vec<String>>) -> Result<Vc<OptionPackagesGlobs>> {
let packages = packages.await?; let packages = packages.await?;
if packages.is_empty() { if packages.is_empty() {
return Ok(Vc::cell(None)); return Ok(Vc::cell(None));
} }
Ok(Vc::cell(Some( let path_glob = Glob::new(format!("**/node_modules/{{{}}}/**", packages.join(",")));
Glob::new(format!("**/node_modules/{{{}}}/**", packages.join(","))) let request_glob = Glob::new(format!(
.resolve() "{{{},{}/**}}",
.await?, packages.join(","),
))) packages.join("/**,")
));
Ok(Vc::cell(Some(PackagesGlobs {
path_glob: path_glob.resolve().await?,
request_glob: request_glob.resolve().await?,
})))
} }
...@@ -18,7 +18,7 @@ use turbopack_binding::{ ...@@ -18,7 +18,7 @@ use turbopack_binding::{
parse::Request, parse::Request,
pattern::Pattern, pattern::Pattern,
plugin::{ResolvePlugin, ResolvePluginCondition}, plugin::{ResolvePlugin, ResolvePluginCondition},
ResolveResult, ResolveResultItem, ResolveResultOption, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
}, },
}, },
}; };
...@@ -289,8 +289,9 @@ impl ResolvePlugin for NextExternalResolvePlugin { ...@@ -289,8 +289,9 @@ impl ResolvePlugin for NextExternalResolvePlugin {
// Replace '/esm/' with '/' to match the CJS version of the file. // Replace '/esm/' with '/' to match the CJS version of the file.
let modified_path = &path[starting_index..].replace("/esm/", "/"); let modified_path = &path[starting_index..].replace("/esm/", "/");
Ok(Vc::cell(Some( Ok(Vc::cell(Some(
ResolveResult::primary(ResolveResultItem::OriginalReferenceTypeExternal( ResolveResult::primary(ResolveResultItem::External(
modified_path.to_string(), modified_path.to_string(),
ExternalType::CommonJs,
)) ))
.into(), .into(),
))) )))
......
...@@ -192,7 +192,7 @@ ...@@ -192,7 +192,7 @@
"@types/ws": "8.2.0", "@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0", "@vercel/ncc": "0.34.0",
"@vercel/nft": "0.26.4", "@vercel/nft": "0.26.4",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240215.5", "@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240216.2",
"acorn": "8.5.0", "acorn": "8.5.0",
"amphtml-validator": "1.0.35", "amphtml-validator": "1.0.35",
"anser": "1.4.9", "anser": "1.4.9",
......
...@@ -1074,8 +1074,8 @@ importers: ...@@ -1074,8 +1074,8 @@ importers:
specifier: 0.26.4 specifier: 0.26.4
version: 0.26.4 version: 0.26.4
'@vercel/turbopack-ecmascript-runtime': '@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240215.5 specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240216.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240215.5' version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240216.2'
acorn: acorn:
specifier: 8.5.0 specifier: 8.5.0
version: 8.5.0 version: 8.5.0
...@@ -25626,8 +25626,8 @@ packages: ...@@ -25626,8 +25626,8 @@ packages:
/zwitch@2.0.4: /zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240215.5': '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240216.2':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240215.5} resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240216.2}
name: '@vercel/turbopack-ecmascript-runtime' name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0 version: 0.0.0
dependencies: dependencies:
......
'use client'
import World1 from 'app-esm-package1/entry'
import World2 from 'app-esm-package2/entry'
import World3 from 'invalid-esm-package/entry'
export default function Index() {
return (
<div>
Hello {World1}+{World2}+{World3}
</div>
)
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import World1 from 'app-esm-package1/entry'
import World2 from 'app-esm-package2/entry'
import World3 from 'invalid-esm-package/entry'
export default function Page() {
return (
<div>
Hello {World1}+{World2}+{World3}
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('esm-externals', () => {
const { next } = nextTestSetup({
files: __dirname,
})
// Pages
{
const expectedHtml =
/Hello <!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World\+World\+World/
const expectedText = /Hello World\+World\+World\+World\+World\+World/
const urls = ['/static', '/ssr', '/ssg']
for (const url of urls) {
it(`should return the correct SSR HTML for ${url}`, async () => {
const res = await next.fetch(url)
const html = await res.text()
expect(html).toMatch(expectedHtml)
})
it(`should render the correct page for ${url}`, async () => {
const browser = await next.browser(url)
expect(await browser.elementByCss('body').text()).toMatch(expectedText)
})
}
}
// App dir
{
// TODO App Dir doesn't use esmExternals: true correctly for webpack and Turbopack
// so we only verify that the page doesn't crash, but ignore the actual content
// const expectedHtml =
// /Hello <!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World/
const expectedHtml =
/Hello <!-- -->Wrong<!-- -->\+<!-- -->Wrong<!-- -->\+<!-- -->Wrong/
// const expectedText = /Hello World\+World\+World/
const urls = ['/server', '/client']
for (const url of urls) {
const expectedText =
url === '/server'
? /Hello Wrong\+Wrong\+Wrong/
: /Hello World\+World\+World/
it(`should return the correct SSR HTML for ${url}`, async () => {
const res = await next.fetch(url)
const html = await res.text()
expect(html).toMatch(expectedHtml)
})
it(`should render the correct page for ${url}`, async () => {
const browser = await next.browser(url)
expect(await browser.elementByCss('body').text()).toMatch(expectedText)
})
}
}
})
module.exports = { module.exports = {
experimental: {
turbo: {
resolveAlias: {
'preact/compat': 'react',
},
},
serverComponentsExternalPackages: [
'app-esm-package1',
'app-esm-package2',
'app-invalid-esm-package',
],
},
webpack(config, { isServer }) { webpack(config, { isServer }) {
config.resolve.alias = { config.resolve.alias = {
...config.resolve.alias, ...config.resolve.alias,
......
export default 'World'
if (!process.browser) throw new Error('Browser only code in server build')
export default 'World' export default 'World'
// ensure it's external // ensure it's external
if (!process.browser && Math.random() < 0) import('fail') if (Math.random() < 0) import('fail')
{
"name": "app-esm-package",
"exports": {
"./entry": {
"browser": "./browser.mjs",
"import": "./correct.mjs",
"require": "./wrong.js"
}
}
}
export default 'World'
if (!process.browser) throw new Error('Browser only code in server build')
export default 'World'
// TLA to test async modules
await 1
// ensure it's external
if (Math.random() < 0) import('fail')
{
"type": "module",
"name": "app-esm-package",
"exports": {
"./entry": {
"browser": "./browser.mjs",
"import": "./correct.js",
"require": "./wrong.cjs"
}
}
}
export default 'World'
if (!process.browser) throw new Error('Browser only code in server build')
module.exports = 'World' module.exports = 'World'
// ensure it's external // ensure it's external
if (!process.browser && Math.random() < 0) require('fail') if (Math.random() < 0) require('fail')
{
"name": "app-invalid-esm-package",
"exports": {
"./entry": {
"browser": "./browser.js",
"import": "./wrong.js",
"require": "./correct.js"
}
}
}
export default 'World'
if (!process.browser) throw new Error('Browser only code in server build')
export default 'World'
// ensure it's external
if (Math.random() < 0) import('fail')
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"name": "esm-package", "name": "esm-package",
"exports": { "exports": {
"./entry": { "./entry": {
"browser": "./browser.mjs",
"import": "./correct.mjs", "import": "./correct.mjs",
"require": "./wrong.js" "require": "./wrong.js"
} }
......
export default 'World'
if (!process.browser) throw new Error('Browser only code in server build')
export default 'World'
// TLA to test async modules
await 1
// ensure it's external
if (Math.random() < 0) import('fail')
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"name": "esm-package", "name": "esm-package",
"exports": { "exports": {
"./entry": { "./entry": {
"browser": "./browser.mjs",
"import": "./correct.js", "import": "./correct.js",
"require": "./wrong.cjs" "require": "./wrong.cjs"
} }
......
export default 'World'
if (!process.browser) throw new Error('Browser only code in server build')
module.exports = 'World'
// ensure it's external
if (Math.random() < 0) require('fail')
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"name": "invalid-esm-package", "name": "invalid-esm-package",
"exports": { "exports": {
"./entry": { "./entry": {
"browser": "./browser.js",
"import": "./wrong.js", "import": "./wrong.js",
"require": "./correct.js" "require": "./correct.js"
} }
......
export default "World";
// ensure it's external
if(!process.browser && Math.random() < 0) import("fail");
\ No newline at end of file
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import {
nextBuild,
findPort,
nextStart,
killApp,
renderViaHTTP,
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let app
describe('Handle ESM externals with esmExternals: true', () => {
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
const expected =
/Hello <!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World<!-- -->\+<!-- -->World\+World\+World/
it('should render the static page', async () => {
const html = await renderViaHTTP(appPort, '/static')
expect(html).toMatch(expected)
})
it('should render the ssr page', async () => {
const html = await renderViaHTTP(appPort, '/ssr')
expect(html).toMatch(expected)
})
it('should render the ssg page', async () => {
const html = await renderViaHTTP(appPort, '/ssg')
expect(html).toMatch(expected)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment