@@ -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
@@ -48,3 +54,30 @@ shelljs.sed('-i', 'var resolvedEntryPoint = null;', `
48
54
var moduleFile = tsFiles.find(f => f.endsWith('/module.ts'));
49
55
var resolvedEntryPoint = publicApiFile || moduleFile || null;
50
56
` , 'node_modules/@angular/compiler-cli/src/ngtsc/entry_point/src/logic.js' ) ;
57
+
58
+ // Workaround for: https://hackmd.io/MlqFp-yrSx-0mw4rD7dnQQ?both. We only want to discard
59
+ // the metadata of files in the bazel managed node modules. That way we keep the default
60
+ // behavior of ngc-wrapped except for dependencies between sources of the library. This makes
61
+ // the "generateCodeForLibraries" flag more accurate in the Bazel environment where previous
62
+ // compilations should not be treated as external libraries. Read more about this in the document.
63
+ 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 ) / , `
64
+ const fs = require('fs');
65
+ const hasFlatModuleBundle = fs.existsSync(filePath.replace('.d.ts', '.metadata.json'));
66
+ if ((filePath.includes('node_modules/') || !hasFlatModuleBundle) && $1` ,
67
+ 'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js' ) ;
68
+ shelljs . cat ( path . join ( __dirname , './flat_module_factory_resolution.patch' ) ) . exec ( 'patch -p0' ) ;
69
+ // The three replacements below ensure that metadata files can be read by NGC and
70
+ // that metadata files are collected as Bazel action inputs.
71
+ shelljs . sed ( '-i' , / ( c o n s t N G C _ A S S E T S = \/ [ ^ ( ] + \( ) ( [ ^ ) ] * ) ( \) .* \/ ; ) / , '$1$2|metadata.json$3' ,
72
+ 'node_modules/@angular/bazel/src/ngc-wrapped/index.js' ) ;
73
+ 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 ] \) ) $ / ,
74
+ `$1#\n$2results = depset(dep.angular.metadata, transitive = [results])` ,
75
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
76
+ shelljs . sed ( '-i' ,
77
+ / ^ ( ( \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 \[ ] \) ) $ / ,
78
+ `$1#\n$2results = depset(target.angular.metadata if hasattr(target, "angular") else [], transitive = [results])` ,
79
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
80
+ // Ensure that "metadata" of transitive dependencies can be collected.
81
+ shelljs . sed ( '-i' , / ( " m e t a d a t a " : o u t s .m e t a d a t a ) , / ,
82
+ `$1 + [m for dep in ctx.attr.deps if hasattr(dep, "angular") for m in dep.angular.metadata],` ,
83
+ 'node_modules/@angular/bazel/src/ng_module.bzl' ) ;
0 commit comments