Skip to content

Commit

Permalink
feat(core): add typescript syntax support in SFS script and script setup
Browse files Browse the repository at this point in the history
closes #156
closes #151
closes #131
  • Loading branch information
Franck Freiburger committed Jan 8, 2024
1 parent 34f614d commit 9722616
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 178 deletions.
2 changes: 2 additions & 0 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ ${ pkg.name } v${ pkg.version } for vue${ vueTarget }
'@vue/compiler-sfc$': require.resolve('@vue/compiler-sfc'),
'@vue/compiler-dom$': require.resolve('@vue/compiler-dom'),

'@babel/plugin-transform-typescript$': require.resolve('@babel/plugin-transform-typescript'),

// not needed
'consolidate': false,
'@vue/compiler-ssr': false,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@babel/plugin-transform-arrow-functions": "^7.16.7",
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
"@babel/plugin-transform-runtime": "^7.16.10",
"@babel/plugin-transform-typescript": "^7.23.6",
"@babel/preset-env": "^7.16.11",
"@types/babel__code-frame": "^7.0.2",
"@types/babel__core": "^7.1.18",
Expand Down
15 changes: 13 additions & 2 deletions src/createVue3SFCModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
import * as vue_CompilerDOM from '@vue/compiler-dom'

// https://github.com/vuejs/jsx-next
import jsx from '@vue/babel-plugin-jsx'
import babelPlugin_jsx from '@vue/babel-plugin-jsx'
import babelPlugin_typescript from '@babel/plugin-transform-typescript'

import {
formatErrorLineColumn,
Expand Down Expand Up @@ -146,7 +147,17 @@ export async function createSFCModule(source : string, filename : AbstractPath,
if ( compileTemplateOptions !== null )
compileTemplateOptions.compilerOptions.bindingMetadata = scriptBlock.bindings;

return await transformJSCode(scriptBlock.content, true, strFilename, [ ...additionalBabelParserPlugins, 'jsx' ], { ...additionalBabelPlugins, jsx }, log, devMode);
let contextBabelParserPlugins : Options['additionalBabelParserPlugins'] = ['jsx'];
let contextBabelPlugins: Options['additionalBabelPlugins'] = { jsx: babelPlugin_jsx };

if (descriptor.script?.lang === 'ts' || descriptor.scriptSetup?.lang === 'ts') {

contextBabelParserPlugins = [ ...contextBabelParserPlugins, 'typescript' ];
contextBabelPlugins = { ...contextBabelPlugins, typescript: babelPlugin_typescript };
}


return await transformJSCode(scriptBlock.content, true, strFilename, [ ...contextBabelParserPlugins, ...additionalBabelParserPlugins ], { ...contextBabelPlugins, ...additionalBabelPlugins }, log, devMode);

});

Expand Down
Loading

0 comments on commit 9722616

Please sign in to comment.