• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

DanielRosenwasser/babel-typescript-sample: A sample setup using Babel CLI to bui ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

DanielRosenwasser/babel-typescript-sample

开源软件地址:

https://github.com/DanielRosenwasser/babel-typescript-sample

开源编程语言:

TypeScript 100.0%

开源软件介绍:

Note: This quick start repository uses a pre-release version of Babel. While you should be aware that some of the dependencies here are in flux, feel free to try it out!

What is this?

This is a small sample repository that uses Babel to transform TypeScript to plain JavaScript, and uses TypeScript for type-checking. For simplicity, it uses babel-cli.

How do I use it?

Building the repo

npm run build

Type-checking the repo

npm run type-check

And to run in --watch mode:

npm run type-check -- --watch

How would I set this up myself?

Install your dependencies

Either run the following:

npm install --save-dev [email protected]
npm install --save-dev @babel/[email protected]
npm install --save-dev @babel/[email protected]
npm install --save-dev @babel/[email protected]
npm install --save-dev @babel/[email protected]
npm install --save-dev @babel/[email protected]
npm install --save-dev @babel/[email protected]

or make sure that you add the appropriate "devDependencies" entries to your package.json and run npm install:

"devDependencies": {
    "@babel/cli": "^7.0.0-beta.32",
    "@babel/core": "^7.0.0-beta.32",
    "@babel/plugin-proposal-class-properties": "^7.0.0-beta.32",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.32",
    "@babel/preset-env": "^7.0.0-beta.32",
    "@babel/preset-typescript": "^7.0.0-beta.32",
    "typescript": "^2.6.2"
}

Create your tsconfig.json

Then run

tsc --init --noEmit --allowJs --allowSyntheticDefaultImports --target esnext

Create your .babelrc

Then copy the .babelrc in this repo, or the below:

{
    "presets": [
        "@babel/env",
        "@babel/typescript"
    ],
    "plugins": [
        "@babel/proposal-class-properties",
        "@babel/proposal-object-rest-spread"
    ]
}

Set up your build tasks

Add the following to the "scripts" section of your package.json

"scripts": {
    "build": "babel src --out-dir lib --extensions \".ts,.tsx\"",
    "type-check": "tsc"
}

How do I change it?

Using JSX (and React)

Install your dependencies

Install the @babel/preset-react package as well as React, ReactDOM, and their respective type declarations

npm install --save react react-dom @types/react @types/react-dom
npm install --save-dev @babel/[email protected]

Update .babelrc

Then add "react" as one of the presets in your .babelrc.

Update tsconfig.json

Update your tsconfig.json to set "jsx" to "react".

Use a .tsx file

Make sure that any files that contain JSX use the .tsx extension. To get going quickly, just rename src/index.ts to src/index.tsx, and add the following line to the bottom:

export let z = <div>Hello world!</div>;

Mixing .js and .ts

Set the "allowJs" compiler option to true in tsconfig.json.

Using Webpack

Install your dependencies

npm install --save-dev webpack babel-loader

Create a webpack.config.js

Create a webpack.config.js at the root of this project with the following contents:

var path = require('path');

module.exports = {
    // Change to your "entry-point".
    entry: './src/index',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.bundle.js'
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [{
            // Include ts, tsx, and js files.
            test: /\.(tsx?)|(js)$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
        }],
    }
};

Create a build task

Add

"bundle": "webpack"

to the scripts section in your package.json.

Run the build task

npm run bundle



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap