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); +}