-
Notifications
You must be signed in to change notification settings - Fork 522
/
js_library.bzl
64 lines (54 loc) · 2.33 KB
/
js_library.bzl
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
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""js_library allows defining a set of javascript sources and assigning a module_name and module_root.
DO NOT USE - this is not fully designed, and exists only to enable testing within this repo.
"""
_AMD_NAMES_DOC = """Mapping from require module names to global variables.
This allows devmode JS sources to load unnamed UMD bundles from third-party libraries."""
AmdNamesInfo = provider(
doc = "provide access to the amd_names attribute of js_library",
fields = {"names": _AMD_NAMES_DOC},
)
def write_amd_names_shim(actions, amd_names_shim, targets):
"""Shim AMD names for UMD bundles that were shipped anonymous.
These are collected from our bootstrap deps (the only place global scripts should appear)
Args:
actions: skylark rule execution context.actions
amd_names_shim: File where the shim is written
targets: dependencies to be scanned for AmdNamesInfo providers
"""
amd_names_shim_content = """// GENERATED by js_library.bzl
// Shim these global symbols which were defined by a bootstrap script
// so that they can be loaded with named require statements.
"""
for t in targets:
if AmdNamesInfo in t:
for n in t[AmdNamesInfo].names.items():
amd_names_shim_content += "define(\"%s\", function() { return %s });\n" % n
actions.write(amd_names_shim, amd_names_shim_content)
def _js_library(ctx):
return [
DefaultInfo(files=depset(ctx.files.srcs)),
AmdNamesInfo(names=ctx.attr.amd_names),
]
js_library = rule(
implementation = _js_library,
attrs = {
"srcs": attr.label_list(allow_files = [".js"]),
# Used to determine module mappings
"module_name": attr.string(),
"module_root": attr.string(),
"amd_names": attr.string_dict(doc = _AMD_NAMES_DOC),
},
)