Skip to content

Commit

Permalink
Can customize which babelParserPlugins to use
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang committed Oct 15, 2018
1 parent 35c060f commit 7e495f5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function getConfig (flags) {
return config
}

cli.command('*', 'vuese cli', async (input, flags) => {
cli.command('*', 'vuese cli', () => {
cli.showHelp()
})

Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = async (config) => {
include,
exclude,
outDir,
markdownDir
markdownDir,
babelParserPlugins
} = config

if (typeof include === 'string') include = [include]
Expand All @@ -26,7 +27,7 @@ module.exports = async (config) => {
return files.map(async p => {
const abs = path.resolve(p)
const source = await majo.fs.readFile(abs, 'utf-8')
const parserRes = parser(source)
const parserRes = parser(source, { babelParserPlugins })
const r = new Render(parserRes)
const renderRes = r.render()
let str = ''
Expand Down
7 changes: 5 additions & 2 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sfcToAST from './sfcToAST'
import parseJavascript from './parseJavascript'
import parseTemplate from './parseTemplate'
import { ParserPlugin } from '@babel/parser'

export type PropType = string | string[] | null

Expand Down Expand Up @@ -55,6 +56,7 @@ export interface ParserOptions {
onName?: {
(name: string): any
}
babelParserPlugins?: ParserPlugin[]
}

export interface ParserResult {
Expand All @@ -69,7 +71,7 @@ export default function(
source: string,
options: ParserOptions = {}
): ParserResult {
const astRes = sfcToAST(source)
const astRes = sfcToAST(source, options.babelParserPlugins)
const res: ParserResult = {}
const defaultOptions: ParserOptions = {
onName(name: string) {
Expand All @@ -94,7 +96,8 @@ export default function(
if (methodRes) {
;(res.methods || (res.methods = [])).push(methodRes)
}
}
},
babelParserPlugins: ['objectRestSpread', 'dynamicImport']
}

const finallyOptions: ParserOptions = Object.assign(defaultOptions, options)
Expand Down
10 changes: 7 additions & 3 deletions src/parser/sfcToAST.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { parse as vueSFCParse } from '@vue/component-compiler-utils'
import * as vueTemplateCompiler from 'vue-template-compiler'
import { parse as babelParse } from '@babel/parser'
import { parse as babelParse, ParserPlugin } from '@babel/parser'

export interface AstResult {
jsAst?: object
templateAst?: object
}

export default function(source: string): AstResult {
export default function(
source: string,
babelParserPlugins?: ParserPlugin[]
): AstResult {
const sfc = vueSFCParse({
source: source,
compiler: vueTemplateCompiler,
Expand All @@ -16,7 +19,8 @@ export default function(source: string): AstResult {
const res: AstResult = {}
if (sfc.script && sfc.script.content) {
res.jsAst = babelParse(sfc.script.content, {
sourceType: 'module'
sourceType: 'module',
plugins: babelParserPlugins || ['objectRestSpread', 'dynamicImport']
})
}
if (sfc.template && sfc.template.content) {
Expand Down

0 comments on commit 7e495f5

Please sign in to comment.