使用webpack,编译前端ts项目
作者:互联网
步骤:
- 下载安装webpack
"webpack","webpack-cli"
npm install webpack webpack-cli --save-dev
-
下载ts-loader和html-webpack-plugin插件,供webpack编译使用
-
创建webpack.config.js文件,内容如下:
// @ts-ignore
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: ['./src/main.ts'],
output: {
clean: true,
path: __dirname + '/build',
filename: './[fullhash]bundle.js',
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"]
},
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.tsx?$/, loader: "ts-loader" }
]
},
plugins: [new HtmlWebpackPlugin(
{
template: 'public/index.html',
filename: 'index.html'
}
)]
}
- 在package.json 上添加启动打包命令
{
"scripts": {
"dev:webpack": "./node_modules/.bin/webpack"
}
}
- main.ts上编写ts内容如下:
let itemName = 'hello typeScript'
let foo = function(name: string){
console.log(name)
setTimeout(()=>{
document.write('hello ts')
},500)
}
foo(itemName)
- 运行打包命令,完成打包,打开打包后的html,即可得如下内容
标签:ts,loader,编译,webpack,html,tsx,js 来源: https://www.cnblogs.com/chenlajiao/p/16024255.html