@@ -10,6 +10,12 @@ const path = require('path');
10
10
shelljs . set ( '-e' ) ;
11
11
shelljs . cd ( path . join ( __dirname , '../..' ) ) ;
12
12
13
+ // Do not apply postinstall patches when running "postinstall" outside. The
14
+ // "generate_build_file.js" file indicates that we run in Bazel managed node modules.
15
+ if ( ! shelljs . test ( '-e' , 'generate_build_file.js' ) ) {
16
+ return ;
17
+ }
18
+
13
19
// Workaround for https://github.com/angular/angular/issues/18810.
14
20
shelljs . exec ( 'ngc -p angular-tsconfig.json' ) ;
15
21
@@ -54,3 +60,30 @@ shelljs.sed('-i', 'var resolvedEntryPoint = null;', `
54
60
if ( ! shelljs . test ( '-f' , 'node_modules/@angular/bazel/src/ng_package/rollup_bin.js' ) ) {
55
61
shelljs . cat ( path . join ( __dirname , './rollup_windows_arguments.patch' ) ) . exec ( 'patch -p0' ) ;
56
62
}
63
+
64
+ // Workaround for: https://hackmd.io/MlqFp-yrSx-0mw4rD7dnQQ?both. We only want to discard
65
+ // the metadata of files in the bazel managed node modules. That way we keep the default
66
+ // behavior of ngc-wrapped except for dependencies between sources of the library. This makes
67
+ // the "generateCodeForLibraries" flag more accurate in the Bazel environment where previous
68
+ // compilations should not be treated as external libraries. Read more about this in the document.
69
+ shelljs . sed ( '-i' , / i f \( ( t h i s \. o p t i o n s \. g e n e r a t e C o d e F o r L i b r a r i e s = = = f a l s e ) / , `
70
+ const fs = require('fs');
71
+ const hasFlatModuleBundle = fs.existsSync(filePath.replace('.d.ts', '.metadata.json'));
72
+ if ((filePath.includes('node_modules/') || !hasFlatModuleBundle) && $1` ,
73
+ 'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js' ) ;
74
+ shelljs . cat ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) . exec ( 'patch -p0' ) ;
75
+ // The three replacements below ensure that metadata files can be read by NGC and
76
+ // that metadata files are collected as Bazel action inputs.
77
+ shelljs . sed ( '-i' , / ( c o n s t N G C _ A S S E T S = \/ [ ^ ( ] + \( ) ( [ ^ ) ] * ) ( \) .* \/ ; ) / , '$1$2|metadata.json$3' ,
78
+ 'node_modules/@angular/bazel/src/ngc-wrapped/index.js' ) ;
79
+ shelljs . sed ( '-i' , / ^ ( ( \s * ) r e s u l t s = d e p s e t \( d e p .a n g u l a r .s u m m a r i e s , t r a n s i t i v e = \[ r e s u l t s ] \) ) $ / ,
80
+ `$1#\n$2results = depset(dep.angular.metadata, transitive = [results])` ,
81
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
82
+ shelljs . sed ( '-i' ,
83
+ / ^ ( ( \s * ) r e s u l t s = d e p s e t \( t a r g e t .a n g u l a r .s u m m a r i e s i f h a s a t t r \( t a r g e t , " a n g u l a r " \) e l s e \[ ] \) ) $ / ,
84
+ `$1#\n$2results = depset(target.angular.metadata if hasattr(target, "angular") else [], transitive = [results])` ,
85
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
86
+ // Ensure that "metadata" of transitive dependencies can be collected.
87
+ shelljs . sed ( '-i' , / ( " m e t a d a t a " : o u t s .m e t a d a t a ) , / ,
88
+ `$1 + [m for dep in ctx.attr.deps if hasattr(dep, "angular") for m in dep.angular.metadata],` ,
89
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
0 commit comments