步骤:
- 下载安装webpack
"webpack","webpack-cli"
npm install webpack webpack-cli --save-dev
// @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,即可得如下内容
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。