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
20 changes: 10 additions & 10 deletions crates/pack-core/src/shared/transforms/webpack_public_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use turbopack_ecmascript::{CustomTransformer, TransformContext};

use super::{EcmascriptTransformStage, get_ecma_transform_rule};

/// Returns a rule that maps webpack's runtime public path global onto utoopack's
/// existing runtime public path hook.
/// Returns a rule that maps webpack's runtime public path global onto the
/// current utoopack runtime's public path.
pub fn get_webpack_public_path_transform_rule() -> ModuleRule {
get_ecma_transform_rule(
Box::new(WebpackPublicPathTransformer {}),
Expand Down Expand Up @@ -97,20 +97,20 @@ impl WebpackPublicPathVisitor {
sym == &atom!("__webpack_public_path__") && ctxt == self.unresolved_ctxt
}

fn global_public_path_member(&self, span: swc_core::common::Span) -> MemberExpr {
fn runtime_public_path_member(&self, span: swc_core::common::Span) -> MemberExpr {
MemberExpr {
span,
obj: Box::new(Expr::Ident(Ident::new(
atom!("globalThis"),
atom!("__turbopack_context__"),
span,
self.unresolved_ctxt,
SyntaxContext::empty(),
))),
prop: MemberProp::Ident(IdentName::new(atom!("publicPath"), span)),
prop: MemberProp::Ident(IdentName::new(atom!("runtimePublicPath"), span)),
}
}

fn global_public_path_expr(&self, span: swc_core::common::Span) -> Expr {
Expr::Member(self.global_public_path_member(span))
fn runtime_public_path_expr(&self, span: swc_core::common::Span) -> Expr {
Expr::Member(self.runtime_public_path_member(span))
}
}

Expand All @@ -121,7 +121,7 @@ impl VisitMut for WebpackPublicPathVisitor {
if self.is_webpack_public_path(&binding_ident.id.sym, binding_ident.id.ctxt) =>
{
assign_expr.left = AssignTarget::Simple(SimpleAssignTarget::Member(
self.global_public_path_member(binding_ident.id.span),
self.runtime_public_path_member(binding_ident.id.span),
));
}
_ => {
Expand All @@ -136,7 +136,7 @@ impl VisitMut for WebpackPublicPathVisitor {
if let Expr::Ident(ident) = expr
&& self.is_webpack_public_path(&ident.sym, ident.ctxt)
{
*expr = self.global_public_path_expr(ident.span);
*expr = self.runtime_public_path_expr(ident.span);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,15 @@ contextPrototype.U = relativeURL;
contextPrototype.z = requireStub;
// Make `globalThis` available to the module in a way that cannot be shadowed by a local variable.
contextPrototype.g = globalThis;
// Each runtime keeps its own public path. Other applications on the page may
// change the global path after this runtime has started.
let runtimePublicPath = typeof globalThis !== 'undefined' ? globalThis.publicPath : undefined;
Object.defineProperty(contextPrototype, 'runtimePublicPath', {
get: ()=>runtimePublicPath,
set: (value)=>{
runtimePublicPath = value;
}
});
let cachedAutomaticPublicPath;
function getAutomaticPublicPath() {
if (cachedAutomaticPublicPath !== undefined) {
Expand All @@ -554,13 +563,13 @@ function getAutomaticPublicPath() {
}
/**
* Gets the public path for runtime assets.
* Checks globalThis.publicPath and falls back to "/".
* Uses this runtime's public path and falls back to "/".
*/ function getPublicPath(mode) {
if (mode === 'auto') {
return getAutomaticPublicPath();
}
if (typeof globalThis !== 'undefined' && typeof globalThis.publicPath === 'string') {
const publicPath = globalThis.publicPath;
if (typeof runtimePublicPath === 'string') {
const publicPath = runtimePublicPath;
return publicPath.endsWith('/') ? publicPath : `${publicPath}/`;
}
return '/';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"entry": false,
"recorded": false,
"id": "entries/detail-server.5d841f26.js",
"size": 32579,
"size": 32928,
"hash": "",
"files": [
"entries/detail-server.5d841f26.js"
Expand All @@ -97,7 +97,7 @@
"entry": false,
"recorded": false,
"id": "entries/index-server.5c83aa4e.js",
"size": 32591,
"size": 32940,
"hash": "",
"files": [
"entries/index-server.5c83aa4e.js"
Expand All @@ -109,7 +109,7 @@
"entry": false,
"recorded": false,
"id": "entries/server.5d80c3de.js",
"size": 32558,
"size": 32907,
"hash": "",
"files": [
"entries/server.5d80c3de.js"
Expand Down Expand Up @@ -145,7 +145,7 @@
"type": "asset",
"name": "entries/detail-server.5d841f26.js",
"info": {},
"size": 32579,
"size": 32928,
"emitted": false,
"comparedForEmit": false,
"cached": false,
Expand All @@ -157,7 +157,7 @@
"type": "asset",
"name": "entries/index-server.5c83aa4e.js",
"info": {},
"size": 32591,
"size": 32940,
"emitted": false,
"comparedForEmit": false,
"cached": false,
Expand All @@ -169,7 +169,7 @@
"type": "asset",
"name": "entries/server.5d80c3de.js",
"info": {},
"size": 32558,
"size": 32907,
"emitted": false,
"comparedForEmit": false,
"cached": false,
Expand Down

Large diffs are not rendered by default.

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
Expand Up @@ -546,6 +546,15 @@ contextPrototype.U = relativeURL;
contextPrototype.z = requireStub;
// Make `globalThis` available to the module in a way that cannot be shadowed by a local variable.
contextPrototype.g = globalThis;
// Each runtime keeps its own public path. Other applications on the page may
// change the global path after this runtime has started.
let runtimePublicPath = typeof globalThis !== 'undefined' ? globalThis.publicPath : undefined;
Object.defineProperty(contextPrototype, 'runtimePublicPath', {
get: ()=>runtimePublicPath,
set: (value)=>{
runtimePublicPath = value;
}
});
let cachedAutomaticPublicPath;
function getAutomaticPublicPath() {
if (cachedAutomaticPublicPath !== undefined) {
Expand All @@ -569,13 +578,13 @@ function getAutomaticPublicPath() {
}
/**
* Gets the public path for runtime assets.
* Checks globalThis.publicPath and falls back to "/".
* Uses this runtime's public path and falls back to "/".
*/ function getPublicPath(mode) {
if (mode === 'auto') {
return getAutomaticPublicPath();
}
if (typeof globalThis !== 'undefined' && typeof globalThis.publicPath === 'string') {
const publicPath = globalThis.publicPath;
if (typeof runtimePublicPath === 'string') {
const publicPath = runtimePublicPath;
return publicPath.endsWith('/') ? publicPath : `${publicPath}/`;
}
return '/';
Expand Down
Loading
Loading