TypeScript+React+webpack简洁框架

创建项目目录

TypeScript文件会放在src文件夹里,通过TypeScript编译器编译,然后经webpack处理,最后生成一个bundle.js文件放在dist目录下。 我们自定义的组件将会放在 src/components文件夹下

初始化工程

npm init

安装依赖包

  1. 首先,确保全局安装了webpack npm install -g webpack
  2. 安装react相关 npm install --save react react-dom @types/react @types/react-dom
  3. 添加开发时依赖awesome-typescript-loadersource-map-loader npm install --save-dev typescript awesome-typescript-loader source-map-loader

添加TypeScript配置文件

项目根路径下增加tsconfig.json

{
	"compilerOptions": {
		"outDir": "./dist/",
		"sourceMap": true,
		"noImplicitAny": true,
		"module": "commonjs",
		"target": "es5",
		"jsx": "react"
	},
	"include": [
		"./src/**/*"
	]
}
复制代码

Demo代码编写

  1. src>component目录下添加greeter.tsx文件
import * as React from 'react';
export interface HelloProps {
	compiler: string;
	framework: string;
}
export class Hello extends React.Component<HelloProps, {}> {
	render() {
		return <h1>Hello from { this.props.compiler } and { this.props.framework } !</h1>;
	}
}
复制代码
  1. src目录下添加index.tsx文件
import * as React from 'react';
import * as ReactDom from 'react-dom';

import { Hello } from './components/greeter';

ReactDom.render(
  <Hello compiler="typescript" framework="react" />,
  document.getElementById('demo')
)
复制代码
  1. 增加一个页面展示组件,根目录下添加index.html文件
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>Hello React!</title>
</head>

<body>
  <div id="demo"></div>

  <!-- Dependencies -->
  <script src="./node_modules/react/umd/react.development.js"></script>
  <script src="./node_modules/react-dom/umd/react-dom.development.js"></script>

  <!-- Main -->
  <script src="./dist/bundle.js"></script>
</body>

</html>
复制代码

webpack配置

工程根目录下创建一个webpack.config.js

module.exports = {
  entry: "./src/index.tsx",
  output: {
    filename: "bundle.js",
    path: __dirname + "/dist"
  },

  // Enable sourcemaps for debugging webpack's output.
  devtool: "source-map",

  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx", ".js", ".json"]
  },

  module: {
    rules: [
      // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
      { test: /\.tsx?$/, loader: "awesome-typescript-loader" },

      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
    ]
  },

  // When importing a module whose path matches one of the following, just
  // assume a corresponding global variable exists and use that instead.
  // This is important because it allows us to avoid bundling all of our
  // dependencies, which allows browsers to cache those libraries between builds.
  externals: {
    "react": "React",
    "react-dom": "ReactDOM"
  },
};
复制代码

构建命令添加

  1. package.json中添加以下内容
...
"scripts": {
    "build": "webpack"
  },
...
复制代码
  1. 运行以下命令进行构建 npm run build
  2. 浏览器打开index.html查看效果

源码地址

转载于:https://juejin.im/post/5c6630c86fb9a049c232f3aa

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值