Skip to content

Commit

Permalink
fix(web_package): normalize root paths consistently with ts_devserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Bedard committed Apr 30, 2019
1 parent f18986b commit 97e1593
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 9 deletions.
31 changes: 31 additions & 0 deletions internal/web_package/test-exports/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package(default_visibility = ["//visibility:public"])

EXTS = [
"js",
"css",
]

[
genrule(
name = "bin-" + e,
outs = ["bin." + e],
cmd = "echo '' > $@",
output_to_bindir = True,
)
for e in EXTS
]

[
genrule(
name = "gen-" + e,
outs = ["gen." + e],
cmd = "echo '' > $@",
output_to_bindir = False,
)
for e in EXTS
]

exports_files([
"file.css",
"file.js",
])
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions internal/web_package/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test", "rollup_bundle")
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")

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

rollup_bundle(
name = "bundle",
srcs = glob(["*.js"]),
Expand Down
2 changes: 1 addition & 1 deletion internal/web_package/test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ describe('web_package', () => {
const actual = fs.readFileSync(require.resolve(output), {encoding: 'utf-8'});
const expected = fs.readFileSync(require.resolve(golden), {encoding: 'utf-8'});
// make the input hermetic by replacing the cache-buster timestamp
expect(actual.replace(/\?v=\d+/, '?v=123')).toBe(expected);
expect(actual.replace(/\?v=\d+/g, '?v=123')).toBe(expected);
});
});
46 changes: 46 additions & 0 deletions internal/web_package/test2/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test", "rollup_bundle")
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")

rollup_bundle(
name = "local_bundle",
srcs = glob(["*.js"]),
entry_point = "internal/web_package/test2/script.js",
)

# Same exts as //internal/web_package/test-exports, //internal/web_package/test2/rel-exports
EXTS = [
"js",
"css",
]

web_package(
name = "pkg",
assets = [
# a bundle from a different directory
"//internal/web_package/test:bundle",
# a bundle in the current path
":local_bundle",
] +
# bin + gen + exported files from a different directory
["//internal/web_package/test-exports:bin-" + e for e in EXTS] +
["//internal/web_package/test-exports:gen-" + e for e in EXTS] +
["//internal/web_package/test-exports:file." + e for e in EXTS] +
# bin + gen + exported files from a sub directory
["//internal/web_package/test2/rel-exports:rel-bin-" + e for e in EXTS] +
["//internal/web_package/test2/rel-exports:rel-gen-" + e for e in EXTS] +
["//internal/web_package/test2/rel-exports:rel-file." + e for e in EXTS],
index_html = "index.html",
)

jasmine_node_test(
name = "test",
srcs = ["spec.js"],
data = [
"index_golden.html_",
":pkg",
],
node_modules = "@build_bazel_rules_nodejs_web_package_deps//:node_modules",
tags = [
"fix-windows",
],
)
1 change: 1 addition & 0 deletions internal/web_package/test2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html></html>
2 changes: 2 additions & 0 deletions internal/web_package/test2/index_golden.html_
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<html><head><link rel="stylesheet" href="/internal/web_package/test-exports/bin.css?v=123"><link rel="stylesheet" href="/internal/web_package/test-exports/gen.css?v=123"><link rel="stylesheet" href="/internal/web_package/test-exports/file.css?v=123"><link rel="stylesheet" href="/rel-exports/rel-bin.css?v=123"><link rel="stylesheet" href="/rel-exports/rel-gen.css?v=123"><link rel="stylesheet" href="/rel-exports/rel-file.css?v=123"></head><body>
<script src="/internal/web_package/test/bundle.min.js?v=123"></script><script src="/local_bundle.min.js?v=123"></script><script src="/internal/web_package/test-exports/bin.js?v=123"></script><script src="/internal/web_package/test-exports/gen.js?v=123"></script><script src="/internal/web_package/test-exports/file.js?v=123"></script><script src="/rel-exports/rel-bin.js?v=123"></script><script src="/rel-exports/rel-gen.js?v=123"></script><script src="/rel-exports/rel-file.js?v=123"></script></body></html>
31 changes: 31 additions & 0 deletions internal/web_package/test2/rel-exports/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package(default_visibility = ["//visibility:public"])

