From 5a3a35d3763a8a0c6452481a596e02a154e46d5f Mon Sep 17 00:00:00 2001 From: rigor789 Date: Tue, 3 Aug 2021 15:41:14 +0200 Subject: [PATCH] fix(webpack): add virtualEntry before main entry fixes running with the JSC runtime, fixes #9469 --- .../webpack5/src/configuration/javascript.ts | 9 ++++--- .../webpack5/src/configuration/typescript.ts | 9 ++++--- packages/webpack5/src/helpers/chain.ts | 24 +++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 packages/webpack5/src/helpers/chain.ts diff --git a/packages/webpack5/src/configuration/javascript.ts b/packages/webpack5/src/configuration/javascript.ts index 5820734e9..da2fe0e3b 100644 --- a/packages/webpack5/src/configuration/javascript.ts +++ b/packages/webpack5/src/configuration/javascript.ts @@ -2,6 +2,7 @@ import Config from 'webpack-chain'; import { getEntryPath, getEntryDirPath } from '../helpers/platform'; import { addVirtualEntry } from '../helpers/virtualModules'; +import { chainedSetAddAfter } from '../helpers/chain'; import { env as _env, IWebpackEnv } from '../index'; import base from './base'; @@ -21,9 +22,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { ` ); - config.entry('bundle').add(virtualEntryPath); - - // config.resolve.extensions.add('.xml'); + chainedSetAddAfter( + config.entry('bundle'), + '@nativescript/core/globals/index.js', + virtualEntryPath + ); // set up core HMR config.module diff --git a/packages/webpack5/src/configuration/typescript.ts b/packages/webpack5/src/configuration/typescript.ts index 71f1d7e4b..3a43f7527 100644 --- a/packages/webpack5/src/configuration/typescript.ts +++ b/packages/webpack5/src/configuration/typescript.ts @@ -2,6 +2,7 @@ import Config from 'webpack-chain'; import { getEntryDirPath, getEntryPath } from '../helpers/platform'; import { addVirtualEntry } from '../helpers/virtualModules'; +import { chainedSetAddAfter } from '../helpers/chain'; import { env as _env, IWebpackEnv } from '../index'; import base from './base'; @@ -21,9 +22,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { ` ); - config.entry('bundle').add(virtualEntryPath); - - // config.resolve.extensions.add('.xml'); + chainedSetAddAfter( + config.entry('bundle'), + '@nativescript/core/globals/index.js', + virtualEntryPath + ); // set up core HMR config.module diff --git a/packages/webpack5/src/helpers/chain.ts b/packages/webpack5/src/helpers/chain.ts new file mode 100644 index 000000000..b405b9117 --- /dev/null +++ b/packages/webpack5/src/helpers/chain.ts @@ -0,0 +1,24 @@ +import { ChainedSet } from 'webpack-chain'; + +/** + * Helper to insert values after a specific item in a ChainedSet. + * + * @param chainedSet + * @param after + * @param itemToAdd + */ +export function chainedSetAddAfter( + chainedSet: ChainedSet, + after: any, + itemToAdd: any +) { + const values = chainedSet.values(); + + if (values.includes(after)) { + values.splice(values.indexOf(after) + 1, 0, itemToAdd); + } else { + values.push(itemToAdd); + } + + chainedSet.clear().merge(values); +}