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) (#8237)

This reverts commit 63c39e6.
  • Loading branch information
wbinnssmith committed May 29, 2024
1 parent f580448 commit 244ca6f
Show file tree
Hide file tree
Showing 5 changed files with 29 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
24 changes: 21 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,25 @@ impl EcmascriptInputTransform {
top_level_mark,
unresolved_mark,
));

if *refresh {
let stmt = quote!(
// AMP / No-JS mode does not inject these helpers
"\nif (typeof globalThis.$RefreshHelpers$ === 'object' && \
globalThis.$RefreshHelpers !== null) { \
__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 244ca6f

Please sign in to comment.