feat(ref-transform): auto infer parser plugins
This commit is contained in:
parent
0805abe573
commit
6453359852
@ -60,7 +60,11 @@ const {
|
||||
} = transform(src, {
|
||||
filename: 'foo.ts',
|
||||
sourceMap: true,
|
||||
parserPlugins: ['typescript']
|
||||
|
||||
// @babel/parser plugins to enable.
|
||||
// 'typescript' and 'jsx' will be auto-inferred from filename if provided,
|
||||
// so in most cases explicit parserPlugins are not necessary
|
||||
parserPlugins: [/* ... */]
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -28,10 +28,11 @@
|
||||
},
|
||||
"homepage": "https://github.com/vuejs/vue-next/tree/dev/packages/ref-transform#readme",
|
||||
"dependencies": {
|
||||
"@babel/parser": "^7.15.0",
|
||||
"@vue/compiler-core": "3.2.4",
|
||||
"@vue/shared": "3.2.4",
|
||||
"@babel/parser": "^7.15.0",
|
||||
"estree-walker": "^2.0.2"
|
||||
"estree-walker": "^2.0.2",
|
||||
"magic-string": "^0.25.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.15.0"
|
||||
|
@ -63,9 +63,19 @@ export function transform(
|
||||
importHelpersFrom = 'vue'
|
||||
}: RefTransformOptions = {}
|
||||
): RefTransformResults {
|
||||
const plugins: ParserPlugin[] = parserPlugins || []
|
||||
if (filename) {
|
||||
if (/\.tsx?$/.test(filename)) {
|
||||
plugins.push('typescript')
|
||||
}
|
||||
if (filename.endsWith('x')) {
|
||||
plugins.push('jsx')
|
||||
}
|
||||
}
|
||||
|
||||
const ast = parse(src, {
|
||||
sourceType: 'module',
|
||||
plugins: [...babelParserDefaultPlugins, ...(parserPlugins || [])]
|
||||
plugins: [...new Set([...babelParserDefaultPlugins, ...plugins])]
|
||||
})
|
||||
const s = new MagicString(src)
|
||||
const res = transformAST(ast, s)
|
||||
|
Loading…
Reference in New Issue
Block a user