fle*_*xit 5 javascript node.js npm bazel npm-start
我如何使用nodejs_binary规则来执行标准的 npm run start。我能够使用此规则运行典型的节点项目。但是我想在 package.json 中运行一个启动脚本。到目前为止,我的构建文件中有以下内容
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
nodejs_binary(
name = "app",
data = [":app_files"],
node="@nodejs//:bin/npm",
entry_point = "workspace_name/src/server.js",
node_modules = "@npm_deps//:node_modules",
args=["start"]
)
Run Code Online (Sandbox Code Playgroud)
这不会启动服务器......不知何故 npm 命令运行不正常。它表示命令的用法不完整。
我目前可以在 WORKSPACE 中执行此操作
bazel run @nodejs//:bin/yarn (运行 yarn install 并安装所有节点模块)
bazel run @nodejs//:bin/npm start (这将启动服务器)
在我的 package.json 我有
{
"scripts": {
"start": "babel-node src/server.js",
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我确实让这个与 nodejs_binary 规则和随后的 node_image 一起使用
我从使用 npm 更改为使用 yarn..workspace_name/src/server.js.. 现在被调用但是然后我遇到了不同的问题,没有找到 babel-node。
我稍微修改了规则。经过仔细研究......我意识到在调用yarn run start时不满足对babel-node的依赖。在我bazel run @nodejs//:bin/yarn运行规则之前运行后,以下工作。
nodejs_binary(
name = "app",
args = ["start"],
data = [
":app_files",
"@//:node_modules",
],
entry_point = "workspace_name/src/server.js",
node = "@nodejs//:bin/yarn",
node_modules = "@npm_deps//:node_modules",
)
Run Code Online (Sandbox Code Playgroud)
看来"@//:node_modules"解决了 babel-node 依赖问题。所以上面的规则本身不起作用......它需要我做bazel run @nodejs//:bin/yarn(更像是 npm/yarn install 来制作 node_modules,其中包含在 npm/yarn start 运行时可用的 babel-node 依赖项)
所以我的问题是我不想bazel run @nodejs//:bin/yarn在执行我的规则之前手动运行。我该怎么做呢。我想如果我停止依赖 babel-node 的话它会起作用……但是我必须更改我的代码以不使用 es6 语法(这很麻烦)。有没有办法用 genrule 来做到这一点?或者其他的东西...
我最终做的是我制定了 babel nodejs_binary 规则。然后用它来编译 gen 规则中的源文件
# Make babel binary
nodejs_binary(
name = "babel",
entry_point = "npm_deps/node_modules/babel-cli/bin/babel",
install_source_map_support = False,
node_modules = "@npm_deps//:node_modules",
)
# Compile source files with babel
genrule(
name = "compiled_src",
srcs = [
":src_files",
],
outs = ["src"],
cmd = "$(location babel) src --out-dir $@",
tools = [":babel"],
)
Run Code Online (Sandbox Code Playgroud)
请注意,在本例中 src incmd = "$(location babel) src --out-dir $@"是文件组中的一个文件夹:src_files。
filegroup(
name = "src_files",
srcs = glob([
"src/**/*",
...
]),
)
Run Code Online (Sandbox Code Playgroud)
之后就不需要使用了npm start,只使用默认节点。我可以这样做
nodejs_binary(
name = "app",
data = [":compiled_src"],
entry_point = "workspace_name/src/server.js",
node_modules = "@npm_deps//:node_modules",
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2525 次 |
| 最近记录: |