mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 02:54:11 +08:00
chore: scaffold config auto-discovery
This commit is contained in:
11
packages/webpack5/src/helpers/errors.ts
Normal file
11
packages/webpack5/src/helpers/errors.ts
Normal 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')}
|
||||||
|
`);
|
||||||
|
}
|
18
packages/webpack5/src/helpers/flavor.ts
Normal file
18
packages/webpack5/src/helpers/flavor.ts
Normal 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';
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
import Config from 'webpack-chain';
|
import Config from 'webpack-chain';
|
||||||
import webpack, { config } from 'webpack';
|
import webpack from 'webpack';
|
||||||
import { configs } from './configuration';
|
import { configs } from './configuration';
|
||||||
|
import { determineProjectFlavor } from './helpers/flavor';
|
||||||
|
|
||||||
export type Platform = 'android' | 'ios' | string;
|
export type Platform = 'android' | 'ios' | string;
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ export interface IWebpackEnv {
|
|||||||
let webpackChains: any[] = [];
|
let webpackChains: any[] = [];
|
||||||
let webpackMerges: any[] = [];
|
let webpackMerges: any[] = [];
|
||||||
let env: IWebpackEnv = {};
|
let env: IWebpackEnv = {};
|
||||||
|
let explicitUseConfig = false;
|
||||||
|
|
||||||
////// PUBLIC API
|
////// PUBLIC API
|
||||||
export const defaultConfigs = configs;
|
export const defaultConfigs = configs;
|
||||||
@ -30,10 +32,10 @@ export function init(_env: IWebpackEnv) {
|
|||||||
if (_env) {
|
if (_env) {
|
||||||
env = _env;
|
env = _env;
|
||||||
}
|
}
|
||||||
// todo: determine default config based on deps and print **useful** error if it fails.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useConfig(config: keyof typeof defaultConfigs) {
|
export function useConfig(config: keyof typeof defaultConfigs) {
|
||||||
|
explicitUseConfig = true;
|
||||||
webpackChains.push(configs[config]);
|
webpackChains.push(configs[config]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +50,10 @@ export function mergeWebpack(mergeFn: (config: Partial<webpack.Configuration>, e
|
|||||||
export function resolveChainableConfig() {
|
export function resolveChainableConfig() {
|
||||||
const config = new Config();
|
const config = new Config();
|
||||||
|
|
||||||
|
if (!explicitUseConfig) {
|
||||||
|
useConfig(determineProjectFlavor());
|
||||||
|
}
|
||||||
|
|
||||||
// this applies all chain configs
|
// this applies all chain configs
|
||||||
webpackChains.forEach((chainFn) => {
|
webpackChains.forEach((chainFn) => {
|
||||||
return chainFn(config, env);
|
return chainFn(config, env);
|
||||||
|
Reference in New Issue
Block a user