From 28721cb0bc7d06473f2b60c6777a2472e932cd5e Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 20 Apr 2023 10:25:06 +0800 Subject: [PATCH] fix(runtime): MODERN_TARGET is not injected when using Rspack (#3502) --- .changeset/six-tigers-own.md | 7 ++++++ .../plugin-runtime/src/ssr/cli/index.ts | 23 +++++-------------- 2 files changed, 13 insertions(+), 17 deletions(-) create mode 100644 .changeset/six-tigers-own.md diff --git a/.changeset/six-tigers-own.md b/.changeset/six-tigers-own.md new file mode 100644 index 000000000000..ab96f8c7caa3 --- /dev/null +++ b/.changeset/six-tigers-own.md @@ -0,0 +1,7 @@ +--- +'@modern-js/runtime': patch +--- + +fix(runtime): MODERN_TARGET is not injected when using Rspack + +fix(runtime): 修复 Rspack 模式未注入 MODERN_TARGET 的问题 diff --git a/packages/runtime/plugin-runtime/src/ssr/cli/index.ts b/packages/runtime/plugin-runtime/src/ssr/cli/index.ts index 220596e1e40b..582178699fcb 100644 --- a/packages/runtime/plugin-runtime/src/ssr/cli/index.ts +++ b/packages/runtime/plugin-runtime/src/ssr/cli/index.ts @@ -83,6 +83,12 @@ export default (): CliPlugin => ({ '@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 }) => { @@ -102,23 +108,6 @@ export default (): CliPlugin => ({ { 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, },