Skip to content

Commit c5ed4f8

Browse files
authored
feat(esbuild): add output_css flag to esbuild() (#2545)
1 parent 75233fd commit c5ed4f8

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

packages/esbuild/esbuild.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def _esbuild_impl(ctx):
2828
elif hasattr(dep, "files"):
2929
deps_depsets.append(dep.files)
3030

31+
if DefaultInfo in dep:
32+
deps_depsets.append(dep[DefaultInfo].data_runfiles.files)
33+
3134
if NpmPackageInfo in dep:
3235
deps_depsets.append(dep[NpmPackageInfo].sources)
3336
npm_workspaces.append(dep[NpmPackageInfo].workspace)
@@ -100,6 +103,9 @@ def _esbuild_impl(ctx):
100103
fail("output_map must be specified if sourcemap is not set to 'inline'")
101104
outputs.append(js_out_map)
102105

106+
if ctx.outputs.output_css:
107+
outputs.append(ctx.outputs.output_css)
108+
103109
if ctx.attr.format:
104110
args.add_joined(["--format", ctx.attr.format], join_with = "=")
105111

@@ -219,6 +225,14 @@ See https://esbuild.github.io/api/#splitting for more details
219225
mandatory = False,
220226
doc = "Name of the output source map when bundling",
221227
),
228+
"output_css": attr.output(
229+
mandatory = False,
230+
doc = """Declare a .css file will be output next to output bundle.
231+
232+
If your JS code contains import statements that import .css files, esbuild will place the
233+
content in a file next to the main output file, which you'll need to declare. If your output
234+
file is named 'foo.js', you should set this to 'foo.css'.""",
235+
),
222236
"platform": attr.string(
223237
default = "browser",
224238
values = ["node", "browser", "neutral", ""],

packages/esbuild/test/css/BUILD.bazel

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
load("//packages/esbuild/test:tests.bzl", "esbuild")
2+
load("//packages/jasmine:index.bzl", "jasmine_node_test")
3+
load("//packages/typescript:index.bzl", "ts_library")
4+
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin")
5+
6+
copy_to_bin(
7+
name = "external_copied",
8+
srcs = ["external.css"],
9+
)
10+
11+
ts_library(
12+
name = "dep",
13+
srcs = [
14+
"dep.ts",
15+
],
16+
data = [
17+
":external_copied",
18+
],
19+
deps = [
20+
"@npm//@types/node",
21+
],
22+
)
23+
24+
ts_library(
25+
name = "main",
26+
srcs = [
27+
"main.ts",
28+
],
29+
deps = [
30+
":dep",
31+
"@npm//@types/node",
32+
],
33+
)
34+
35+
esbuild(
36+
name = "default",
37+
args = [
38+
"--keep-names",
39+
"--resolve-extensions=.mjs,.js",
40+
],
41+
entry_point = "main.ts",
42+
deps = [
43+
":main",
44+
],
45+
)
46+
47+
esbuild(
48+
name = "with_css",
49+
args = [
50+
"--keep-names",
51+
"--resolve-extensions=.mjs,.js",
52+
],
53+
entry_point = "main.ts",
54+
output_css = "with_css.css",
55+
deps = [
56+
":main",
57+
],
58+
)
59+
60+
jasmine_node_test(
61+
name = "bundle_test",
62+
srcs = ["bundle_test.js"],
63+
data = [
64+
":default",
65+
":with_css",
66+
],
67+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { readFileSync } = require("fs");
2+
const path = require("path");
3+
4+
const helper = require(process.env.BAZEL_NODE_RUNFILES_HELPER);
5+
const locationBase = "build_bazel_rules_nodejs/packages/esbuild/test/css/";
6+
7+
const cssExpected = helper.resolve(path.join(locationBase, "with_css.css"));
8+
9+
describe("esbuild css", () => {
10+
it("no css by default", () => {
11+
if (process.platform === "win32") {
12+
// Windows has no sandbox, and the runfiles helper will return files
13+
// that happen to exist in the folder, even if they are not declared outputs
14+
return;
15+
}
16+
expect(() =>
17+
helper.resolve(path.join(locationBase, "default.css"))
18+
).toThrow();
19+
});
20+
21+
it("css if requested", () => {
22+
const contents = readFileSync(cssExpected, { encoding: "utf8" });
23+
expect(contents).toContain("external-content");
24+
});
25+
});

packages/esbuild/test/css/dep.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./external.css";
2+
3+
export function dep() {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.external-content {
2+
}

packages/esbuild/test/css/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { dep } from "./dep";
2+
3+
dep();

0 commit comments

Comments
 (0)