refactor(@angular-devkit/build-angular): remove virtualfs host usage from browser/dev-server

The virtualfs Host was only used for exist and directory checks during asset and file replacement option normalization within the browser and dev-server builders.
This commit is contained in:
Charles Lyding 2021-01-19 12:03:50 -05:00 committed by Charles
parent 2d7f268baf
commit 6b2a8e175e
8 changed files with 13 additions and 35 deletions

View File

@ -12,7 +12,6 @@ import {
targetFromTargetString, targetFromTargetString,
} from '@angular-devkit/architect'; } from '@angular-devkit/architect';
import { JsonObject, normalize, resolve } from '@angular-devkit/core'; import { JsonObject, normalize, resolve } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import { BrowserBuilderOutput } from '../browser'; import { BrowserBuilderOutput } from '../browser';
@ -47,7 +46,6 @@ async function _renderUniversal(
const zonePackage = require.resolve('zone.js', { paths: [root] }); const zonePackage = require.resolve('zone.js', { paths: [root] });
await import(zonePackage); await import(zonePackage);
const host = new NodeJsSyncHost();
const projectName = context.target && context.target.project; const projectName = context.target && context.target.project;
if (!projectName) { if (!projectName) {
throw new Error('The builder requires a target.'); throw new Error('The builder requires a target.');
@ -121,7 +119,6 @@ async function _renderUniversal(
if (browserOptions.serviceWorker) { if (browserOptions.serviceWorker) {
await augmentAppWithServiceWorker( await augmentAppWithServiceWorker(
host,
normalize(root), normalize(root),
projectRoot, projectRoot,
normalize(outputPath), normalize(outputPath),

View File

@ -7,8 +7,7 @@
*/ */
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect'; import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { EmittedFiles, WebpackLoggingCallback, runWebpack } from '@angular-devkit/build-webpack'; import { EmittedFiles, WebpackLoggingCallback, runWebpack } from '@angular-devkit/build-webpack';
import { getSystemPath, json, normalize, resolve, tags, virtualFs } from '@angular-devkit/core'; import { getSystemPath, json, normalize, resolve, tags } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import { Observable, from } from 'rxjs'; import { Observable, from } from 'rxjs';
@ -127,7 +126,6 @@ export function getCompilerConfig(wco: WebpackConfigOptions): webpack.Configurat
async function initialize( async function initialize(
options: BrowserBuilderSchema, options: BrowserBuilderSchema,
context: BuilderContext, context: BuilderContext,
host: virtualFs.Host<fs.Stats>,
differentialLoadingNeeded: boolean, differentialLoadingNeeded: boolean,
webpackConfigurationTransform?: ExecutionTransformer<webpack.Configuration>, webpackConfigurationTransform?: ExecutionTransformer<webpack.Configuration>,
): Promise<{ ): Promise<{
@ -158,7 +156,6 @@ async function initialize(
getCompilerConfig(wco), getCompilerConfig(wco),
wco.buildOptions.webWorkerTsConfig ? getWorkerConfig(wco) : {}, wco.buildOptions.webWorkerTsConfig ? getWorkerConfig(wco) : {},
], ],
host,
{ differentialLoadingNeeded }, { differentialLoadingNeeded },
); );
@ -166,7 +163,6 @@ async function initialize(
if (options.assets?.length && !adjustedOptions.assets?.length) { if (options.assets?.length && !adjustedOptions.assets?.length) {
normalizeAssetPatterns( normalizeAssetPatterns(
options.assets, options.assets,
new virtualFs.SyncDelegateHost(host),
normalize(context.workspaceRoot), normalize(context.workspaceRoot),
normalize(projectRoot), normalize(projectRoot),
projectSourceRoot === undefined ? undefined : normalize(projectSourceRoot), projectSourceRoot === undefined ? undefined : normalize(projectSourceRoot),
@ -199,7 +195,6 @@ export function buildWebpackBrowser(
indexHtml?: IndexHtmlTransform; indexHtml?: IndexHtmlTransform;
} = {}, } = {},
): Observable<BrowserBuilderOutput> { ): Observable<BrowserBuilderOutput> {
const host = new NodeJsSyncHost();
const root = normalize(context.workspaceRoot); const root = normalize(context.workspaceRoot);
const projectName = context.target?.project; const projectName = context.target?.project;
@ -248,7 +243,7 @@ export function buildWebpackBrowser(
} }
return { return {
...(await initialize(options, context, host, isDifferentialLoadingNeeded, transforms.webpackConfiguration)), ...(await initialize(options, context, isDifferentialLoadingNeeded, transforms.webpackConfiguration)),
buildBrowserFeatures, buildBrowserFeatures,
isDifferentialLoadingNeeded, isDifferentialLoadingNeeded,
target, target,
@ -645,7 +640,6 @@ export function buildWebpackBrowser(
await copyAssets( await copyAssets(
normalizeAssetPatterns( normalizeAssetPatterns(
options.assets, options.assets,
new virtualFs.SyncDelegateHost(host),
root, root,
normalize(projectRoot), normalize(projectRoot),
projectSourceRoot === undefined ? undefined : normalize(projectSourceRoot), projectSourceRoot === undefined ? undefined : normalize(projectSourceRoot),
@ -719,7 +713,6 @@ export function buildWebpackBrowser(
for (const [locale, outputPath] of outputPaths.entries()) { for (const [locale, outputPath] of outputPaths.entries()) {
try { try {
await augmentAppWithServiceWorker( await augmentAppWithServiceWorker(
host,
root, root,
normalize(projectRoot), normalize(projectRoot),
normalize(outputPath), normalize(outputPath),

View File

@ -13,7 +13,6 @@ import {
runWebpackDevServer, runWebpackDevServer,
} from '@angular-devkit/build-webpack'; } from '@angular-devkit/build-webpack';
import { json, tags } from '@angular-devkit/core'; import { json, tags } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import * as path from 'path'; import * as path from 'path';
import { Observable, from, of } from 'rxjs'; import { Observable, from, of } from 'rxjs';
import { concatMap, switchMap } from 'rxjs/operators'; import { concatMap, switchMap } from 'rxjs/operators';
@ -83,7 +82,6 @@ export function serveWebpackBrowser(
assertCompatibleAngularVersion(workspaceRoot, logger); assertCompatibleAngularVersion(workspaceRoot, logger);
const browserTarget = targetFromTargetString(options.browserTarget); const browserTarget = targetFromTargetString(options.browserTarget);
const host = new NodeJsSyncHost();
async function setup(): Promise<{ async function setup(): Promise<{
browserOptions: json.JsonObject & BrowserBuilderSchema; browserOptions: json.JsonObject & BrowserBuilderSchema;
@ -148,7 +146,6 @@ export function serveWebpackBrowser(
getCompilerConfig(wco), getCompilerConfig(wco),
browserOptions.webWorkerTsConfig ? getWorkerConfig(wco) : {}, browserOptions.webWorkerTsConfig ? getWorkerConfig(wco) : {},
], ],
host,
devServerOptions, devServerOptions,
); );

View File

@ -10,12 +10,13 @@ import {
Path, Path,
basename, basename,
dirname, dirname,
getSystemPath,
join, join,
normalize, normalize,
relative, relative,
resolve, resolve,
virtualFs,
} from '@angular-devkit/core'; } from '@angular-devkit/core';
import { statSync } from 'fs';
import { AssetPattern, AssetPatternClass } from '../browser/schema'; import { AssetPattern, AssetPatternClass } from '../browser/schema';
@ -27,7 +28,6 @@ export class MissingAssetSourceRootException extends BaseException {
export function normalizeAssetPatterns( export function normalizeAssetPatterns(
assetPatterns: AssetPattern[], assetPatterns: AssetPattern[],
host: virtualFs.SyncDelegateHost,
root: Path, root: Path,
projectRoot: Path, projectRoot: Path,
maybeSourceRoot: Path | undefined, maybeSourceRoot: Path | undefined,
@ -56,7 +56,7 @@ export function normalizeAssetPatterns(
let isDirectory = false; let isDirectory = false;
try { try {
isDirectory = host.isDirectory(resolvedAssetPath); isDirectory = statSync(getSystemPath(resolvedAssetPath)).isDirectory();
} catch { } catch {
isDirectory = true; isDirectory = true;
} }

View File

@ -8,7 +8,7 @@
*/ */
import { Path, virtualFs } from '@angular-devkit/core'; import { Path } from '@angular-devkit/core';
import { import {
AssetPatternClass, AssetPatternClass,
Schema as BrowserBuilderSchema, Schema as BrowserBuilderSchema,
@ -35,19 +35,17 @@ export type NormalizedBrowserBuilderSchema = BrowserBuilderSchema & BuildOptions
}; };
export function normalizeBrowserSchema( export function normalizeBrowserSchema(
host: virtualFs.Host<{}>,
root: Path, root: Path,
projectRoot: Path, projectRoot: Path,
sourceRoot: Path | undefined, sourceRoot: Path | undefined,
options: BrowserBuilderSchema, options: BrowserBuilderSchema,
): NormalizedBrowserBuilderSchema { ): NormalizedBrowserBuilderSchema {
const syncHost = new virtualFs.SyncDelegateHost(host);
const normalizedSourceMapOptions = normalizeSourceMaps(options.sourceMap || false); const normalizedSourceMapOptions = normalizeSourceMaps(options.sourceMap || false);
return { return {
...options, ...options,
assets: normalizeAssetPatterns(options.assets || [], syncHost, root, projectRoot, sourceRoot), assets: normalizeAssetPatterns(options.assets || [], root, projectRoot, sourceRoot),
fileReplacements: normalizeFileReplacements(options.fileReplacements || [], syncHost, root), fileReplacements: normalizeFileReplacements(options.fileReplacements || [], root),
optimization: normalizeOptimization(options.optimization), optimization: normalizeOptimization(options.optimization),
sourceMap: normalizedSourceMapOptions, sourceMap: normalizedSourceMapOptions,
preserveSymlinks: options.preserveSymlinks === undefined ? process.execArgv.includes('--preserve-symlinks') : options.preserveSymlinks, preserveSymlinks: options.preserveSymlinks === undefined ? process.execArgv.includes('--preserve-symlinks') : options.preserveSymlinks,

View File

@ -12,8 +12,8 @@ import {
getSystemPath, getSystemPath,
join, join,
normalize, normalize,
virtualFs,
} from '@angular-devkit/core'; } from '@angular-devkit/core';
import { existsSync } from 'fs';
import { FileReplacement } from '../browser/schema'; import { FileReplacement } from '../browser/schema';
@ -30,7 +30,6 @@ export interface NormalizedFileReplacement {
export function normalizeFileReplacements( export function normalizeFileReplacements(
fileReplacements: FileReplacement[], fileReplacements: FileReplacement[],
host: virtualFs.SyncDelegateHost,
root: Path, root: Path,
): NormalizedFileReplacement[] { ): NormalizedFileReplacement[] {
if (fileReplacements.length === 0) { if (fileReplacements.length === 0) {
@ -41,11 +40,11 @@ export function normalizeFileReplacements(
.map(replacement => normalizeFileReplacement(replacement, root)); .map(replacement => normalizeFileReplacement(replacement, root));
for (const { replace, with: replacementWith } of normalizedReplacement) { for (const { replace, with: replacementWith } of normalizedReplacement) {
if (!host.exists(replacementWith)) { if (!existsSync(getSystemPath(replacementWith))) {
throw new MissingFileReplacementException(getSystemPath(replacementWith)); throw new MissingFileReplacementException(getSystemPath(replacementWith));
} }
if (!host.exists(replace)) { if (!existsSync(getSystemPath(replace))) {
throw new MissingFileReplacementException(getSystemPath(replace)); throw new MissingFileReplacementException(getSystemPath(replace));
} }
} }

View File

@ -15,6 +15,7 @@ import {
tags, tags,
virtualFs, virtualFs,
} from '@angular-devkit/core'; } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { import {
Filesystem, Filesystem,
Generator, Generator,
@ -70,13 +71,13 @@ class CliFilesystem implements Filesystem {
} }
export async function augmentAppWithServiceWorker( export async function augmentAppWithServiceWorker(
host: virtualFs.Host,
projectRoot: Path, projectRoot: Path,
appRoot: Path, appRoot: Path,
outputPath: Path, outputPath: Path,
baseHref: string, baseHref: string,
ngswConfigPath?: string, ngswConfigPath?: string,
): Promise<void> { ): Promise<void> {
const host = new NodeJsSyncHost();
const distPath = normalize(outputPath); const distPath = normalize(outputPath);
const systemProjectRoot = getSystemPath(projectRoot); const systemProjectRoot = getSystemPath(projectRoot);

View File

@ -11,10 +11,7 @@ import {
logging, logging,
normalize, normalize,
resolve, resolve,
virtualFs,
} from '@angular-devkit/core'; } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as webpack from 'webpack'; import * as webpack from 'webpack';
import { merge as webpackMerge } from 'webpack-merge'; import { merge as webpackMerge } from 'webpack-merge';
@ -125,7 +122,6 @@ export async function generateI18nBrowserWebpackConfigFromContext(
options: BrowserBuilderSchema, options: BrowserBuilderSchema,
context: BuilderContext, context: BuilderContext,
webpackPartialGenerator: (wco: BrowserWebpackConfigOptions) => webpack.Configuration[], webpackPartialGenerator: (wco: BrowserWebpackConfigOptions) => webpack.Configuration[],
host: virtualFs.Host<fs.Stats> = new NodeJsSyncHost(),
extraBuildOptions: Partial<NormalizedBrowserBuilderSchema> = {}, extraBuildOptions: Partial<NormalizedBrowserBuilderSchema> = {},
): Promise<{ config: webpack.Configuration; projectRoot: string; projectSourceRoot?: string, i18n: I18nOptions }> { ): Promise<{ config: webpack.Configuration; projectRoot: string; projectSourceRoot?: string, i18n: I18nOptions }> {
const { buildOptions, i18n } = await configureI18nBuild(context, options); const { buildOptions, i18n } = await configureI18nBuild(context, options);
@ -133,7 +129,6 @@ export async function generateI18nBrowserWebpackConfigFromContext(
buildOptions, buildOptions,
context, context,
webpackPartialGenerator, webpackPartialGenerator,
host,
extraBuildOptions, extraBuildOptions,
); );
const config = result.config; const config = result.config;
@ -195,7 +190,6 @@ export async function generateBrowserWebpackConfigFromContext(
options: BrowserBuilderSchema, options: BrowserBuilderSchema,
context: BuilderContext, context: BuilderContext,
webpackPartialGenerator: (wco: BrowserWebpackConfigOptions) => webpack.Configuration[], webpackPartialGenerator: (wco: BrowserWebpackConfigOptions) => webpack.Configuration[],
host: virtualFs.Host<fs.Stats> = new NodeJsSyncHost(),
extraBuildOptions: Partial<NormalizedBrowserBuilderSchema> = {}, extraBuildOptions: Partial<NormalizedBrowserBuilderSchema> = {},
): Promise<{ config: webpack.Configuration; projectRoot: string; projectSourceRoot?: string }> { ): Promise<{ config: webpack.Configuration; projectRoot: string; projectSourceRoot?: string }> {
const projectName = context.target && context.target.project; const projectName = context.target && context.target.project;
@ -212,7 +206,6 @@ export async function generateBrowserWebpackConfigFromContext(
: undefined; : undefined;
const normalizedOptions = normalizeBrowserSchema( const normalizedOptions = normalizeBrowserSchema(
host,
workspaceRoot, workspaceRoot,
projectRoot, projectRoot,
sourceRoot, sourceRoot,