From 021469f4bda6a4fc45a564f320cc83f472a0007d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez=20Jim=C3=A9nez?=
Date: Sat, 9 Oct 2021 21:04:02 +0200
Subject: [PATCH 01/18] feat: add css compiler
---
examples/vite-vue3/App.vue | 7 +++++++
src/core/compilers/css.ts | 20 ++++++++++++++++++++
src/core/compilers/index.ts | 2 ++
src/core/svgId.ts | 3 ++-
src/index.ts | 12 +++++++++++-
src/types.ts | 2 +-
6 files changed, 43 insertions(+), 3 deletions(-)
create mode 100644 src/core/compilers/css.ts
diff --git a/examples/vite-vue3/App.vue b/examples/vite-vue3/App.vue
index 97d258e1..091544cb 100644
--- a/examples/vite-vue3/App.vue
+++ b/examples/vite-vue3/App.vue
@@ -28,6 +28,11 @@
+ CSS Icons
+
+
+
+
Collection alias
@@ -40,4 +45,6 @@
diff --git a/src/core/compilers/css.ts b/src/core/compilers/css.ts
new file mode 100644
index 00000000..d44ece1a
--- /dev/null
+++ b/src/core/compilers/css.ts
@@ -0,0 +1,20 @@
+// import { ResolvedOptions } from '../../types'
+import { Compiler } from './types'
+
+export const CssCompiler = ((
+ svg: string,
+ collection: string,
+ icon: string,
+ // options: ResolvedOptions,
+) => {
+ return `.${collection}-${icon} {
+background: url(data:image/svg+xml;base64,${Buffer.from(svg, 'utf-8').toString('base64')}) no-repeat center;
+background-size: 16px 16px;
+height: 16px;
+opacity: 0.87;
+width: 16px;
+max-width: 16px;
+display: inline-block;
+}
+`
+})
diff --git a/src/core/compilers/index.ts b/src/core/compilers/index.ts
index 0e570327..1742507a 100644
--- a/src/core/compilers/index.ts
+++ b/src/core/compilers/index.ts
@@ -8,8 +8,10 @@ import { Compiler } from './types'
import { Vue2Compiler } from './vue2'
import { Vue3Compiler } from './vue3'
import { WebComponentsCompiler } from './web-components'
+import { CssCompiler } from './css'
export const compilers: Record = {
+ 'css': CssCompiler,
'vue2': Vue2Compiler,
'vue3': Vue3Compiler,
'solid': SolidCompiler,
diff --git a/src/core/svgId.ts b/src/core/svgId.ts
index 81db1ea9..981e8ced 100644
--- a/src/core/svgId.ts
+++ b/src/core/svgId.ts
@@ -16,7 +16,8 @@ export function handleSVGId(svg: string) {
return `:id="idMap['${id}']"`
return full
})
- injectScripts = randIdFn + `const idMap = {${Object.values(idMap).join(',')}};`
+ // eslint-disable-next-line prefer-template
+ injectScripts = randIdFn + `const idMap = {${Object.values(idMap).join(',')}};`
}
return {
diff --git a/src/index.ts b/src/index.ts
index d803e1ad..9b0f70f7 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -11,6 +11,9 @@ const unplugin = createUnplugin((options = {}) => {
enforce: 'pre',
resolveId(id) {
if (isIconPath(id)) {
+ if (id.endsWith('.css'))
+ return id
+
const res = normalizeIconPath(id)
.replace(/\.\w+$/i, '')
.replace(/^\//, '')
@@ -26,7 +29,14 @@ const unplugin = createUnplugin((options = {}) => {
async load(id) {
const config = await resolved
if (isIconPath(id)) {
- const code = await generateComponentFromPath(id, config) || null
+ let code: string | null = null
+ const css = id.endsWith('.css')
+ if (css)
+ code = await generateComponentFromPath(id, { ...config, compiler: 'css' }) || null
+
+ else
+ code = await generateComponentFromPath(id, config) || null
+
if (code) {
return {
code,
diff --git a/src/types.ts b/src/types.ts
index 9fb19c45..b6adec13 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -57,7 +57,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' | 'none' | 'raw' | 'css'
/**
* JSX style, works only when compiler set to `jsx`
From 2cc474c114ae2d4ebe7f6b1752079b5672f3cd64 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez=20Jim=C3=A9nez?=
Date: Sat, 9 Oct 2021 21:47:10 +0200
Subject: [PATCH 02/18] chore: add options for css fix: add svg xmlns if
missing on source
---
src/core/compilers/css.ts | 27 +++++++++++++++++++--------
src/core/options.ts | 2 ++
src/types.ts | 24 ++++++++++++++++++++++++
3 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/src/core/compilers/css.ts b/src/core/compilers/css.ts
index d44ece1a..a5af843e 100644
--- a/src/core/compilers/css.ts
+++ b/src/core/compilers/css.ts
@@ -1,20 +1,31 @@
-// import { ResolvedOptions } from '../../types'
+import { ResolvedOptions } from '../../types'
import { Compiler } from './types'
export const CssCompiler = ((
svg: string,
collection: string,
icon: string,
- // options: ResolvedOptions,
+ options: ResolvedOptions,
) => {
+ // we need to add the cvg xml namespace
+ let inlineSvg = svg.includes('xmlns=')
+ ? svg
+ : svg.replace(/"/g, '\'').replace('