| # 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), |
| runfiles = ctx.runfiles(files = ctx.files.srcs), |
| ), |
| AmdNamesInfo(names = ctx.attr.amd_names), |
| ] |
| |
| js_library = rule( |
| implementation = _js_library, |
| attrs = { |
| "srcs": attr.label_list(allow_files = [".js"]), |
| "amd_names": attr.string_dict(doc = _AMD_NAMES_DOC), |
| # Used to determine module mappings |
| "module_name": attr.string(), |
| "module_root": attr.string(), |
| }, |
| ) |