Skip to content

Commit 84f1af7

Browse files
authored
feat(pack): support write to dir out of dist (#2596)
1 parent 6b1e3d9 commit 84f1af7

11 files changed

Lines changed: 74 additions & 62 deletions

File tree

crates/pack-api/src/app.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,18 @@ impl Endpoint for AppEndpoint {
366366

367367
let dist_root = this.project.dist_root().await?;
368368

369-
let (server_paths, client_paths) = (vec![], vec![]);
370-
371369
let written_endpoint = EndpointOutputPaths::NodeJs {
372370
server_entry_path: dist_root.path.to_string(),
373-
server_paths,
374-
client_paths,
371+
// FIXME: No server path when bundling library
372+
server_paths: vec![],
373+
client_paths: vec![],
375374
};
376375

377-
let output_assets = if *this.project.should_create_webpack_stats().await? {
376+
let should_create_webpack_stats = *this.project.should_create_webpack_stats().await?;
377+
378+
let output_assets = if !should_create_webpack_stats {
379+
output_assets
380+
} else {
378381
let webpack_stats = generate_webpack_stats(output_assets, this.project.dist_root());
379382
let webpack_stats_read = webpack_stats.await?;
380383
let dist_root_owned = this.project.dist_root().owned().await?;
@@ -386,8 +389,6 @@ impl Endpoint for AppEndpoint {
386389
.to_resolved()
387390
.await?;
388391
output_assets.concatenate(*ResolvedVc::cell(vec![ResolvedVc::upcast(stats_output)]))
389-
} else {
390-
output_assets
391392
};
392393

393394
Ok(EndpointOutput {

crates/pack-api/src/library.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,11 @@ impl Endpoint for LibraryEndpoint {
326326

327327
let dist_root = self.project().dist_root().await?;
328328

329-
let (server_paths, client_paths) = (vec![], vec![]);
330-
331329
let written_endpoint = EndpointOutputPaths::NodeJs {
332330
// FIXME: No server path when bundling library
333331
server_entry_path: dist_root.to_string(),
334-
server_paths,
335-
client_paths,
332+
server_paths: vec![],
333+
client_paths: vec![],
336334
};
337335

338336
Ok(EndpointOutput {

crates/pack-core/src/emit.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use turbopack_core::{
1515
#[turbo_tasks::function]
1616
pub async fn emit_assets(
1717
assets: Vc<ExpandedOutputAssets>,
18-
node_root: FileSystemPath,
18+
_node_root: FileSystemPath,
1919
client_relative_path: FileSystemPath,
2020
client_output_path: FileSystemPath,
2121
) -> Result<()> {
@@ -24,18 +24,16 @@ pub async fn emit_assets(
2424
.iter()
2525
.copied()
2626
.map(|asset| {
27-
let node_root = node_root.clone();
2827
let client_relative_path = client_relative_path.clone();
2928
let client_output_path = client_output_path.clone();
3029

3130
async move {
3231
let path = asset.path();
3332
let span = tracing::trace_span!("emit asset", name = %path.to_string().await?);
33+
// We allow to write output out of dist path, this is different with next.js
3434
async move {
3535
let path = path.await?;
36-
Ok(if path.is_inside_ref(&node_root) {
37-
Some(emit(*asset))
38-
} else if path.is_inside_ref(&client_relative_path) {
36+
Ok(if path.is_inside_ref(&client_relative_path) {
3937
// Client assets are emitted to the client output path, which is prefixed
4038
// with _next. We need to rebase them to remove that
4139
// prefix.
@@ -45,7 +43,7 @@ pub async fn emit_assets(
4543
client_output_path,
4644
))
4745
} else {
48-
None
46+
Some(emit(*asset))
4947
})
5048
}
5149
.instrument(span)

crates/pack-napi/src/pack_api/project.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use pack_core::tracing_presets::{
3333
TRACING_TURBOPACK_TARGETS,
3434
};
3535
use tracing::Instrument;
36-
use tracing_subscriber::{EnvFilter, Registry, layer::SubscriberExt, util::SubscriberInitExt};
36+
use tracing_subscriber::{
37+
EnvFilter, Registry, fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt,
38+
};
3739
use turbo_rcstr::RcStr;
3840
use turbo_tasks::{
3941
NonLocalValue, OperationValue, PrettyPrintError, ReadRef, ResolvedVc, TaskInput,
@@ -367,12 +369,18 @@ pub async fn project_new(
367369
});
368370
} else {
369371
TRACING_INIT.call_once(|| {
372+
let env_filter = EnvFilter::try_from_default_env();
373+
let env_filter_enabled = env_filter.is_ok();
370374
tracing_subscriber::fmt()
371-
.with_env_filter(EnvFilter::try_from_default_env().unwrap_or_else(|_| {
375+
.with_env_filter(env_filter.unwrap_or_else(|_| {
372376
EnvFilter::new("pack_napi=info,pack_api=info,pack_core=info")
373377
}))
374-
.with_target(false)
375-
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::NONE)
378+
.with_target(env_filter_enabled)
379+
.with_span_events(if env_filter_enabled {
380+
FmtSpan::CLOSE
381+
} else {
382+
FmtSpan::NONE
383+
})
376384
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::new(
377385
"%Y-%m-%d %H:%M:%S.%3f".to_string(),
378386
))

package-lock.json

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/pack-shared/src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ export interface HtmlConfig {
213213
inject?: boolean | "body" | "head";
214214
scriptLoading?: "blocking" | "defer" | "module";
215215
meta?: Record<string, string | { [key: string]: string }>;
216+
output?: {
217+
path?: string;
218+
};
216219
}
217220

218221
export interface StyledComponentsConfig {

packages/pack/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@swc/helpers": "0.5.15",
4242
"@utoo/pack-shared": "*",
4343
"@utoo/style-loader": "^1.0.0",
44-
"domparser-rs": "^0.0.5",
44+
"domparser-rs": "^0.0.7",
4545
"find-up": "4.1.0",
4646
"nanoid": "^3.3.11",
4747
"picocolors": "^1.1.1",
@@ -67,7 +67,6 @@
6767
"styled-jsx": "^5.1.6",
6868
"typescript": "^5.8.3"
6969
},
70-
7170
"engines": {
7271
"node": ">= 20"
7372
},

packages/pack/src/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async function buildInternal(
6969
packPath: getPackPath(),
7070
},
7171
{
72-
persistentCaching: false,
72+
persistentCaching: bundleOptions.config.persistentCaching ?? false,
7373
},
7474
);
7575

packages/pack/src/core/hmr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export async function createHotReloader(
135135
packPath: getPackPath(),
136136
},
137137
{
138-
persistentCaching: true,
138+
persistentCaching: bundleOptions.config.persistentCaching ?? false,
139139
},
140140
);
141141

packages/pack/src/plugins/HtmlPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ export class HtmlPlugin {
117117

118118
const finalHtml = doc.outerHTML;
119119
const filename = this.config.filename || "index.html";
120-
fs.writeFileSync(path.join(outputDir, filename), finalHtml);
120+
fs.writeFileSync(
121+
path.join(this.config.output?.path ?? outputDir, filename),
122+
finalHtml,
123+
);
121124
}
122125
}

0 commit comments

Comments
 (0)