Skip to content

Commit

Permalink
Turbopack: Register react refresh exports in module factory (vercel/t…
Browse files Browse the repository at this point in the history
…urborepo#8191)

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 authored May 22, 2024
1 parent e170a45 commit 9348298
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 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
21 changes: 18 additions & 3 deletions crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use swc_core::{
common::{chain, comments::Comments, util::take::Take, Mark, SourceMap},
ecma::{
ast::{Module, ModuleItem, Program, Script},
preset_env::{
Targets, {self},
},
preset_env::{self, Targets},
transforms::{
base::{feature::FeatureFlag, helpers::inject_helpers, Assumptions},
react::react,
},
visit::{FoldWith, VisitMutWith},
},
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

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

Loading

0 comments on commit 9348298

Please sign in to comment.