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

Commit

Permalink
Bump dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
titanous committed Mar 1, 2022
1 parent f547658 commit 941bfe0
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 500 deletions.
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
5.0.0
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ exports_files(

pkg_npm(
name = "npm_package",
package_name = "@bazel/postcss",
srcs = [
"BUILD",
"LICENSE",
Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()

load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies")

build_bazel_rules_nodejs_dependencies()

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
43 changes: 23 additions & 20 deletions examples/additional_outputs/list-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,32 @@

const fs = require('fs');
const path = require('path');
const postcss = require('postcss');

// Outputs a file listing all unique CSS selectors found.
module.exports = postcss.plugin('list-selectors', (opts = {}) => {
return css => {
let totalSelectors = 0;
const selectorCounts = {};
css.walkRules(rule => {
for (const selector of rule.selectors) {
if (!(selector in selectorCounts)) {
selectorCounts[selector] = 0;
module.exports = (opts = {}) => {
return {
postcssPlugin: 'list-selectors',
Once(css) {
let totalSelectors = 0;
const selectorCounts = {};
css.walkRules(rule => {
for (const selector of rule.selectors) {
if (!(selector in selectorCounts)) {
selectorCounts[selector] = 0;
}
selectorCounts[selector]++;
totalSelectors++;
}
selectorCounts[selector]++;
totalSelectors++;
}
});
});

let markdown = `# Found ${Object.keys(selectorCounts).length} unique selectors`
let markdown = `# Found ${Object.keys(selectorCounts).length} unique selectors`
+ ` (of ${totalSelectors} total)\n`;
for (let selector in selectorCounts) {
markdown += `\n- ${selector} (${selectorCounts[selector]} total)`
}
for (let selector in selectorCounts) {
markdown += `\n- ${selector} (${selectorCounts[selector]} total)`
}

fs.writeFileSync(opts.output, markdown);
};
});
fs.writeFileSync(opts.output, markdown);
},
}
}
module.exports.postcss = true;
26 changes: 14 additions & 12 deletions examples/custom_plugin/unquote.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
* to stylesheets.
*/

const postcss = require('postcss');

// Replaces all "unquote('hello')" found with "hello", supporting
// both ' and ". Doesn't do quote escaping.
module.exports = postcss.plugin('unquote', (opts = {}) => {
module.exports = (opts = {}) => {
/**
* Unquote implementation. http://stackoverflow.com/a/19584742
* @param {string} str
Expand All @@ -36,12 +34,16 @@ module.exports = postcss.plugin('unquote', (opts = {}) => {
return str;
};

return css => {
// 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));
};
});
return {
postcssPlugin: "unquote",
Once(css) {
// 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));
},
}
}
module.exports.postcss = true;
18 changes: 4 additions & 14 deletions examples/custom_plugin_ts/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@npm//@bazel/typescript:index.bzl", "ts_library")
load("@npm//@bazel/typescript:index.bzl", "ts_project")
load("//:index.bzl", "postcss_binary", "postcss_plugin")

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

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

filegroup(
name = "unquote_js",
srcs = [":unquote_ts"],
output_group = "es5_sources",
deps = ["@npm//@types"],
)

postcss_plugin(
name = "unquote",
srcs = [
":unquote_js",
":unquote_ts",
],
node_require = "build_bazel_rules_postcss/examples/custom_plugin_ts/unquote.js",
)
Expand Down
3 changes: 3 additions & 0 deletions examples/custom_plugin_ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compilerOptions": {}
}
26 changes: 14 additions & 12 deletions examples/custom_plugin_ts/unquote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
* 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', () => {
module.exports = (opts = {}) => {
/**
* Unquote implementation. http://stackoverflow.com/a/19584742
* @param str
Expand All @@ -36,12 +34,16 @@ module.exports = postcss.plugin('unquote', () => {
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));
};
});
return {
postcssPlugin: "unquote",
Once(css) {
// 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));
},
}
}
module.exports.postcss = true;
14 changes: 9 additions & 5 deletions examples/data_attr/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
const fs = require('fs');
const postcss = require('postcss');

module.exports = postcss.plugin('header', (opts = {}) => {
module.exports = (opts = {}) => {
const contents = fs.readFileSync(opts.path, 'utf8').trim();

return css => {
css.prepend(postcss.comment({text: contents}));
};
});
return {
postcssPlugin: "header",
Once(css) {
css.prepend(postcss.comment({ text: contents }));
},
}
}
module.exports.postcss = true;
2 changes: 1 addition & 1 deletion examples/multi_binary/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ postcss_multi_binary(
],
output_pattern = "{rule}/{dir}/{name}",
plugins = {
":autoprefixer": "[{ browsers: '%s' }]" % AUTO_PREFIXER_BROWSERS,
":autoprefixer": "[{ overrideBrowserslist: '%s' }]" % AUTO_PREFIXER_BROWSERS,
},
)
2 changes: 1 addition & 1 deletion examples/multi_sourcemap/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ postcss_multi_binary(
],
output_pattern = "{rule}/{dir}/{name}",
plugins = {
":autoprefixer": "[{ browsers: '%s' }]" % AUTO_PREFIXER_BROWSERS,
":autoprefixer": "[{ overrideBrowserslist: '%s' }]" % AUTO_PREFIXER_BROWSERS,
},
sourcemap = True,
deps = ["@npm//autoprefixer"],
Expand Down
14 changes: 9 additions & 5 deletions examples/positional_data_attr/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
const fs = require('fs');
const postcss = require('postcss');

module.exports = postcss.plugin('header', (opts = {}) => {
module.exports = (opts = {}) => {
const contents = fs.readFileSync(opts.path, 'utf8').trim();

return css => {
css.prepend(postcss.comment({text: contents}));
};
});
return {
postcssPlugin: "header",
Once(css) {
css.prepend(postcss.comment({ text: contents }));
},
}
}
module.exports.postcss = true;
20 changes: 13 additions & 7 deletions internal/plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@ Node.js source files, as well as promoting reuse of plugins across multiple
postcss_binary targets.
"""

load("@build_bazel_rules_nodejs//:providers.bzl", "NpmPackageInfo")
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "ExternalNpmPackageInfo", "JSModuleInfo")

PostcssPluginInfo = provider("""Provides extra metadata about this PostCSS
plugin required when using postcss_binary.""", fields = ["node_require"])

def _postcss_plugin_info_impl(ctx):
transitive_sources = []
for d in ctx.attr.deps:
if ExternalNpmPackageInfo in d:
transitive_sources.append(d[ExternalNpmPackageInfo].sources)
if JSModuleInfo in d:
transitive_sources.append(d[JSModuleInfo].sources)
if DeclarationInfo in d:
transitive_sources.append(d[DeclarationInfo].declarations)

return [
PostcssPluginInfo(node_require = ctx.attr.node_require),
NpmPackageInfo(
ExternalNpmPackageInfo(
direct_sources = depset(ctx.files.srcs),
sources = depset(
ctx.files.srcs,
transitive = [
dep[NpmPackageInfo].sources
for dep in ctx.attr.deps
if NpmPackageInfo in dep
],
transitive = transitive_sources,
),
path = "",
workspace = "npm",
),
]
Expand Down
22 changes: 12 additions & 10 deletions internal/run.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Runs a internal PostCSS runner, generated via the postcss_gen_runner rule."""

load("@build_bazel_rules_nodejs//:providers.bzl", "run_node")
load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "run_node")
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":plugin.bzl", "PostcssPluginInfo")

Expand Down Expand Up @@ -57,12 +57,6 @@ def _run_one(ctx, input_css, input_map, output_css, output_map):
if ctx.attr.sourcemap:
args.add("--outCssMapFile", output_map.path)

# The command may only access files declared in inputs.
inputs = depset(
[input_css] + ([input_map] if input_map else []),
transitive = data,
)

outputs = [output_css]
if ctx.attr.sourcemap:
args.add("--sourcemap")
Expand All @@ -71,20 +65,28 @@ def _run_one(ctx, input_css, input_map, output_css, output_map):
if hasattr(ctx.outputs, "additional_outputs"):
outputs.extend(ctx.outputs.additional_outputs)

plugins = []
plugin_deps = []
for plugin_key, plugin_options in ctx.attr.plugins.items():
node_require = plugin_key[PostcssPluginInfo].node_require
args.add("--pluginRequires", node_require)
args.add("--pluginArgs", plugin_options if plugin_options else "[]")
plugin_deps.append(plugin_key[ExternalNpmPackageInfo].sources)

# The command may only access files declared in inputs.
inputs = depset(
[input_css] + ([input_map] if input_map else []),
transitive = data + plugin_deps,
)

# If a wrapper binary is passed, run it. It gets the actual binary as an
# input and the path to it as the first arg.
if ctx.executable.wrapper:
# If using a wrapper, running as a worker is currently unsupported.
ctx.actions.run(
run_node(
ctx = ctx,
inputs = inputs,
outputs = outputs,
executable = ctx.executable.wrapper,
executable = "wrapper",
tools = [ctx.executable.runner],
arguments = [args],
progress_message = "Running PostCSS wrapper on %s" % input_css.short_path,
Expand Down
Loading

0 comments on commit 941bfe0

Please sign in to comment.