chore: scaffold config auto-discovery

This commit is contained in:
Igor Randjelovic
2020-11-18 20:36:54 +01:00
parent f8cd50e495
commit 4518cfa31b
3 changed files with 37 additions and 2 deletions

View File

@ -0,0 +1,11 @@
// todo: refine
export function error(message: string, info?: { possibleCauses?: string[] }) {
console.error(`
NativeScript Webpack encountered an error and cannot proceed with the build:
${message}
Possible causes:
${info?.possibleCauses?.map((cause) => `- ${cause}`).join('\n')}
`);
}

View File

@ -0,0 +1,18 @@
import { defaultConfigs } from '@nativescript/webpack';
export function determineProjectFlavor(): keyof typeof defaultConfigs {
// todo;
// error(`
// Could not determine project flavor.
//
// Please use webpack.useConfig('<flavor>') to explicitly set the base config.
// `, {
// possibleCauses: [
// 'Not in a NativeScript project',
// 'The project is not at the current working directory'
// ]
// })
return 'vue';
}

View File

@ -1,6 +1,7 @@
import Config from 'webpack-chain';
import webpack, { config } from 'webpack';
import webpack from 'webpack';
import { configs } from './configuration';
import { determineProjectFlavor } from './helpers/flavor';
export type Platform = 'android' | 'ios' | string;
@ -22,6 +23,7 @@ export interface IWebpackEnv {
let webpackChains: any[] = [];
let webpackMerges: any[] = [];
let env: IWebpackEnv = {};
let explicitUseConfig = false;
////// PUBLIC API
export const defaultConfigs = configs;
@ -30,10 +32,10 @@ export function init(_env: IWebpackEnv) {
if (_env) {
env = _env;
}
// todo: determine default config based on deps and print **useful** error if it fails.
}
export function useConfig(config: keyof typeof defaultConfigs) {
explicitUseConfig = true;
webpackChains.push(configs[config]);
}
@ -48,6 +50,10 @@ export function mergeWebpack(mergeFn: (config: Partial<webpack.Configuration>, e
export function resolveChainableConfig() {
const config = new Config();
if (!explicitUseConfig) {
useConfig(determineProjectFlavor());
}
// this applies all chain configs
webpackChains.forEach((chainFn) => {
return chainFn(config, env);