Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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