Skip to content

Commit

Permalink
chore: add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jan 11, 2019
1 parent da8241d commit 0813d37
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 59 deletions.
3 changes: 2 additions & 1 deletion example/poi.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
},
chainWebpack(config) {
config.module.rules.delete('svg')
config.module.rule('svg')
config.module
.rule('svg')
.test(/\.svg$/)
.use('vue')
.loader('vue-loader')
Expand Down
29 changes: 21 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const path = require('path')
const posthtml = require('posthtml')

const REGISTER_STYLE_COMPONENT = path.join(__dirname, './lib/registerStyleComponent.js')
const REGISTER_STYLE_COMPONENT = path.join(
__dirname,
'./lib/registerStyleComponent.js'
)

const plugin = state => tree => {
tree.match({ tag: 'svg' }, node => {
Expand All @@ -10,7 +13,11 @@ const plugin = state => tree => {
node.attrs = node.attrs || {}
for (const name of Object.keys(node.attrs)) {
// Don't add unnecessary attrs
if (name !== 'version' && name !== 'xmlns' && !name.startsWith('xmlns:')) {
if (
name !== 'version' &&
name !== 'xmlns' &&
!name.startsWith('xmlns:')
) {
attrs[name] = node.attrs[name]
}
delete node.attrs[name]
Expand All @@ -26,13 +33,21 @@ const plugin = state => tree => {
node.attrs['v-bind'] = 'data.attrs'

// Merge exiting class with the class prop on the component
node.attrs['v-bind:class'] = `[${JSON.stringify(existingClass)}, data.staticClass, data.class]`
node.attrs['v-bind:class'] = `[${JSON.stringify(
existingClass
)}, data.staticClass, data.class]`

// Merge exiting style with the style prop on the component
node.attrs['v-bind:style'] = `[${JSON.stringify(existingStyle)}, data.style]`
node.attrs['v-bind:style'] = `[${JSON.stringify(
existingStyle
)}, data.style]`

for (const key of Object.keys(attrs)) {
node.attrs[`v-bind:${key}`] = `!data.attrs || data.attrs[${JSON.stringify(key)}] === undefined ? ${JSON.stringify(attrs[key])} : data.attrs[${JSON.stringify(key)}]`
node.attrs[`v-bind:${key}`] = `!data.attrs || data.attrs[${JSON.stringify(
key
)}] === undefined ? ${JSON.stringify(
attrs[key]
)} : data.attrs[${JSON.stringify(key)}]`
}

// Adding v-on
Expand Down Expand Up @@ -73,9 +88,7 @@ const createComponent = (svg, state) => {
module.exports = (input, { sync } = {}) => {
const state = {}

const stream = posthtml([
plugin(state)
]).process(input, { sync })
const stream = posthtml([plugin(state)]).process(input, { sync })

if (stream.then) {
return stream.then(res => ({
Expand Down
2 changes: 1 addition & 1 deletion loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const merge = require('merge-deep')
const JoyCon = require('joycon').default
const toSFC = require('.')

module.exports = async function (source) {
module.exports = async function(source) {
this.cacheable()

const cb = this.async()
Expand Down
25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@
"ava": "^0.25.0",
"commitizen": "^3.0.5",
"cz-conventional-changelog": "^2.1.0",
"eslint-config-prettier": "^3.3.0",
"eslint-config-rem": "^3.0.0",
"eslint-plugin-prettier": "^3.0.1",
"husky": "^1.3.1",
"lint-staged": "^8.1.0",
"poi": "^12.0.0-beta.6",
"prettier": "^1.15.3",
"semantic-release": "^15.13.3",
"vue": "^2.5.17",
"vue-template-compiler": "^2.5.17",
"xo": "^0.18.0"
},
"xo": {
"extends": "rem",
"extends": [
"rem",
"plugin:prettier/recommended"
],
"rules": {
"unicorn/filename": "off"
},
Expand All @@ -58,5 +66,20 @@
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": [
"xo --fix",
"git add"
],
"*.{md,json}": [
"prettier --write",
"git add"
]
}
}
4 changes: 3 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ test('main', t => {
})

test('replace style tag', t => {
const sfc = toSFC(`<svg><style>svg {width:20px;}</style></svg>`, { sync: true })
const sfc = toSFC(`<svg><style>svg {width:20px;}</style></svg>`, {
sync: true
})

t.true(sfc.component.includes('/lib/registerStyleComponent.js'))
t.true(sfc.component.includes('<svg2vue-style>'))
Expand Down
Loading

0 comments on commit 0813d37

Please sign in to comment.