Skip to content

Commit

Permalink
Core code
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfanatic committed Sep 24, 2018
1 parent 8a658b1 commit 3a23e75
Show file tree
Hide file tree
Showing 3 changed files with 1,280 additions and 0 deletions.
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const SVGO = require('svgo');
const { compile } = require('vue-template-compiler');
const stripWith = require('vue-template-es2015-compiler');

module.exports = (content, options = {}) => {
const {
svgoConfig = {},
svgoPath = null,
} = options;

let svg = Promise.resolve(content);

if (svgoConfig !== false) {
svg = new SVGO(svgoConfig)
.optimize(content, { path: svgoPath })
.then(result => result.data);
}

return svg.then((result) => {
let { render: renderFunction } = compile(result, {
preserveWhitespace: false,
modules: [
{
transformNode: (el) => {
if (el.tag === 'svg') {
el.classBinding = '[data.class, data.staticClass]';
el.styleBinding = '[data.style, data.staticStyle]';
}
},
},
],
});

renderFunction = `
function render(_h, _vm) {
${renderFunction};
}
`;

renderFunction = stripWith(renderFunction, {
transforms: {
stripWithFunctional: true,
},
});

return `
module.exports = {
functional: true,
render: ${renderFunction},
};
`;
});
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "svg-to-vue",
"description": "Utility to convert SVG code into Vue component definition",
"version": "0.1.0",
"keywords": [
"vue",
"svg",
"svgo"
],
"bugs": "https://github.com/visualfanatic/svg-to-vue/issues",
"repository": "visualfanatic/svg-to-vue",
"license": "MIT",
"author": "Damian Stasik <[email protected]>",
"main": "index.js",
"dependencies": {
"svgo": "^1.1.1",
"vue-template-compiler": "^2.5.17",
"vue-template-es2015-compiler": "^1.6.0"
},
"devDependencies": {
"eslint": "^5.6.0",
"eslint-config-airbnb-base": "13.1.0",
"eslint-plugin-import": "^2.14.0"
}
}
Loading

0 comments on commit 3a23e75

Please sign in to comment.