refactor: move getPlatform to helpers

This commit is contained in:
Igor Randjelovic
2020-11-19 22:20:48 +01:00
parent 054d4e6f87
commit e23c8356af
4 changed files with 24 additions and 16 deletions

View File

@ -24,6 +24,7 @@ exports[`vue configuration for android 1`] = `
vue: 'nativescript-vue' vue: 'nativescript-vue'
}, },
extensions: [ extensions: [
'.android.vue',
'.vue', '.vue',
'.android.ts', '.android.ts',
'.ts', '.ts',
@ -195,6 +196,7 @@ exports[`vue configuration for ios 1`] = `
vue: 'nativescript-vue' vue: 'nativescript-vue'
}, },
extensions: [ extensions: [
'.ios.vue',
'.vue', '.vue',
'.ios.ts', '.ios.ts',
'.ts', '.ts',

View File

@ -5,6 +5,7 @@ import {
getDistPath, getDistPath,
getEntryPath, getEntryPath,
getPackageJson, getPackageJson,
getPlatform,
} from '../helpers/project'; } from '../helpers/project';
import { CleanWebpackPlugin } from 'clean-webpack-plugin'; import { CleanWebpackPlugin } from 'clean-webpack-plugin';
@ -15,7 +16,7 @@ import TerserPlugin from 'terser-webpack-plugin';
export default function (config: Config, env: IWebpackEnv): Config { export default function (config: Config, env: IWebpackEnv): Config {
const entryPath = getEntryPath(); const entryPath = getEntryPath();
const distPath = getDistPath(); const distPath = getDistPath();
const platform = determinePlatformFromEnv(env); const platform = getPlatform();
const packageJson = getPackageJson(); const packageJson = getPackageJson();
const mode = env.production ? 'production' : 'development'; const mode = env.production ? 'production' : 'development';
@ -175,20 +176,8 @@ export default function (config: Config, env: IWebpackEnv): Config {
} }
function shouldIncludeInspectorModules(env: IWebpackEnv): boolean { function shouldIncludeInspectorModules(env: IWebpackEnv): boolean {
const platform = determinePlatformFromEnv(env); const platform = getPlatform();
// todo: check if core modules are external // todo: check if core modules are external
// todo: check if we are testing // todo: check if we are testing
return platform === 'ios'; return platform === 'ios';
} }
function determinePlatformFromEnv(env: IWebpackEnv): Platform {
if (env?.android) {
return 'android';
}
if (env?.ios) {
return 'ios';
}
throw new Error('You need to provide a target platform!');
}

View File

@ -3,12 +3,16 @@ import Config from 'webpack-chain';
import { VueLoaderPlugin } from 'vue-loader'; import { VueLoaderPlugin } from 'vue-loader';
import { env as _env, IWebpackEnv } from '../index'; import { env as _env, IWebpackEnv } from '../index';
import { merge } from 'webpack-merge'; import { merge } from 'webpack-merge';
import { getPlatform } from '../helpers/project';
export default function (config: Config, env: IWebpackEnv = _env): Config { export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env); base(config, env);
const platform = getPlatform();
// resolve .vue files // resolve .vue files
config.resolve.extensions.prepend('.vue'); // the order is reversed because we are using prepend!
config.resolve.extensions.prepend('.vue').prepend(`.${platform}.vue`);
// add a rule for .vue files // add a rule for .vue files
config.module config.module

View File

@ -1,4 +1,4 @@
import { env } from '../index'; import { env, Platform } from '../index';
import { resolve, basename } from 'path'; import { resolve, basename } from 'path';
export function getProjectRootPath(): string { export function getProjectRootPath(): string {
@ -33,6 +33,19 @@ export function getDistPath() {
// 3rd party platforms would be treated the same // 3rd party platforms would be treated the same
} }
export function getPlatform(): Platform {
if (env?.android) {
return 'android';
}
if (env?.ios) {
return 'ios';
}
// todo: maybe no throw?
throw new Error('You need to provide a target platform!');
}
interface IPackageJson { interface IPackageJson {
main?: string; main?: string;
dependencies?: { dependencies?: {