Bazel 的滚动规则
滚出规则运行 滚出.js 和巴泽尔在一起.
安装
添加数据 @bazel/rollup npm 软件包到您的 devDependencies 内 package.json. (中文(简体) )rollup 本身也应包括在 devDependencies除非你计划通过定制目标提供它。 )
安装用户管理的依赖
如果你没有使用 yarn_install 或者说 npm_install 规则,你得在你的根部宣布一条规则 BUILD.bazel 要执行滚动的文件 :
# Create a rollup rule to use in rollup_bundle#rollup_bin
# attribute when using user-managed dependencies
nodejs_binary(
name = "rollup_bin",
entry_point = "//:node_modules/rollup/bin/rollup",
# Point bazel to your node_modules to find the entry point
data = ["//:node_modules"],
)
使用量
这个 rollup_bundle 规则用于引用一些 JavaScript 输入的 Rollup .
API 文件出现 下级。 。 。 .
典型实例:
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
rollup_bundle(
name = "bundle",
srcs = ["dependency.js"],
entry_point = "input.js",
config_file = "rollup.config.js",
)
请注意, Bazel 设定的命令行选项会覆盖滚动配置文件中显示的内容 .
这意味着,典型的单一 rollup.config.js 可以为您的整个 repo 设置,
和多个 rollup_bundle 规则可以共享配置.
因此,设定Bazel控制的选项不会产生任何效果,例如:.
module.exports = {
output: { file: 'this_is_ignored.js' },
}
产出类型
您必须提前确定 Rollup 会写一个文件还是目录 .
Rollup的CLI也有同样的行为,强迫你挑 --output.file 或者说 --output.dir。 。 。 .
当您有动态导入导致代码分割时, 或者当您有代码分割时, 写入目录会被使用
提供多个切入点。 使用 output_dir 属性来指定您想要一个
目录输出.
每个 rollup_bundle 规则只通过一次运行滚出 CLI 来产生一个输出.
要获得多种输出格式,可以将规则用宏或列表理解来包裹,例如.
[
rollup_bundle(
name = "bundle.%s" % format,
entry_point = "foo.js",
format = format,
)
for format in [
"cjs",
"umd",
]
]
这将按要求的格式产生一个产出.
贴花
您可以通过在滚动配置中写入一些代码来将当前版本控制信息印入输出. 见 打印文档。 。 。 .
通过传递 --stamp 选项到 Bazel , 将可读取另外两个输入文件.
- 变量
bazel_version_file将指向bazel-out/volatile-status.txt包含 经常变化的状态; 这样的变化不会导致滚动- bundle 的重建. - 变量
bazel_info_file将指向bazel-out/stable-status.txt包含 状态不变; 任何更改的值都会导致滚动 bundle 重建.
两者 bazel_version_file 财务报告和审定财务报表 bazel_info_file 将会是 undefined 如果建筑没有运行 --stamp。 。 。 .
请注意
--stamp,只重建了捆绑,而不是生产投入的编译步骤. 这避免了缓慢地重建一整棵行动树.
为了使用这些文件,你把JS代码写在你的 rollup.config.js 从状态文件读取并解析行.
每行为空格分隔的密钥/值对.
/**
* The status files are expected to look like
* BUILD_SCM_HASH 83c699db39cfd74526cdf9bebb75aa6f122908bb
* BUILD_SCM_LOCAL_CHANGES true
* STABLE_BUILD_SCM_VERSION 6.0.0-beta.6+12.sha-83c699d.with-local-changes
* BUILD_TIMESTAMP 1520021990506
*
* Parsing regex is created based on Bazel's documentation describing the status file schema:
* The key names can be anything but they may only use upper case letters and underscores. The
* first space after the key name separates it from the value. The value is the rest of the line
* (including additional whitespaces).
*
* @param {string} p the path to the status file
* @returns a two-dimensional array of key/value pairs
*/
function parseStatusFile(p) {
if (!p) return [];
const results = {};
const statusFile = require('fs').readFileSync(p, {encoding: 'utf-8'});
for (const match of `
${statusFile}`.matchAll(/^([A-Z_]+) (.*)/gm)) {
// Lines which go unmatched define an index value of `0` and should be skipped.
if (match.index === 0) {
continue;
}
results[match[1]] = match[2];
}
return results;
}
// This undefined variable will be replaced with the full path during the build.
const statuses = parseStatusFile(bazel_version_file);
// Parse the stamp file produced by Bazel from the version control system
let version = '<unknown>';
// Don't assume BUILD_SCM_VERSION exists
if (statuses['BUILD_SCM_VERSION']) {
version = 'v' + statuses['BUILD_SCM_VERSION'];
if (DEBUG) {
version += '_debug';
}
}
调试和 Opt 构建
使用时 --compilation_mode=dbg,巴泽尔在其中生产出一棵独特的产出树 bazel-out/[arch]-dbg/bin。 。 。 .
密码在您的 rollup.config.js 可以在环境中查看是否正在进行调试构建,
并包含额外的开发者信息 在捆绑,你通常不会运送生产.
同样, --compilation_mode=opt 是巴泽尔的信号 执行额外的优化.
您可以使用这个值来进行额外的只生产优化 .
例如, 您可以定义启用调试的常数 :
const DEBUG = process.env['COMPILATION_MODE'] === 'dbg';
并配置不同的滚动 DEBUG 实值 true 或者说 false。 。 。 .
增加卷起的堆积内存
这个 rollup_bin 属性允许您自定义我们执行的 rollup.js 程序,
这样你就可以使用 nodejs_binary 来建造你自己.
你随时可以打电话
bazel query --output=build [default rollup_bin]来看看什么 默认定义看起来是,然后从那里复制到确定您的 火柴。 比赛.
nodejs_binary(
name = "rollup_more_mem",
data = ["@npm//rollup:rollup"],
entry_point = "@npm//:node_modules/rollup/dist/bin/rollup",
templated_args = [
"--node_options=--max-old-space-size=<SOME_SIZE>",
],
)
rollup_bundle(
...
rollup_bin = ":rollup_more_mem",
)
滚动( b)
用户
rollup_bundle(name, args, config_file, deps, entry_point, entry_points, format, link_workspace_root, output_dir, rollup_bin, rollup_worker_bin, silent, silent_on_success, sourcemap, srcs, stamp, supports_workers)
在Bazel下运行滚出.js CLI.
捐款
名称
(单位:千美元)名称,强制:这个目标的独有名称.
参数
(单位:千美元)字符串列表: 命令行参数传递给 Rollup. 可用于覆盖配置文件设置 .
这些论点在命令行上传入了规则所添加的论据前.
运行 bazel 与 --subcommands 以查看所引用的 Rollup CLI 命令行.
见 滚动CLI文件 用于完整列出支持的参数.
默认为 []
配置文件( f)
(单位:千美元)标签页:1 rollup.config.js 文件
传到 --config 选项,参见 配置文件
如果没有设置,则使用默认的基本滚动配置 .
默认为 @npm//@bazel/rollup:rollup.config.js
代码
(单位:千美元)标签列表: 代码要求的其他库,或由卷起. config.js
默认为 []
输入点( c)
(单位:千美元)标签:捆绑的切入点(例如:您的主干.js或app.js或index.js).
这只是一个捷径 entry_points 属性,带有一个与规则名称相同的单一输出块.
例如,这些是等同的:
rollup_bundle(
name = "bundle",
entry_point = "index.js",
)
rollup_bundle(
name = "bundle",
entry_points = {
"index.js": "bundle"
}
)
若为 rollup_bundle 用于一个 ts_library,则 rollup_bundle 规则处理从 ts_library。 。 。 .
在这种情况下, entry_point 可指定为 .ts 文档和 rollup_bundle 将处理绘图到 .mjs 输出文件 .
例如:
ts_library(
name = "foo",
srcs = [
"foo.ts",
"index.ts",
],
)
rollup_bundle(
name = "bundle",
deps = [ "foo" ],
entry_point = "index.ts",
)
默认为 None
条目( 点)
(单位:千美元)词典:标签 - > 字符串:捆绑的切入点(如:您的主.js或app.js或index.js).
传到 --input 选项 在滚滚.
本词典中的密钥是指向.js入站文件的标签. 值是给相应输出块取的名称.
此属性或 entry_point 必须具体说明,但不能同时说明.
默认为 {}
格式
(单位:千美元)字符串:指定生成捆绑的格式。 以下是:
amd: 同步模块定义, 与模块加载器使用, 如RequestJScjs: 普通JS, 适合节点和其他捆绑器esm: 将捆绑保留为ES模块文件,适合其他捆绑者,并将其作为包含<script type=module>现代浏览器中的标记iife: 适合作为<script>标签。 (如果您想要为您的应用程序创建捆绑, 您可能想要使用这个 。)umd: 通用模块定义,工作方式为 amd、cjs 和 iife 全部在一个system: SystemJS 加载器的原生格式
默认为 "esm"
链接 工作空间 root
(单位:千美元)布尔: 将工作空间根链接到 bin dir 支持绝对要求, 如“ my wksp/ path/ to/ file ” . 如果需要源文件,则可以复制到bin dir并复制到 bin.
默认为 False
输出 dir
(单位:千美元)布尔: 是否生成目录输出 .
我们会使用 --output.dir 选项 正在滚动
而不是说 --output.file。 。 。 .
如果程序产生多个块, 您必须指定此属性 . 否则,产出被假定为一个文件.
默认为 False
滚动 宾
(单位:千美元)标签: 执行滚动二进制的目标
默认为 @npm//rollup/bin:rollup
滚动 工人 宾
(单位:千美元)标签: 仅内部使用
默认为 @npm//@bazel/rollup/bin:rollup-worker
无声
(单位:千美元)布尔: 是否使用 -- silent 旗帜执行滚动二进制, 默认为 False .
使用 -- silent 会导致卷起到 忽略错误/警告 仅通过伐木而浮出水面。 由于巴泽尔预计不会打印任何成功信息,所以要静默地写给特鲁伊 更是巴泽尔式的经验.
默认为 False
静态( a)
(单位:千美元)布尔:甚至比 -- 沉默、默认和虚伪更强大.
由于该建筑仍然发出一些已发短信, 即使通过 -- silent, 这使用与 npm package bin 相同的旗帜 给subess上的所有输出加压.
默认为 False
源图
(单位:千美元)字符串:是否生产出源图.
传到 --sourcemap 选项 在滚动中
默认为 "inline"
弧线
(单位:千美元)标签列表: 来自工作空间的非输入点JavaScript源文件.
您不能重复通过的文件到入站/入站点.
默认为 []
邮票
(单位:千美元)标签: 是否将构建信息编码到输出中. 可能的数值 :
- 说吧 @rules_nodejs//nodejs/stamp:always数字 :
总是将构建信息印入输出中,甚至在 [--nostamp [stamp] 构建中也是如此.
应当避免这种设置,因为它可能造成缓存错过远程缓存
任何下游行动 取决于它.
- 说吧 @rules_nodejs//nodejs/stamp:never数字 :
总是用常数替换构建信息 。 这给了良好的建材结果缓冲.
- 说吧 @rules_nodejs//nodejs/stamp:use_stamp_flag数字 :
嵌入建构信息由[---[无]标 [标]旗所控制.
被标记的二进制除非其依赖关系发生变化,否则不会被重建.
[印 : https://docs.bazel.building/versions/main/user-manual.html#flag-stamp 页面存档备份,存于互联网档案馆 这个属性的依赖性必须提供: StampSettingInfo
默认为 @rules_nodejs//nodejs/stamp:use_stamp_flag
支持工作者
(单位:千美元)布尔实验! 只用谨慎.
允许您启用此库的 Bazel Worker 策略 . 启用时, 此规则引用“ rollup worker bin” 工人知道二进制,而不是“滚出 bin”.
默认为 False