Skip to content

Commit

Permalink
Reapply "Turbopack: Register react refresh exports in module factory (#…
Browse files Browse the repository at this point in the history
…8191)" (#8214)

This reverts commit 63c39e6.
  • Loading branch information
wbinnssmith committed May 28, 2024
1 parent 9765fe8 commit d7741fb
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 @@ -8,16 +8,15 @@ use swc_core::{
common::{chain, collections::AHashMap, 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},
optimization::inline_globals2,
react::react,
},
visit::{FoldWith, VisitMutWith},
},
quote,
};
use turbo_tasks::{ValueDefault, Vc};
use turbo_tasks_fs::FileSystemPath;
Expand Down Expand Up @@ -198,6 +197,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 d7741fb

Please sign in to comment.