Webpack:Typescript打包

安装npm

创建项目

npm init -y

安装typescript

# ts-loader为webpack loader,clean-webpack-plugin copy-webpack-plugin为webpack插件
npm install --save-dev typescript ts-loader clean-webpack-plugin copy-webpack-plugin

配置tsconfig.json

{
  "compilerOptions": {
    "outDir": "./build/",
     /* 开启source map,利于调试 */
    "sourceMap": true,
    "module": "commonjs",
    "noImplicitAny": true,
    "target": "es5",
    "allowJs": true
  }
}

安装webpack

npm install webpack webpack-cli --save-dev

配置webpack.config.js

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {
  // 配置多个入口
  entry: {
    BaseInfo: './ts/src/BaseInfo.ts',
    RandomData: '/ts/src/RandomData.ts'
  },
  // 配置source-map,方便调试
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        // 正则:查出所有ts、tsx结尾的文件
        test: /\.tsx?$/,
        // 配置ts-loader
        use: 'ts-loader',
        // 正则:过滤node_modules
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    // clean-webpack-plugin插件,用于每次打包都清理旧的数据
    new CleanWebpackPlugin(),
    // copy-webpack-plugin,把指定的文件夹内容复制到指定目录
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, 'html'),
          to: path.resolve(__dirname, 'build')
        },
      ],
    }),
  ],
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  // 指定出口
  output: {
    filename: '[name]-bundle.js',
    path: path.resolve(__dirname, 'build')
  }
};
posted @ 2020-11-14 15:42  Feng1024  阅读(810)  评论(0编辑  收藏  举报