Skip to content

Commit

Permalink
feat(stylus): add initial stylus rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Jul 24, 2019
1 parent 2a0be49 commit 804a788
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 2 deletions.
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ local_repository(
path = "packages/typescript/src",
)

local_repository(
name = "npm_bazel_stylus",
path = "packages/stylus/src",
)

#
# Install rules_nodejs dev dependencies
#
Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'karma',
'labs',
'protractor',
'stylus',
'rollup',
'typescript',
]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"shelljs": "0.8.3",
"sinon": "^7.3.2",
"source-map-support": "0.5.9",
"stylus": "~0.54.5",
"terser": "3.17.0",
"tmp": "0.1.0",
"tsickle": "0.33.1",
Expand Down
50 changes: 50 additions & 0 deletions packages/stylus/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")

# Copy the license from our parent folder
genrule(
name = "copy_LICENSE",
srcs = ["@build_bazel_rules_nodejs//:LICENSE"],
outs = ["LICENSE"],
cmd = "cp $< $@",
)

# Ugly genrule depending on local linux environment to build the README out of skylark doc generation.
# Only referenced when we do a release.
# TODO: This ought to be possible with stardoc alone. Need to coordinate with Chris Parsons.
genrule(
name = "generate_README",
srcs = [
"//packages/stylus/docs:index.md",
"//packages/stylus/docs:install.md",
],
outs = ["README.md"],
cmd = """cat $(location //packages/stylus/docs:install.md) $(location //packages/stylus/docs:index.md) | sed 's/^##/\\\n##/' > $@""",
)

npm_package(
name = "npm_package",
srcs = [
"@npm_bazel_stylus//:package_contents",
],
vendor_external = [
"npm_bazel_stylus",
],
deps = [
":copy_LICENSE",
":generate_README",
],
)
37 changes: 37 additions & 0 deletions packages/stylus/docs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@io_bazel_skydoc//stardoc:stardoc.bzl", "stardoc")

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

exports_files(["install.md"])

stardoc(
name = "docs",
testonly = True,
out = "index.md",
input = "@npm_bazel_stylus//:index.bzl",
deps = [
"@npm_bazel_stylus//:bzl",
# We need to restate local workspace dependencies here in `//foo:bzl`
# format to work-around a bug in stardoc where .bzl files from
# `@build_bazel_rules_nodejs//foo:bzl` style deps are not found
# by the doc generator:
# ```
# ```
"//:bzl",
"//internal/common:bzl",
],
)
16 changes: 16 additions & 0 deletions packages/stylus/docs/install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Stylus rules for Bazel

**WARNING: this is beta-quality software. Breaking changes are likely. Not recommended for production use without expert support.**

The Stylus rules run the Stylus CSS preprocessor with Bazel.

## Installation

Add the `@bazel/stylus` npm package to your `devDependencies` in `package.json`.

Your `WORKSPACE` should declare a `yarn_install` or `npm_install` rule named `npm`.
It should then install the rules found in the npm packages using the `install_bazel_dependencies' function.
See https://github.com/bazelbuild/rules_nodejs/#quickstart

This causes the `@bazel/stylus` package to be installed as a Bazel workspace named `npm_bazel_stylus`.

36 changes: 36 additions & 0 deletions packages/stylus/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@npm_bazel_typescript//:defaults.bzl", "ts_library")

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

bzl_library(
name = "bzl",
testonly = True,
srcs = glob(["*.bzl"]),
deps = [
"@build_bazel_rules_nodejs//:bzl",
"@build_bazel_rules_nodejs//internal/common:bzl",
],
)

filegroup(
name = "package_contents",
srcs = [
"index.bzl",
"package.json",
],
)
15 changes: 15 additions & 0 deletions packages/stylus/src/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(name = "npm_bazel_stylus")
37 changes: 37 additions & 0 deletions packages/stylus/src/index.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Support running the stylus processor as a Bazel rule.
"""

def _stylus_binary(ctx):
src = ctx.file.src

# We want foo.styl to produce foo.css
output = ctx.actions.declare_file(src.basename[:-5] + ".css")
ctx.actions.run(
outputs = [output],
inputs = [src],
executable = ctx.executable._compiler,
arguments = [
"--out",
ctx.bin_dir.path + "/" + ctx.label.package,
src.path,
],
)
return [
DefaultInfo(files = depset([output])),
]

stylus_binary = rule(
implementation = _stylus_binary,
attrs = {
"src": attr.label(
mandatory = True,
allow_single_file = True,
),
"_compiler": attr.label(
default = Label("@npm//stylus/bin:stylus"),
cfg = "host",
executable = True,
),
},
)
29 changes: 29 additions & 0 deletions packages/stylus/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@bazel/stylus",
"dependencies": {
"stylus": "~0.54.5"
},
"description": "Run Stylus CSS preprocessor under Bazel",
"license": "Apache-2.0",
"version": "0.0.0-PLACEHOLDER",
"repository": {
"type" : "git",
"url" : "https://github.com/bazelbuild/rules_nodejs.git",
"directory": "packages/stylus"
},
"bugs": {
"url": "https://github.com/bazelbuild/rules_nodejs/issues"
},
"keywords": [
"stylus",
"bazel"
],
"bazelWorkspaces": {
"npm_bazel_stylus": {
"rootPath": "."
}
},
"scripts": {
"test": "bazel test //..."
}
}
17 changes: 17 additions & 0 deletions packages/stylus/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@npm_bazel_stylus//:index.bzl", "stylus_binary")

stylus_binary(
name = "styles",
src = "file.styl",
)

load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_test")

nodejs_test(
name = "test",
data = [
"stylus_binary_spec.js",
":styles",
],
entry_point = ":stylus_binary_spec.js",
)
6 changes: 6 additions & 0 deletions packages/stylus/test/file.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
font: 14px/1.5 Helvetica, arial, sans-serif;
#logo {
border-radius: 5px;
}
}
7 changes: 7 additions & 0 deletions packages/stylus/test/stylus_binary_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log(process.cwd());
const cssPath = require.resolve('build_bazel_rules_nodejs/packages/stylus/test/file.css');
const content = require('fs').readFileSync(cssPath, {encoding: 'utf-8'});
if (content.indexOf('body #logo') < 0) {
console.error('Expected the css file to be transformed');
process.exitCode = 1;
}
Loading

0 comments on commit 804a788

Please sign in to comment.