Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel: How do i use nodeJS_binary rule to do "npm run start"

How do i use the nodejs_binary rule to do a standard npm run start. I am able to run a typical node project using this rule. However i want to run a the start script in package.json. So far i have the following below in my build file

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"]
)

This does not start the server..somehow npm command is not running properly. it indicates usage of the command in incomplete.

I am currently able to do this within the WORKSPACE

bazel run @nodejs//:bin/yarn (runs yarn install and installs all node-modulse)

bazel run @nodejs//:bin/npm start (this starts the server)

In my package.json i have

{
  "scripts": {
    "start": "babel-node src/server.js",
   ...
  }
...
}

I do i get this to work with nodejs_binary rule and subsequently node_image

I changed from using npm to using yarn..workspace_name/src/server.js.. is called now but Then i had different set of problems, babel-node was not found. I modified the rule a bit. After careful study...I realise that there is a dependency on babel-node that is not satisfied at the time yarn run start is called. The following worked after i had run bazel run @nodejs//:bin/yarn before running the rule.

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",
)

It appears that "@//:node_modules" solves the babel-node dependency issue. So the rule above does not work on its own...it needs me to do bazel run @nodejs//:bin/yarn (more like npm/yarn install to make the node_modules, which contain babel-node dependecy available when npm/yarn start is run)

So my problem is that I do not want to have to manually run bazel run @nodejs//:bin/yarn before executing my rule. how do i do this. I suppose it would work if i stopped depending on babel-node...but then i would have to change my code to not use es6 syntax (that is a hustle). Is there a way i can do this with a genrule? or something...

like image 866
flexxxit Avatar asked Nov 07 '22 02:11

flexxxit


1 Answers

What I ended up doing was that i made a babel nodejs_binary rule. Then used that to compile my source files in a gen rule

# 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"],
)

Note that in this case src in cmd = "$(location babel) src --out-dir $@" is a folder in the :src_files filegroup.

filegroup(
    name = "src_files",
    srcs = glob([
        "src/**/*",
        ...
    ]),
)

After this it was unnecessary to use npm start, just used default node. I could just do

nodejs_binary(
    name = "app",
    data = [":compiled_src"],
    entry_point = "workspace_name/src/server.js",
    node_modules = "@npm_deps//:node_modules",
)
like image 199
flexxxit Avatar answered Nov 11 '22 17:11

flexxxit