Skip to content

Commit

Permalink
feat(ref-transform): auto infer parser plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 23, 2021
1 parent 0805abe commit 6453359
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/ref-transform/README.md
Expand Up @@ -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: [/* ... */]
})
```

Expand Down
5 changes: 3 additions & 2 deletions packages/ref-transform/package.json
Expand Up @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion packages/ref-transform/src/index.ts
Expand Up @@ -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)
Expand Down

0 comments on commit 6453359

Please sign in to comment.