Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Experiment with outputs DO NOT MERGE
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhw committed Dec 12, 2019
1 parent 9e02be1 commit 838cab3
Show file tree
Hide file tree
Showing 11 changed files with 2,204 additions and 18 deletions.
14 changes: 14 additions & 0 deletions examples/postcss-sprites/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("//internal:binary.bzl", "postcss_binary")

postcss_binary(
name = "style_processed",
src = "style.css",
plugins = {
"postcss-sprites": "[{spritePath: './bazel-out/darwin-fastbuild/bin/examples/postcss-sprites', verbose: true}]",
},
data = ["green.png", "red.png"],
additional_outputs = ["sprite.png"],
deps = [
"@npm//postcss-sprites",
],
)
Binary file added examples/postcss-sprites/green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/postcss-sprites/red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions examples/postcss-sprites/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.red { background: url(red.png) no-repeat 0 0; }
.green { background: url(green.png) no-repeat 0 0; }
11 changes: 10 additions & 1 deletion internal/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def postcss_binary(
plugins,
deps,
src,
data = [],
additional_outputs = [],
output_name = "",
map_annotation = False,
visibility = None):
Expand All @@ -39,6 +41,8 @@ def postcss_binary(
for each respective plugin.
deps: A list of NodeJS modules the config depends on. The PostCSS module
is always implicitly included.
data: Foo
additional_outputs: Foo
src: The input .css, and optionally .css.map files. (This includes
outputs from preprocessors such as sass_binary.)
output_name: Output name.
Expand All @@ -54,14 +58,19 @@ def postcss_binary(
name = runner_name,
plugins = plugins,
deps = deps,
data = data,
map_annotation = map_annotation,
visibility = ["//visibility:private"],
)

output_name = output_name or "%s.css" % name
postcss_run(
name = name,
src = src,
output_name = output_name,
data = data,
output_css = output_name,
output_css_map = "%s.map" % output_name,
additional_outputs = additional_outputs,
runner = runner_name,
visibility = visibility,
)
8 changes: 8 additions & 0 deletions internal/gen_runner.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ postcss_runner_src = rule(
allow_single_file = [".js"],
mandatory = True,
),
"data": attr.label_list(
allow_files = True,
mandatory = False,
),
},
outputs = {
"postcss_runner_src": "%{name}.js",
Expand All @@ -65,6 +69,7 @@ def postcss_gen_runner(
plugins,
deps,
map_annotation,
data = [],
visibility = None):
"""Generates a PostCSS runner binary.
Expand All @@ -80,6 +85,7 @@ def postcss_gen_runner(
map_annotation: Whether to add (or modify, if already existing) the
sourceMappingURL comment in the output .css to point to the output
.css.map.
data: Foo
visibility: The visibility of the build rule.
"""

Expand All @@ -89,6 +95,7 @@ def postcss_gen_runner(
name = runner_src_name,
plugins = plugins,
map_annotation = map_annotation,
data = data,
template = "//internal:runner-template.js",
visibility = ["//visibility:private"],
)
Expand All @@ -100,5 +107,6 @@ def postcss_gen_runner(
"@npm//minimist",
"@npm//postcss",
] + deps,
data = data,
visibility = visibility,
)
27 changes: 14 additions & 13 deletions internal/run.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,45 @@ def _postcss_run_impl(ctx):
# Generate the command line.
args = [
"--cssFile=%s" % css_file.path,
"--outCssFile=%s" % ctx.outputs.css_file.path,
"--outCssMapFile=%s" % ctx.outputs.css_map_file.path,
"--outCssFile=%s" % ctx.outputs.output_css.path,
"--outCssMapFile=%s" % ctx.outputs.output_css_map.path,
]
if css_map_file:
args.append("--cssMapFile=%s" % css_map_file.path)

for out in ctx.outputs.additional_outputs:
print(out.path)

# The command may only access files declared in inputs.
inputs = [css_file] + ([css_map_file] if css_map_file else [])
inputs = [css_file] + ([css_map_file] if css_map_file else []) + (ctx.files.data if ctx.files.data else [])

ctx.actions.run(
outputs = [ctx.outputs.css_file, ctx.outputs.css_map_file],
outputs = [ctx.outputs.output_css, ctx.outputs.output_css_map] + ctx.outputs.additional_outputs,
inputs = inputs,
executable = ctx.executable.runner,
arguments = args,
progress_message = "Running PostCSS runner on %s" % ctx.attr.src.label,
)

def _postcss_run_outputs(output_name):
output_name = output_name or "%{name}.css"
return {
"css_file": output_name,
"css_map_file": "%s.map" % output_name,
}

postcss_run = rule(
implementation = _postcss_run_impl,
attrs = {
"src": attr.label(
allow_files = [".css", ".css.map"],
mandatory = True,
),
"output_name": attr.string(default = ""),
"data": attr.label_list(
allow_files = True,
mandatory = False,
),
"output_css": attr.output(mandatory = True),
"output_css_map": attr.output(mandatory = True),
"additional_outputs": attr.output_list(),
"runner": attr.label(
executable = True,
cfg = "host",
allow_files = True,
mandatory = True,
),
},
outputs = _postcss_run_outputs,
)
8 changes: 8 additions & 0 deletions internal/runner-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ const options = {
}
};

const testFolder = './';
fs.readdirSync(testFolder).forEach(file => {
console.log('File: '+file);
});

postcss(TEMPLATED_plugins)
.process(cssString, options)
.then(
result => {
console.log('cwd', process.cwd());
console.log('will write to', args.outCssFile);
console.log('which is the same as', require('path').resolve(args.outCssFile));
fs.writeFileSync(args.outCssFile, result.css);
if (result.map) {
fs.writeFileSync(args.outCssMapFile, result.map);
Expand Down
4 changes: 3 additions & 1 deletion internal/runner_bin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ def postcss_runner_bin(
name,
src,
deps,
data = [],
visibility = None):
"""Convenience helper for using nodejs_binary with the PostCSS runner.
Args:
name: The name of the build rule.
src: The source file and entry point of the nodejs_binary.
deps: What the nodejs_binary depends on.
data: Foo
visibility: The visibility of the build rule.
"""

nodejs_binary(
name = name,
entry_point = ":%s" % (src),
data = deps,
data = deps + data,
visibility = visibility,
)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"autoprefixer": "9.4.3",
"minimist": "1.2.0",
"postcss": "7.0.7",
"postcss-sprites": "^4.2.1",
"rtlcss": "^2.4.0",
"typescript": "^3.7.2"
},
Expand Down
Loading

0 comments on commit 838cab3

Please sign in to comment.