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' } )] }
{ "scripts": { "dev:webpack": "./node_modules/.bin/webpack" } }
let itemName = 'hello typeScript' let foo = function(name: string){ console.log(name) setTimeout(()=>{ document.write('hello ts') },500) } foo(itemName)