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!
This is a small sample repository that uses Babel to transform TypeScript to plain JavaScript, and uses TypeScript for type-checking. This README will also explain step-by-step how you can set up this repository so you can understand how each component fits together.
For simplicity, we've used babel-cli with a bare-bones TypeScript setup, but we'll also demonstrate integration with JSX/React, as well as adding Webpack into the mix.
npm run buildnpm run type-checkAnd to run in --watch mode:
npm run type-check -- --watchEither run the following:
npm install --save-dev typescript@2.7.2
npm install --save-dev @babel/core@7.0.0-beta.44
npm install --save-dev @babel/cli@7.0.0-beta.44
npm install --save-dev @babel/plugin-proposal-class-properties@7.0.0-beta.44
npm install --save-dev @babel/plugin-proposal-object-rest-spread@7.0.0-beta.44
npm install --save-dev @babel/preset-env@7.0.0-beta.44
npm install --save-dev @babel/preset-typescript@7.0.0-beta.44or make sure that you add the appropriate "devDependencies" entries to your package.json and run npm install:
"devDependencies": {
"@babel/cli": "^7.0.0-beta.44",
"@babel/core": "^7.0.0-beta.44",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.44",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.44",
"@babel/preset-env": "^7.0.0-beta.44",
"@babel/preset-typescript": "^7.0.0-beta.44",
"typescript": "^2.8.1"
}Then run
tsc --init --declaration --allowSyntheticDefaultImports --target esnext --outDir libThen copy the .babelrc in this repo, or the below:
{
"presets": [
"@babel/env",
"@babel/typescript"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}Add the following to the "scripts" section of your package.json
"scripts": {
"build": "tsc --emitDeclarationOnly && babel src --out-dir lib --extensions \".ts,.tsx\"",
"type-check": "tsc --noEmit"
}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/preset-react@7.0.0-beta.44Then add "@babel/react" as one of the presets in your .babelrc.
Update your tsconfig.json to set "jsx" to "react".
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 lines to the bottom:
import React from 'react';
export let z = <div>Hello world!</div>;npm install --save-dev webpack babel-loader@8.0.0-beta.2Create 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',
}],
}
};Add
"bundle": "webpack"to the scripts section in your package.json.
npm run bundle