diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap index 410938a57..6b3878a5e 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/react.spec.ts.snap @@ -175,10 +175,10 @@ exports[`react configuration > android > adds ReactRefreshWebpackPlugin when HMR 'process.env.NODE_ENV': '\\"development\\"' } ), - /* config.plugin('BundleAnalyzerPlugin') */ - new BundleAnalyzerPlugin(), /* config.plugin('WatchStateLoggerPlugin') */ new WatchStateLoggerPlugin(), + /* config.plugin('HotModuleReplacementPlugin') */ + new HotModuleReplacementPlugin(), /* config.plugin('ReactRefreshWebpackPlugin') */ new ReactRefreshWebpackPlugin( { @@ -370,8 +370,6 @@ exports[`react configuration > android > base config 1`] = ` 'process.env.NODE_ENV': '\\"development\\"' } ), - /* config.plugin('BundleAnalyzerPlugin') */ - new BundleAnalyzerPlugin(), /* config.plugin('WatchStateLoggerPlugin') */ new WatchStateLoggerPlugin() ], @@ -558,10 +556,10 @@ exports[`react configuration > ios > adds ReactRefreshWebpackPlugin when HMR ena 'process.env.NODE_ENV': '\\"development\\"' } ), - /* config.plugin('BundleAnalyzerPlugin') */ - new BundleAnalyzerPlugin(), /* config.plugin('WatchStateLoggerPlugin') */ new WatchStateLoggerPlugin(), + /* config.plugin('HotModuleReplacementPlugin') */ + new HotModuleReplacementPlugin(), /* config.plugin('ReactRefreshWebpackPlugin') */ new ReactRefreshWebpackPlugin( { @@ -756,8 +754,6 @@ exports[`react configuration > ios > base config 1`] = ` 'process.env.NODE_ENV': '\\"development\\"' } ), - /* config.plugin('BundleAnalyzerPlugin') */ - new BundleAnalyzerPlugin(), /* config.plugin('WatchStateLoggerPlugin') */ new WatchStateLoggerPlugin() ], diff --git a/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap b/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap index 652e4acb5..4c9c8f317 100644 --- a/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap +++ b/packages/webpack5/__tests__/configuration/__snapshots__/vue.spec.ts.snap @@ -184,8 +184,6 @@ exports[`vue configuration for android 1`] = ` profile: '() => {}' } ), - /* config.plugin('BundleAnalyzerPlugin') */ - new BundleAnalyzerPlugin(), /* config.plugin('WatchStateLoggerPlugin') */ new WatchStateLoggerPlugin() ], @@ -381,8 +379,6 @@ exports[`vue configuration for ios 1`] = ` profile: '() => {}' } ), - /* config.plugin('BundleAnalyzerPlugin') */ - new BundleAnalyzerPlugin(), /* config.plugin('WatchStateLoggerPlugin') */ new WatchStateLoggerPlugin() ], diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index b1eb97f1f..87f78d52c 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -7,7 +7,7 @@ import { } from '../helpers/project'; import { CleanWebpackPlugin } from 'clean-webpack-plugin'; -import { DefinePlugin } from 'webpack'; +import { DefinePlugin, HotModuleReplacementPlugin } from 'webpack'; import { WatchStateLoggerPlugin } from '../plugins/WatchStateLoggerPlugin'; import TerserPlugin from 'terser-webpack-plugin'; import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; @@ -188,12 +188,17 @@ export default function (config: Config, env: IWebpackEnv): Config { // }, // ]); - // todo: make opt-in with a flag - config.plugin('BundleAnalyzerPlugin').use(BundleAnalyzerPlugin); - // add the WatchStateLogger plugin used to notify the CLI of build state config.plugin('WatchStateLoggerPlugin').use(WatchStateLoggerPlugin); + config.when(env.hmr, (config) => { + config.plugin('HotModuleReplacementPlugin').use(HotModuleReplacementPlugin); + }); + + config.when(env.report, (config) => { + config.plugin('BundleAnalyzerPlugin').use(BundleAnalyzerPlugin); + }); + return config; } diff --git a/packages/webpack5/src/helpers/config.ts b/packages/webpack5/src/helpers/config.ts new file mode 100644 index 000000000..b194acf32 --- /dev/null +++ b/packages/webpack5/src/helpers/config.ts @@ -0,0 +1,20 @@ +import { env } from '../index'; +import { error } from './log'; + +function getCLILib() { + if (!env.nativescriptLibPath) { + throw error(` + Cannot find NativeScript CLI path. Make sure --env.nativescriptLibPath is passed + `); + } + + return require(env.nativescriptLibPath); +} + +export function getValue(key: string): T { + const lib = getCLILib(); + + return (lib.projectConfigService as { getValue(key: string): T }).getValue( + key + ); +} diff --git a/packages/webpack5/src/helpers/index.ts b/packages/webpack5/src/helpers/index.ts new file mode 100644 index 000000000..f80ab7c8c --- /dev/null +++ b/packages/webpack5/src/helpers/index.ts @@ -0,0 +1,42 @@ +import { getValue } from './config'; +import { getAllDependencies, getDependencyPath } from './dependencies'; +import { determineProjectFlavor } from './flavor'; +import { error, info, warn } from './log'; +import { + getAbsoluteDistPath, + getDistPath, + getEntryPath, + getPackageJson, + getPlatform, + getProjectRootPath, +} from './project'; + +// intentionally populated manually +// as this generates nicer typings +// that show all the utils inline +// rather than imports to types +export default { + config: { + getValue, + }, + dependencies: { + getAllDependencies, + getDependencyPath, + }, + flavor: { + determineProjectFlavor, + }, + log: { + info, + warn, + error, + }, + project: { + getProjectRootPath, + getAbsoluteDistPath, + getEntryPath, + getDistPath, + getPlatform, + getPackageJson, + }, +}; diff --git a/packages/webpack5/src/index.ts b/packages/webpack5/src/index.ts index b9c20f480..c50b54d61 100644 --- a/packages/webpack5/src/index.ts +++ b/packages/webpack5/src/index.ts @@ -6,6 +6,7 @@ import { configs } from './configuration'; import { determineProjectFlavor } from './helpers/flavor'; import { applyExternalConfigs } from './helpers/externalConfigs'; import { error, info } from './helpers/log'; +import helpers from './helpers'; export type Platform = 'android' | 'ios' | string; @@ -15,6 +16,8 @@ export interface IWebpackEnv { appPath?: string; appResourcesPath?: string; + nativescriptLibPath?: string; + android?: boolean; ios?: boolean; @@ -39,6 +42,7 @@ export let env: IWebpackEnv = {}; ////// PUBLIC API export const defaultConfigs = configs; +export const Utils = helpers; export function init(_env: IWebpackEnv) { hasInitialized = true;