feat: env based replacements (#9286)

* feat: file replacement handling for TS and pure file copy replacements

* test: add tests for replacements & refactor a bit

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
This commit is contained in:
Nathan Walker
2021-03-28 16:13:51 -07:00
committed by GitHub
parent 0b32d5a88d
commit 7594d00ed9
11 changed files with 211 additions and 322 deletions

View File

@ -1,12 +1,11 @@
import { resolve, dirname } from 'path';
import Config from 'webpack-chain';
import { existsSync } from 'fs';
import get from 'lodash.get'
import get from 'lodash.get';
import { getProjectFilePath, getProjectRootPath } from '../helpers/project';
import { getEntryPath, getPlatformName } from '../helpers/platform';
import { env as _env, IWebpackEnv } from '../index';
import { addCopyRule } from "../helpers/copyRules";
import base from './base';
export default function (config: Config, env: IWebpackEnv = _env): Config {
@ -14,10 +13,8 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
const tsConfigPath = [
getProjectFilePath('tsconfig.app.json'),
getProjectFilePath('tsconfig.json')
].find(path => existsSync(path))
applyFileReplacements(config)
getProjectFilePath('tsconfig.json'),
].find((path) => existsSync(path));
// remove default ts rule
config.module.rules.delete('ts');
@ -63,32 +60,6 @@ function getAngularCompilerPlugin() {
return AngularCompilerPlugin;
}
/**
* @internal exported for tests
*/
export function applyFileReplacements(
config,
fileReplacements = getFileReplacementsFromWorkspaceConfig()
) {
if (!fileReplacements) {
return
}
Object.entries(fileReplacements).forEach(([_replace, _with]) => {
// in case we are replacing source files - we'll use aliases
if (_replace.match(/\.ts$/)) {
return config.resolve.alias.set(_replace, _with)
}
// otherwise we will override the replaced file with the replacement
addCopyRule({
from: _with, // copy the replacement file
to: _replace, // to the original "to-be-replaced" file
force: true,
})
})
}
// todo: move into project helper if used elsewhere
// todo: write tests
function findFile(fileName, currentDir): string | null {
@ -96,88 +67,29 @@ function findFile(fileName, currentDir): string | null {
const path = resolve(currentDir, fileName);
if (existsSync(path)) {
return path
return path;
}
// bail if we reached the root dir
if (currentDir === resolve('/')) {
return null
return null;
}
// traverse to the parent folder
return findFile(fileName, resolve(currentDir, '..'))
return findFile(fileName, resolve(currentDir, '..'));
}
function findWorkspaceConfig(): string {
const possibleConfigNames = [
'angular.json',
'workspace.json'
]
const possibleConfigNames = ['angular.json', 'workspace.json'];
for (const name of possibleConfigNames) {
const path = findFile(name, getProjectRootPath());
if (path) {
return path
return path;
}
}
// not found
return null;
}
interface IWorkspaceConfigFileReplacement {
replace: string,
with: string
}
interface IReplacementMap {
[from: string]: string
}
/**
* @internal exported for tests
*/
export function getFileReplacementsFromWorkspaceConfig(
configPath: string = findWorkspaceConfig()
): IReplacementMap | null {
const platform = getPlatformName();
if (!_env.configuration || !_env.projectName) {
return null;
}
const configurations = _env.configuration.split(',').map(c => c.trim())
if (!configPath || configPath === '') {
return null;
}
const config = require(configPath);
const project = get(config, `projects.${_env.projectName}`)
const targetProp = project?.architect ? 'architect' : 'targets';
if (!project) {
return null;
}
const replacements = {};
const setReplacement = (entry: IWorkspaceConfigFileReplacement) => {
const relativeReplace = resolve(dirname(configPath), entry.replace)
const relativeWith = resolve(dirname(configPath), entry.with)
replacements[relativeReplace] = relativeWith
}
configurations.forEach(configuration => {
const defaultReplacements = get(project, `${targetProp}.default.configurations.${configuration}.fileReplacements`)
const platformReplacements = get(project, `${targetProp}.${platform}.configurations.${configuration}.fileReplacements`)
// add default replacements
defaultReplacements?.map(setReplacement)
// add platform replacements (always override defaults!)
platformReplacements?.map(setReplacement)
})
return replacements;
}