diff --git a/packages/rules_prerender/BUILD.bazel b/packages/rules_prerender/BUILD.bazel
index 8e0d6314..34483822 100644
--- a/packages/rules_prerender/BUILD.bazel
+++ b/packages/rules_prerender/BUILD.bazel
@@ -168,13 +168,18 @@ jasmine_node_test(
ts_library(
name = "styles",
srcs = ["styles.ts"],
- deps = ["//common/models:prerender_annotation"],
+ deps = [
+ "//common:fs",
+ "//common:runfiles",
+ "//common/models:prerender_annotation",
+ ],
)
ts_library(
name = "styles_test_lib",
srcs = ["styles_test.ts"],
testonly = True,
+ data = ["//packages/rules_prerender/testdata:styles.css"],
deps = [
":styles",
"@npm//@types/jasmine",
diff --git a/packages/rules_prerender/index.ts b/packages/rules_prerender/index.ts
index fd2b9c34..7bfeba3c 100644
--- a/packages/rules_prerender/index.ts
+++ b/packages/rules_prerender/index.ts
@@ -2,4 +2,4 @@
export { PrerenderResource } from 'rules_prerender/common/models/prerender_resource';
export { includeScript } from 'rules_prerender/packages/rules_prerender/scripts';
-export { includeStyle } from 'rules_prerender/packages/rules_prerender/styles';
+export { includeStyle, inlineStyle } from 'rules_prerender/packages/rules_prerender/styles';
diff --git a/packages/rules_prerender/styles.ts b/packages/rules_prerender/styles.ts
index 0e66c607..e657a038 100644
--- a/packages/rules_prerender/styles.ts
+++ b/packages/rules_prerender/styles.ts
@@ -1,4 +1,6 @@
+import * as fs from 'rules_prerender/common/fs';
import { createAnnotation } from 'rules_prerender/common/models/prerender_annotation';
+import { resolveRunfile } from 'rules_prerender/common/runfiles';
/**
* Returns a prerender annotation as a string to be included in prerendered
@@ -12,3 +14,16 @@ export function includeStyle(path: string): string {
});
return ``;
}
+
+/**
+ * Reads the given CSS file at the provided runfiles path and returns it in a
+ * `` tag to be inlined in the document.
+ */
+export async function inlineStyle(path: string): Promise {
+ const styles = await fs.readFile(resolveRunfile(path), 'utf-8');
+ return `
+
+ `.trim();
+}
diff --git a/packages/rules_prerender/styles_test.ts b/packages/rules_prerender/styles_test.ts
index 0312a6aa..5219e4b5 100644
--- a/packages/rules_prerender/styles_test.ts
+++ b/packages/rules_prerender/styles_test.ts
@@ -1,14 +1,32 @@
import 'jasmine';
-import { includeStyle } from 'rules_prerender/packages/rules_prerender/styles';
+import { includeStyle, inlineStyle } from 'rules_prerender/packages/rules_prerender/styles';
describe('styles', () => {
describe('includeStyle()', () => {
it('returns a style annotation in an HTML comment', () => {
const annotation = includeStyle('foo/bar/baz.css');
- expect(annotation)
- .toBe('');
+ expect(annotation).toBe(
+ '');
+ });
+ });
+
+ describe('inlineStyle()', () => {
+ it('resolves to a `` element with the runfiles content of the given file path', async () => {
+ const styles = await inlineStyle(
+ 'rules_prerender/packages/rules_prerender/testdata/styles.css');
+ expect(styles).toBe(`
+
+ `.trim());
+ });
+
+ it('rejects when the given file path is not found in runfiles', async () => {
+ await expectAsync(inlineStyle('rules_prerender/does/not/exist.css'))
+ .toBeRejected();
});
});
});
diff --git a/packages/rules_prerender/testdata/BUILD.bazel b/packages/rules_prerender/testdata/BUILD.bazel
new file mode 100644
index 00000000..bb54df0c
--- /dev/null
+++ b/packages/rules_prerender/testdata/BUILD.bazel
@@ -0,0 +1,3 @@
+package(default_visibility = ["//packages/rules_prerender:__pkg__"])
+
+exports_files(["styles.css"])
diff --git a/packages/rules_prerender/testdata/styles.css b/packages/rules_prerender/testdata/styles.css
new file mode 100644
index 00000000..2d130df9
--- /dev/null
+++ b/packages/rules_prerender/testdata/styles.css
@@ -0,0 +1 @@
+.foo { color: red; }