Skip to content

Commit

Permalink
fix: remove region tags from jsfiddle js and html (#88)
Browse files Browse the repository at this point in the history
Former-commit-id: bc10f16
Former-commit-id: 9233cb3ee4e4c6d84fcda35055640e3f501c609a
  • Loading branch information
jpoehnelt committed Dec 10, 2019
1 parent 7ec1132 commit a1ccfa6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
11 changes: 10 additions & 1 deletion rules/nunjucks.bzl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
load("@npm//nunjucks-cli:index.bzl", npm_nunjucks = "nunjucks")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("//rules:strip_region_tags.bzl", "strip_region_tags")


def nunjucks():
[_nunjucks(jsfiddle) for jsfiddle in [False, True]]

def _nunjucks(jsfiddle):
_html = "_html_jsfiddle" if jsfiddle else "_html"
html = "html_jsfiddle" if jsfiddle else "html_"
out = "jsfiddle.html" if jsfiddle else "index.html"
out = "_jsfiddle.html" if jsfiddle else "index.html"

if jsfiddle:
nodejs_binary(
Expand Down Expand Up @@ -57,3 +59,10 @@ def _nunjucks(jsfiddle):
outs = [out],
visibility = ["//visibility:public"],
)

if jsfiddle:
strip_region_tags(
name="_html_strip_region_tags",
input=out,
output="jsfiddle.html"
)
12 changes: 10 additions & 2 deletions rules/sample.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ load("@npm//@babel/cli:index.bzl", "babel")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
load("//rules:nunjucks.bzl", "nunjucks")
load("//rules:strip_region_tags.bzl", "strip_region_tags")


def sample():
rollup_bundle(
Expand All @@ -15,15 +17,21 @@ def sample():
visibility = ["//visibility:public"],
)

strip_region_tags(
name = "_js_without_region_tags",
input = ":app",
output = "_without_region_tags.js",
)

babel(
name = "transpiled",
args = [
"$(location :app)",
"$(location :_js_without_region_tags)",
"--out-file",
"$@/transpiled.js",
],
data = [
":app",
":_js_without_region_tags",
"@npm//@babel/preset-env",
"//:babel.config.json",
],
Expand Down
31 changes: 31 additions & 0 deletions rules/strip_region_tags.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def _strip_region_tags_impl(ctx):
STRIP_REGION_TAGS_CMD = """
cp $1 $2
sed -i 's/\\/\\/ \\[START[A-Z_]* .*\\]//g' $2
sed -i 's/\\/\\/ \\[END[A-Z_]* .*\\]//g' $2
sed -i 's/<\\!-- \\[START[A-Z_]* .*\\] -->//g' $2
sed -i 's/<\\!-- \\[END[A-Z_]* .*\\] -->//g' $2
"""
in_file = ctx.file.input
out_file = ctx.outputs.output

ctx.actions.run_shell(
outputs = [out_file],
inputs = [in_file],
arguments = [in_file.path, out_file.path],
command = STRIP_REGION_TAGS_CMD,
)

strip_region_tags = rule(
implementation = _strip_region_tags_impl,
attrs = {
"input": attr.label(
allow_single_file = True,
mandatory = True,
doc = "The file to strip region trags from",
),
"output": attr.output(doc = "The output file"),
},
doc = "Transforms a file by removing region tags.",
)

0 comments on commit a1ccfa6

Please sign in to comment.