diff --git a/examples/local/BUILD.bazel b/examples/local/BUILD.bazel new file mode 100644 index 0000000000..c7551687f5 --- /dev/null +++ b/examples/local/BUILD.bazel @@ -0,0 +1,30 @@ +# NOTE: the example uses //:defs.bzl because it lives in the same WORKSPACE +# as the rule declarations. +# Users should use @build_bazel_rules_nodejs//:defs.bzl +load("//:defs.bzl", "nodejs_binary") + +filegroup( + name = "node_modules", + srcs = glob([ + "node_modules/**/*", + ]), +) + +nodejs_binary( + name = "local", + data = [ + "local.js", + ], + entry_point = "local.js", + node_modules = ":node_modules", +) + +load(":local.bzl", "run_local") +run_local( + name = "local_output", +) + +run_local( + name = "local_output2", + hello = "bazel", +) diff --git a/examples/local/local.bzl b/examples/local/local.bzl new file mode 100644 index 0000000000..3413aa72c9 --- /dev/null +++ b/examples/local/local.bzl @@ -0,0 +1,37 @@ +# 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. + +def _local(ctx): + args = ["--hello", ctx.attr.hello] + args += ["--output", ctx.outputs.text.path] + ctx.action( + executable = ctx.executable.binary, + outputs = [ctx.outputs.text], + arguments = args, + ) + return struct() + +run_local = rule( + implementation = _local, + attrs = { + "hello": attr.string(default = "world"), + "binary": attr.label( + default = Label("//examples/local"), + executable = True, + cfg = "host"), + }, + outputs = { + "text": "%{name}.txt" + }, +) diff --git a/examples/local/local.js b/examples/local/local.js new file mode 100644 index 0000000000..aa45351472 --- /dev/null +++ b/examples/local/local.js @@ -0,0 +1,5 @@ +const fs = require('fs'); +const minimist = require('minimist'); + +const args = minimist(process.argv.slice(2)); +fs.writeFileSync(args.output, `Hello, ${args.hello}!`); diff --git a/examples/local/package.json b/examples/local/package.json new file mode 100644 index 0000000000..3df0edb38b --- /dev/null +++ b/examples/local/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "minimist": "1.2.0" + } +} diff --git a/examples/local/test.sh b/examples/local/test.sh new file mode 100644 index 0000000000..42b7eed945 --- /dev/null +++ b/examples/local/test.sh @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +readonly EXPECTED="${TEST_TMPDIR}/EXPECTED.txt" +(cat <<'EOF' +Hello, Node.js! +EOF +) > $EXPECTED + +readonly EXPECTED="${TEST_TMPDIR}/ACTUAL.txt" +${TEST_SCRDIR}/build_bazel_rules_nodejs/examples/local/local.js + +readonly ACTUAL="${TEST_SRCDIR}/build_bazel_rules_nodejs/examples/rollup/bundle.js" +diff $ACTUAL $EXPECTED || ( + echo "Expected" + cat $EXPECTED + echo "But was" + cat $ACTUAL + exit 1 +) diff --git a/examples/local/yarn.lock b/examples/local/yarn.lock new file mode 100644 index 0000000000..505103783b --- /dev/null +++ b/examples/local/yarn.lock @@ -0,0 +1,7 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +minimist@1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" diff --git a/internal/node.bzl b/internal/node.bzl index d10f667045..a91d9bb3b9 100644 --- a/internal/node.bzl +++ b/internal/node.bzl @@ -54,6 +54,7 @@ def _write_loader_script(ctx): "TEMPLATED_entry_point": ctx.attr.entry_point, "TEMPLATED_label_package": ctx.attr.node_modules.label.package, "TEMPLATED_workspace_name": ctx.workspace_name, + "TEMPLATED_build_file_path": ctx.build_file_path, }, executable=True, ) diff --git a/internal/node_loader.js b/internal/node_loader.js index 246a0890bd..a7e4f224a8 100644 --- a/internal/node_loader.js +++ b/internal/node_loader.js @@ -102,6 +102,10 @@ module.constructor._resolveFilename = var resolveLocations = [ request, resolveRunfiles(request), + resolveRunfiles( + 'TEMPLATED_workspace_name', + path.dirname('TEMPLATED_build_file_path'), + request), resolveRunfiles( 'TEMPLATED_workspace_name', 'TEMPLATED_label_package', 'node_modules', request),