Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Commit 1879fe2

Browse files
committed
feat: support es module and extracting styles
1 parent fce653a commit 1879fe2

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

__test__/md2vue.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,32 @@ describe('md2vue', () => {
3333
)
3434
expect(app.find('.vue-demo > button').text()).toBe('Test Button')
3535
})
36+
37+
it('should emit styles', async () => {
38+
const path = resolve(__dirname, './fixtures/test.md')
39+
const file = await md2vue(path, {
40+
extractCSS: true,
41+
moduleType: 'esm'
42+
})
43+
44+
expect(file.extname).toEqual('.js')
45+
expect(file.data.toc.length).toEqual(3)
46+
expect(file.data.frontmatter).toEqual({
47+
heading: 'Test',
48+
pageClass: 'my-custom-class'
49+
})
50+
51+
expect(file.contents).toContain('export default')
52+
expect(file.stylesheet.extname).toEqual('.css')
53+
expect(file.stylesheet.contents).toContain('button')
54+
55+
file.contents = file.contents.replace('export default', 'module.exports = ')
56+
const app = mountVfile(file)
57+
expect(app.classes()).toContain('markdown-body')
58+
expect(app.classes()).toContain('my-custom-class')
59+
expect(app.contains('pre.vue-demo-source-code > code.language-html')).toBe(
60+
true
61+
)
62+
expect(app.find('.vue-demo > button').text()).toBe('Test Button')
63+
})
3664
})

lib/index.js

+35-15
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,41 @@ module.exports = function process(path, option, callback) {
8989
const { render, staticRenderFns } = compileTemplate(content)
9090

9191
file.extname = '.js'
92-
file.contents = [
93-
`const styleInject = require('${styleInjectFile}')`,
94-
`const styles = ${styles ? JSON.stringify(styles) : null};`,
95-
'module.exports = {',
96-
` render: ${render},`,
97-
` components: ${components},`,
98-
` staticRenderFns: ${staticRenderFns},`,
99-
` mounted () {`,
100-
` this.cleanup = styles && styleInject(styles) `,
101-
` },`,
102-
` destroyed () {`,
103-
` this.cleanup && this.cleanup()`,
104-
` }`,
105-
'}'
106-
].join('\n')
92+
93+
let epxortDelc = `module.exports = `
94+
if (option.moduleType === 'esm') {
95+
epxortDelc = 'export default'
96+
}
97+
98+
if (option.extractCSS) {
99+
file.stylesheet = VFile({
100+
path: file.path.replace(/\.js$/, '.css'),
101+
contents: styles || ''
102+
})
103+
file.contents = [
104+
`${epxortDelc} {`,
105+
` render: ${render},`,
106+
` components: ${components},`,
107+
` staticRenderFns: ${staticRenderFns},`,
108+
'}'
109+
].join('\n')
110+
} else {
111+
file.contents = [
112+
`const styleInject = require('${styleInjectFile}')`,
113+
`const styles = ${styles ? JSON.stringify(styles) : null};`,
114+
`${epxortDelc} {`,
115+
` render: ${render},`,
116+
` components: ${components},`,
117+
` staticRenderFns: ${staticRenderFns},`,
118+
` mounted () {`,
119+
` this.cleanup = styles && styleInject(styles) `,
120+
` },`,
121+
` destroyed () {`,
122+
` this.cleanup && this.cleanup()`,
123+
` }`,
124+
'}'
125+
].join('\n')
126+
}
107127

108128
callback(null, file)
109129
}

0 commit comments

Comments
 (0)