From ee3e41bb382ae156b3e24d8d0168f0c4d487f13d Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Tue, 15 Aug 2017 09:21:49 -0500 Subject: [PATCH] feat(sourcemaps): preserve prod sourcemaps out of code dir --- src/util/config.ts | 8 ++++++++ src/util/constants.ts | 3 +++ src/util/interfaces.ts | 1 + src/util/source-maps.ts | 17 ++++++++++++++--- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/util/config.ts b/src/util/config.ts index c1f98632..3acc9b5e 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -90,6 +90,10 @@ export function generateContext(context?: BuildContext): BuildContext { setProcessEnvVar(Constants.ENV_VAR_BUILD_DIR, context.buildDir); Logger.debug(`buildDir set to ${context.buildDir}`); + context.sourcemapDir = resolve(context.sourcemapDir || getConfigValue(context, '--sourcemapDir', null, Constants.ENV_VAR_SOURCEMAP_DIR, Constants.ENV_VAR_SOURCEMAP_DIR.toLowerCase(), Constants.SOURCEMAP_DIR)); + setProcessEnvVar(Constants.ENV_VAR_SOURCEMAP_DIR, context.sourcemapDir); + Logger.debug(`sourcemapDir set to ${context.sourcemapDir}`); + context.pagesDir = resolve(context.pagesDir || getConfigValue(context, '--pagesDir', null, Constants.ENV_VAR_PAGES_DIR, Constants.ENV_VAR_PAGES_DIR.toLowerCase(), join(context.srcDir, 'pages'))); setProcessEnvVar(Constants.ENV_VAR_PAGES_DIR, context.pagesDir); Logger.debug(`pagesDir set to ${context.pagesDir}`); @@ -164,6 +168,10 @@ export function generateContext(context?: BuildContext): BuildContext { setProcessEnvVar(Constants.ENV_VAR_SOURCE_MAP_TYPE, sourceMapTypeValue); Logger.debug(`sourceMapType set to ${sourceMapTypeValue}`); + const moveSourceMaps = getConfigValue(context, '--moveSourceMaps', null, Constants.ENV_VAR_MOVE_SOURCE_MAPS, Constants.ENV_VAR_MOVE_SOURCE_MAPS.toLowerCase(), 'true'); + setProcessEnvVar(Constants.ENV_VAR_MOVE_SOURCE_MAPS, moveSourceMaps); + Logger.debug(`moveSourceMaps set to ${moveSourceMaps}`); + const tsConfigPathValue = resolve(getConfigValue(context, '--tsconfig', null, Constants.ENV_TS_CONFIG, Constants.ENV_TS_CONFIG.toLowerCase(), join(context.rootDir, 'tsconfig.json'))); setProcessEnvVar(Constants.ENV_TS_CONFIG, tsConfigPathValue); Logger.debug(`tsconfig set to ${tsConfigPathValue}`); diff --git a/src/util/constants.ts b/src/util/constants.ts index 16ce484b..212c7767 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -10,6 +10,7 @@ export const SOURCE_MAP_TYPE_EXPENSIVE = 'source-map'; export const BUILD_DIR = 'build'; export const SRC_DIR = 'src'; export const TMP_DIR = '.tmp'; +export const SOURCEMAP_DIR = '.sourcemaps'; export const WWW_DIR = 'www'; export const NODE_MODULES = 'node_modules'; export const IONIC_ANGULAR = 'ionic-angular'; @@ -35,6 +36,7 @@ export const ENV_VAR_PIPES_DIR = 'IONIC_PIPES_DIR'; export const ENV_VAR_PROVIDERS_DIR = 'IONIC_PROVIDERS_DIR'; export const ENV_VAR_TMP_DIR = 'IONIC_TMP_DIR'; export const ENV_VAR_WWW_DIR = 'IONIC_WWW_DIR'; +export const ENV_VAR_SOURCEMAP_DIR = 'IONIC_SOURCEMAP_DIR'; export const ENV_VAR_HTML_TO_SERVE = 'IONIC_HTML_TO_SERVE'; export const ENV_VAR_BUILD_DIR = 'IONIC_BUILD_DIR'; export const ENV_VAR_NODE_MODULES_DIR = 'IONIC_NODE_MODULES_DIR'; @@ -50,6 +52,7 @@ export const ENV_VAR_IONIC_ANGULAR_ENTRY_POINT = 'IONIC_ANGULAR_ENTRY_POINT'; export const ENV_VAR_APP_SCRIPTS_DIR = 'IONIC_APP_SCRIPTS_DIR'; export const ENV_VAR_GENERATE_SOURCE_MAP = 'IONIC_GENERATE_SOURCE_MAP'; export const ENV_VAR_SOURCE_MAP_TYPE = 'IONIC_SOURCE_MAP_TYPE'; +export const ENV_VAR_MOVE_SOURCE_MAPS = 'IONIC_MOVE_SOURCE_MAPS'; export const ENV_TS_CONFIG = 'IONIC_TS_CONFIG'; export const ENV_APP_ENTRY_POINT = 'IONIC_APP_ENTRY_POINT'; export const ENV_APP_NG_MODULE_PATH = 'IONIC_APP_NG_MODULE_PATH'; diff --git a/src/util/interfaces.ts b/src/util/interfaces.ts index 0c2b6606..5e1a028d 100644 --- a/src/util/interfaces.ts +++ b/src/util/interfaces.ts @@ -40,6 +40,7 @@ export interface BuildContext { inlineTemplates?: boolean; webpackWatch?: any; ionicGlobal?: any; + sourcemapDir?: string; sassState?: BuildState; transpileState?: BuildState; diff --git a/src/util/source-maps.ts b/src/util/source-maps.ts index 9aa608e6..8e2dcc06 100644 --- a/src/util/source-maps.ts +++ b/src/util/source-maps.ts @@ -1,6 +1,7 @@ -import { join } from 'path'; +import { join, relative, basename } from 'path'; +import { mkdirpSync } from 'fs-extra'; import * as Constants from './constants'; -import { getBooleanPropertyValue, readDirAsync, unlinkAsync } from './helpers'; +import { copyFileAsync, getBooleanPropertyValue, readDirAsync, unlinkAsync } from './helpers'; import { BuildContext } from './interfaces'; export function purgeSourceMapsIfNeeded(context: BuildContext): Promise { @@ -12,8 +13,18 @@ export function purgeSourceMapsIfNeeded(context: BuildContext): Promise { const sourceMaps = fileNames.filter(fileName => fileName.endsWith('.map')); const fullPaths = sourceMaps.map(sourceMap => join(context.buildDir, sourceMap)); const promises: Promise[] = []; + const copyBeforePurge = getBooleanPropertyValue(Constants.ENV_VAR_MOVE_SOURCE_MAPS); for (const fullPath of fullPaths) { - promises.push(unlinkAsync(fullPath)); + if (copyBeforePurge) { + mkdirpSync(context.sourcemapDir) + const relativeTo = relative(fullPath, context.sourcemapDir) + const fileName = basename(fullPath) + promises.push(copyFileAsync(fullPath, join(context.sourcemapDir, fileName)).then(() => { + return unlinkAsync(fullPath) + })) + } else { + promises.push(unlinkAsync(fullPath)) + } } return Promise.all(promises); });