diff --git a/packages/rules_prerender/inline_style_map.ts b/packages/rules_prerender/inline_style_map.ts index 3bf5d415..2ab632ec 100644 --- a/packages/rules_prerender/inline_style_map.ts +++ b/packages/rules_prerender/inline_style_map.ts @@ -8,8 +8,7 @@ let map: ReadonlyMap | undefined; /** Returns the inline style map. */ -export function getMap(): ReadonlyMap { - if (!map) throw new Error('Inline style map not set.'); +export function getMap(): ReadonlyMap | undefined { return map; } diff --git a/packages/rules_prerender/styles.ts b/packages/rules_prerender/styles.ts index 4d6c0c75..c36b6feb 100644 --- a/packages/rules_prerender/styles.ts +++ b/packages/rules_prerender/styles.ts @@ -24,13 +24,17 @@ export function includeStyle(path: string): string { */ export function inlineStyle(importPath: string): string { // Look up the import path in the inline style map to get its actual file - // path on disk. + // path on disk. This will always exist when executed as part of the + // renderer, however tests which directly call components won't set the + // inline style map and it won't exist. In these cases, we just ignore it + // since such tests would not actually care. const inlineStyleMap = getInlineStyleMap(); - const filePath = inlineStyleMap.get(importPath); + const filePath = inlineStyleMap ? inlineStyleMap.get(importPath) : importPath; if (!filePath) { throw new Error(`Could not find "${ importPath}" in the inline style map. Available imports are:\n\n${ - Array.from(inlineStyleMap.keys()).join('\n')}`); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + Array.from(inlineStyleMap!.keys()).join('\n')}`); } // Return an annotation with the real file path. diff --git a/packages/rules_prerender/styles_test.ts b/packages/rules_prerender/styles_test.ts index 6ebf38d0..76e7a860 100644 --- a/packages/rules_prerender/styles_test.ts +++ b/packages/rules_prerender/styles_test.ts @@ -45,6 +45,16 @@ wksp/foo/bar/baz.css wksp/hello/world.css `.trim()); }); + + it('ignores the inline style map when not defined', () => { + spyOn(inlineStyleMap, 'getMap').and.returnValue(undefined); + + expect(inlineStyle('wksp/foo/bar/baz.css')).toBe(``); + }); }); describe('inlineStyleLegacy()', () => {