Skip to content

Commit

Permalink
Fixes and get all tests passing (#480)
Browse files Browse the repository at this point in the history
Also updates rollup_bundle entry_point and parcel example entry_point to labels
  • Loading branch information
gregmagolan committed Jun 5, 2019
1 parent 289a6dd commit 3346bc6
Show file tree
Hide file tree
Showing 38 changed files with 210 additions and 138 deletions.
10 changes: 0 additions & 10 deletions e2e/jasmine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
jasmine_node_test(
name = "test",
srcs = ["test.spec.js"],
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
],
)

jasmine_node_test(
name = "shared_env_test",
srcs = ["jasmine_shared_env_test.spec.js"],
bootstrap = ["e2e_jasmine/jasmine_shared_env_bootstrap.js"],
data = ["jasmine_shared_env_bootstrap.js"],
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
"@npm//jasmine",
"@npm//jasmine-core",
"@npm//zone.js",
Expand All @@ -30,8 +24,4 @@ jasmine_node_test(
"coverage_source.js",
],
coverage = True,
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
],
)
2 changes: 1 addition & 1 deletion e2e/ts_library/some_module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ nodejs_binary(
":main",
":some_module",
],
entry_point = ":main.js",
entry_point = ":main.ts",
)

sh_test(
Expand Down
23 changes: 3 additions & 20 deletions e2e/typescript_3.1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary", "rollup_bundle")
load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

Expand Down Expand Up @@ -57,34 +57,17 @@ jasmine_node_test(
],
)

nodejs_binary(
name = "main_js",
data = [
":main",
],
entry_point = "e2e_typescript_3_1/main.js",
)

nodejs_binary(
name = "main_js_sm",
data = [
":main",
"@npm//source-map-support",
],
entry_point = "e2e_typescript_3_1/main.js",
)

rollup_bundle(
name = "bundle",
entry_point = "main",
entry_point = ":main.ts",
deps = [
":main",
],
)

