@@ -19,15 +19,45 @@ def _npm_import_external_impl(ctx, plugins):
19
19
20
20
ctx .execute (["rm" , "-r" , "tmp" ])
21
21
22
+ patch_args = "--directory=npm --strip=1 --forward --reject-file=-"
23
+ for patch in ctx .attr .patches :
24
+ patch_result = ctx .execute ([
25
+ "sh" ,
26
+ "-c" ,
27
+ "patch %s < %s" % (patch_args , ctx .path (patch )),
28
+ ])
29
+
30
+ # Ignore return code 2, which signals the patch has already been applied
31
+ if patch_result .return_code != 0 and patch_result .return_code != 2 :
32
+ fail ("Could not apply patch %s: %s" % (patch , patch_result .stderr ))
33
+
22
34
files_result = ctx .execute (["find" , "npm" , "-type" , "f" ])
23
35
if files_result .return_code :
24
36
fail ("Could not list files" )
25
37
files = [file [len ("npm/" ):] for file in files_result .stdout .split ("\n " )]
26
38
39
+ final_package_path = ctx .attr .package
40
+ if ctx .attr .patches :
41
+ tar_result = ctx .execute ([
42
+ "tar" ,
43
+ "czf" ,
44
+ "patched-package.tgz" ,
45
+ "--strip-components=1" ,
46
+ "npm/" ,
47
+ ])
48
+ if tar_result .return_code :
49
+ fail ("Could not tar up patched-package.tgz" )
50
+ final_package_path = "patched-package.tgz"
51
+
52
+ # Don't leave the package contents sitting around now that we're done. Bazel
53
+ # builds will always extract from the .tgz file, so anyone wanting to tinker
54
+ # should go poke at the .tgz.
55
+ ctx .execute (["rm" , "-r" , "npm/" ])
56
+
27
57
build = ""
28
58
29
59
package = struct (
30
- archive = ctx . attr . package ,
60
+ archive = final_package_path ,
31
61
deps = deps ,
32
62
extra_deps = extra_deps ,
33
63
name = package_name ,
@@ -63,6 +93,10 @@ def npm_import_external_rule(plugins):
63
93
doc = "Package name." ,
64
94
mandatory = True ,
65
95
),
96
+ "patches" : attr .label_list (
97
+ allow_files = True ,
98
+ mandatory = True ,
99
+ ),
66
100
},
67
101
)
68
102
@@ -155,6 +189,7 @@ def npm(name, packages, roots, plugins = DEFAULT_PLUGINS, auth_patterns = None,
155
189
name = repo_name ,
156
190
package = file ,
157
191
package_name = package ["name" ],
192
+ patches = package .get ("patches" , []),
158
193
deps = [json .encode ({"id" : package_repo_name (name , dep ["id" ]), "name" : dep .get ("name" )}) for dep in package ["deps" ]],
159
194
extra_deps = extra_deps ,
160
195
)
0 commit comments