Skip to content

Commit

Permalink
Turbopack: Register react refresh exports in module factory
Browse files Browse the repository at this point in the history
This moves registration of a module’s exports with react refresh to within the module factory itself, rather than the code that executes the factory. This colocates this conditional code with the code that registers individual symbols, and allows us to only run it on modules that truly require refresh (i.e. user app code, not `node_modules`).
  • Loading branch information
wbinnssmith committed May 21, 2024
1 parent 4121a69 commit e550c87
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare var $RefreshInterceptModuleExecution$:
type RefreshContext = {
register: RefreshRuntimeGlobals["$RefreshReg$"];
signature: RefreshRuntimeGlobals["$RefreshSig$"];
registerExports: typeof registerExportsAndSetupBoundaryForReactRefresh;
};

type RefreshHelpers = RefreshRuntimeGlobals["$RefreshHelpers$"];
Expand Down Expand Up @@ -403,16 +404,8 @@ function runModuleExecutionHooks(
executeModule({
register: globalThis.$RefreshReg$,
signature: globalThis.$RefreshSig$,
registerExports: registerExportsAndSetupBoundaryForReactRefresh,
});

if ("$RefreshHelpers$" in globalThis) {
// This pattern can also be used to register the exports of
// a module with the React Refresh runtime.
registerExportsAndSetupBoundaryForReactRefresh(
module,
globalThis.$RefreshHelpers$
);
}
} catch (e) {
throw e;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-ecmascript/src/chunk/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl EcmascriptChunkItemContent {
if this.options.refresh {
args.push("k: __turbopack_refresh__");
}
if this.options.module {
if this.options.module || this.options.refresh {
args.push("m: module");
}
if this.options.exports {
Expand Down
27 changes: 21 additions & 6 deletions crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use std::{fmt::Debug, hash::Hash, sync::Arc};
use std::{borrow::BorrowMut, fmt::Debug, hash::Hash, sync::Arc};

Check failure on line 1 in crates/turbopack-ecmascript/src/transform/mod.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

unused import: `borrow::BorrowMut`

Check failure on line 1 in crates/turbopack-ecmascript/src/transform/mod.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

unused import: `borrow::BorrowMut`

use anyhow::Result;
use async_trait::async_trait;
use swc_core::{
base::SwcComments,
common::{chain, comments::Comments, util::take::Take, Mark, SourceMap},
ecma::{
ast::{Module, ModuleItem, Program, Script},
preset_env::{
Targets, {self},
},
ast::{Expr, Module, ModuleItem, Program, Script},

Check failure on line 9 in crates/turbopack-ecmascript/src/transform/mod.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

unused import: `Expr`

Check failure on line 9 in crates/turbopack-ecmascript/src/transform/mod.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

unused import: `Expr`
preset_env::{self, Targets},
transforms::{
base::{feature::FeatureFlag, helpers::inject_helpers, Assumptions},
react::react,
},
visit::{FoldWith, VisitMutWith},
visit::{FoldWith, VisitMut, VisitMutWith},

Check failure on line 15 in crates/turbopack-ecmascript/src/transform/mod.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

unused import: `VisitMut`

Check failure on line 15 in crates/turbopack-ecmascript/src/transform/mod.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust check

unused import: `VisitMut`
},
quote,
};
use turbo_tasks::{ValueDefault, Vc};
use turbo_tasks_fs::FileSystemPath;
Expand Down Expand Up @@ -182,6 +181,22 @@ impl EcmascriptInputTransform {
top_level_mark,
unresolved_mark,
));

if *refresh {
let stmt = quote!(
"\n__turbopack_refresh__.registerExports(module, \
globalThis.$RefreshHelpers$); }\n" as Stmt
);

match program {
Program::Module(module) => {
module.body.push(ModuleItem::Stmt(stmt));
}
Program::Script(script) => {
script.body.push(stmt);
}
}
}
}
EcmascriptInputTransform::CommonJs => {
// Explicit type annotation to ensure that we don't duplicate transforms in the
Expand Down

0 comments on commit e550c87

Please sign in to comment.