Skip to content
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
2 changes: 1 addition & 1 deletion crates/pack-api/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl LibraryEndpoint {
self.project().client_compile_time_info().environment(),
self.project().mode(),
self.project().config(),
Vc::cell(true),
Vc::cell(false),
Vc::cell(false),
self.project().pack_path().owned().await?,
))
Expand Down
20 changes: 17 additions & 3 deletions crates/pack-core/src/library/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use turbopack_core::{
ChunkingContext, MangleType, MinifyType, SourceMapSourceType, SourceMapsType,
module_id_strategies::ModuleIdStrategy,
},
environment::Environment,
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, NodeJsVersion},
module_graph::export_usage::OptionExportUsageInfo,
};

Expand All @@ -22,6 +22,9 @@ pub struct LibraryChunkingContextOptions {
pub root_path: FileSystemPath,
pub output_root: FileSystemPath,
pub output_root_to_root_path: RcStr,
/// The environment provided by the caller. Note that this is ignored in favor of
/// an EdgeWorker environment to disable async chunk splitting. See
/// `get_library_chunking_context` for details.
pub environment: Vc<Environment>,
pub module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
pub no_mangling: Vc<bool>,
Expand All @@ -40,7 +43,8 @@ pub async fn get_library_chunking_context(
root_path,
output_root,
output_root_to_root_path,
environment,
// Note: We ignore the provided environment and use EdgeWorker instead.
environment: _provided_environment,
module_id_strategy,
no_mangling,
runtime_root,
Expand Down Expand Up @@ -70,12 +74,22 @@ pub async fn get_library_chunking_context(
};

let output = config.output().await?;
// Use an EdgeWorker environment to disable async chunk splitting for library builds.
// This ensures the library is bundled as a single, self-contained file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这样的话 dynamic_import_to_require 还需要吗?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

好像也可以去掉了,这里会自动处理成 inline 的 import,我更新一下

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

跟 app 一样默认关闭了,看上去不会拆出额外的 chunk 来

image

let library_environment = Environment::new(ExecutionEnvironment::EdgeWorker(
EdgeWorkerEnvironment {
node_version: NodeJsVersion::default().resolved_cell(),
}
.resolved_cell(),
))
.to_resolved()
.await?;

let mut builder = LibraryChunkingContext::builder(
root_path,
output_root,
output_root_to_root_path,
environment.to_resolved().await?,
library_environment,
runtime_type,
(*runtime_root.await?).clone(),
(*runtime_export.await?).clone(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"config": {
"entry": [
{
"import": "input/index.ts",
"name": "main",
"library": {
"name": "MyLibrary"
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = "a";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export async function Test() {
const module = await import('./a');
return module;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
suggestion - [analysis] /dynamic_import/input/index.ts:2:11 lint TP1001 import(???*0*) is very dynamic
suggestion - [analysis] /dynamic_import/very-dynamic/input/index.ts:2:11 lint TP1001 import(???*0*) is very dynamic

1 | export async function importUrl(url: string) {
+ v--------------v
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
suggestion - [analysis] /dynamic_import/input/index.ts:6:11 lint TP1002 require(???*0*) is very dynamic
suggestion - [analysis] /dynamic_import/very-dynamic/input/index.ts:6:11 lint TP1002 require(???*0*) is very dynamic

2 | return import(`${url}`).then((module) => module.default);
3 | }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(globalThis.TURBOPACK || (globalThis.TURBOPACK = [])).push([
typeof document === "object" ? document.currentScript : undefined,
{"otherChunks":["dynamic_import_input_index_ts_403d7278.js"],"runtimeModuleIds":[12]}
{"otherChunks":["dynamic_import_very-dynamic_input_index_ts_570ff46a.js"],"runtimeModuleIds":[67]}
]);
// Dummy runtime
32 changes: 16 additions & 16 deletions crates/pack-tests/tests/snapshot/umd/async-module/output/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading