Skip to content

Commit

Permalink
feat: add support for svg templates (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 authored and mgechev committed Mar 25, 2019
1 parent bd2a7a8 commit dadf8ec
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/angular/config.ts
@@ -1,11 +1,11 @@
import { CodeWithSourceMap } from './metadata';

export interface StyleTransformer {
(style: string, url?: string): CodeWithSourceMap;
(code: string, url?: string): CodeWithSourceMap;
}

export interface TemplateTransformer {
(template: string, url?: string): CodeWithSourceMap;
(code: string, url?: string): CodeWithSourceMap;
}

export interface UrlResolver {
Expand All @@ -14,9 +14,11 @@ export interface UrlResolver {

export const LogLevel = { Debug: 0b111, Error: 0b001, Info: 0b011, None: 0 };

type ValueOf<T> = T[keyof T];

export interface Config {
interpolation: [string, string];
logLevel: number;
logLevel: ValueOf<typeof LogLevel>;
predefinedDirectives: DirectiveDeclaration[];
resolveUrl: UrlResolver;
transformStyle: StyleTransformer;
Expand All @@ -33,10 +35,17 @@ export interface DirectiveDeclaration {
selector: string;
}

const CSS_FILE_EXTENSION = '.css';
const HTML_FILE_EXTENSION = '.html';
const SVG_FILE_EXTENSION = '.svg';
let BUILD_TYPE = '<%= BUILD_TYPE %>';

const transform = (code: string, extension: '.css' | '.html', url?: string): { code: string; url?: string } => {
return { code: !url || url.endsWith(extension) ? code : '', url };
type FileExtension = typeof CSS_FILE_EXTENSION | typeof HTML_FILE_EXTENSION | typeof SVG_FILE_EXTENSION;

const transform = (code: string, fileExtensions: ReadonlyArray<FileExtension>, url?: string): { code: string; url?: string } => {
const parsedCode = !url || fileExtensions.some(fileExtension => url.endsWith(fileExtension)) ? code : '';

return { code: parsedCode, url };
};

export const Config: Config = {
Expand Down Expand Up @@ -71,13 +80,11 @@ export const Config: Config = {
{ selector: 'md-select', exportAs: 'mdSelect' }
],

resolveUrl(url: string | null) {
return url;
},
resolveUrl: (url: string | null) => url,

transformStyle: (code: string, url?: string) => transform(code, '.css', url),
transformStyle: (code: string, url?: string) => transform(code, [CSS_FILE_EXTENSION], url),

transformTemplate: (code: string, url?: string) => transform(code, '.html', url)
transformTemplate: (code: string, url?: string) => transform(code, [HTML_FILE_EXTENSION, SVG_FILE_EXTENSION], url)
};

try {
Expand Down

0 comments on commit dadf8ec

Please sign in to comment.