@@ -65,9 +65,8 @@ _FP_DIRECT_TMPL = \
65
65
66
66
_FP_DIRECT_TARGET_TMPL = \
67
67
"""
68
- for link_package in {link_packages}:
69
- if link_package == bazel_package:
70
- link_targets.append("//{{}}:{{}}/{pkg}".format(bazel_package, name))"""
68
+ if bazel_package in {link_packages} and ((not prod and not dev) or {prod_or_dev}):
69
+ link_targets.append("//{{}}:{{}}/{pkg}".format(bazel_package, name))"""
71
70
72
71
_BZL_LIBRARY_TMPL = \
73
72
"""bzl_library(
@@ -148,67 +147,71 @@ sh_binary(
148
147
"package" : name ,
149
148
"path" : dep_path ,
150
149
"link_packages" : {},
150
+ "link_dev_packages" : {},
151
151
"deps" : transitive_deps ,
152
152
}
153
153
154
154
# Look for first-party links in importers
155
155
for import_path , importer in importers .items ():
156
- dependencies = importer .get ("all_deps" )
157
- if type (dependencies ) != "dict" :
158
- msg = "expected dict of dependencies in processed importer '{}'" .format (import_path )
159
- fail (msg )
160
156
link_package = helpers .link_package (root_package , import_path )
161
- for dep_package , dep_version in dependencies .items ():
162
- if dep_version .startswith ("file:" ):
163
- if dep_version in packages and packages [dep_version ]["id" ]:
164
- dep_path = helpers .link_package (root_package , packages [dep_version ]["id" ][len ("file:" ):])
165
- else :
166
- dep_path = helpers .link_package (root_package , dep_version [len ("file:" ):])
167
- dep_key = "{}+{}" .format (dep_package , dep_version )
168
- if not dep_key in fp_links .keys ():
169
- msg = "Expected to file: referenced package {} in first-party links" .format (dep_key )
170
- fail (msg )
171
- fp_links [dep_key ]["link_packages" ][link_package ] = True
172
- elif dep_version .startswith ("link:" ):
173
- dep_version = dep_version [len ("link:" ):]
174
- dep_importer = paths .normalize ("{}/{}" .format (import_path , dep_version ) if import_path else dep_version )
175
- dep_path = helpers .link_package (root_package , import_path , dep_version )
176
- dep_key = "{}+{}" .format (dep_package , dep_path )
177
- if fp_links .get (dep_key , False ):
178
- fp_links [dep_key ]["link_packages" ][link_package ] = True
179
- else :
180
- transitive_deps = {}
181
- raw_deps = {}
182
- if importers .get (dep_importer , False ):
183
- raw_deps = importers .get (dep_importer ).get ("deps" )
184
- for raw_package , raw_version in raw_deps .items ():
185
- package_store_name = utils .package_store_name (raw_package , raw_version )
186
- dep_store_target = """"//{root_package}:{package_store_root}/{{}}/{package_store_name}".format(name)""" .format (
187
- root_package = root_package ,
188
- package_store_name = package_store_name ,
189
- package_store_root = utils .package_store_root ,
190
- )
191
- if dep_store_target not in transitive_deps :
192
- transitive_deps [dep_store_target ] = [raw_package ]
193
- else :
194
- transitive_deps [dep_store_target ].append (raw_package )
195
-
196
- # collapse link aliases lists into to a comma separated strings
197
- for dep_store_target in transitive_deps .keys ():
198
- transitive_deps [dep_store_target ] = "," .join (transitive_deps [dep_store_target ])
199
- fp_links [dep_key ] = {
200
- "package" : dep_package ,
201
- "path" : dep_path ,
202
- "link_packages" : {link_package : True },
203
- "deps" : transitive_deps ,
204
- }
157
+ for deps_type , link_type in [("deps" , "link_packages" ), ("dev_deps" , "link_dev_packages" )]:
158
+ dependencies = importer .get (deps_type )
159
+ if type (dependencies ) != "dict" :
160
+ msg = "expected dict of dependencies in processed importer '{}'" .format (import_path )
161
+ fail (msg )
162
+ for dep_package , dep_version in dependencies .items ():
163
+ if dep_version .startswith ("file:" ):
164
+ if dep_version in packages and packages [dep_version ]["id" ]:
165
+ dep_path = helpers .link_package (root_package , packages [dep_version ]["id" ][len ("file:" ):])
166
+ else :
167
+ dep_path = helpers .link_package (root_package , dep_version [len ("file:" ):])
168
+ dep_key = "{}+{}" .format (dep_package , dep_version )
169
+ if not dep_key in fp_links .keys ():
170
+ msg = "Expected to file: referenced package {} in first-party links" .format (dep_key )
171
+ fail (msg )
172
+ fp_links [dep_key ][link_type ][link_package ] = True
173
+ elif dep_version .startswith ("link:" ):
174
+ dep_version = dep_version [len ("link:" ):]
175
+ dep_importer = paths .normalize ("{}/{}" .format (import_path , dep_version ) if import_path else dep_version )
176
+ dep_path = helpers .link_package (root_package , import_path , dep_version )
177
+ dep_key = "{}+{}" .format (dep_package , dep_path )
178
+ if not fp_links .get (dep_key , False ):
179
+ transitive_deps = {}
180
+ raw_deps = {}
181
+ if importers .get (dep_importer , False ):
182
+ raw_deps = importers .get (dep_importer ).get ("deps" )
183
+ for raw_package , raw_version in raw_deps .items ():
184
+ package_store_name = utils .package_store_name (raw_package , raw_version )
185
+ dep_store_target = """"//{root_package}:{package_store_root}/{{}}/{package_store_name}".format(name)""" .format (
186
+ root_package = root_package ,
187
+ package_store_name = package_store_name ,
188
+ package_store_root = utils .package_store_root ,
189
+ )
190
+ if dep_store_target not in transitive_deps :
191
+ transitive_deps [dep_store_target ] = [raw_package ]
192
+ else :
193
+ transitive_deps [dep_store_target ].append (raw_package )
194
+
195
+ # collapse link aliases lists into to a comma separated strings
196
+ for dep_store_target in transitive_deps .keys ():
197
+ transitive_deps [dep_store_target ] = "," .join (transitive_deps [dep_store_target ])
198
+ fp_links [dep_key ] = {
199
+ "package" : dep_package ,
200
+ "path" : dep_path ,
201
+ "link_packages" : {},
202
+ "link_dev_packages" : {},
203
+ "deps" : transitive_deps ,
204
+ }
205
+ fp_links [dep_key ][link_type ][link_package ] = True
205
206
206
207
npm_link_packages_const = """_LINK_PACKAGES = {link_packages}""" .format (link_packages = str (link_packages ))
207
208
208
209
npm_link_targets_bzl = [
209
210
"""\
210
211
# buildifier: disable=function-docstring
211
- def npm_link_targets(name = "node_modules", package = None):
212
+ def npm_link_targets(name = "node_modules", package = None, prod = False, dev = False):
213
+ if prod and dev:
214
+ fail("prod and dev attributes cannot both be set to true")
212
215
bazel_package = package if package != None else native.package_name()
213
216
link = bazel_package in _LINK_PACKAGES
214
217
@@ -219,7 +222,7 @@ def npm_link_targets(name = "node_modules", package = None):
219
222
npm_link_all_packages_bzl = [
220
223
"""\
221
224
# buildifier: disable=function-docstring
222
- def npm_link_all_packages(name = "node_modules", imported_links = []):
225
+ def npm_link_all_packages(name = "node_modules", imported_links = [], prod = False, dev = False ):
223
226
bazel_package = native.package_name()
224
227
root_package = "{root_package}"
225
228
is_root = bazel_package == root_package
@@ -271,9 +274,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
271
274
i = i ,
272
275
pkg = _import .package ,
273
276
))
274
- for link_package , _link_aliases in _import .link_packages .items ():
275
- link_aliases = _link_aliases or [_import .package ]
276
-
277
+ for link_package , link_names in _import .link_packages .items ():
277
278
# the build file for the package being linked
278
279
build_file = "{}/{}" .format (link_package , "BUILD.bazel" ) if link_package else "BUILD.bazel"
279
280
if build_file not in rctx_files :
@@ -286,7 +287,10 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
286
287
links_targets_bzl [link_package ] = []
287
288
288
289
# for each alias of this package
289
- for link_alias in link_aliases :
290
+ for link_name in link_names :
291
+ link_alias = link_name ["pkg" ]
292
+ dev = link_name ["dev" ]
293
+
290
294
# link the alias to the underlying package
291
295
links_bzl [link_package ].append (""" link_{i}(name = "{{}}/{pkg}".format(name))""" .format (
292
296
i = i ,
@@ -295,7 +299,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
295
299
296
300
# expose the alias if public
297
301
if "//visibility:public" in _import .package_visibility :
298
- add_to_link_targets = """ link_targets.append("//{{}}:{{}}/{pkg}".format(bazel_package, name))""" .format (pkg = link_alias )
302
+ add_to_link_targets = """ link_targets.append("//{{}}:{{}}/{pkg}".format(bazel_package, name)) if (not prod and not dev) or {prod_or_dev} else None """ .format (pkg = link_alias , prod_or_dev = "dev" if dev else "prod" )
299
303
links_bzl [link_package ].append (add_to_link_targets )
300
304
links_targets_bzl [link_package ].append (add_to_link_targets )
301
305
package_scope = link_alias [:link_alias .find ("/" , 1 )] if link_alias [0 ] == "@" else None
@@ -319,19 +323,19 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
319
323
if rctx .attr .generate_bzl_library_targets :
320
324
rctx_files [build_file ].append ("""load("@bazel_skylib//:bzl_library.bzl", "bzl_library")""" )
321
325
322
- for link_alias in link_aliases :
326
+ for link_name in link_names :
323
327
rctx_files [build_file ].append (_BZL_LIBRARY_TMPL .format (
324
- name = link_alias ,
325
- src = ":{}/{}" .format (link_alias , _PACKAGE_JSON_BZL_FILENAME ),
328
+ name = link_name [ "pkg" ] ,
329
+ src = ":{}/{}" .format (link_name [ "pkg" ] , _PACKAGE_JSON_BZL_FILENAME ),
326
330
dep = "@{repo_name}//{link_package}:{package_name}_bzl_library" .format (
327
331
repo_name = helpers .to_apparent_repo_name (_import .name ),
328
332
link_package = link_package ,
329
- package_name = link_package [link_package .rfind ("/" ) + 1 ] if link_package else link_alias .split ("/" )[- 1 ],
333
+ package_name = link_package [link_package .rfind ("/" ) + 1 ] if link_package else link_name [ "pkg" ] .split ("/" )[- 1 ],
330
334
),
331
335
))
332
336
333
- for link_alias in link_aliases :
334
- package_json_bzl_file_path = "{}/{}/{}" .format (link_package , link_alias , _PACKAGE_JSON_BZL_FILENAME ) if link_package else "{}/{}" .format (link_alias , _PACKAGE_JSON_BZL_FILENAME )
337
+ for link_name in link_names :
338
+ package_json_bzl_file_path = "{}/{}/{}" .format (link_package , link_name [ "pkg" ] , _PACKAGE_JSON_BZL_FILENAME ) if link_package else "{}/{}" .format (link_name [ "pkg" ] , _PACKAGE_JSON_BZL_FILENAME )
335
339
repo_package_json_bzl = "@@{repo_name}//{link_package}:{package_json_bzl}" .format (
336
340
repo_name = _import .name ,
337
341
link_package = link_package ,
@@ -373,7 +377,6 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
373
377
for fp_link in fp_links .values ():
374
378
fp_package = fp_link .get ("package" )
375
379
fp_path = fp_link .get ("path" )
376
- fp_link_packages = fp_link .get ("link_packages" ).keys ()
377
380
fp_deps = fp_link .get ("deps" )
378
381
fp_target = "//{}:{}" .format (
379
382
fp_path ,
@@ -392,28 +395,31 @@ def npm_link_all_packages(name = "node_modules", imported_links = []):
392
395
if len (package_visibility ) == 0 :
393
396
package_visibility = ["//visibility:public" ]
394
397
395
- if len (fp_link_packages ) > 0 :
396
- npm_link_all_packages_bzl .append (_FP_DIRECT_TMPL .format (
397
- link_packages = fp_link_packages ,
398
- link_visibility = package_visibility ,
399
- pkg = fp_package ,
400
- package_directory_output_group = utils .package_directory_output_group ,
401
- root_package = root_package ,
402
- package_store_name = utils .package_store_name (fp_package , "0.0.0" ),
403
- package_store_root = utils .package_store_root ,
404
- ))
398
+ for link_type in ["link_packages" , "link_dev_packages" ]:
399
+ fp_link_packages = fp_link .get (link_type ).keys ()
400
+ if len (fp_link_packages ) > 0 :
401
+ npm_link_all_packages_bzl .append (_FP_DIRECT_TMPL .format (
402
+ link_packages = fp_link_packages ,
403
+ link_visibility = package_visibility ,
404
+ pkg = fp_package ,
405
+ package_directory_output_group = utils .package_directory_output_group ,
406
+ root_package = root_package ,
407
+ package_store_name = utils .package_store_name (fp_package , "0.0.0" ),
408
+ package_store_root = utils .package_store_root ,
409
+ ))
405
410
406
- npm_link_targets_bzl .append (_FP_DIRECT_TARGET_TMPL .format (
407
- link_packages = fp_link_packages ,
408
- pkg = fp_package ,
409
- ))
411
+ npm_link_targets_bzl .append (_FP_DIRECT_TARGET_TMPL .format (
412
+ link_packages = fp_link_packages ,
413
+ pkg = fp_package ,
414
+ prod_or_dev = "dev" if link_type == "link_dev_packages" else "prod" ,
415
+ ))
410
416
411
- if "//visibility:public" in package_visibility :
412
- add_to_link_targets = """ link_targets.append(":{{}}/{pkg}".format(name))""" .format (pkg = fp_package )
413
- npm_link_all_packages_bzl .append (add_to_link_targets )
414
- package_scope = fp_package [:fp_package .find ("/" , 1 )] if fp_package [0 ] == "@" else None
415
- if package_scope :
416
- npm_link_all_packages_bzl .append (_ADD_SCOPE_TARGET .format (package_scope = package_scope ))
417
+ if "//visibility:public" in package_visibility :
418
+ add_to_link_targets = """ link_targets.append(":{{}}/{pkg}".format(name))""" .format (pkg = fp_package )
419
+ npm_link_all_packages_bzl .append (add_to_link_targets )
420
+ package_scope = fp_package [:fp_package .find ("/" , 1 )] if fp_package [0 ] == "@" else None
421
+ if package_scope :
422
+ npm_link_all_packages_bzl .append (_ADD_SCOPE_TARGET .format (package_scope = package_scope ))
417
423
418
424
# Generate catch all & scoped js_library targets
419
425
npm_link_all_packages_bzl .append ("""
@@ -544,8 +550,11 @@ def _gen_npm_import(rctx, system_tar, _import, link_workspace):
544
550
maybe_replace_package = ("""
545
551
replace_package = "%s",""" % _import .replace_package ) if _import .replace_package else ""
546
552
553
+ link_packages = {}
554
+ for link_package , link_names in _import .link_packages .items ():
555
+ link_packages [link_package ] = [link_name ["pkg" ] for link_name in link_names ]
547
556
return _NPM_IMPORT_TMPL .format (
548
- link_packages = starlark_codegen_utils .to_dict_attr (_import . link_packages , 2 , quote_value = False ),
557
+ link_packages = starlark_codegen_utils .to_dict_attr (link_packages , 2 , quote_value = False ),
549
558
link_workspace = link_workspace ,
550
559
maybe_bins = maybe_bins ,
551
560
maybe_commit = maybe_commit ,
0 commit comments