EXTS = [
"js",
"css",
]

[
genrule(
name = "rel-bin-" + e,
outs = ["rel-bin." + e],
cmd = "echo '' > $@",
output_to_bindir = True,
)
for e in EXTS
]

[
genrule(
name = "rel-gen-" + e,
outs = ["rel-gen." + e],
cmd = "echo '' > $@",
output_to_bindir = False,
)
for e in EXTS
]

exports_files([
"rel-file.css",
"rel-file.js",
])
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions internal/web_package/test2/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const el = document.createElement('div');
el.innerText = 'Hello, World';
document.body.appendChild(el);
15 changes: 15 additions & 0 deletions internal/web_package/test2/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs');
const path = require('path');

process.chdir(path.join(process.env['TEST_SRCDIR'], 'build_bazel_rules_nodejs'));
console.error(fs.readdirSync('.'));
describe('web_package paths', () => {
it('should match the golden file', () => {
const output = 'build_bazel_rules_nodejs/internal/web_package/test2/pkg/index.html';
const golden = 'build_bazel_rules_nodejs/internal/web_package/test2/index_golden.html_';
const actual = fs.readFileSync(require.resolve(output), {encoding: 'utf-8'});
const expected = fs.readFileSync(require.resolve(golden), {encoding: 'utf-8'});
// make the input hermetic by replacing the cache-buster timestamp
expect(actual.replace(/\?v=\d+/g, '?v=123')).toBe(expected);
});
});
16 changes: 13 additions & 3 deletions internal/web_package/web_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,28 @@ def move_files(output_name, files, action_factory, assembler, root_paths):
)
return depset([www_dir])

def _web_package(ctx):
root_paths = ctx.attr.additional_root_paths + [
def additional_root_paths(ctx):
return ctx.attr.additional_root_paths + [
# package path is the root, including in bin/gen
ctx.label.package,
"/".join([ctx.bin_dir.path, ctx.label.package]),
"/".join([ctx.genfiles_dir.path, ctx.label.package]),

# bazel-bin/gen dirs to absolute paths
ctx.genfiles_dir.path,
ctx.bin_dir.path,

# package re-rooted subdirectory
"/".join([p for p in [ctx.bin_dir.path, ctx.label.package, "_" + ctx.label.name, ctx.label.package] if p]),
]

def _web_package(ctx):
root_paths = additional_root_paths(ctx)

# Create the output file in a re-rooted subdirectory so it doesn't collide with the input file
html = ctx.actions.declare_file("_%s/%s" % (ctx.label.name, ctx.file.index_html.path))

# Move that index file back into place inside the package
root_paths.append("/".join([p for p in [ctx.bin_dir.path, ctx.label.package, "_" + ctx.label.name, ctx.label.package] if p]))
populated_index = html_asset_inject(
ctx.file.index_html,
ctx.actions,
Expand Down
7 changes: 2 additions & 5 deletions packages/typescript/internal/devserver/ts_devserver.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ load(
)
load(
"@build_bazel_rules_nodejs//internal/web_package:web_package.bzl",
"additional_root_paths",
"html_asset_inject",
)

Expand Down Expand Up @@ -99,11 +100,7 @@ def _ts_devserver(ctx):
ctx.file.index_html,
ctx.actions,
ctx.executable._injector,
ctx.attr.additional_root_paths + [
ctx.label.package,
"/".join([ctx.bin_dir.path, ctx.label.package]),
"/".join([ctx.genfiles_dir.path, ctx.label.package]),
],
additional_root_paths(ctx),
[_short_path_to_manifest_path(ctx, f.short_path) for f in ctx.files.static_files] + [bundle_script],
injected_index,
)
Expand Down

0 comments on commit 97e1593

Please sign in to comment.