Unverified Commit 34671167 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fix node externals resolving (#58129)

### What?

Various fixes in handling of externals in node.js

* add extensions to builtin externals to allow node.js ESM to work
* improve the auto-externals logic to detect more edge cases
* prepare for `esmExternals` support, but that's blocked by client
manifest missing the async flag
* currently only `esmExternals: false` is supported

### Why?

### Turbopack Changes

* https://github.com/vercel/turbo/pull/6531 <!-- Leah - fix(turbopack):
postcss should be applied to `@import`ed CSS files -->
* https://github.com/vercel/turbo/pull/6530 <!-- Tobias Koppers - fix
node.js externals -->

---------
Co-authored-by: 's avatarLeah <github.leah@hrmny.sh>
Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
parent 8b11264e
This diff is collapsed.
......@@ -43,11 +43,11 @@ next-transform-strip-page-exports = { path = "packages/next-swc/crates/next-tran
testing = { version = "0.35.10" }
# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231120.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231121.2" }
# [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-231120.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231121.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231120.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231121.2" }
# General Deps
......
......@@ -5,6 +5,7 @@ use turbopack_binding::{
turbopack::{
core::{
issue::{Issue, IssueExt, IssueSeverity, StyledString},
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::{parse::Request, pattern::Pattern, resolve},
},
node::transforms::webpack::WebpackLoaderItem,
......@@ -122,6 +123,7 @@ pub async fn maybe_add_babel_loader(
pub async fn is_babel_loader_available(project_path: Vc<FileSystemPath>) -> Result<Vc<bool>> {
let result = resolve(
project_path,
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
Request::parse(Value::new(Pattern::Constant(
"babel-loader/package.json".to_string(),
))),
......
......@@ -832,6 +832,10 @@ async fn load_next_config_and_custom_routes_internal(
import_map.insert_exact_alias("next", ImportMapping::External(None).into());
import_map.insert_wildcard_alias("next/", ImportMapping::External(None).into());
import_map.insert_exact_alias("styled-jsx", ImportMapping::External(None).into());
import_map.insert_exact_alias(
"styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".to_string())).cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", ImportMapping::External(None).into());
let context = node_evaluate_asset_context(
......
......@@ -7,6 +7,7 @@ use turbopack_binding::{
turbo::tasks_fs::{glob::Glob, FileSystem, FileSystemPath},
turbopack::{
core::{
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::{
options::{ConditionValue, ImportMap, ImportMapping, ResolveOptions, ResolvedMap},
parse::Request,
......@@ -219,6 +220,10 @@ pub fn get_next_build_import_map() -> Vc<ImportMap> {
import_map.insert_exact_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/style",
ImportMapping::External(Some("styled-jsx/style.js".to_string())).cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", external);
import_map.cell()
......@@ -293,6 +298,10 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("react-dom", external);
import_map.insert_wildcard_alias("react-dom/", external);
import_map.insert_exact_alias("styled-jsx", external);
import_map.insert_exact_alias(
"styled-jsx/style",
ImportMapping::External(Some("styled-jsx/style.js".to_string())).cell(),
);
import_map.insert_wildcard_alias("styled-jsx/", external);
// TODO: we should not bundle next/dist/build/utils in the pages renderer at all
import_map.insert_wildcard_alias("next/dist/build/utils", external);
......@@ -508,7 +517,7 @@ async fn insert_next_server_special_aliases(
"@opentelemetry/api",
// TODO(WEB-625) this actually need to prefer the local version of
// @opentelemetry/api
external_if_node(pages_dir, "next/dist/compiled/@opentelemetry/api"),
external_if_node(pages_dir, "next/dist/compiled/@opentelemetry/api/index.js"),
);
insert_alias_to_alternatives(
import_map,
......@@ -544,7 +553,10 @@ async fn insert_next_server_special_aliases(
"@opentelemetry/api",
// TODO(WEB-625) this actually need to prefer the local version of
// @opentelemetry/api
request_to_import_mapping(app_dir, "next/dist/compiled/@opentelemetry/api"),
request_to_import_mapping(
app_dir,
"next/dist/compiled/@opentelemetry/api/index.js",
),
);
import_map.insert_exact_alias(
"styled-jsx",
......@@ -880,6 +892,7 @@ async fn package_lookup_resolve_options(
pub async fn get_next_package(context_directory: Vc<FileSystemPath>) -> Result<Vc<FileSystemPath>> {
let result = resolve(
context_directory,
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
Request::parse(Value::new(Pattern::Constant(
"next/package.json".to_string(),
))),
......
......@@ -126,6 +126,8 @@ pub async fn get_server_resolve_options_context(
project_path,
project_path.root(),
ExternalPredicate::Only(Vc::cell(external_packages)).cell(),
// TODO(sokra) esmExternals support
false,
);
let ty = ty.into_value();
......@@ -144,6 +146,8 @@ pub async fn get_server_resolve_options_context(
project_path,
project_path.root(),
ExternalPredicate::AllExcept(next_config.transpile_packages()).cell(),
// TODO(sokra) esmExternals support
false,
);
let next_external_plugin = NextExternalResolvePlugin::new(project_path);
......
......@@ -10,6 +10,7 @@ use turbopack_binding::{
diagnostics::DiagnosticExt,
file_source::FileSource,
issue::{unsupported_module::UnsupportedModuleIssue, IssueExt},
reference_type::ReferenceType,
resolve::{
parse::Request,
pattern::Pattern,
......@@ -67,6 +68,7 @@ impl ResolvePlugin for UnsupportedModulesResolvePlugin {
&self,
_fs_path: Vc<FileSystemPath>,
file_path: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
if let Request::Module {
......@@ -131,6 +133,7 @@ impl ResolvePlugin for NextExternalResolvePlugin {
&self,
fs_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
_request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
let raw_fs_path = &*fs_path.await?;
......@@ -179,6 +182,7 @@ impl ResolvePlugin for NextNodeSharedRuntimeResolvePlugin {
&self,
fs_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
_request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
let stem = fs_path.file_stem().await?;
......@@ -240,6 +244,7 @@ impl ResolvePlugin for ModuleFeatureReportResolvePlugin {
&self,
_fs_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
if let Request::Module {
......@@ -294,6 +299,7 @@ impl ResolvePlugin for NextSharedRuntimeResolvePlugin {
&self,
fs_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
_request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
let raw_fs_path = &*fs_path.await?;
......
......@@ -40,7 +40,7 @@ pub async fn get_swc_ecma_transform_plugin_impl(
core::{
asset::Asset,
issue::IssueSeverity,
reference_type::ReferenceType,
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::{handle_resolve_error, parse::Request, pattern::Pattern, resolve},
},
ecmascript_plugin::transform::swc_ecma_transform_plugins::{
......@@ -68,8 +68,14 @@ pub async fn get_swc_ecma_transform_plugin_impl(
);
let plugin_wasm_module_resolve_result = handle_resolve_error(
resolve(project_path, request, resolve_options).as_raw_module_result(),
Value::new(ReferenceType::Undefined),
resolve(
project_path,
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
request,
resolve_options,
)
.as_raw_module_result(),
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
project_path,
request,
resolve_options,
......
......@@ -193,7 +193,7 @@
"@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0",
"@vercel/nft": "0.22.6",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231120.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231121.2",
"acorn": "8.5.0",
"amphtml-validator": "1.0.35",
"anser": "1.4.9",
......
......@@ -540,8 +540,9 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc
}
}
const actionHandler =
ComponentMod.__next_app__.require(actionModId)[actionId]
const actionHandler = (
await ComponentMod.__next_app__.require(actionModId)
)[actionId]
const returnVal = await actionHandler.apply(null, bound)
......
......@@ -18,6 +18,7 @@ export const hookPropertyMap = new Map()
export const defaultOverrides = {
'styled-jsx': path.dirname(resolve('styled-jsx/package.json')),
'styled-jsx/style': resolve('styled-jsx/style'),
'styled-jsx/style.js': resolve('styled-jsx/style'),
}
const toResolveMap = (map: Record<string, string>): [string, string][] =>
......
......@@ -1065,8 +1065,8 @@ importers:
specifier: 0.22.6
version: 0.22.6
'@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231120.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231120.2(react-refresh@0.12.0)(webpack@5.86.0)'
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231121.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231121.2(react-refresh@0.12.0)(webpack@5.86.0)'
acorn:
specifier: 8.5.0
version: 8.5.0
......@@ -24646,9 +24646,9 @@ packages:
/zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231120.2(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {registry: https://registry.npmjs.org/, tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231120.2}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231120.2'
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231121.2(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231121.2}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-231121.2'
name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0
dependencies:
......
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