From 398569fdaea7b5bef9981db996b9caf41a0ce07b Mon Sep 17 00:00:00 2001 From: userquin Date: Sat, 16 Oct 2021 05:49:30 +0200 Subject: [PATCH] feat(compiler): add marko compiler (#102) --- src/core/compilers/index.ts | 2 ++ src/core/compilers/marko.ts | 20 ++++++++++++++++++++ src/index.ts | 20 ++++++++++++-------- src/types.ts | 2 +- 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 src/core/compilers/marko.ts diff --git a/src/core/compilers/index.ts b/src/core/compilers/index.ts index 0e570327..32952c46 100644 --- a/src/core/compilers/index.ts +++ b/src/core/compilers/index.ts @@ -1,5 +1,6 @@ import { ResolvedOptions } from '../../types' import { JSXCompiler } from './jsx' +import { MarkoCompiler } from './marko' import { NoneCompiler } from './none' import { RawCompiler } from './raw' import { SolidCompiler } from './solid' @@ -15,6 +16,7 @@ export const compilers: Record = { 'solid': SolidCompiler, 'svelte': SvelteCompiler, 'jsx': JSXCompiler, + 'marko': MarkoCompiler, 'none': NoneCompiler, 'raw': RawCompiler, 'web-components': WebComponentsCompiler, diff --git a/src/core/compilers/marko.ts b/src/core/compilers/marko.ts new file mode 100644 index 00000000..a5f99578 --- /dev/null +++ b/src/core/compilers/marko.ts @@ -0,0 +1,20 @@ +import { Compiler } from './types' + +export const MarkoCompiler = ((svg: string) => { + const openTagEnd = svg.indexOf('>', svg.indexOf('` + const content = `$!{\`${escapeTemplateLiteral(svg.slice(openTagEnd, closeTagStart))}\`}` + const closeTag = svg.slice(closeTagStart) + return `${openTag}${content}${closeTag}` +}) + +export function escapeTemplateLiteral(str: string): string { + return str.replace(/\\.|[$`]/g, (m) => { + switch (m) { + case '$': return '$' + case '`': return '`' + default: return m + } + }) +} diff --git a/src/index.ts b/src/index.ts index 4736c56c..67a3f875 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,14 +14,18 @@ const unplugin = createUnplugin((options = {}) => { const res = normalizeIconPath(id) .replace(/\.\w+$/i, '') .replace(/^\//, '') - const ext = options.compiler === 'jsx' - ? '.jsx' - : options.compiler === 'svelte' - ? '.svelte' - : options.compiler === 'solid' - ? '.tsx' - : '' - return res + ext + switch (options.compiler || '') { + case 'jsx': + return `${res}.jsx` + case 'svelte': + return `${res}.svelte` + case 'solid': + return `${res}.tsx` + case 'marko': + return `${res}.marko` + default: + return res + } } return null }, diff --git a/src/types.ts b/src/types.ts index aa2fbec5..1a2d7a24 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,7 +45,7 @@ export interface Options { * * @default (detect automatically, fallback to 'vue3') */ - compiler?: 'vue2' | 'vue3' | 'jsx' | 'solid' | 'svelte' | 'web-components' | 'none' | 'raw' + compiler?: 'vue2' | 'vue3' | 'jsx' | 'solid' | 'svelte' | 'web-components' | 'marko' | 'none' | 'raw' /** * JSX style, works only when compiler set to `jsx`