From 1d0c4247d747afae383948aee5066fdee43265f1 Mon Sep 17 00:00:00 2001 From: Richard Wang Date: Wed, 11 Oct 2017 17:47:13 +1100 Subject: [PATCH 1/3] Add BUILD file path to module resolve locations list --- internal/node.bzl | 1 + internal/node_loader.js | 4 ++++ 2 files changed, 5 insertions(+) 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), From 96e20abeb3366d68d7032269fbf7b8edf08bb091 Mon Sep 17 00:00:00 2001 From: Richard Wang Date: Wed, 11 Oct 2017 17:48:44 +1100 Subject: [PATCH 2/3] Add example that uses a local nodejs binary N.B. multiple node_repository declarations don't work, so to run this first cd into examples/local and run yarn --- examples/local/BUILD.bazel | 30 ++++++++++++++++++++++++++++++ examples/local/local.bzl | 37 +++++++++++++++++++++++++++++++++++++ examples/local/local.js | 5 +++++ examples/local/package.json | 5 +++++ examples/local/yarn.lock | 7 +++++++ 5 files changed, 84 insertions(+) create mode 100644 examples/local/BUILD.bazel create mode 100644 examples/local/local.bzl create mode 100644 examples/local/local.js create mode 100644 examples/local/package.json create mode 100644 examples/local/yarn.lock 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/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" From 20ba1e81757ea7f00dbf5517dd02bc28c36c7fdf Mon Sep 17 00:00:00 2001 From: Richard Wang Date: Wed, 7 Feb 2018 12:37:39 +1100 Subject: [PATCH 3/3] dunno what this is but i wrote it a while back --- examples/local/test.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 examples/local/test.sh 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 +)