Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main() {
const config = ngc.readConfiguration(path.join(__dirname, 'tsconfig-build.json'));
const host = ngc.createCompilerHost({options: config.options});
const program = new NgtscProgram(config.rootNames, config.options, host);
// Determine all lazy routes in the dev-app and setup their entry-point in
// the rollup inputs object.
program.listLazyRoutes().forEach(route => {
console.log('>>> Building route entry-point:', route.route);
rollupInputs[route.route.split('#')[0]] = `${devAppOut}/${path
.relative(__dirname, route.referencedModule.filePath.split('#')[0])
.replace('.ts', '.js')}`;
});
const build = await rollup.rollup({
input: rollupInputs,
plugins: [
sourceMap: false,
inlineSources: true,
inlineSourceMap: true,
outDir: '',
declaration: true,
// ng compiler to options
enableResourceInlining: true,
// these are required to set the appropriate EmitFlags
flatModuleId: 'AUTOGENERATED',
flatModuleOutFile: 'AUTOGENERATED',
};
const config = ng.readConfiguration(fileName, extraOptions);
const options = config.options;
// todo: alanagius4 - the below shouldn't be needed but it seems that setting it only in create-emit-callback.ts
// is not working correctly
const transformDecorators = !options.enableIvy && options.annotationsAs !== 'decorators';
if ((options.annotateForClosureCompiler || options.annotationsAs === 'static fields') && transformDecorators) {
// This is needed as a workaround for https://github.com/angular/tsickle/issues/635
// Otherwise tsickle might emit references to non imported values
// as TypeScript elided the import.
options.emitDecoratorMetadata = true;
}
return config;
}
function createNgcProgram(createHost, tsconfigPath) {
const { rootNames, options } = compiler_cli_1.readConfiguration(tsconfigPath);
// https://github.com/angular/angular/commit/ec4381dd401f03bded652665b047b6b90f2b425f made Ivy
// the default. This breaks the assumption that "createProgram" from compiler-cli returns the
// NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to false.
options.enableIvy = false;
const host = createHost(options);
// For this migration, we never need to read resources and can just return
// an empty string for requested resources. We need to handle requested resources
// because our created NGC compiler program does not know about special resolutions
// which are set up by the Angular CLI. i.e. resolving stylesheets through "tilde".
host.readResource = () => '';
host.resourceNameToFileName = () => '$fake-file$';
const ngcProgram = compiler_cli_1.createProgram({ rootNames, options, host });
// The "AngularCompilerProgram" does not expose the "AotCompiler" instance, nor does it
// expose the logic that is necessary to analyze the determined modules. We work around
// this by just accessing the necessary private properties using the bracket notation.
const compiler = ngcProgram['compiler'];
private async _createOrUpdateProgram() {
// Get the root files from the ts config.
// When a new root name (like a lazy route) is added, it won't be available from
// following imports on the existing files, so we need to get the new list of root files.
const config = readConfiguration(this._tsConfigPath);
this._rootNames = config.rootNames;
// Update the forked type checker with all changed compilation files.
// This includes templates, that also need to be reloaded on the type checker.
if (this._forkTypeChecker && this._typeCheckerProcess && !this._firstRun) {
this._updateForkedTypeChecker(this._rootNames, this._getChangedCompilationFiles());
}
const oldTsProgram = this._getTsProgram();
if (this._JitMode) {
// Create the TypeScript program.
time('AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram');
this._program = ts.createProgram(
this._rootNames,
this._compilerOptions,
export function createNgcProgram(
createHost: (options: ts.CompilerOptions) => CompilerHost, tsconfigPath: string) {
const {rootNames, options} = readConfiguration(tsconfigPath);
// https://github.com/angular/angular/commit/ec4381dd401f03bded652665b047b6b90f2b425f made Ivy
// the default. This breaks the assumption that "createProgram" from compiler-cli returns the
// NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to false.
options.enableIvy = false;
const host = createHost(options);
// For this migration, we never need to read resources and can just return
// an empty string for requested resources. We need to handle requested resources
// because our created NGC compiler program does not know about special resolutions
// which are set up by the Angular CLI. i.e. resolving stylesheets through "tilde".
host.readResource = () => '';
host.resourceNameToFileName = () => '$fake-file$';
const ngcProgram = createProgram({rootNames, options, host});
setup() {
const {rootNames, options} = readConfiguration(this.projectPath);
// https://github.com/angular/angular/commit/ec4381dd401f03bded652665b047b6b90f2b425f made Ivy
// the default. This breaks the assumption that "createProgram" from compiler-cli returns the
// NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to
// false.
options.enableIvy = false;
const aotProgram = createProgram({rootNames, options, host: this.host});
// The "AngularCompilerProgram" does not expose the "AotCompiler" instance, nor does it
// expose the logic that is necessary to analyze the determined modules. We work around
// this by just accessing the necessary private properties using the bracket notation.
this.compiler = (aotProgram as any)['compiler'];
this.metadataResolver = this.compiler !['_metadataResolver'];
// Modify the "DirectiveNormalizer" to not normalize any referenced external stylesheets.
}
// TS represents paths internally with '/' and expects the tsconfig path to be in this format
this._tsConfigPath = forwardSlashPath(options.tsConfigPath);
// Check the base path.
const maybeBasePath = path.resolve(process.cwd(), this._tsConfigPath);
let basePath = maybeBasePath;
if (fs.statSync(maybeBasePath).isFile()) {
basePath = path.dirname(basePath);
}
if (options.basePath !== undefined) {
basePath = path.resolve(process.cwd(), options.basePath);
}
// Parse the tsconfig contents.
const config = readConfiguration(this._tsConfigPath);
if (config.errors && config.errors.length) {
throw new Error(formatDiagnostics(config.errors));
}
this._rootNames = config.rootNames;
this._compilerOptions = { ...config.options, ...options.compilerOptions };
this._basePath = config.options.basePath || basePath || '';
// Overwrite outDir so we can find generated files next to their .ts origin in compilerHost.
this._compilerOptions.outDir = '';
this._compilerOptions.suppressOutputPathCheck = true;
// Default plugin sourceMap to compiler options setting.
if (!options.hasOwnProperty('sourceMap')) {
options.sourceMap = this._compilerOptions.sourceMap || false;
}
},
plugins: [
new DefinePlugin({
BUILD_ANGULAR_COMPILATION_FLAGS: JSON.stringify(angularCompilationFlags),
}),
new AngularCompilerPlugin({
contextElementDependencyConstructor: require("webpack/lib/dependencies/ContextElementDependency"),
tsConfigPath: tsConfigFile,
compilerOptions: {
preserveWhitespaces: false,
disableTypeScriptVersionCheck: true,
strictInjectionParameters: true,
fullTemplateTypeCheck: angularCompilationFlags.aot || angularCompilationFlags.ivy,
ivyTemplateTypeCheck: angularCompilationFlags.ivy,
enableIvy: angularCompilationFlags.ivy,
...readConfiguration(tsConfigFile).options,
},
platform: PLATFORM.Browser,
skipCodeGeneration: !angularCompilationFlags.aot,
nameLazyFiles: true,
discoverLazyRoutes: true, // TODO disable "discoverLazyRoutes" once switched to Ivy renderer
directTemplateLoading: false,
entryModule: `${browserWindowAppPath("./app.module")}#AppModule`,
additionalLazyModules: {
["./_db-view/db-view.module#DbViewModule"]: browserWindowAppPath("./_db-view/db-view.module.ts"),
},
}),
],
optimization: {
splitChunks: {
cacheGroups: {
commons: {
constructor(
private tsconfigPath: string,
private resourceResolver: ResourceResolver,
private errorReporter: ErrorReporter
) {
const config = readConfiguration(this.tsconfigPath);
this.options = config.options;
this.init(this.options, config.rootNames);
}