mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 02:54:11 +08:00
chore: cleanup
This commit is contained in:
@ -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 {
|
||||
|
@ -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();
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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';
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import CopyWebpackPlugin from 'copy-webpack-plugin';
|
||||
|
||||
import { getEntryDirPath } from './project';
|
||||
import { getEntryDirPath } from './platform';
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
@ -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,
|
||||
},
|
||||
};
|
||||
|
@ -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...');
|
||||
|
97
packages/webpack5/src/helpers/platform.ts
Normal file
97
packages/webpack5/src/helpers/platform.ts
Normal file
@ -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<keyof typeof platforms, string>;
|
||||
|
||||
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=<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/<platform>/dist folder
|
||||
return `platforms/${getPlatformName()}/dist`;
|
||||
}
|
||||
|
||||
export function getAbsoluteDistPath() {
|
||||
return resolve(getProjectRootPath(), getDistPath());
|
||||
}
|
@ -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?: {
|
||||
|
@ -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(`
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { INativeScriptPlatform } from '.';
|
||||
import { INativeScriptPlatform } from "../helpers/platform";
|
||||
|
||||
function getDistPath() {
|
||||
return `platforms/android/app/src/main/assets/app`;
|
||||
|
@ -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=<platform> or --env=android, --env=ios to specify the target platform.
|
||||
`);
|
||||
}
|
@ -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());
|
||||
|
Reference in New Issue
Block a user