feat: extract platforms

This commit is contained in:
Igor Randjelovic
2020-12-07 13:41:22 +01:00
parent 7df2f09cfc
commit 2f39cf5ed6
15 changed files with 377 additions and 44 deletions

View File

@@ -11,17 +11,17 @@ 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 {
getAbsoluteDistPath,
getEntryDirPath,
getEntryPath,
getPlatform,
} from '../helpers/project';
export default function (config: Config, env: IWebpackEnv): Config {
const entryPath = getEntryPath();
const platform = getPlatform();
const platform = getPlatformName();
const mode = env.production ? 'production' : 'development';
// set mode
@@ -51,7 +51,7 @@ export default function (config: Config, env: IWebpackEnv): Config {
.add(entryPath);
// inspector_modules
config.when(shouldIncludeInspectorModules(env), (config) => {
config.when(shouldIncludeInspectorModules(), (config) => {
config
.entry('tns_modules/@nativescript/core/inspector_modules')
.add('@nativescript/core/inspector_modules');
@@ -272,8 +272,8 @@ export default function (config: Config, env: IWebpackEnv): Config {
return config;
}
function shouldIncludeInspectorModules(env: IWebpackEnv): boolean {
const platform = getPlatform();
function shouldIncludeInspectorModules(): boolean {
const platform = getPlatformName();
// todo: check if core modules are external
// todo: check if we are testing
return platform === 'ios';

View File

@@ -2,13 +2,13 @@ import { merge } from 'webpack-merge';
import Config from 'webpack-chain';
import { env as _env, IWebpackEnv } from '../index';
import { getPlatform } from '../helpers/project';
import { getPlatformName } from '../platforms';
import base from './base';
export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env);
const platform = getPlatform();
const platform = getPlatformName();
const mode = env.production ? 'production' : 'development';
const production = mode === 'production';

View File

@@ -1,15 +1,16 @@
import svelteNativePreprocessor from 'svelte-native-preprocessor';
import Config from 'webpack-chain';
import { getProjectRootPath } from '../helpers/project';
import { env as _env, IWebpackEnv } from '../index';
import { getPlatform, getProjectRootPath } from '../helpers/project';
import base from './base';
import { getPlatformName } from '../platforms';
import { error } from '../helpers/log';
import base from './base';
export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env);
const platform = getPlatform();
const platform = getPlatformName();
const mode = env.production ? 'production' : 'development';
const production = mode === 'production';

View File

@@ -3,13 +3,13 @@ import { merge } from 'webpack-merge';
import Config from 'webpack-chain';
import { env as _env, IWebpackEnv } from '../index';
import { getPlatform } from '../helpers/project';
import { getPlatformName } from '../platforms';
import base from './base';
export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env);
const platform = getPlatform();
const platform = getPlatformName();
// resolve .vue files
// the order is reversed because we are using prepend!

View File

@@ -15,9 +15,9 @@ import {
getEntryPath,
getEntryDirPath,
getPackageJson,
getPlatform,
getProjectRootPath,
} from './project';
import { getPlatform, getPlatformName, addPlatform } from '../platforms';
// intentionally populated manually
// as this generates nicer typings
@@ -50,7 +50,11 @@ export default {
getEntryPath,
getEntryDirPath,
getDistPath,
getPlatform,
getPackageJson,
},
platform: {
getPlatform,
getPlatformName,
addPlatform,
},
};

View File

@@ -1,20 +1,20 @@
import { resolve, basename, dirname } from 'path';
import { resolve, dirname } from 'path';
import { env, Platform } from '../index';
import { error } from './log';
import { getPlatform } from '../platforms';
export function getProjectRootPath(): string {
// todo: find actual path?
return process.cwd();
//__dirname
}
export function getAbsoluteDistPath() {
return resolve(getProjectRootPath(), getDistPath());
}
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);
@@ -25,30 +25,19 @@ export function getEntryDirPath() {
}
export function getDistPath() {
if (env.ios) {
const appName = basename(getProjectRootPath());
return `platforms/ios/${appName}/app`;
const platform = getPlatform();
// use platform specific entry path
if (platform.getDistPath) {
return platform.getDistPath();
}
if (env.android) {
return `platforms/android/app/src/main/assets/app`;
}
// todo: additional platforms
// perhaps we could combine platform specifics into "plugins"
// 3rd party platforms would be treated the same
// fallback to a generic dist folder
return 'dist';
}
export function getPlatform(): Platform {
if (env?.android) {
return 'android';
}
if (env?.ios) {
return 'ios';
}
error('You need to provide a target platform!');
export function getAbsoluteDistPath() {
return resolve(getProjectRootPath(), getDistPath());
}
interface IPackageJson {

View File

@@ -21,6 +21,8 @@ export interface IWebpackEnv {
android?: boolean;
ios?: boolean;
// for custom platforms
platform?: string;
production?: boolean;
report?: boolean;

View File

@@ -0,0 +1,11 @@
import { INativeScriptPlatform } from '.';
function getDistPath() {
return `platforms/android/app/src/main/assets/app`;
}
const AndroidPlatform: INativeScriptPlatform = {
getDistPath,
}
export default AndroidPlatform;

View File

@@ -0,0 +1,47 @@
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.
`);
}

View File

@@ -0,0 +1,15 @@
import { basename } from "path";
import { getProjectRootPath } from "../helpers/project";
import { INativeScriptPlatform } from '.';
function getDistPath() {
const appName = basename(getProjectRootPath());
return `platforms/ios/${appName}/app`;
}
const iOSPlatform: INativeScriptPlatform = {
getDistPath,
}
export default iOSPlatform;