解决Bazel monorepo中的本地NodeJS依赖项

时间:2019-09-12 12:41:25

标签: node.js bazel

我正在尝试学习如何与Bazel建立Monorepo。 My code is on GitHub

我在Mac OS 10.14.5上将bazelbuild/rules_nodejs版本0.37与Bazel 0.29.1-homebrew一起使用。

我遇到的问题是,通过bazelbuild/rules_nodejs中的require规则运行时,库模块的rollup_bundle找不到生成的库。

我认为我的配置中有错误,但是从阅读文档中尚不清楚。相关代码部分复制在下面,但最新版本始终位于存储库中。

编码和生成文件

/WORKSPACE

workspace(
    name = "bazel_example",
    managed_directories = {"@npm": ["node_modules"]},
)

# load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "build_bazel_rules_nodejs",
    sha256 = "0d9660cf0894f1fe1e9840818553e0080fbce0851169812d77a70bdb9981c946",
    urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.37.0/rules_nodejs-0.37.0.tar.gz"],
)

load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")
yarn_install(
    name = 'npm',
    package_json = '//:package.json',
    yarn_lock = '//:yarn.lock',
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
install_bazel_dependencies()

# Set up TypeScript toolchain
load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
ts_setup_workspace()

/apps/jsapp003中,我有一个简单的“ Hello world”应用程序,它使用了来自同一存储库(/libs/jslib001)的库。

/libs/jslib001/index.js

module.exports = () => 'Hello world'

/libs/jslib001/BUILD.bazel

package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")

filegroup(
    name = 'src',
    srcs = [
        'index.js',
    ],
)

npm_package(
    name = 'pkg',
    srcs = [
        'package.json',
    ],
    deps = [
        ':src'
    ],
)

/apps/jsapp003/index.js

const jslib001 = require('jslib001')

module.exports = () => {
    console.log(jslib001())
}

/apps/jsapp003/BUILD.bazel

package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package", "nodejs_binary", "rollup_bundle")

filegroup(
    name = 'src',
    srcs = [
        'index.js',
    ],
)

npm_package(
    name = 'pkg',
    srcs = [
        'package.json'
    ],
    deps = [
        ':src',
        '//libs/jslib001:pkg'
    ],
)

nodejs_binary(
    name = 'bin',
    entry_point = ':index.js',
    data = [
        ':pkg'
    ]
)

rollup_bundle(
    name = 'bundle',
    entry_point = ':index.js',
    deps = [
        ':pkg',
    ],
)

构建命令和错误消息

bazel build ...
INFO: Analyzed 12 targets (0 packages loaded, 14 targets configured).
INFO: Found 12 targets...
ERROR: /Users/ian/code/bazel-example/apps/jsapp003/BUILD.bazel:31:1: Bundling JavaScript apps/jsapp003/bund
le_chunks_es2015 [rollup] failed (Exit 1) rollup.sh failed: error executing command bazel-out/host/bin/external/
build_bazel_rules_nodejs/internal/rollup/rollup.sh --config bazel-out/darwin-fastbuild/bin/apps/jsapp003/_bundle
.rollup.conf.js --output.dir ... (remaining 3 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
[!] (commonjs plugin) Error: Could not resolve import 'jslib001' from '/private/var/tmp/ian/17079101
d889bedf61702aa4af25ca27/sandbox/darwin-sandbox/74/execroot/bazel_example/bazel-out/darwin-fastbuild/bin/apps/jsapp003/bundle.es6/apps/jsapp003/index.js'
Error: Could not resolve import 'jslib001' from '/private/var/tmp/_bazel_ian/17079101d889bedf61702aa4af25ca27/sandbox/darwin-sandbox/74/execroot/bazel_example/bazel-out/darwin-fastbuild/bin/apps/jsapp003/bundle.es6/apps/jsapp003/index.js'
    at Object.notResolved (/private/var/tmp/_bazel_ian/17079101d889bedf61702aa4af25ca27/execroot/bazel_example/bazel-out/darwin-fastbuild/bin/apps/jsapp003/_bundle.rollup.conf.js:148:9)
    at /private/var/tmp/_bazel_ian/17079101d889bedf61702aa4af25ca27/sandbox/darwin-sandbox/74/execroot/bazel_example/bazel-out/host/bin/external/build_bazel_rules_nodejs/internal/rollup/rollup.sh.runfiles/build_bazel_rules_nodejs_rollup_deps/node_modules/rollup-plugin-commonjs/src/utils.js:20:68

INFO: Elapsed time: 10.651s, Critical Path: 7.74s
INFO: 4 processes: 4 local.
FAILED: Build did NOT complete successfully

0 个答案:

没有答案