Skip to content

Commit

Permalink
fix: don't expose an npm dependency from builtin
Browse files Browse the repository at this point in the history
Add an example test for generated_file_test
  • Loading branch information
Alex Eagle authored and alexeagle committed Jun 8, 2020
1 parent 3fbf2c0 commit 7b2b4cf
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 88 deletions.
3 changes: 0 additions & 3 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ example_integration_test(

example_integration_test(
name = "examples_parcel",
npm_packages = {
"//packages/jasmine:npm_package": "@bazel/jasmine",
},
# Parcel spawns a subprocess which requires our node-patches
# but we don't yet have a mechanism on Windows for spawned processes
# to inherit the --require script needed to install the patches
Expand Down
8 changes: 4 additions & 4 deletions examples/parcel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load(":parcel.bzl", "parcel")

parcel(
Expand All @@ -9,8 +9,8 @@ parcel(
entry_point = ":foo.js",
)

jasmine_node_test(
generated_file_test(
name = "test",
srcs = glob(["*.spec.js"]),
deps = [":bundle"],
src = "bundle.golden.js_",
generated = ":bundle.js",
)
6 changes: 6 additions & 0 deletions examples/parcel/bundle.golden.js_
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(parcelRequire=f,i)throw i;return f}({"MrOh":[function(require,module,exports) {
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.name=void 0;var e="Bob";exports.name=e;
},{}],"wwEC":[function(require,module,exports) {
"use strict";var e=require("./bar");console.log("Hello, ".concat(e.name));
},{"./bar":"MrOh"}]},{},["wwEC"], null)
//# sourceMappingURL=/bundle.js.map
62 changes: 0 additions & 62 deletions examples/parcel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/parcel/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"private": true,
"devDependencies": {
"@bazel/jasmine": "^1.6.0",
"jasmine": "3.4.0",
"parcel-bundler": "1.12.3",
"v8-coverage": "1.0.9"
},
Expand Down
11 changes: 0 additions & 11 deletions examples/parcel/parcel.spec.js

This file was deleted.

26 changes: 24 additions & 2 deletions internal/generated_file_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//packages/rollup:index.bzl", "rollup_bundle")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/write_file.bzl", "write_file")

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

Expand All @@ -8,9 +10,29 @@ bzl_library(
visibility = ["//visibility:public"],
)

exports_files(["bin.js"])
write_file(
name = "write_BUILD",
out = "BUILD",
content = ["""exports_files(["bundle.js"])"""],
)

filegroup(
name = "package_contents",
srcs = glob(["*"]),
srcs = [
"BUILD",
"bundle.js",
"generated_file_test.bzl",
],
)

rollup_bundle(
name = "bundle",
config_file = "rollup.config.js",
entry_point = "bin.js",
sourcemap = "hidden",
deps = [
"@npm//rollup-plugin-commonjs",
"@npm//rollup-plugin-node-resolve",
"@npm//unidiff",
],
)
3 changes: 2 additions & 1 deletion internal/generated_file_test/bin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs');
const path = require('path');
// We run rollup so this import will be vendored into the resulting bundle
import * as unidiff from 'unidiff/unidiff';
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);

function main(args) {
Expand All @@ -21,7 +23,6 @@ function main(args) {
return 0;
}
if (mode === '--verify') {
const unidiff = require('unidiff');
// Generated does not match golden
const diff = unidiff.diffLines(goldenContents, actualContents);
let prettyDiff =
Expand Down
6 changes: 3 additions & 3 deletions internal/generated_file_test/generated_file_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def generated_file_test(name, generated, src, src_dbg = None, **kwargs):
In this case you can specify what the dbg version of the output should look like
**kwargs: extra arguments passed to the underlying nodejs_test or nodejs_binary
"""
data = [src, generated, "@npm//unidiff"]
data = [src, generated]

if src_dbg:
data.append(src_dbg)
Expand All @@ -26,7 +26,7 @@ def generated_file_test(name, generated, src, src_dbg = None, **kwargs):
loc = "$(rootpath %s)"
nodejs_test(
name = name,
entry_point = "@build_bazel_rules_nodejs//internal/generated_file_test:bin.js",
entry_point = "@build_bazel_rules_nodejs//internal/generated_file_test:bundle.js",
templated_args = ["--verify", loc % src, loc % src_dbg, loc % generated],
data = data,
**kwargs
Expand All @@ -35,7 +35,7 @@ def generated_file_test(name, generated, src, src_dbg = None, **kwargs):
nodejs_binary(
name = name + ".update",
testonly = True,
entry_point = "@build_bazel_rules_nodejs//internal/generated_file_test:bin.js",
entry_point = "@build_bazel_rules_nodejs//internal/generated_file_test:bundle.js",
templated_args = ["--out", loc % src, loc % src_dbg, loc % generated],
data = data,
**kwargs
Expand Down
9 changes: 9 additions & 0 deletions internal/generated_file_test/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const commonjs = require('rollup-plugin-commonjs');
const nodeRequire = require('rollup-plugin-node-resolve');

module.exports = {
plugins: [
nodeRequire(),
commonjs(),
],
};

0 comments on commit 7b2b4cf

Please sign in to comment.