rollup_bundle(
name = "bundle_sm",
entry_point = "main",
entry_point = ":main.ts",
deps = [
":main",
"@npm//source-map-support",
Expand Down
2 changes: 1 addition & 1 deletion examples/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ts_devserver(

rollup_bundle(
name = "bundle",
entry_point = "examples_app/app",
entry_point = ":app.ts",
deps = [":app"],
)

Expand Down
3 changes: 1 addition & 2 deletions examples/parcel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ parcel(
name = "bundle",
srcs = [
"bar.js",
"foo.js",
],
entry_point = "foo.js",
entry_point = ":foo.js",
)

jasmine_node_test(
Expand Down
9 changes: 6 additions & 3 deletions examples/parcel/parcel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def _parcel_impl(ctx):
"""

# Options documented at https://parceljs.org/cli.html
args = ["build", ctx.attr.entry_point]
args = ["build", ctx.file.entry_point.short_path]
args += ["--out-dir", ctx.outputs.bundle.dirname]
args += ["--out-file", ctx.outputs.bundle.basename]

ctx.actions.run(
inputs = ctx.files.srcs,
inputs = ctx.files.srcs + [ctx.file.entry_point],
executable = ctx.executable.parcel,
outputs = [ctx.outputs.bundle, ctx.outputs.sourcemap],
arguments = args,
Expand All @@ -44,7 +44,10 @@ parcel = rule(
implementation = _parcel_impl,
attrs = {
"srcs": attr.label_list(allow_files = True),
"entry_point": attr.string(mandatory = True),
"entry_point": attr.label(
allow_single_file = True,
mandatory = True,
),
"parcel": attr.label(
# This default assumes that users name their install "npm"
default = Label("@npm//parcel-bundler/bin:parcel"),
Expand Down
2 changes: 1 addition & 1 deletion examples/protocol_buffers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ts_devserver(
# Test for production mode
rollup_bundle(
name = "bundle",
entry_point = "examples_protocol_buffers/app",
entry_point = ":app.ts",
# TODO(alexeagle): we should be able to get this from //:protobufjs_bootstrap_scripts
# and automatically plumb it through to Rollup.
globals = {
Expand Down
2 changes: 1 addition & 1 deletion examples/webapp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_pac
rollup_bundle(
name = "bundle",
srcs = glob(["*.js"]),
entry_point = "index.js",
entry_point = ":index.js",
)

web_package(
Expand Down
5 changes: 5 additions & 0 deletions internal/common/collect_es6_sources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def collect_es6_sources(ctx):
non_rerooted_files = [d for d in ctx.files.deps if d.is_source]
if hasattr(ctx.attr, "srcs"):
non_rerooted_files += ctx.files.srcs

# Some rules such as rollup_bundle specify an entry_point which should
# be collected if the file is a js file.
if hasattr(ctx.attr, "entry_point"):
non_rerooted_files += [s for s in ctx.files.entry_point if s.extension == "js"]
for dep in ctx.attr.deps:
if hasattr(dep, "typescript"):
non_rerooted_files += dep.typescript.transitive_es6_sources.to_list()
Expand Down
7 changes: 2 additions & 5 deletions internal/e2e/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ load("//:defs.bzl", "rollup_bundle")

rollup_bundle(
name = "bundle",
srcs = [
"bar.js",
"foo.js",
],
entry_point = "internal/e2e/rollup/foo.js",
srcs = ["bar.js"],
entry_point = ":foo.js",
globals = {"some_global_var": "runtime_name_of_global_var"},
license_banner = ":license.txt",
deps = [
Expand Down
7 changes: 4 additions & 3 deletions internal/e2e/rollup_code_splitting/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ rollup_bundle(
["*.js"],
exclude = ["*.spec.js"],
),
additional_entry_points = ["internal/e2e/rollup_code_splitting/additional_entry.js"],
entry_point = "internal/e2e/rollup_code_splitting/main1.js",
additional_entry_points = ["build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/additional_entry"],
entry_point = ":main1.js",
license_banner = ":license.txt",
)

Expand All @@ -18,10 +18,11 @@ jasmine_node_test(
"main1.spec.js",
],
data = glob([
"*_golden.js_",
"goldens/*",
]) + [
":bundle",
":bundle.min.js",
":bundle.min.es2015.js",
],
deps = [
"//internal/e2e:check_lib",
Expand Down
3 changes: 2 additions & 1 deletion internal/e2e/rollup_code_splitting/additional_entry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const path = __dirname;

describe('bundling additional entry point', () => {
it('should work', () => {
check(path, 'bundle.min.js', 'bundle-min_golden.js_');
check(path, 'bundle.min.js', 'goldens/bundle.min.js_');
check(path, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
});

// Disabled because native ESModules can't be loaded in current nodejs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('./bundle_chunks_min_es2015/main1.js');
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(function(global) {
System.config({
packages: {
'': {map: {"./main1.js": "bundle_chunks_min/main1.js", "./additional_entry.js": "bundle_chunks_min/additional_entry.js"}, defaultExtension: 'js'},
'': {map: {"./main1": "bundle_chunks_min/main1", "./additional_entry": "bundle_chunks_min/additional_entry"}, defaultExtension: 'js'},
}
});
System.import('main1.js').catch(function(err) {
Expand Down
12 changes: 4 additions & 8 deletions internal/e2e/rollup_fine_grained_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ load("//:defs.bzl", "jasmine_node_test", "nodejs_binary", "rollup_bundle")
# and no fine grained deps
rollup_bundle(
name = "bundle_no_deps",
srcs = ["no-deps.js"],
entry_point = "internal/e2e/rollup_fine_grained_deps/no-deps.js",
entry_point = ":no-deps.js",
)

# You can have a rollup_bundle with no node_modules attribute
# and fine grained deps
rollup_bundle(
name = "bundle",
srcs = ["has-deps.js"],
entry_point = "internal/e2e/rollup_fine_grained_deps/has-deps.js",
entry_point = ":has-deps.js",
deps = [
"@fine_grained_deps_yarn//@gregmagolan/test-a",
"@fine_grained_deps_yarn//@gregmagolan/test-b",
Expand All @@ -24,17 +22,15 @@ rollup_bundle(
# and no fine grained deps
rollup_bundle(
name = "bundle_legacy",
srcs = ["has-deps.js"],
entry_point = "internal/e2e/rollup_fine_grained_deps/has-deps.js",
entry_point = ":has-deps.js",
node_modules = "@fine_grained_deps_yarn//:node_modules",
)

# You can have a rollup_bundle with both a node_modules attribute
# and fine grained deps so long as they come from the same root
rollup_bundle(
name = "bundle_hybrid",
srcs = ["has-deps.js"],
entry_point = "internal/e2e/rollup_fine_grained_deps/has-deps.js",
entry_point = ":has-deps.js",
node_modules = "@fine_grained_deps_yarn//:node_modules",
deps = [
"@fine_grained_deps_yarn//@gregmagolan/test-a",
Expand Down
61 changes: 50 additions & 11 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def _write_loader_script(ctx):
if len(ctx.attr.entry_point.files) != 1:
fail("labels in entry_point must contain exactly one file")

entry_point_path = expand_path_into_runfiles(ctx, ctx.file.entry_point.short_path)

# If the entry point specified is a typescript file then set the entry
# point to the corresponding .js file
if entry_point_path.endswith(".ts"):
entry_point_path = entry_point_path[:-3] + ".js"

ctx.actions.expand_template(
template = ctx.file._loader_template,
output = ctx.outputs.loader,
Expand All @@ -97,7 +104,7 @@ def _write_loader_script(ctx):
"TEMPLATED_bootstrap": "\n " + ",\n ".join(
["\"" + d + "\"" for d in ctx.attr.bootstrap],
),
"TEMPLATED_entry_point": expand_path_into_runfiles(ctx, ctx.file.entry_point.short_path),
"TEMPLATED_entry_point": entry_point_path,
"TEMPLATED_gen_dir": ctx.genfiles_dir.path,
"TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(),
"TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings),
Expand Down Expand Up @@ -172,7 +179,11 @@ def _nodejs_binary_impl(ctx):
is_executable = True,
)

runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args, ctx.file.entry_point], transitive = [sources, node_modules])
runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args], transitive = [sources, node_modules])

# entry point is only needed in runfiles if it is a .js file
if ctx.file.entry_point.extension == "js":
runfiles = depset([ctx.file.entry_point], transitive = [runfiles])

return [DefaultInfo(
executable = ctx.outputs.script,
Expand Down Expand Up @@ -215,32 +226,60 @@ _NODEJS_EXECUTABLE_ATTRS = {
"entry_point": attr.label(
doc = """The script which should be executed first, usually containing a main function.
The `entry_point` accepts a target's name as an entry point.
If the target is a rule, it should produce the JavaScript entry file that will be passed to the nodejs_binary rule).
If the entry JavaScript file belongs to the same package (as the BUILD file),
you can simply reference it by its relative name to the package directory:
```
nodejs_binary(
name = "my_binary",
...
entry_point = ":file.js",
)
```
You can specify the entry point as a typescript file so long as you also include
the ts_library target in data:
```
ts_library(
name = "main",
srcs = ["main.ts"],
)
nodejs_binary(
name = "bin",
data = [":main"]
entry_point = ":main.ts",
)
```
The rule will use the corresponding es5 `.js` output of the ts_library rule as the entry point.
If the entry point target is a rule, it should produce the JavaScript entry file that will be passed to the nodejs_binary rule).
For example:
```
filegroup(
name = "entry_file",
srcs = ["workspace/path/to/entry/file"]
srcs = ["main.js"],
)
nodejs_binary(
name = "my_binary",
...
entry_point = ":entry_file",
)
```
If the entry JavaScript file belongs to the same package (as the BUILD file),
you can simply reference it by its relative name to the package directory:
The entry_point can also be a label in another workspace:
```
nodejs_binary(
name = "my_binary",
...
entry_point = ":file.js",
name = "history-server",
entry_point = "@npm//node_modules/history-server:modules/cli.js",
data = ["@npm//history-server"],
)
```
""",
mandatory = True,
allow_single_file = True,
Expand Down
2 changes: 1 addition & 1 deletion internal/npm_install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nodejs_binary(
":browserify-wrapped.js",
"//third_party/github.com/browserify/browserify:sources",
],
entry_point = "build_bazel_rules_nodejs/internal/npm_install/browserify-wrapped.js",
entry_point = ":browserify-wrapped.js",
install_source_map_support = False,
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 3346bc6

Please sign in to comment.