diff --git a/packages/webpack5/src/configuration/angular.ts b/packages/webpack5/src/configuration/angular.ts index 85c29dd17..75e517a9f 100644 --- a/packages/webpack5/src/configuration/angular.ts +++ b/packages/webpack5/src/configuration/angular.ts @@ -1,8 +1,9 @@ import Config from 'webpack-chain'; import path from 'path'; -import { getEntryPath, getProjectRootPath } from '../helpers/project'; +import { getProjectRootPath } from '../helpers/project'; import { env as _env, IWebpackEnv } from '../index'; +import { getEntryPath } from '../helpers/platform'; import base from './base'; export default function (config: Config, env: IWebpackEnv = _env): Config { diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 53625f69b..22cdb4f7f 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -11,13 +11,13 @@ import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin'; import { addCopyRule, applyCopyRules } from '../helpers/copyRules'; import { WatchStatePlugin } from '../plugins/WatchStatePlugin'; import { hasDependency } from '../helpers/dependencies'; -import { getPlatformName } from '../platforms'; import { IWebpackEnv } from '../index'; import { + getPlatformName, getAbsoluteDistPath, getEntryDirPath, getEntryPath, -} from '../helpers/project'; +} from '../helpers/platform'; export default function (config: Config, env: IWebpackEnv): Config { const entryPath = getEntryPath(); diff --git a/packages/webpack5/src/configuration/javascript.ts b/packages/webpack5/src/configuration/javascript.ts index 3ac0e6aef..e42d0b5d2 100644 --- a/packages/webpack5/src/configuration/javascript.ts +++ b/packages/webpack5/src/configuration/javascript.ts @@ -4,8 +4,8 @@ import Config from 'webpack-chain'; import dedent from 'ts-dedent'; import { join } from 'path'; +import { getEntryDirPath } from '../helpers/platform'; import { env as _env, IWebpackEnv } from '../index'; -import { getEntryDirPath } from '../helpers/project'; import base from './base'; export default function (config: Config, env: IWebpackEnv = _env): Config { diff --git a/packages/webpack5/src/configuration/react.ts b/packages/webpack5/src/configuration/react.ts index 5aafabf6a..d0da8cd54 100644 --- a/packages/webpack5/src/configuration/react.ts +++ b/packages/webpack5/src/configuration/react.ts @@ -2,7 +2,7 @@ import { merge } from 'webpack-merge'; import Config from 'webpack-chain'; import { env as _env, IWebpackEnv } from '../index'; -import { getPlatformName } from '../platforms'; +import { getPlatformName } from '../helpers/platform'; import base from './base'; export default function (config: Config, env: IWebpackEnv = _env): Config { diff --git a/packages/webpack5/src/configuration/svelte.ts b/packages/webpack5/src/configuration/svelte.ts index d674928d0..2199ee56f 100644 --- a/packages/webpack5/src/configuration/svelte.ts +++ b/packages/webpack5/src/configuration/svelte.ts @@ -3,7 +3,7 @@ import Config from 'webpack-chain'; import { getProjectRootPath } from '../helpers/project'; import { env as _env, IWebpackEnv } from '../index'; -import { getPlatformName } from '../platforms'; +import { getPlatformName } from '../helpers/platform'; import { error } from '../helpers/log'; import base from './base'; diff --git a/packages/webpack5/src/configuration/vue.ts b/packages/webpack5/src/configuration/vue.ts index 29e2fcf23..b9f8a62b9 100644 --- a/packages/webpack5/src/configuration/vue.ts +++ b/packages/webpack5/src/configuration/vue.ts @@ -3,7 +3,7 @@ import { merge } from 'webpack-merge'; import Config from 'webpack-chain'; import { env as _env, IWebpackEnv } from '../index'; -import { getPlatformName } from '../platforms'; +import { getPlatformName } from '../helpers/platform'; import base from './base'; export default function (config: Config, env: IWebpackEnv = _env): Config { diff --git a/packages/webpack5/src/helpers/copyRules.ts b/packages/webpack5/src/helpers/copyRules.ts index 0bf7a9a1b..df0be51dc 100644 --- a/packages/webpack5/src/helpers/copyRules.ts +++ b/packages/webpack5/src/helpers/copyRules.ts @@ -1,6 +1,6 @@ import CopyWebpackPlugin from 'copy-webpack-plugin'; -import { getEntryDirPath } from './project'; +import { getEntryDirPath } from './platform'; /** * @internal diff --git a/packages/webpack5/src/helpers/index.ts b/packages/webpack5/src/helpers/index.ts index 89422d798..11b772059 100644 --- a/packages/webpack5/src/helpers/index.ts +++ b/packages/webpack5/src/helpers/index.ts @@ -9,15 +9,16 @@ import { hasDependency, getDependencyPath, } from './dependencies'; +import { getPackageJson, getProjectRootPath } from './project'; import { + addPlatform, getAbsoluteDistPath, getDistPath, - getEntryPath, getEntryDirPath, - getPackageJson, - getProjectRootPath, -} from './project'; -import { getPlatform, getPlatformName, addPlatform } from '../platforms'; + getEntryPath, + getPlatform, + getPlatformName, +} from './platform'; // intentionally populated manually // as this generates nicer typings @@ -46,15 +47,15 @@ export default { }, project: { getProjectRootPath, - getAbsoluteDistPath, - getEntryPath, - getEntryDirPath, - getDistPath, getPackageJson, }, platform: { + addPlatform, + getAbsoluteDistPath, + getDistPath, + getEntryDirPath, + getEntryPath, getPlatform, getPlatformName, - addPlatform, }, }; diff --git a/packages/webpack5/src/helpers/log.ts b/packages/webpack5/src/helpers/log.ts index 3fe9783e8..f2dedb64f 100644 --- a/packages/webpack5/src/helpers/log.ts +++ b/packages/webpack5/src/helpers/log.ts @@ -15,7 +15,9 @@ export function error(...data: any): Error { // we return the error - the caller can throw or ignore if (typeof data[0] === 'string') { - return new Error(data[0]); + return new Error( + '\n\n[@nativescript/webpack]\n---\n\n' + dedent(data[0]) + '\n\n---\n' + ); } return new Error('@nativescript/webpack ran into a problem...'); diff --git a/packages/webpack5/src/helpers/platform.ts b/packages/webpack5/src/helpers/platform.ts new file mode 100644 index 000000000..271d40b34 --- /dev/null +++ b/packages/webpack5/src/helpers/platform.ts @@ -0,0 +1,97 @@ +import { dirname, resolve } from 'path'; + +import { getPackageJson, getProjectRootPath } from './project'; +import { error } from './log'; +import { env } from '../'; + +import AndroidPlatform from '../platforms/android'; +import iOSPlatform from '../platforms/ios'; + +export interface INativeScriptPlatform { + getEntryPath?(): string; + + getDistPath?(): string; +} + +export type Platform = Extract; + +const platforms: { + [name: string]: INativeScriptPlatform; +} = { + android: AndroidPlatform, + ios: iOSPlatform, +}; + +export function addPlatform(name: string, platform: INativeScriptPlatform) { + console.log('adding platform', name, platform); + platforms[name] = platform; +} + +export function getPlatform(): INativeScriptPlatform { + return platforms[getPlatformName()]; +} + +export function getPlatformName(): Platform { + if (env?.android) { + return 'android'; + } + + if (env?.ios) { + return 'ios'; + } + + // support custom platforms + if (env?.platform) { + if (platforms[env.platform]) { + return env.platform; + } + + throw error(` + Invalid platform: ${env.platform} + + Valid platforms: ${Object.keys(platforms).join(', ')} + `); + } + + throw error(` + You need to provide a target platform! + + Available platforms: ${Object.keys(platforms).join(', ')} + + Use --env=platform= or --env=android, --env=ios to specify the target platform. + `); +} + +export function getEntryPath() { + const platform = getPlatform(); + + // use platform specific entry path + if (platform.getEntryPath) { + return platform.getEntryPath(); + } + + // fallback to main field in package.json + const packageJson = getPackageJson(); + + return resolve(getProjectRootPath(), packageJson.main); +} + +export function getEntryDirPath() { + return dirname(getEntryPath()); +} + +export function getDistPath() { + const platform = getPlatform(); + + // use platform specific entry path + if (platform.getDistPath) { + return platform.getDistPath(); + } + + // fallback to a generic platforms//dist folder + return `platforms/${getPlatformName()}/dist`; +} + +export function getAbsoluteDistPath() { + return resolve(getProjectRootPath(), getDistPath()); +} diff --git a/packages/webpack5/src/helpers/project.ts b/packages/webpack5/src/helpers/project.ts index 680f5db6d..e2f5e1cd7 100644 --- a/packages/webpack5/src/helpers/project.ts +++ b/packages/webpack5/src/helpers/project.ts @@ -1,45 +1,9 @@ -import { resolve, dirname } from 'path'; - -import { getPlatform } from '../platforms'; +import { resolve } from 'path'; export function getProjectRootPath(): string { return process.cwd(); } -export function getEntryPath() { - const platform = getPlatform(); - - // use platform specific entry path - if (platform.getEntryPath) { - return platform.getEntryPath(); - } - - // fallback to main field in package.json - const packageJson = getPackageJson(); - - return resolve(getProjectRootPath(), packageJson.main); -} - -export function getEntryDirPath() { - return dirname(getEntryPath()); -} - -export function getDistPath() { - const platform = getPlatform(); - - // use platform specific entry path - if (platform.getDistPath) { - return platform.getDistPath(); - } - - // fallback to a generic dist folder - return 'dist'; -} - -export function getAbsoluteDistPath() { - return resolve(getProjectRootPath(), getDistPath()); -} - interface IPackageJson { main?: string; dependencies?: { diff --git a/packages/webpack5/src/index.ts b/packages/webpack5/src/index.ts index 07bb71957..d027d6308 100644 --- a/packages/webpack5/src/index.ts +++ b/packages/webpack5/src/index.ts @@ -9,8 +9,6 @@ import { error, info } from './helpers/log'; import { configs } from './configuration'; import helpers from './helpers'; -export type Platform = 'android' | 'ios' | string; - export interface IWebpackEnv { [name: string]: any; @@ -125,12 +123,16 @@ export function resolveChainableConfig(): Config { 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} - `); + // catch and print errors from plugins + return error(` + Unable to apply chain function from: ${plugin}. + Error is: ${err} + `); } + + // otherwise throw - as the error is likely from the user config + // or missing env flags (eg. missing platform) + throw err; } }); diff --git a/packages/webpack5/src/platforms/android.ts b/packages/webpack5/src/platforms/android.ts index 8fbc59f85..c83f9db8f 100644 --- a/packages/webpack5/src/platforms/android.ts +++ b/packages/webpack5/src/platforms/android.ts @@ -1,4 +1,4 @@ -import { INativeScriptPlatform } from '.'; +import { INativeScriptPlatform } from "../helpers/platform"; function getDistPath() { return `platforms/android/app/src/main/assets/app`; diff --git a/packages/webpack5/src/platforms/index.ts b/packages/webpack5/src/platforms/index.ts deleted file mode 100644 index 6f995cd8f..000000000 --- a/packages/webpack5/src/platforms/index.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { error } from "../helpers/log"; -import { env, Platform } from "../"; - -import AndroidPlatform from "./android"; -import iOSPlatform from "./ios"; - -export interface INativeScriptPlatform { - getEntryPath?(): string; - getDistPath?(): string -} - -const platforms = { - android: AndroidPlatform, - ios: iOSPlatform, -} - -export function addPlatform(name: string, platform: INativeScriptPlatform) { - console.log('adding platform', name, platform) - platforms[name] = platform; -} - -export function getPlatform(): INativeScriptPlatform { - return platforms[getPlatformName()] -} - -export function getPlatformName(): Platform { - if (env?.android) { - return 'android'; - } - - if (env?.ios) { - return 'ios'; - } - - // support custom platforms - if(env?.platform) { - return env.platform; - } - - throw error(` - You need to provide a target platform! - - Available platforms: ${Object.keys(platforms).join(', ')} - - Use --env=platform= or --env=android, --env=ios to specify the target platform. - `); -} diff --git a/packages/webpack5/src/platforms/ios.ts b/packages/webpack5/src/platforms/ios.ts index 3cbb4402a..7fc648c38 100644 --- a/packages/webpack5/src/platforms/ios.ts +++ b/packages/webpack5/src/platforms/ios.ts @@ -1,7 +1,7 @@ import { basename } from "path"; +import { INativeScriptPlatform } from "../helpers/platform"; import { getProjectRootPath } from "../helpers/project"; -import { INativeScriptPlatform } from '.'; function getDistPath() { const appName = basename(getProjectRootPath());