Skip to content

Commit 2b5cf02

Browse files
authored
feat(pack): publicPath support runtime (#2245)
* feat(pack): support output.publicPath: runtime * feat(pack): support output.publicPath: runtime
1 parent d0c719e commit 2b5cf02

27 files changed

+1145
-66
lines changed

crates/pack-core/js/src/umd/runtime-base.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined;
2121
declare var CHUNK_BASE_PATH: string;
2222
declare var CHUNK_SUFFIX_PATH: string;
2323

24-
function normalizeChunkPath(path: string) {
25-
if (path.startsWith("/")) {
26-
path = path.substring(1);
27-
} else if (path.startsWith("./")) {
28-
path = path.substring(2);
29-
}
30-
31-
if (!path.endsWith("/")) {
32-
path += "/";
33-
}
34-
35-
return path;
36-
}
37-
38-
const NORMALIZED_CHUNK_BASE_PATH = normalizeChunkPath(CHUNK_BASE_PATH);
39-
4024
interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
4125
R: ResolvePathFromModule;
4226
}
@@ -242,7 +226,7 @@ function instantiateRuntimeModule(
242226
* Returns the URL relative to the origin where a chunk can be fetched from.
243227
*/
244228
function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl {
245-
return `${NORMALIZED_CHUNK_BASE_PATH}${chunkPath
229+
return `${CHUNK_BASE_PATH}${chunkPath
246230
.split("/")
247231
.map((p) => encodeURIComponent(p))
248232
.join("/")}${CHUNK_SUFFIX_PATH}` as ChunkUrl;

crates/pack-core/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,12 @@ impl Config {
14261426
.and_then(|o| o.public_path.clone())
14271427
.unwrap_or("".into());
14281428

1429+
// Special handling for "runtime" value - return a marker that will be
1430+
// replaced at runtime with window.publicPath
1431+
if public_path.as_str() == "runtime" {
1432+
return Ok(Vc::cell("__RUNTIME_PUBLIC_PATH__".into()));
1433+
}
1434+
14291435
Ok(Vc::cell(
14301436
format!("{}/", public_path.trim_end_matches("/")).into(),
14311437
))

crates/pack-tests/tests/snapshot/basic/public_path/config.json renamed to crates/pack-tests/tests/snapshot/public_path/basic/config.json

File renamed without changes.

crates/pack-tests/tests/snapshot/basic/public_path/input/asset.jpg renamed to crates/pack-tests/tests/snapshot/public_path/basic/input/asset.jpg

File renamed without changes.

crates/pack-tests/tests/snapshot/basic/public_path/input/index.js renamed to crates/pack-tests/tests/snapshot/public_path/basic/input/index.js

File renamed without changes.

crates/pack-tests/tests/snapshot/basic/public_path/input/lazy.js renamed to crates/pack-tests/tests/snapshot/public_path/basic/input/lazy.js

File renamed without changes.

crates/pack-tests/tests/snapshot/basic/public_path/output/asset.c77b3abb.jpg renamed to crates/pack-tests/tests/snapshot/public_path/basic/output/asset.c77b3abb.jpg

File renamed without changes.

crates/pack-tests/tests/snapshot/basic/public_path/output/crates_pack-tests_tests_snapshot_basic_public_path_input_4bf7ccdf.js renamed to crates/pack-tests/tests/snapshot/public_path/basic/output/crates_pack-tests_tests_snapshot_public_path_basic_input_4bf91212.js

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

crates/pack-tests/tests/snapshot/basic/public_path/output/crates_pack-tests_tests_snapshot_basic_public_path_input_4bf7ccdf.js.map renamed to crates/pack-tests/tests/snapshot/public_path/basic/output/crates_pack-tests_tests_snapshot_public_path_basic_input_4bf91212.js.map

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

crates/pack-tests/tests/snapshot/basic/public_path/output/main.js renamed to crates/pack-tests/tests/snapshot/public_path/basic/output/main.js

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

0 commit comments

Comments
 (0)