mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat: additional base setup
This commit is contained in:
@@ -1,9 +1,52 @@
|
||||
import Config from 'webpack-chain';
|
||||
import { IWebpackEnv } from './index';
|
||||
import { IWebpackEnv, WebpackPlatform } from './index';
|
||||
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
|
||||
import { getDistPath } from '../helpers/projectHelpers';
|
||||
|
||||
// todo: add base configuration that's shared across all flavors
|
||||
export default function (env: IWebpackEnv): Config {
|
||||
const config = new Config();
|
||||
config.entry('');
|
||||
const distPath = getDistPath(env);
|
||||
|
||||
// inspector_modules
|
||||
config.when(shouldIncludeInspectorModules(env), (config) => {
|
||||
config.entry('inspector_modules').add('tns_modules/@nativescript/core/inspector_modules').end();
|
||||
});
|
||||
|
||||
config.entry('bundle').add('todo/main').end();
|
||||
|
||||
// base aliases
|
||||
config.resolve.alias.set('~/package.json', 'package.json').set('~', '<TODO>appFullPath').set('@', '<TODO>appFullPath');
|
||||
|
||||
// resolve symlinks
|
||||
config.resolve.symlinks(true);
|
||||
|
||||
// items to clean
|
||||
config.plugin('clean').use(CleanWebpackPlugin, [
|
||||
{
|
||||
cleanOnceBeforeBuildPatterns: [`${distPath}/**/*`],
|
||||
verbose: true,
|
||||
},
|
||||
]);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
function shouldIncludeInspectorModules(env: IWebpackEnv): boolean {
|
||||
const platform = determinePlatformFromEnv(env);
|
||||
// todo: check if core modules are external
|
||||
// todo: check if we are testing
|
||||
return platform === WebpackPlatform.ios;
|
||||
}
|
||||
|
||||
function determinePlatformFromEnv(env: IWebpackEnv): WebpackPlatform {
|
||||
if (env?.android) {
|
||||
return WebpackPlatform.android;
|
||||
}
|
||||
|
||||
if (env?.ios) {
|
||||
return WebpackPlatform.ios;
|
||||
}
|
||||
|
||||
throw new Error('You need to provide a target platform!');
|
||||
}
|
||||
|
||||
@@ -7,19 +7,36 @@ import svelte from './svelte';
|
||||
import typescript from './typescript';
|
||||
import vue from './vue';
|
||||
|
||||
// export chain configs
|
||||
// todo: rename if needed
|
||||
export { base as __base, angular as __angular, javascript as __javascript, react as __react, svelte as __svelte, typescript as __typescript, vue as __vue };
|
||||
|
||||
// export final configs
|
||||
// todo: perhaps we can export chain configs as well
|
||||
export const baseConfig = (env: IWebpackEnv) => base(env).toConfig();
|
||||
|
||||
export const baseConfig = (env) => base(env).toConfig();
|
||||
|
||||
export const angularConfig = (env) => angular(env).toConfig();
|
||||
export const javascriptConfig = (env) => javascript(env).toConfig();
|
||||
export const reactConfig = (env) => react(env).toConfig();
|
||||
export const svelteConfig = (env) => svelte(env).toConfig();
|
||||
export const typescriptConfig = (env) => typescript(env).toConfig();
|
||||
export const vueConfig = (env) => vue(env).toConfig();
|
||||
export const angularConfig = (env: IWebpackEnv) => angular(env).toConfig();
|
||||
export const javascriptConfig = (env: IWebpackEnv) => javascript(env).toConfig();
|
||||
export const reactConfig = (env: IWebpackEnv) => react(env).toConfig();
|
||||
export const svelteConfig = (env: IWebpackEnv) => svelte(env).toConfig();
|
||||
export const typescriptConfig = (env: IWebpackEnv) => typescript(env).toConfig();
|
||||
export const vueConfig = (env: IWebpackEnv) => vue(env).toConfig();
|
||||
|
||||
export interface IWebpackEnv {
|
||||
hmr: boolean;
|
||||
[name: string]: any;
|
||||
|
||||
appPath?: string;
|
||||
appResourcesPath?: string;
|
||||
|
||||
android?: boolean;
|
||||
ios?: boolean;
|
||||
|
||||
production?: boolean;
|
||||
report?: boolean;
|
||||
hmr?: boolean;
|
||||
// todo: add others
|
||||
}
|
||||
|
||||
export enum WebpackPlatform {
|
||||
'ios',
|
||||
'android',
|
||||
}
|
||||
|
||||
@@ -1,27 +1,40 @@
|
||||
import { existsSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import { IWebpackEnv } from '@nativescript/webpack';
|
||||
|
||||
import { existsSync } from "fs";
|
||||
import { resolve } from "path";
|
||||
export function getDistPath(env: IWebpackEnv) {
|
||||
if (env.ios) {
|
||||
return `platforms/ios/[todo]/app`;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
export function getPackageJson(projectDir: string) {
|
||||
const packageJsonPath = getPackageJsonPath(projectDir);
|
||||
const result = readJsonFile(packageJsonPath);
|
||||
const packageJsonPath = getPackageJsonPath(projectDir);
|
||||
const result = readJsonFile(packageJsonPath);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
export function readJsonFile(filePath:string) {
|
||||
return require(filePath) as {
|
||||
main:string
|
||||
// to be extended?
|
||||
};
|
||||
export function readJsonFile(filePath: string) {
|
||||
return require(filePath) as {
|
||||
main: string;
|
||||
// to be extended?
|
||||
};
|
||||
}
|
||||
|
||||
export function getPackageJsonPath (projectDir: string) {
|
||||
const packagePath = resolve(projectDir, "package.json");
|
||||
if (existsSync(packagePath)) {
|
||||
return packagePath;
|
||||
} else {
|
||||
return getPackageJsonPath(resolve(projectDir, '..'));
|
||||
}
|
||||
|
||||
}
|
||||
export function getPackageJsonPath(projectDir: string) {
|
||||
const packagePath = resolve(projectDir, 'package.json');
|
||||
if (existsSync(packagePath)) {
|
||||
return packagePath;
|
||||
} else {
|
||||
return getPackageJsonPath(resolve(projectDir, '..'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user