一、项目初始化

  1. npm init -y

二、安装依赖

  1. cnpm i -D webpack webpack-cli typescript ts-loader

三、编写webpack.config.js

  1. // 引入一个包
  2. const path = require("path")
  3. // webpack中的所有配置信息都应该写在module.exports中
  4. module.exports = {
  5. // 指定入口文件
  6. entry:"./src/index.ts",
  7. // 指定打包文件所在的目录
  8. output:{
  9. // 指定打包文件的目录
  10. path:path.resolve(__dirname,"dist"),
  11. // 打包后文件的文件名
  12. filename:"bundle.js"
  13. },
  14. // 指定webpack打包时要使用的模块
  15. module:{
  16. // 指定要加载的规则
  17. rules:[
  18. {
  19. // test指定的是规则生效的文件
  20. test:/\.ts$/,
  21. // 要使用的loader
  22. use:'ts-loader',
  23. // 要排除的文件夹
  24. exclude:/node_modules/
  25. }
  26. ]
  27. }
  28. }

四、编写tsconfig.json

  1. {
  2. "compilerOptions": {
  3. "module": "ES2015",
  4. "target": "ES2015",
  5. }
  6. }

五、修改package.json

  1. "scripts": {
  2. ...
  3. "build":"webpack"
  4. },