Skip to content

Commit

Permalink
fix(runtime): MODERN_TARGET is not injected when using Rspack (#3502)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Apr 20, 2023
1 parent 0f087c4 commit 28721cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .changeset/six-tigers-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/runtime': patch
---

fix(runtime): MODERN_TARGET is not injected when using Rspack

fix(runtime): 修复 Rspack 模式未注入 MODERN_TARGET 的问题
23 changes: 6 additions & 17 deletions packages/runtime/plugin-runtime/src/ssr/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export default (): CliPlugin<AppTools> => ({
'@modern-js/utils/ssr': require.resolve('@modern-js/utils/ssr'),
'@modern-js/runtime/plugins': pluginsExportsUtils.getPath(),
},
globalVars: (values, { target }) => {
values['process.env.MODERN_TARGET'] =
target === 'node' || target === 'service-worker'
? 'node'
: 'browser';
},
},
tools: {
webpackChain: (chain, { isServer, isServiceWorker, CHAIN_ID }) => {
Expand All @@ -102,23 +108,6 @@ export default (): CliPlugin<AppTools> => ({
{ filename: LOADABLE_STATS_FILE },
]);
}

// add environment variables to determine the node/browser
const modernVars = {
[`process.env.MODERN_TARGET`]: JSON.stringify(
isServer || isServiceWorker ? 'node' : 'browser',
),
};
chain.plugin(CHAIN_ID.PLUGIN.DEFINE).tap(args => {
const [vars, ...rest] = args;
return [
{
...vars,
...modernVars,
},
...rest,
];
});
},
babel: babelConfig,
},
Expand Down

0 comments on commit 28721cb

Please sign in to comment.