-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdefs.bzl
More file actions
915 lines (744 loc) · 29.8 KB
/
Copy pathdefs.bzl
File metadata and controls
915 lines (744 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
"""Node Rules
Bazel rules for working with node.
"""
# Helper functions
runfiles_tmpl = '''#!/bin/bash -eu
# If a binary is listed as a data dep for another tool, you
# need to scan a bit differently for the runfiles dir. Of course, once
# this is resolved by realpath, the path will never have .runfiles in
# it when developing because of the way the files are symlinked, hence
# splitting the abspath.
#
# This also allows symlinks to bazel built binaries in production to correctly
# find their runfiles. This is commonly used in production to put bazel
# binaries in the PATH.
#
# Note: We call python by passing the script through stdin instead of using -c
# to prevent Python from prepending '' to sys.path and allowing
# modules from $PWD to be imported as top level modules.
STUBPATH=$(/usr/bin/env python2.7 -ESs <(echo "import os.path; print(os.path.realpath(os.path.abspath('$0').split('.runfiles')[0]));"))
STUBPATH=$STUBPATH.runfiles
export RUNFILES=$STUBPATH/{workspace_name}
{content}
'''
def _get_runfiles_tmpl(ctx, content):
# If we're building an external binary, then we need to use the
# workspace name of that binary, not the current workspace.
if ctx.label.workspace_root:
if not ctx.label.workspace_root.startswith('external/'):
fail('Workspace root must start with external/: {}'.format(
ctx.label.workspace_root,
))
workspace_name = ctx.label.workspace_root[len('external/'):]
else:
workspace_name = ctx.workspace_name
return runfiles_tmpl.format(
content=content,
workspace_name=workspace_name,
)
def _get_package_dir(ctx):
return ctx.label.package
def _get_output_dir(ctx):
# If it's an external label, output to workspace_root.
if ctx.label.workspace_root:
return ctx.configuration.bin_dir.path + '/' + ctx.label.workspace_root + '/' + _get_package_dir(ctx)
return ctx.configuration.bin_dir.path + '/' + _get_package_dir(ctx)
def _get_relpath(ctx, src):
package_prefix = _get_package_dir(ctx) + '/'
relpath = src.short_path
# If relpath starts with '../', that means it belongs to an
# external dependency, and it'll look like:
# '../{workspace_name}/{package_path}/{file}
# We want to transform it so it includes the package path
# and the file.
if relpath.startswith('../'):
parts = relpath.split('/')
relpath = '/'.join(parts[2:])
if not relpath.startswith(package_prefix):
fail('Path must be in the package: (path: {relpath}, package prefix: {package_prefix})'.format(
relpath=relpath,
package_prefix=package_prefix,
))
return relpath[len(package_prefix):]
def _new_node_modules_srcs_dict():
'''Returns a new `node_modules_srcs_dict` dict. It's just a normal
dict, this function exists so we can document how it works in one
place.
`node_modules_srcs_dict` is a map from paths relative to the
highest-level node_modules directory to Files.
E.g. if you have a file that's going to end up in:
main-module/node_modules/some-dep/dep.js
The key should be `some-dep/dep.js`.
NOTE: Make sure to fail if you need to add a path to
`node_modules_srcs_dict` that already exists.
'''
return {}
def _new_all_node_modules():
'''Returns a new `all_node_modules` dict. It's just a normal
dict, this function exists so we can document how it works in one
place.
`all_node_modules` is a map from unique targets to
`node_modules_srcs_dict`s.
The key should be unique to the package, and doesn't necessarily
have any other semantic meaning.
'''
return {}
def _npm_library_impl(ctx):
node_module_srcs = []
npm_req = ctx.attr.npm_req
if '@' not in npm_req:
fail("npm_req must contain a '@': {npm_req}".format(npm_req=npm_req))
# The module name is everything before the last '@'.
require_name = npm_req.rsplit('@', 1)[0]
require_prefix = require_name + '/'
node_modules_srcs_dict = _new_node_modules_srcs_dict()
for content in ctx.attr.contents:
if content in node_modules_srcs_dict:
fail('Duplicate path in node_modules: {content}'.format(content=content))
if not content.startswith(require_prefix) and not content.startswith('.bin/'):
fail(("npm library cannot include paths outside of {require_name} "+
"or '.bin': {content}").format(require_name=require_name, content=content))
# The created files are all under `node_modules/` because
# that's where the npm installer puts them.
node_modules_srcs_dict[content] = ctx.new_file('node_modules/' + content)
shrinkwrap = ctx.file.shrinkwrap
command_args = [
shrinkwrap.path,
_get_output_dir(ctx),
]
if ctx.attr.npm_installer_extra_args:
command_args.extend(ctx.attr.npm_installer_extra_args)
env = {}
if 'HTTP_PROXY' in ctx.var:
env['HTTP_PROXY'] = ctx.var['HTTP_PROXY']
if 'HTTPS_PROXY' in ctx.var:
env['HTTPS_PROXY'] = ctx.var['HTTPS_PROXY']
ctx.action(
inputs = [shrinkwrap],
outputs = node_modules_srcs_dict.values(),
executable = ctx.executable.npm_installer,
arguments = command_args,
progress_message = 'installing node modules from {}'.format(shrinkwrap.path),
mnemonic = 'InstallNPMModules',
env = env,
)
all_node_modules = _new_all_node_modules()
all_node_modules[str(ctx.label)] = node_modules_srcs_dict
return struct(
all_node_modules = all_node_modules,
)
_npm_library_internal = rule(
implementation = _npm_library_impl,
attrs = {
'shrinkwrap': attr.label(allow_single_file=True, mandatory=True),
'npm_req': attr.string(mandatory=True),
'contents': attr.string_list(mandatory=True),
'npm_installer': attr.label(
executable = True,
cfg = 'host',
),
'npm_installer_extra_args': attr.string_list(),
},
)
def npm_library(name, npm_req, shrinkwrap, contents,
no_import_main_test=False,
npm_installer='@org_dropbox_rules_node//node/tools/npm:install',
npm_installer_extra_args=[]):
'''Defines an external npm module.
This rule should usually be generated using
`//node/tools/npm:gen_build_npm`, like this:
bazel run @org_dropbox_rules_node//node/tools/npm:gen_build_npm -- my-module@1.2.3 ~/src/myrepo/npm/my-module
Defines an external npm module. The module and its dependencies
are downloaded using `npm_installer`.
All of the module's dependencies are declared in the shrinkwrap but
aren't known by Bazel. This is to work around Bazel's restrictions on
circular dependencies.
One restriction that this rule places on it's output is that all
the files output must either be in the module's directory or in
'.bin'. E.g. if the module is named `module-name`, then this list
of contents is legal:
contents = [
'module-name/src/something.js',
'.bin/some-binary',
]
But this list of contents is not:
contents = [
'module-name/src/something.js',
'another-modules/something-else.js',
]
This allows us to be reasonably sure that any two modules can be
used together (except when '.bin' has conflicts, which should be
rare).
Args:
npm_req: The npm string used to download this module. Must be in the form `module-name@1.2.3`.
Used to auto-generate this rule.
no_import_main_test: Don't test that the npm library can be required as if it has a main file. Defaults to False.
We test that all imports can be imported like: `require('module')`. For some imports that don't
have a `main` js file to execute, this import will fail. For example, `@types/` npm modules will
fail. Set this to True to disable that check.
NOTE: This rule will still generate a test to make sure that module version is correct.
shrinkwrap: The shrinkwrap file that lists out all the node modules to install.
contents: All of the files needed by the module.
contents is a string list instead of a label list to get around Bazel's restrictions on
label names, which are violated by npm packages pretty often. Some restrictions still exist,
like the restriction that file names cannot contain whitespace.
npm_installer: The binary to use to install the npm modules.
The default npm_installer downloads them from the public npm registry, but ideally you
should replace it with a binary that downloads from your private mirror.
npm_installer_args: Extra arguments to pass to `npm_installer`.
'''
_npm_library_internal(
name = name,
shrinkwrap = shrinkwrap,
npm_req = npm_req,
contents = contents,
npm_installer = npm_installer,
npm_installer_extra_args = npm_installer_extra_args,
)
_node_import_test(
name = name + '_import_test',
deps = [name],
npm_req = npm_req,
no_import_main_test = no_import_main_test,
)
def _collect_srcs_and_deps(ctx):
'''
Collects all the srcs and deps from ctx's direct and transitive dependencies.
Returns the tuple (srcs, all_node_modules).
'''
srcs = depset()
all_node_modules = _new_all_node_modules()
srcs += ctx.files.srcs
# Verify that `srcs` are not node_library or npm_library rules.
for src in ctx.attr.srcs:
if hasattr(src, 'all_node_modules'):
fail('Invalid src, srcs must only be files: {}'.format(src.label))
for dep in ctx.attr.deps:
if hasattr(dep, 'srcs'):
srcs += dep.srcs
if hasattr(dep, 'all_node_modules'):
for key, value in dep.all_node_modules.items():
if key not in all_node_modules:
all_node_modules[key] = value
return (srcs, all_node_modules)
def _node_library_impl(ctx):
srcs, all_node_modules = _collect_srcs_and_deps(ctx)
return struct(
srcs = srcs,
all_node_modules = all_node_modules,
runfiles = ctx.runfiles(collect_default=True),
)
node_library = rule(
implementation = _node_library_impl,
attrs = {
'srcs': attr.label_list(allow_files=True),
'deps': attr.label_list(allow_files=False),
'data': attr.label_list(cfg='data', allow_files=True),
}
)
"""
Groups node.js sources and deps together.
Args:
srcs: The list of source files that are processed to create the target.
deps: The list of other libraries or node modules needed to be linked into
the target library.
data: The list of files needed by this library at runtime.
"""
def _construct_runfiles(ctx, srcs, all_node_modules):
'''
Create the set of runfiles for node rules.
Puts all of the node modules in `$RUNFILES/{pkg_dir}/node_modules`
for the binary target.
Fails if any files in all_node_modules conflict with each other.
'''
package_dir = _get_package_dir(ctx)
if ctx.label.workspace_root:
if not ctx.label.workspace_root.startswith('external/'):
fail('Workspace root must start with external/: {}'.format(
ctx.label.workspace_root,
))
package_dir = ctx.label.workspace_root[len('external/'):] + '/' + package_dir
symlinks = {}
for node_modules_srcs_dict in all_node_modules.values():
for content_path, src in node_modules_srcs_dict.items():
path = package_dir + '/node_modules/' + content_path
if path in symlinks:
fail(('Cannot have conflicting paths in node_modules. This path was ' +
'in your node modules twice: {path}. These are the deps included ' +
'in your node_modules folder: {all_node_modules_keys}').format(
path=path,
all_node_modules_keys=all_node_modules.keys(),
))
symlinks[path] = src
# If the binary is in an external workspace, then we should create
# symlinks relative to the runfiles root.
if ctx.label.workspace_root:
return ctx.runfiles(
files = list(srcs),
root_symlinks = symlinks,
collect_default=True,
)
else:
return ctx.runfiles(
files = list(srcs),
symlinks = symlinks,
collect_default=True,
)
def _node_binary_impl(ctx):
srcs, all_node_modules = _collect_srcs_and_deps(ctx)
# Add node binary to runfiles
srcs += [ctx.file.node]
node_flags = ['--preserve-symlinks']
if ctx.attr.max_old_memory:
node_flags.append('--max_old_space_size={}'.format(ctx.attr.max_old_memory))
if ctx.attr.expose_gc:
node_flags.append('--expose-gc')
default_args = ' '.join(ctx.attr.extra_args + ['"$@"'])
# We use --preserve-symlinks so that node will respect the symlink
# tree in runfiles. One quirk of preserve symlinks is that
# symlinks aren't preserved for the "main" module. To work around
# this, we create an wrapper js file that just has a call to
# `require('/path/to/main.js')`.
#
# We can't do `node --eval "require('/path/to/main.js')"` because
# node won't populate process.argv correctly.
# Create the inner wrapper with the require call.
inner_wrapper = ctx.new_file(ctx.outputs.executable, ctx.outputs.executable.basename + '-wrapper.js')
srcs += [inner_wrapper]
ctx.file_action(
inner_wrapper,
'''require(process.env['BAZEL_NODE_MAIN_ID']);''',
)
# Some scripts use the pattern `if (require.main === module) {` to
# only execute something if the node script is the "main" script.
# If you want that to work with bazel, you should check
# BAZEL_NODE_MAIN_ID against the module id instead, like this:
# if (process.env['BAZEL_NODE_MAIN_ID'] === module.id || require.main === module) {
node_runfile_tmpl = '''
export BAZEL_NODE_MAIN_ID=$RUNFILES/{main}
export NODE_PATH=$RUNFILES/{node_path}
$RUNFILES/{node} {node_flags} $RUNFILES/{inner_wrapper} {default_args}
'''
runfile_content = _get_runfiles_tmpl(
ctx = ctx,
content = node_runfile_tmpl.format(
main = ctx.file.main.short_path,
node = ctx.file.node.short_path,
node_flags = ' '.join(node_flags),
inner_wrapper = inner_wrapper.short_path,
default_args = default_args,
node_path = _get_package_dir(ctx) + '/node_modules',
)
)
# Generate output executable
ctx.file_action(
output = ctx.outputs.executable,
content = runfile_content,
executable = True
)
runfiles = _construct_runfiles(ctx, srcs, all_node_modules)
return struct(runfiles=runfiles)
_node_bin_attrs = {
'srcs': attr.label_list(allow_files=True),
'deps': attr.label_list(allow_files=False),
'data': attr.label_list(cfg='data', allow_files=True),
'main': attr.label(allow_single_file=True, mandatory=True),
'extra_args': attr.string_list(),
'node': attr.label(
allow_single_file = True,
default = Label('@nodejs//:node'),
),
'max_old_memory': attr.int(),
'expose_gc': attr.bool(default=False),
}
node_binary = rule(
implementation = _node_binary_impl,
executable = True,
attrs = _node_bin_attrs,
)
"""Creates a node binary, which is an executable Node program consisting
of a collection of `.js` source files.
Node binaries are created using `--preserve-symlinks`.
One quirk of this rule is that if your script uses the pattern:
```javascript
if (require.main === module) {
```
to only execute something if the node script is the "main" script,
you'll need to modify it to check `BAZEL_NODE_MAIN_ID` against the
module id instead, like this:
```javascript
if (process.env['BAZEL_NODE_MAIN_ID'] === module.id || require.main === module) {
```
All node modules that this rule depends on, direct and transitive, end
up flattened in `{package-directory}/node_modules`, where
`package-directory` is the directory that the node_binary target
is it. This means that, across all of your transitive dependencies
tracked by Bazel, you can't depend on different versions of the same
node module. (This does not apply to transitive dependencies of
npm_library rules, which are not tracked by Bazel.)
The environmental variable `NODE_PATH` is set to
`{package-directory}/node_modules` so that all the files that end up
running can find all the included node modules.
One side-effect of that is that node libraries have access to all the
transitive dependencies for the node binary that depends on them.
Examples:
```python
node_binary(
name = 'mybin',
srcs = [
'mybin.js',
],
expose_gc = True,
main = 'mybin.js',
deps = [
'//npm/loose-envify',
'//npm/minimist',
'//npm/mkdirp',
],
)```
```python
node_library(
name = 'lib',
srcs = ['lib.js'],
deps = [
'//npm/mkdirp',
],
)
node_binary(
name = 'bin',
srcs = ['bin.js'],
deps = [
':lib',
],
)
```
Args:
srcs: The list of source files that are processed to create the target.
deps: The list of other libraries included in the binary target.
data: The list of files needed by this binary at runtime.
main: The name of the source file that is the main entry point of the
application.
extra_args: Command line arguments that bazel will pass to the target when it
is executed either by the `run` command or as a test. These arguments
are passed before the ones that are specified on the `bazel run` or
`bazel test` command line.
node: The node binary used to run the binary. Must be greater than 6.2.0.
max_old_memory: Optional. Node, by default, doesn't run its garbage collector on old space
until a maximum old space size limit is reached. This overrides the default (of around 1.4gb)
with the provided value in MB.
expose_gc: Optional. Expose `global.gc()` in the node process to allow manual requests for
garbage collection.
"""
node_test = rule(
implementation = _node_binary_impl,
test = True,
attrs = _node_bin_attrs,
)
"""
Defines a basic node test. Succeeds if the program has a return code of
0, otherwise it fails.
Args:
srcs: The list of source files that are processed to create the target.
deps: The list of other libraries included in the binary target.
data: The list of files needed by this binary at runtime.
main: The name of the source file that is the main entry point of the
application.
extra_args: Command line arguments that bazel will pass to the target when it
is executed either by the `run` command or as a test. These arguments
are passed before the ones that are specified on the `bazel run` or
`bazel test` command line.
node: The node binary used to run the binary. Must be greater than 6.2.0.
"""
def mocha_test(name, srcs=[], deps=[], extra_args=[],
mocha_target='@org_dropbox_rules_node//npm/mocha',
chai_target='@org_dropbox_rules_node//npm/chai',
**kwargs):
"""
Defines a node test that uses mocha. Takes the same args as
`node_binary`, except that you can't pass a `main` arg,
because mocha is run as the main js file.
Args:
srcs: The list of files to test.
deps: The list of other libraries included in the binary target.
extra_args: The list of other args for this test.
mocha_target: The target to use for including 'mocha'.
chai_target: The target ot use for including 'chai'.
"""
if "main" in kwargs:
fail("You cannot provide a 'main' to node_test rules. Use 'srcs' instead.")
if not srcs:
fail("'srcs' cannot be empty")
root = "$RUNFILES/" + PACKAGE_NAME
mocha_args = [root + "/" + src for src in srcs]
additional_deps = [mocha_target, chai_target]
# bin/mocha is the outer wrapper, and it calls `_mocha` as a
# new process, which messes up the `require` logic somehow.
#
# TODO: After node is upgraded to 7.1.0+, set the env
# NODE_PRESERVE_SYMLINKS=1 and see if `bin/mocha` works.
main = 'node_modules/mocha/bin/_mocha'
node_test(
name = name,
srcs = srcs,
main = main,
deps = depset(additional_deps) + deps,
extra_args = extra_args + mocha_args,
**kwargs
)
def _node_internal_module_impl(ctx):
if ctx.attr.package_json and ctx.attr.main:
fail('Can only specify one of package_json or main')
srcs, all_node_modules = _collect_srcs_and_deps(ctx)
require_name = None
if ctx.attr.require_name:
require_name = ctx.attr.require_name
else:
require_name = ctx.label.name
if ctx.attr.package_json:
srcs += [ctx.file.package_json]
elif ctx.attr.main:
# Create a package.json file that points to the 'main' js
# file.
package_json_src = ctx.new_file('package.json')
main = ctx.file.main
main_relpath = _get_relpath(ctx, main)
ctx.file_action(
package_json_src,
'''{{"main": "{main_relpath}"}}'''.format(main_relpath=main_relpath),
)
srcs += [package_json_src]
node_modules_srcs_dict = _new_node_modules_srcs_dict()
for src in srcs:
relpath = _get_relpath(ctx, src)
require_path = require_name + '/' + relpath
if require_path in node_modules_srcs_dict:
fail('Duplicate path in node_modules: {require_path}'.format(
require_path=require_path,
))
node_modules_srcs_dict[require_path] = src
all_node_modules[str(ctx.label)] = node_modules_srcs_dict
return struct(
all_node_modules = all_node_modules,
runfiles = ctx.runfiles(collect_default=True),
)
node_internal_module = rule(
implementation = _node_internal_module_impl,
attrs = {
'srcs': attr.label_list(allow_files=True),
'deps': attr.label_list(allow_files=False),
'data': attr.label_list(cfg='data', allow_files=True),
'require_name': attr.string(),
'package_json': attr.label(allow_single_file=True),
'main': attr.label(allow_single_file=True),
}
)
"""
Create an internal node module that can be included in the `deps` for
other rules and that can be required as `require('module_name')`.
Args:
srcs: The list of source files that are processed to create the target.
deps: The list of other libraries included in the target.
data: The list of files needed by this target at runtime.
require_name: Optional. The name for the internal node module. E.g.
if `require_name = "my-module"`, it should be used like
`require('my-module')`. The default is the target name.
package_json: Optional. The package.json for the project. Cannot be
specified along with `main`.
main: Optional. Defaults to the `main` field in the package.json or
`index.js` if that doesn't exist. Cannot be specified along with
`package_json`.
"""
def _node_import_test(name, deps, npm_req, no_import_main_test):
"""
Test that the module `npm_req` can be imported and that its
version is correct.
Args:
deps: The list of other libraries included in the target.
npm_req: Must be in the form `module-name@1.2.3`.
no_import_main_test: Don't test that the npm library can be required as if it has a main file.
See npm_library for details.
"""
# Break npm_req into its name and version.
if '@' not in npm_req:
fail('npm_req is malformed, it must look like `module-name@1.2.3`: ' + npm_req)
split = npm_req.split('@')
import_name = '@'.join(split[:-1])
import_version = split[-1]
args = [
'--import_name=' + import_name,
'--import_version=' + import_version,
]
if no_import_main_test:
args.append('--no_import_main_test')
node_test(
name = name,
srcs = ['@org_dropbox_rules_node//node/tools:import_check.js'],
main = '@org_dropbox_rules_node//node/tools:import_check.js',
deps = deps,
extra_args = args,
size = 'small',
)
def _node_build_impl(ctx):
env = dict(ctx.attr.env.items() + {
'BAZEL_OUTPUT_DIR': _get_output_dir(ctx),
}.items())
args = []
if ctx.var["COMPILATION_MODE"] == "opt" and ctx.attr.optimize_flag:
args.append(ctx.attr.optimize_flag)
ctx.action(
executable = ctx.executable.builder,
outputs = ctx.outputs.outs,
env = env,
arguments = args + ctx.attr.extra_args,
mnemonic = 'NodeBuild',
progress_message = 'building {} with node'.format(ctx.label.name),
)
return struct(
files = depset(ctx.outputs.outs),
runfiles = ctx.runfiles(
files = ctx.outputs.outs,
collect_default = True,
),
)
node_build = rule(
implementation = _node_build_impl,
attrs = {
'outs': attr.output_list(mandatory=True),
'data': attr.label_list(cfg='data', allow_files=True),
'builder': attr.label(executable=True, cfg='host', mandatory=True),
'env': attr.string_dict(),
'extra_args': attr.string_list(),
'optimize_flag': attr.string(),
},
)
"""Build JS/CSS/etc with a node binary.
This is a low-level rule, and is only recommended for completely
custom node builds. If you're using webpack, you should use the
webpack_build rule. If you're using another standard JS build system
(rollup, gulp, grunt, ...), you should write a macro that follows the
conventions of webpack_build.
This rule does not have a `srcs` attribute because it expects all the
srcs needed for the build to be included in the `builder` binary.
The environmental variable `BAZEL_OUTPUT_DIR` is set for all builds.
The `builder` binary should output to that directory.
For more specific use cases, you should write a macro that creates
this rule (see webpack_build).
Args:
outs: The list of files output by this rule.
data: The list of files needed by this target at runtime.
builder: The node binary used to build.
env: Additional environmental variables to set for the build.
extra_args: The list of additional args for this build.
optimize_flag: The flag to pass to the build when it's run in "opt" mode.
If this flag is not defined, then no flag is passed in "opt" mode.
"""
def webpack_build(name, srcs=[], deps=[], data=[], config='', outs=[], env={},
extra_args=[], webpack_target='@org_dropbox_rules_node//npm/webpack'):
"""
Build JS/CSS/etc with webpack.
It's recommend that you use the internal node module `dbx-bazel-utils`
(`@org_dropbox_rules_node//node/dbx-bazel-utils`). If you use it like
this:
```javascript
var dbxBazelUtils = require('dbx-bazel-utils');
var env = dbxBazelUtils.initBazelEnv(__dirname);
```
Then it will set the working directory to the directory that contains
webpack.config.js, which is usually what you want, and you should
output to the directory in `env.outputRoot`.
If the webpack build is run outside of Bazel, then `env.outputRoot`
will be `__dirname`.
Defaults to using webpack@3.4.1. You can specify your own webpack
target with `webpack_target`.
Examples:
```python
# webpack_build/BUILD
load('@org_dropbox_rules_node//node:defs.bzl', 'webpack_build')
webpack_build(
name = 'webpack_build',
srcs = glob([
'src/*.js',
]),
outs = ['bundle.js'],
config = 'webpack.config.js',
deps = [
'@org_dropbox_rules_node//node/dbx-bazel-utils',
],
)
```
```javascript
// webpack_build/webpack.config.js
var path = require('path');
var dbxBazelUtils = require('dbx-bazel-utils');
var env = dbxBazelUtils.initBazelEnv(__dirname);
module.exports = {
entry: ['entry.ts'],
output: {
filename: 'bundle.js',
path: env.outputRoot,
},
}
```
Args:
srcs: The list of source files that are processed to create the target.
deps: The list of other libraries included in the target.
data: The list of files needed by this target at runtime.
outs: The list of files output by this rule.
extra_args: The list of additional args for this build.
env: Additional environmental variables to set for the build.
config: The webpack.config.js file.
webpack_target: The webpack target to use.
"""
if not config:
fail('Must specify a config')
if not outs:
fail('Must specify output')
srcs += [config]
deps += [webpack_target]
node_binary(
name = name + '_bin',
srcs = srcs,
deps = deps,
data = data,
main = 'node_modules/webpack/bin/webpack.js',
extra_args = [
'--config',
'$RUNFILES/{pkg}/{config}'.format(
pkg=PACKAGE_NAME,
config=config,
),
] + extra_args,
)
node_build(
name = name,
outs = outs,
data = data,
builder = name + '_bin',
optimize_flag = '-p',
env = env,
)
NODEJS_BUILD_FILE_CONTENT = r"""
package(default_visibility = [ "//visibility:public" ])
filegroup(
name = "node",
srcs = [ "bin/node" ],
)
filegroup(
name = "npm",
srcs = glob([
"bin/npm",
"bin/node",
"lib/node_modules/npm/**/*",
]),
)
"""
def node_repositories(omit_nodejs=False):
if not omit_nodejs:
native.new_http_archive(
name = "nodejs",
url = "https://nodejs.org/dist/v6.11.1/node-v6.11.1-linux-x64.tar.xz",
type = "tar.xz",
strip_prefix = "node-v6.11.1-linux-x64",
sha256 = "e68cc956f0ca5c54e7f3016d639baf987f6f9de688bb7b31339ab7561af88f41",
build_file_content = NODEJS_BUILD_FILE_CONTENT,
)