Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"typescript": "3.3.4000"
},
"devDependencies": {
"@angular/compiler": "^8.0.0-beta.9",
"@angular/compiler-cli": "^8.0.0-beta.9",
"@angular/compiler": "^8.0.0-beta.10",
"@angular/compiler-cli": "^8.0.0-beta.10",
"@bazel/karma": "~0.26.0",
"@bazel/typescript": "~0.26.0",
"@ngtools/json-schema": "^1.1.0",
Expand Down
22 changes: 11 additions & 11 deletions packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@
"webpack-subresource-integrity": "1.1.0-rc.6"
},
"devDependencies": {
"@angular/animations": "^8.0.0-beta.9",
"@angular/animations": "^8.0.0-beta.10",
"@angular/cdk": "^7.3.4",
"@angular/common": "^8.0.0-beta.9",
"@angular/compiler": "^8.0.0-beta.9",
"@angular/compiler-cli": "^8.0.0-beta.9",
"@angular/core": "^8.0.0-beta.9",
"@angular/http": "^8.0.0-beta.9",
"@angular/common": "^8.0.0-beta.10",
"@angular/compiler": "^8.0.0-beta.10",
"@angular/compiler-cli": "^8.0.0-beta.10",
"@angular/core": "^8.0.0-beta.10",
"@angular/http": "^8.0.0-beta.10",
"@angular/material": "^7.3.4",
"@angular/platform-browser": "^8.0.0-beta.9",
"@angular/platform-browser-dynamic": "^8.0.0-beta.9",
"@angular/platform-server": "^8.0.0-beta.9",
"@angular/router": "^8.0.0-beta.9",
"@angular/service-worker": "^8.0.0-beta.9",
"@angular/platform-browser": "^8.0.0-beta.10",
"@angular/platform-browser-dynamic": "^8.0.0-beta.10",
"@angular/platform-server": "^8.0.0-beta.10",
"@angular/router": "^8.0.0-beta.10",
"@angular/service-worker": "^8.0.0-beta.10",
"codelyzer": "^5.0.0",
"core-js": "^2.4.1",
"bootstrap": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/angular_devkit/build_ng_packagr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"ng-packagr": "^4.0.0 || ^5.0.0"
},
"devDependencies": {
"@angular/compiler": "^8.0.0-beta.9",
"@angular/compiler-cli": "^8.0.0-beta.9",
"@angular/compiler": "^8.0.0-beta.10",
"@angular/compiler-cli": "^8.0.0-beta.10",
"@angular-devkit/core": "0.0.0",
"ng-packagr": "^5.0.1",
"tslib": "^1.9.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/ngtools/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"webpack": "^4.0.0"
},
"devDependencies": {
"@angular/compiler": "^8.0.0-beta.9",
"@angular/compiler-cli": "^8.0.0-beta.9",
"@angular/compiler": "^8.0.0-beta.10",
"@angular/compiler-cli": "^8.0.0-beta.10",
"typescript": "3.3.4000",
"webpack": "4.29.6"
}
Expand Down
116 changes: 60 additions & 56 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ import * as path from 'path';
import * as ts from 'typescript';
import { Compiler, compilation } from 'webpack';
import { time, timeEnd } from './benchmark';
import { WebpackCompilerHost, workaroundResolve } from './compiler_host';
import { WebpackCompilerHost } from './compiler_host';
import { resolveEntryModuleFromMain } from './entry_resolver';
import { DiagnosticMode, gatherDiagnostics, hasErrors } from './gather_diagnostics';
import {
AngularCompilerPluginOptions,
ContextElementDependencyConstructor,
PLATFORM,
} from './interfaces';
import { LazyRouteMap, findLazyRoutes } from './lazy_routes';
import { NgccProcessor } from './ngcc_processor';
import { TypeScriptPathsPlugin } from './paths-plugin';
import { WebpackResourceLoader } from './resource_loader';
import {
Expand All @@ -63,6 +69,7 @@ import {
MESSAGE_KIND,
UpdateMessage,
} from './type_checker_messages';
import { flattenArray, workaroundResolve } from './utils';
import {
VirtualFileSystemDecorator,
VirtualWatchFileSystemDecorator,
Expand All @@ -76,61 +83,6 @@ import { WebpackInputHost } from './webpack-input-host';

const treeKill = require('tree-kill');

export interface ContextElementDependency { }

export interface ContextElementDependencyConstructor {
new(modulePath: string, name: string): ContextElementDependency;
}

/**
* Option Constants
*/
export interface AngularCompilerPluginOptions {
sourceMap?: boolean;
tsConfigPath: string;
basePath?: string;
entryModule?: string;
mainPath?: string;
skipCodeGeneration?: boolean;
hostReplacementPaths?: { [path: string]: string } | ((path: string) => string);
forkTypeChecker?: boolean;
i18nInFile?: string;
i18nInFormat?: string;
i18nOutFile?: string;
i18nOutFormat?: string;
locale?: string;
missingTranslation?: string;
platform?: PLATFORM;
nameLazyFiles?: boolean;
logger?: logging.Logger;
directTemplateLoading?: boolean;
// When using the loadChildren string syntax, @ngtools/webpack must query @angular/compiler-cli
// via a private API to know which lazy routes exist. This increases build and rebuild time.
// When using Ivy, the string syntax is not supported at all. Thus we shouldn't attempt that.
// This option is also used for when the compilation doesn't need this sort of processing at all.
discoverLazyRoutes?: boolean;
importFactories?: boolean;

// added to the list of lazy routes
additionalLazyModules?: { [module: string]: string };
additionalLazyModuleResources?: string[];

// The ContextElementDependency of correct Webpack compilation.
// This is needed when there are multiple Webpack installs.
contextElementDependencyConstructor?: ContextElementDependencyConstructor;

// Use tsconfig to include path globs.
compilerOptions?: ts.CompilerOptions;

host?: virtualFs.Host<fs.Stats>;
platformTransformers?: ts.TransformerFactory<ts.SourceFile>[];
}

export enum PLATFORM {
Browser,
Server,
}

export class AngularCompilerPlugin {
private _options: AngularCompilerPluginOptions;

Expand Down Expand Up @@ -172,6 +124,8 @@ export class AngularCompilerPlugin {
// Logging.
private _logger: logging.Logger;

private _mainFields: string[] = [];

constructor(options: AngularCompilerPluginOptions) {
this._options = Object.assign({}, options);
this._setupOptions(this._options);
Expand Down Expand Up @@ -643,6 +597,16 @@ export class AngularCompilerPlugin {
// Registration hook for webpack plugin.
// tslint:disable-next-line:no-big-function
apply(compiler: Compiler & { watchMode?: boolean }) {
// The below is require by NGCC processor
// since we need to know which fields we need to process
compiler.hooks.environment.tap('angular-compiler', () => {
const { options } = compiler;
const mainFields = options.resolve && options.resolve.mainFields;
if (mainFields) {
this._mainFields = flattenArray(mainFields);
}
});

// cleanup if not watching
compiler.hooks.thisCompilation.tap('angular-compiler', compilation => {
compilation.hooks.finishModules.tap('angular-compiler', () => {
Expand Down Expand Up @@ -694,13 +658,33 @@ export class AngularCompilerPlugin {
}
}

let ngccProcessor: NgccProcessor | undefined;
if (this._compilerOptions.enableIvy) {
let ngcc: typeof import('@angular/compiler-cli/ngcc') | undefined;
try {
// this is done for the sole reason that @ngtools/webpack
// support versions of Angular that don't have NGCC API
ngcc = require('@angular/compiler-cli/ngcc');
} catch {
}

if (ngcc) {
ngccProcessor = new NgccProcessor(
ngcc,
this._mainFields,
compilerWithFileSystems.inputFileSystem,
);
}
Comment thread
petebacondarwin marked this conversation as resolved.
Resolved
}

// Create the webpack compiler host.
const webpackCompilerHost = new WebpackCompilerHost(
this._compilerOptions,
this._basePath,
host,
true,
this._options.directTemplateLoading,
ngccProcessor,
);

// Create and set a new WebpackResourceLoader in AOT
Expand Down Expand Up @@ -812,6 +796,26 @@ export class AngularCompilerPlugin {
});

compiler.hooks.afterResolvers.tap('angular-compiler', compiler => {
if (this._compilerOptions.enableIvy) {
// When Ivy is enabled we need to add the fields added by NGCC
// to take precedence over the provided mainFields.
// NGCC adds fields in package.json suffixed with '_ivy_ngcc'
// Example: module -> module__ivy_ngcc
// tslint:disable-next-line:no-any
(compiler as any).resolverFactory.hooks.resolveOptions
.for('normal')
// tslint:disable-next-line:no-any
.tap('WebpackOptionsApply', (resolveOptions: any) => {
const mainFields = (resolveOptions.mainFields as string[])
.map(f => [`${f}_ivy_ngcc`, f]);

return {
...resolveOptions,
mainFields: flattenArray(mainFields),
};
});
}

// tslint:disable-next-line:no-any
(compiler as any).resolverFactory.hooks.resolver
.for('normal')
Expand Down
50 changes: 43 additions & 7 deletions packages/ngtools/webpack/src/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
} from '@angular-devkit/core';
import { Stats } from 'fs';
import * as ts from 'typescript';
import { NgccProcessor } from './ngcc_processor';
import { WebpackResourceLoader } from './resource_loader';
import { workaroundResolve } from './utils';


export interface OnErrorFn {
Expand Down Expand Up @@ -47,6 +49,7 @@ export class WebpackCompilerHost implements ts.CompilerHost {
host: virtualFs.Host,
private readonly cacheSourceFiles: boolean,
private readonly directTemplateLoading = false,
private readonly ngccProcessor?: NgccProcessor,
) {
this._syncHost = new virtualFs.SyncDelegateHost(host);
this._memoryHost = new virtualFs.SyncDelegateHost(new virtualFs.SimpleMemoryHost());
Expand Down Expand Up @@ -354,13 +357,46 @@ export class WebpackCompilerHost implements ts.CompilerHost {
trace(message: string) {
console.log(message);
}
}

resolveModuleNames(
moduleNames: string[],
containingFile: string,
): (ts.ResolvedModule | undefined)[] {
return moduleNames.map(moduleName => {
const { resolvedModule } = ts.resolveModuleName(
Comment thread
alan-agius4 marked this conversation as resolved.
Resolved
moduleName,
workaroundResolve(containingFile),
this._options,
this,
);

if (this._options.enableIvy && resolvedModule && this.ngccProcessor) {
this.ngccProcessor.processModule(moduleName, resolvedModule);
}

return resolvedModule;
});
}

resolveTypeReferenceDirectives(
typeReferenceDirectiveNames: string[],
containingFile: string,
redirectedReference?: ts.ResolvedProjectReference,
): (ts.ResolvedTypeReferenceDirective | undefined)[] {
return typeReferenceDirectiveNames.map(moduleName => {
const { resolvedTypeReferenceDirective } = ts.resolveTypeReferenceDirective(
moduleName,
workaroundResolve(containingFile),
this._options,
this,
redirectedReference,
);

if (this._options.enableIvy && resolvedTypeReferenceDirective && this.ngccProcessor) {
this.ngccProcessor.processModule(moduleName, resolvedTypeReferenceDirective);
}

// `TsCompilerAotCompilerTypeCheckHostAdapter` in @angular/compiler-cli seems to resolve module
// names directly via `resolveModuleName`, which prevents full Path usage.
// To work around this we must provide the same path format as TS internally uses in
// the SourceFile paths.
export function workaroundResolve(path: Path | string) {
return getSystemPath(normalize(path)).replace(/\\/g, '/');
return resolvedTypeReferenceDirective;
});
}
}
1 change: 1 addition & 0 deletions packages/ngtools/webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

export * from './angular_compiler_plugin';
export * from './interfaces';
export { ngcLoader as default } from './loader';

export const NgToolsLoader = __filename;
67 changes: 67 additions & 0 deletions packages/ngtools/webpack/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { logging, virtualFs } from '@angular-devkit/core';
import * as fs from 'fs';
import * as ts from 'typescript';

export enum PLATFORM {
Browser,
Server,
}

export interface ContextElementDependency { }

export interface ContextElementDependencyConstructor {
new(modulePath: string, name: string): ContextElementDependency;
}

/**
* Option Constants
*/
export interface AngularCompilerPluginOptions {
sourceMap?: boolean;
tsConfigPath: string;
basePath?: string;
entryModule?: string;
mainPath?: string;
skipCodeGeneration?: boolean;
hostReplacementPaths?: { [path: string]: string } | ((path: string) => string);
forkTypeChecker?: boolean;
i18nInFile?: string;
i18nInFormat?: string;
i18nOutFile?: string;
i18nOutFormat?: string;
locale?: string;
missingTranslation?: string;
platform?: PLATFORM;
nameLazyFiles?: boolean;
logger?: logging.Logger;
directTemplateLoading?: boolean;

// When using the loadChildren string syntax, @ngtools/webpack must query @angular/compiler-cli
// via a private API to know which lazy routes exist. This increases build and rebuild time.
// When using Ivy, the string syntax is not supported at all. Thus we shouldn't attempt that.
// This option is also used for when the compilation doesn't need this sort of processing at all.
discoverLazyRoutes?: boolean;
importFactories?: boolean;

// added to the list of lazy routes
additionalLazyModules?: { [module: string]: string };
additionalLazyModuleResources?: string[];

// The ContextElementDependency of correct Webpack compilation.
// This is needed when there are multiple Webpack installs.
contextElementDependencyConstructor?: ContextElementDependencyConstructor;

// Use tsconfig to include path globs.
compilerOptions?: ts.CompilerOptions;

host?: virtualFs.Host<fs.Stats>;
platformTransformers?: ts.TransformerFactory<ts.SourceFile>[];
}
Loading