chore: organize test files

This commit is contained in:
Igor Randjelovic
2020-12-01 19:25:29 +01:00
parent 65b214b845
commit 016ecd19a8
22 changed files with 339 additions and 140 deletions

View File

@@ -1,8 +1,10 @@
import path from 'path';
import fs from 'fs';
import * as lib from '../index';
import { error, info, warn } from './log';
import { getAllDependencies, getDependencyPath } from './dependencies';
import { info, warn } from './log';
import * as lib from '../index';
import { clearCurrentPlugin, setCurrentPlugin } from '../index';
export function applyExternalConfigs() {
getAllDependencies().forEach((dependency) => {
@@ -11,7 +13,7 @@ export function applyExternalConfigs() {
if (fs.existsSync(configPath)) {
info(`Discovered config: ${configPath}`);
setCurrentPlugin(dependency);
try {
const externalConfig = require(configPath);
@@ -27,14 +29,13 @@ export function applyExternalConfigs() {
);
}
} catch (err) {
error(
`
Unable to apply config: ${configPath}.
Error is:
`,
err
);
warn(`
Unable to apply config: ${configPath}.
Error is: ${err}
`);
}
}
});
clearCurrentPlugin();
}

View File

@@ -11,7 +11,7 @@ function cleanup(data: any[]) {
}
export function error(...data: any): Error {
console.error(`[@nativescript/webpack] Error: \n`, ...cleanup(data));
console.warn(`[@nativescript/webpack] Error: \n`, ...cleanup(data));
// we return the error - the caller can throw or ignore
if (typeof data[0] === 'string') {
@@ -26,7 +26,7 @@ export function warn(...data: any): void {
}
export function info(...data: any): void {
console.info(`[@nativescript/webpack] Info: \n`, ...cleanup(data));
console.log(`[@nativescript/webpack] Info: \n`, ...cleanup(data));
}
// todo: refine

View File

@@ -30,20 +30,36 @@ export interface IWebpackEnv {
// todo: add others
}
let webpackChains = {
base: [],
normal: [],
last: [],
};
interface IChainEntry {
chainFn: any;
order?: number;
plugin?: string;
}
let webpackChains: IChainEntry[] = [];
let webpackMerges: any[] = [];
let explicitUseConfig = false;
let hasInitialized = false;
let currentPlugin: string | undefined;
/**
* @internal
*/
export let env: IWebpackEnv = {};
/**
* @internal
*/
export function setCurrentPlugin(plugin: string) {
currentPlugin = plugin;
}
/**
* @internal
*/
export function clearCurrentPlugin() {
currentPlugin = undefined;
}
////// PUBLIC API
export const defaultConfigs = configs;
export const Utils = helpers;
@@ -58,16 +74,22 @@ export function init(_env: IWebpackEnv) {
export function useConfig(config: keyof typeof defaultConfigs | false) {
explicitUseConfig = true;
if (config) {
webpackChains.base.push(configs[config]);
webpackChains.push({
order: -1,
chainFn: configs[config],
});
}
}
export function chainWebpack(
chainFn: (config: Config, env: IWebpackEnv) => any,
options?: { last?: boolean }
options?: { order?: number }
) {
const type = options?.last ? 'last' : 'normal';
webpackChains[type].push(chainFn);
webpackChains.push({
order: options?.order || 0,
chainFn,
plugin: currentPlugin,
});
}
export function mergeWebpack(
@@ -90,19 +112,24 @@ export function resolveChainableConfig(): Config {
// todo: allow opt-out
applyExternalConfigs();
const applyChains = (chains) => {
// this applies the chain configs
chains.forEach((chainFn) => {
return chainFn(config, env);
webpackChains
.splice(0)
.sort((a, b) => {
return a.order - b.order;
})
.forEach(({ chainFn, plugin }) => {
try {
chainFn(config, env);
} catch (err) {
if (plugin) {
// print error with plugin name that causes it
error(`
Unable to apply chain function from: ${plugin}.
Error is: ${err}
`);
}
}
});
};
// first we apply base configs
applyChains(webpackChains.base);
// then regular configs
applyChains(webpackChains.normal);
// finally configs that opted to be called last
applyChains(webpackChains.last);
if (env.verbose) {
info('Resolved chainable config (before merges):');

View File

@@ -29,6 +29,7 @@ export class PlatformSuffixPlugin {
);
const platformRE = new RegExp(`\.${this.platform}\.`);
// require.context
compiler.hooks.contextModuleFactory.tap(id, (cmf) => {
cmf.hooks.alternativeRequests.tap(id, (modules, options) => {
const additionalModules = [];