Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build typescript vue apps with only babel?

How can I transpile and build my typescript vue app with only babel? I used the vue-cli-service extensively but reached a point where I just need a minimal setup, without webpack or anything.

My .babelrc

{
    "presets": ["babel-preset-typescript-vue"],
    "plugins": ["@babel/plugin-transform-typescript"]
}

My package.json dependencies:

"devDependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/plugin-transform-typescript": "^7.11.0",
    "@babel/preset-env": "^7.11.0",
    "babel-loader": "^8.1.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-typescript-vue": "^1.1.1",
    "typescript": "~3.9.3",
    "vue-template-compiler": "^2.6.11"
},
"dependencies": {
    "vue": "^2.6.12",
    "vue-class-component": "^7.2.3",
    "vue-property-decorator": "^8.4.2",
    "vuex": "^3.5.1"
}

My entry main.ts file:

import Vue from 'vue'
import Vuex from 'vuex';
import App from './App.vue'

Vue.config.productionTip = false;
Vue.use(Vuex);
    
new Vue({render: h => h(App)}).$mount('#app');

My App.vue

<script lang="ts">
    import {Component, Vue} from 'vue-property-decorator';

    @Component
    class App extends Vue {}

    export default App;
</script>
    
<template>
    <div class="foobar">Babel worked</div>
</template>

My build script:

babel src/main.ts --out-dir build
like image 326
Asperger Avatar asked Aug 25 '20 11:08

Asperger


People also ask

Can I use TypeScript with Babel?

By using babel's support for TypeScript, you get the ability to work with existing build pipelines and are more likely to have a faster JS emit time because Babel does not type check your code.

Does Vue use Babel?

A default Vue CLI project uses @vue/babel-preset-app, which uses @babel/preset-env and the browserslist config to determine the Polyfills needed for your project.

Do I need TypeScript for Vue?

Vue is written in TypeScript itself and provides first-class TypeScript support. All official Vue packages come with bundled type declarations that should work out-of-the-box.


1 Answers

Unfortunately you cannot build with babel, as it only transpiles languages and it cannot build modules. You will still need some type of bundler to resolve require / import imports. If you don't want giant hefty webpack configs you can look at Rollup or Parcel

If you want to actually just compile you typescript and vue typescript you need to install @babel/preset-typescript @babel/preset-env, include them in .babelrc file.

Then use babel ./src/main.ts --out-dir build --extensions ".ts" or better yet use locally installed babel ./node_modules/.bin/babel ./src/main.ts --out-dir build --extensions ".ts"

like image 66
ColdHands Avatar answered Oct 16 '22 15:10

ColdHands