Skip to content

feat: support custom blocks #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
test/fixture/output
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Supported transforms (via `lang` attribute):
- `<style>` tag:
- `postcss` (default): use your own `postcss.config.js`
- `stylus` `sass` `scss`
- Custom blocks: nope.
- Custom blocks: They are not touched.

Gotchas:

Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'node'
}
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class VueCompile extends EventEmitter {
{
script,
styles,
template
template,
customBlocks: sfcDescriptor.customBlocks
},
outFile
)
Expand Down
15 changes: 14 additions & 1 deletion lib/writeSFC.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const fs = require('fs-extra')
const stringifyAttrs = require('stringify-attributes')
const { cssExtensionsRe } = require('./utils')

module.exports = async ({ script, styles, template }, outFile) => {
module.exports = async (
{ script, styles, template, customBlocks },
outFile
) => {
const parts = []

if (template) {
Expand Down Expand Up @@ -34,6 +37,16 @@ module.exports = async ({ script, styles, template }, outFile) => {
}
}

if (customBlocks) {
for (const block of customBlocks) {
parts.push(
`<${block.type}${stringifyAttrs(block.attrs)}>${
block.content ? block.content.trim() : ''
}</${block.type}>`
)
}
}

await fs.ensureDir(path.dirname(outFile))
await fs.writeFile(outFile, parts.join('\n\n'), 'utf8')
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"babel.js"
],
"scripts": {
"test": "npm run lint && echo 'no tests!'",
"test": "npm run lint && jest",
"lint": "xo",
"postinstall": "node -e \"console.log('\\u001b[35m\\u001b[1mLove vue-compile? You can now donate to support the author:\\u001b[22m\\u001b[39m\\n> \\u001b[36mhttps://patreon.com/egoist\\u001b[39m')\""
},
Expand Down Expand Up @@ -43,12 +43,14 @@
"devDependencies": {
"eslint-config-rem": "^4.0.0",
"husky": "^1.0.0-rc.4",
"jest": "^24.8.0",
"lint-staged": "^7.1.0",
"stylus": "^0.54.5",
"xo": "^0.18.0"
},
"xo": {
"extends": "rem",
"envs": ["jest"],
"rules": {
"unicorn/filename-case": "off"
}
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`custom blocks 1`] = `
"<template>
<div></div>
</template>

<script>
export default {};
</script>

<foo>this is a custom block</foo>"
`;
13 changes: 13 additions & 0 deletions test/fixture/custom-blocks/foo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div></div>
</template>

<script>
export default {
}
</script>

<foo>
this is a custom block
</foo>
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require('path')
const fs = require('fs-extra')
const compile = require('../lib')

const fixture = (...args) => path.resolve(__dirname, 'fixture', ...args)
const tmp = name => fixture('output', name)

test('custom blocks', async () => {
const outDir = tmp('custom-blocks')
const compiler = compile({
input: fixture('custom-blocks'),
output: outDir
})
await compiler.normalize()
const content = await fs.readFile(path.join(outDir, 'foo.vue'), 'utf8')
expect(content).toMatchSnapshot()
})

Loading