From 429328495caa0d354fff8c106c246c089fb66c97 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Fri, 19 Jan 2024 20:23:25 +0100 Subject: [PATCH] feat(wepback): custom `buildPath`support (#10477) Co-authored-by: farfromrefuge --- packages/webpack5/src/configuration/angular.ts | 1 + packages/webpack5/src/configuration/base.ts | 8 +++++--- packages/webpack5/src/helpers/copyRules.ts | 4 +--- packages/webpack5/src/helpers/fileReplacements.ts | 2 +- packages/webpack5/src/helpers/index.ts | 2 +- packages/webpack5/src/helpers/log.ts | 2 +- packages/webpack5/src/helpers/platform.ts | 2 +- packages/webpack5/src/helpers/virtualModules.ts | 12 ++++++------ packages/webpack5/src/index.ts | 3 +++ packages/webpack5/src/platforms/android.ts | 3 ++- packages/webpack5/src/platforms/ios.ts | 3 ++- packages/webpack5/src/platforms/visionos.ts | 3 ++- 12 files changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/webpack5/src/configuration/angular.ts b/packages/webpack5/src/configuration/angular.ts index a106a114d..18fddf47b 100644 --- a/packages/webpack5/src/configuration/angular.ts +++ b/packages/webpack5/src/configuration/angular.ts @@ -13,6 +13,7 @@ import { getPlatformName, } from '../helpers/platform'; import base from './base'; + // until we can switch to async/await on the webpack config, copy this from '@angular/compiler-cli' const GLOBAL_DEFS_FOR_TERSER = { ngDevMode: false, diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 6da829a88..9435b2c9a 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -11,11 +11,11 @@ import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; import TerserPlugin from 'terser-webpack-plugin'; +import { getProjectFilePath, getProjectTSConfigPath } from '../helpers/project'; import { PlatformSuffixPlugin } from '../plugins/PlatformSuffixPlugin'; import { applyFileReplacements } from '../helpers/fileReplacements'; import { addCopyRule, applyCopyRules } from '../helpers/copyRules'; import { WatchStatePlugin } from '../plugins/WatchStatePlugin'; -import { getProjectFilePath, getProjectTSConfigPath } from '../helpers/project'; import { hasDependency } from '../helpers/dependencies'; import { applyDotEnvPlugin } from '../helpers/dotEnv'; import { env as _env, IWebpackEnv } from '../index'; @@ -84,7 +84,9 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { // when using hidden-source-map, output source maps to the `platforms/{platformName}-sourceMaps` folder if (env.sourceMap === 'hidden-source-map') { const sourceMapAbsolutePath = getProjectFilePath( - `./platforms/${platform}-sourceMaps/[file].map[query]` + `./${ + env.buildPath ?? 'platforms' + }/${platform}-sourceMaps/[file].map[query]` ); const sourceMapRelativePath = relative(outputPath, sourceMapAbsolutePath); config.output.sourceMapFilename(sourceMapRelativePath); @@ -131,7 +133,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { config.watchOptions({ ignored: [ - `${getProjectFilePath('platforms')}/**`, + `${getProjectFilePath(env.buildPath ?? 'platforms')}/**`, `${getProjectFilePath(env.appResourcesPath ?? 'App_Resources')}/**`, ], }); diff --git a/packages/webpack5/src/helpers/copyRules.ts b/packages/webpack5/src/helpers/copyRules.ts index 188443441..e659275ca 100644 --- a/packages/webpack5/src/helpers/copyRules.ts +++ b/packages/webpack5/src/helpers/copyRules.ts @@ -1,9 +1,7 @@ import CopyWebpackPlugin from 'copy-webpack-plugin'; -import { basename, relative, resolve } from 'path'; import Config from 'webpack-chain'; -import { sync as globbySync } from 'globby'; +import { basename } from 'path'; -import { getProjectRootPath } from './project'; import { getEntryDirPath } from './platform'; import { env } from '..'; diff --git a/packages/webpack5/src/helpers/fileReplacements.ts b/packages/webpack5/src/helpers/fileReplacements.ts index f36f1b8a4..ae7c96fa5 100644 --- a/packages/webpack5/src/helpers/fileReplacements.ts +++ b/packages/webpack5/src/helpers/fileReplacements.ts @@ -1,8 +1,8 @@ import { resolve } from 'path'; import { env as _env, IWebpackEnv } from '../index'; -import { addCopyRule } from './copyRules'; import { getProjectRootPath } from './project'; +import { addCopyRule } from './copyRules'; interface IReplacementMap { [_replace: string]: /* _with */ string; diff --git a/packages/webpack5/src/helpers/index.ts b/packages/webpack5/src/helpers/index.ts index 88c5b6267..260095b6a 100644 --- a/packages/webpack5/src/helpers/index.ts +++ b/packages/webpack5/src/helpers/index.ts @@ -10,6 +10,7 @@ import { applyFileReplacements } from './fileReplacements'; import { addCopyRule, removeCopyRule } from './copyRules'; import { error, info, warn, warnOnce } from './log'; import { determineProjectFlavor } from './flavor'; +import { readTsConfig } from './typescript'; import { getValue } from './config'; import { getIPS } from './host'; import { @@ -26,7 +27,6 @@ import { getPlatform, getPlatformName, } from './platform'; -import { readTsConfig } from './typescript'; // intentionally populated manually // as this generates nicer typings diff --git a/packages/webpack5/src/helpers/log.ts b/packages/webpack5/src/helpers/log.ts index ac70c163a..753027821 100644 --- a/packages/webpack5/src/helpers/log.ts +++ b/packages/webpack5/src/helpers/log.ts @@ -1,5 +1,5 @@ -import dedent from 'ts-dedent'; import { env } from '@nativescript/webpack'; +import dedent from 'ts-dedent'; // de-indents strings so multi-line string literals can be used function cleanup(data: any[]) { diff --git a/packages/webpack5/src/helpers/platform.ts b/packages/webpack5/src/helpers/platform.ts index b60e5925b..5b23a7714 100644 --- a/packages/webpack5/src/helpers/platform.ts +++ b/packages/webpack5/src/helpers/platform.ts @@ -138,7 +138,7 @@ export function getDistPath() { } // fallback to a generic platforms//dist folder - return `platforms/${getPlatformName()}/dist`; + return `${env.buildPath ?? 'platforms'}/${getPlatformName()}/dist`; } /** diff --git a/packages/webpack5/src/helpers/virtualModules.ts b/packages/webpack5/src/helpers/virtualModules.ts index b03990111..88f681582 100644 --- a/packages/webpack5/src/helpers/virtualModules.ts +++ b/packages/webpack5/src/helpers/virtualModules.ts @@ -1,11 +1,11 @@ -import { ContextExclusionPlugin } from 'webpack'; -import Config from 'webpack-chain'; -import { dirname, join } from 'path'; -import { writeFileSync, mkdirSync } from 'fs'; - import VirtualModulesPlugin from 'webpack-virtual-modules'; -import { getEntryDirPath } from './platform'; +import { ContextExclusionPlugin } from 'webpack'; +import { mkdirSync, writeFileSync } from 'fs'; +import { dirname, join } from 'path'; +import Config from 'webpack-chain'; import dedent from 'ts-dedent'; + +import { getEntryDirPath } from './platform'; import { getProjectFilePath } from './project'; /** diff --git a/packages/webpack5/src/index.ts b/packages/webpack5/src/index.ts index f9ac6f073..7b79120b3 100644 --- a/packages/webpack5/src/index.ts +++ b/packages/webpack5/src/index.ts @@ -26,6 +26,7 @@ export interface IWebpackEnv { appPath?: string; appResourcesPath?: string; + buildPath?: string; appComponents?: string[]; nativescriptLibPath?: string | boolean; @@ -65,6 +66,7 @@ let webpackMerges: any[] = []; let explicitUseConfig = false; let hasInitialized = false; let currentPlugin: string | undefined; + /** * @internal */ @@ -85,6 +87,7 @@ export function clearCurrentPlugin() { } ////// PUBLIC API + /** * The default flavor specific configs */ diff --git a/packages/webpack5/src/platforms/android.ts b/packages/webpack5/src/platforms/android.ts index c83f9db8f..4e7b6718b 100644 --- a/packages/webpack5/src/platforms/android.ts +++ b/packages/webpack5/src/platforms/android.ts @@ -1,7 +1,8 @@ import { INativeScriptPlatform } from "../helpers/platform"; +import { env } from '../'; function getDistPath() { - return `platforms/android/app/src/main/assets/app`; + return `${env.buildPath ?? "platforms"}/android/app/src/main/assets/app`; } const AndroidPlatform: INativeScriptPlatform = { diff --git a/packages/webpack5/src/platforms/ios.ts b/packages/webpack5/src/platforms/ios.ts index dd2d3defb..2ae91ba98 100644 --- a/packages/webpack5/src/platforms/ios.ts +++ b/packages/webpack5/src/platforms/ios.ts @@ -2,6 +2,7 @@ import { basename } from "path"; import { INativeScriptPlatform } from "../helpers/platform"; import { getProjectRootPath } from "../helpers/project"; +import { env } from '../'; function sanitizeName(appName: string): string { return appName.split("").filter((c) => @@ -10,7 +11,7 @@ function sanitizeName(appName: string): string { } function getDistPath() { const appName = sanitizeName(basename(getProjectRootPath())); - return `platforms/ios/${appName}/app`; + return `${env.buildPath ?? "platforms"}/ios/${appName}/app`; } const iOSPlatform: INativeScriptPlatform = { diff --git a/packages/webpack5/src/platforms/visionos.ts b/packages/webpack5/src/platforms/visionos.ts index 9e8a4e4ce..fde5d5a97 100644 --- a/packages/webpack5/src/platforms/visionos.ts +++ b/packages/webpack5/src/platforms/visionos.ts @@ -2,6 +2,7 @@ import { basename } from "path"; import { INativeScriptPlatform } from "../helpers/platform"; import { getProjectRootPath } from "../helpers/project"; +import { env } from '../'; function sanitizeName(appName: string): string { return appName.split("").filter((c) => @@ -10,7 +11,7 @@ function sanitizeName(appName: string): string { } function getDistPath() { const appName = sanitizeName(basename(getProjectRootPath())); - return `platforms/visionos/${appName}/app`; + return `${env.buildPath ?? "platforms"}/visionos/${appName}/app`; } const visionOSPlatform: INativeScriptPlatform = {