Jumpstart with @parcel-bundler, typescript, react and @ant-design
# Clone the repo
git clone --depth=1 https://github.com/umstek/parcel-typescript-react-antd.git you_project_name
# Remove the .git directory
rm -rf !$/.git
yarn
yarn start
You know the de-facto UI library for the web is React, and you want to use a beautiful component library such as Ant Design to jump start development rather than building everything from scratch. Also you know that if you were to use TypeScript instead of JavaScript, your application will be easy to maintain and there will be less pain caused by undefined
things here and there. You also want to load modules dynamically, minimize the bundle size, and maybe install many other libraries in the future.
Although Ant-Design is mainly a React library and it works fine with TypeScript, there is a slight problem with the ant-design suggested babel-plugin-import
, which is used to load only the necessary CSS without bloating your app, because, obviously, this is a babel plugin and you are using typescript. Yes, you can configure babel to work with typescript and ant design, configure webpack to do bundling etc. But configuring everything manually is a pain, so you use CRA, and lose the flexibility. If you eject, you are on your own; and if you use other hacks, things get complicated.
Or you can just use Parcel, that’s why.
yarn init
and answer the questions.yarn add --dev parcel-bundler
to install parcel bundler.package.json
.
"scripts": {
"start": "parcel index.html --open",
"build": "parcel build index.html",
"clean": "rm -rf .cache dist"
}
index.html
file and add basic html5 template using vscode html:5
snippet.<div id="app-root"></div>
in the body.<script src="src/index.tsx"></script>
.Create tsconfig.json
and add the following.
{
"compilerOptions": {
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom"],
"moduleResolution": "node",
"rootDir": "src",
"jsx": "react",
"allowSyntheticDefaultImports": true
},
"exclude": ["node_modules", "dist"]
}
Create .babelrc
and add the below.
{
"presets": ["@babel/preset-env"],
"plugins": [
[
"import",
{
"libraryName": "antd",
"libraryDirectory": "es",
"style": "css"
}
]
]
}
Create the src
folder and add index.tsx
file.
import * as React from "react";
import { render } from "react-dom";
import { Typography } from "antd";
const App = () => (
<div>
<Typography.Title level={1}>Hello, world!</Typography.Title>
</div>
);
render(<App ></App>, document.getElementById("app-root"));
Run yarn start
. If that fails, cancel and run yarn start
again.