feat: export Utils

This commit is contained in:
Igor Randjelovic
2020-11-22 14:01:41 +01:00
committed by Nathan Walker
parent d46d59abe4
commit a1abd07c73
6 changed files with 79 additions and 16 deletions

View File

@ -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()
],

View File

@ -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()
],

View File

@ -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;
}

View File

@ -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<T = any>(key: string): T {
const lib = getCLILib();
return (lib.projectConfigService as { getValue(key: string): T }).getValue(
key
);
}

View File

@ -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,
},
};

View File

@ -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;