Skip to content

Commit d5c0012

Browse files
authored
feat: support custom blocks (#7)
1 parent 76f644b commit d5c0012

File tree

10 files changed

+1803
-17
lines changed

10 files changed

+1803
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
test/fixture/output

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Supported transforms (via `lang` attribute):
4040
- `<style>` tag:
4141
- `postcss` (default): use your own `postcss.config.js`
4242
- `stylus` `sass` `scss`
43-
- Custom blocks: nope.
43+
- Custom blocks: They are not touched.
4444

4545
Gotchas:
4646

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: 'node'
3+
}

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ class VueCompile extends EventEmitter {
9898
{
9999
script,
100100
styles,
101-
template
101+
template,
102+
customBlocks: sfcDescriptor.customBlocks
102103
},
103104
outFile
104105
)

lib/writeSFC.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const fs = require('fs-extra')
33
const stringifyAttrs = require('stringify-attributes')
44
const { cssExtensionsRe } = require('./utils')
55

6-
module.exports = async ({ script, styles, template }, outFile) => {
6+
module.exports = async (
7+
{ script, styles, template, customBlocks },
8+
outFile
9+
) => {
710
const parts = []
811

912
if (template) {
@@ -34,6 +37,16 @@ module.exports = async ({ script, styles, template }, outFile) => {
3437
}
3538
}
3639

40+
if (customBlocks) {
41+
for (const block of customBlocks) {
42+
parts.push(
43+
`<${block.type}${stringifyAttrs(block.attrs)}>${
44+
block.content ? block.content.trim() : ''
45+
}</${block.type}>`
46+
)
47+
}
48+
}
49+
3750
await fs.ensureDir(path.dirname(outFile))
3851
await fs.writeFile(outFile, parts.join('\n\n'), 'utf8')
3952
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"babel.js"
1515
],
1616
"scripts": {
17-
"test": "npm run lint && echo 'no tests!'",
17+
"test": "npm run lint && jest",
1818
"lint": "xo",
1919
"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')\""
2020
},
@@ -43,12 +43,14 @@
4343
"devDependencies": {
4444
"eslint-config-rem": "^4.0.0",
4545
"husky": "^1.0.0-rc.4",
46+
"jest": "^24.8.0",
4647
"lint-staged": "^7.1.0",
4748
"stylus": "^0.54.5",
4849
"xo": "^0.18.0"
4950
},
5051
"xo": {
5152
"extends": "rem",
53+
"envs": ["jest"],
5254
"rules": {
5355
"unicorn/filename-case": "off"
5456
}

test/__snapshots__/index.test.js.snap

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`custom blocks 1`] = `
4+
"<template>
5+
<div></div>
6+
</template>
7+
8+
<script>
9+
export default {};
10+
</script>
11+
12+
<foo>this is a custom block</foo>"
13+
`;

test/fixture/custom-blocks/foo.vue

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div></div>
3+
</template>
4+
5+
<script>
6+
export default {
7+
8+
}
9+
</script>
10+
11+
<foo>
12+
this is a custom block
13+
</foo>

test/index.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const path = require('path')
2+
const fs = require('fs-extra')
3+
const compile = require('../lib')
4+
5+
const fixture = (...args) => path.resolve(__dirname, 'fixture', ...args)
6+
const tmp = name => fixture('output', name)
7+
8+
test('custom blocks', async () => {
9+
const outDir = tmp('custom-blocks')
10+
const compiler = compile({
11+
input: fixture('custom-blocks'),
12+
output: outDir
13+
})
14+
await compiler.normalize()
15+
const content = await fs.readFile(path.join(outDir, 'foo.vue'), 'utf8')
16+
expect(content).toMatchSnapshot()
17+
})
18+

0 commit comments

Comments
 (0)