Skip to content

Commit

Permalink
Add TypeScript dependencies and copy of example plugin (bazelbuild#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhw authored Nov 12, 2019
1 parent c6d377b commit ace403b
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 11 deletions.
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
# limitations under the License.

exports_files(["LICENSE"])

exports_files(["tsconfig.json"], visibility = ["//visibility:public"])
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ yarn_install(
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()

# Set up TypeScript toolchain
load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
ts_setup_workspace()
62 changes: 62 additions & 0 deletions examples/custom_plugin_ts/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2019 The Bazel Authors
#
# 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
#
# https://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_rules_sass//:defs.bzl", "sass_binary")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//:defs.bzl", "postcss_binary", "postcss_plugin")

package(default_visibility = ["//tests:__subpackages__"])

AUTO_PREFIXER_BROWSERS = "ie >= 9, edge >= 12, firefox >= 42, chrome >= 32, safari >= 8, opera >= 38, ios_saf >= 9.2, android >= 4.3, and_uc >= 9.9"

ts_library(
name = "unquote_ts",
srcs = ["unquote.ts"],
deps = [
"@npm//@types",
"@npm//postcss",
],
)

filegroup(
name = "unquote_js",
srcs = [":unquote_ts"],
output_group = "es5_sources",
)

postcss_plugin(
name = "unquote",
srcs = [
":unquote_js",
],
)

sass_binary(
name = "style",
src = "style.scss",
output_style = "expanded",
)

postcss_binary(
name = "style_processed",
src = ":style",
plugins = {
"autoprefixer": "[{ browsers: '%s' }]" % AUTO_PREFIXER_BROWSERS,
"build_bazel_rules_postcss/examples/custom_plugin_ts/unquote.js": "",
},
deps = [
":unquote",
"@npm//autoprefixer",
],
)
12 changes: 12 additions & 0 deletions examples/custom_plugin_ts/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@function mediaSafe($expr) {
@return unquote('unquote("' + $expr + '")');
}

$sidebar-max-width-toggleable: 42px;

@media screen and (max-width: mediaSafe($sidebar-max-width-toggleable)) {
body {
font: 42px 'Comic Sans MS';
flex: 42 -42 auto;
}
}
47 changes: 47 additions & 0 deletions examples/custom_plugin_ts/unquote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2019 The Bazel Authors
*
* 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
*
* https://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.
*/

/**
* @fileoverview Example PostCSS plugin that introduces an "unquote" function
* to stylesheets.
*/

import * as postcss from 'postcss';

// Replaces all "unquote('hello')" found with "hello", supporting
// both ' and ". Doesn't do quote escaping.
module.exports = postcss.plugin('unquote', () => {
/**
* Unquote implementation. http://stackoverflow.com/a/19584742
* @param str
* @return The string with unquote instances handled.
*/
const unquoteStr = (str: string) => {
if (str.indexOf('unquote(') !== -1) {
return str.replace(/unquote\((["'])(.+?)\1\)/g, '$2');
}
return str;
};

return (css: postcss.Root) => {
// Handle declaration values in rules.
css.walkRules(rule => {
rule.walkDecls((decl, i) => decl.value = unquoteStr(decl.value));
});
// Handle params in @rules.
css.walkAtRules(rule => rule.params = unquoteStr(rule.params));
};
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
"description": "Run PostCSS with Bazel",
"version": "0.0.1",
"dependencies": {
"@types/node": "^12.12.7",
"autoprefixer": "9.4.3",
"minimist": "1.2.0",
"postcss": "7.0.7"
"postcss": "7.0.7",
"typescript": "^3.7.2"
},
"devDependencies": {
"@bazel/typescript": "^0.39.1"
}
}
26 changes: 18 additions & 8 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ load("@bazel_tools//tools/build_rules:test_rules.bzl", "file_test")

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

file_test(
name = "autoprefixer_test",
content = """body {
AUTOPREFIXER_OUTPUT = """body {
font: 42px 'Comic Sans MS';
-webkit-box-flex: 42;
-webkit-flex: 42 -42 auto;
-ms-flex: 42 -42 auto;
flex: 42 -42 auto;
}
""",
"""

file_test(
name = "autoprefixer_test",
content = AUTOPREFIXER_OUTPUT,
file = "//examples/autoprefixer:style_processed.css",
)

file_test(
name = "custom_plugin_test",
content = """@media screen and (max-width: 42px) {
CUSTOM_PLUGIN_OUTPUT = """@media screen and (max-width: 42px) {
body {
font: 42px "Comic Sans MS";
-webkit-box-flex: 42;
Expand All @@ -42,6 +42,16 @@ file_test(
}
/*# sourceMappingURL=style.css.map */
""",
"""

file_test(
name = "custom_plugin_test",
content = CUSTOM_PLUGIN_OUTPUT,
file = "//examples/custom_plugin:style_processed.css",
)

file_test(
name = "custom_plugin_ts_test",
content = CUSTOM_PLUGIN_OUTPUT,
file = "//examples/custom_plugin_ts:style_processed.css",
)
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es5",
"es6",
"es2015.collection",
"es2015.iterable",
"es2015.core",
"dom"
],
"types": [
"node"
],
"target": "es2015",
"strict": true
},
"include": [
"examples/*.ts",
]
}
Loading

0 comments on commit ace403b

Please sign in to comment.