webpack打包純ts項目

場景

當(dāng)我們單純用ts開發(fā)一個公有庫時,沒有了腳手架的幫助,我們需要借助webpack完成該ts項目的打包。
核心要點:

  1. 將ts編譯成js
  2. 對打包進行自定義配置

依賴包

  • webapck(打包工具,必須)
  • ts-loader(將ts編譯成js的loader,必須)
  • ts-lint(檢測ts代碼是否規(guī)范,非必須)
  • clean-webpack-plugin(每次打包時,刪除上次打包的殘留文件,保證打出的包整潔,非必須)

tsconfig.json配置

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/",// 打包到的目錄
    "sourceMap": true,// 是否生成sourceMap(用于瀏覽器調(diào)試)
    "noImplicitAny": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "declaration": true,// 是否生成聲明文件
    "declarationDir": "./dist/types/",// 聲明文件打包的位置
    "declarationMap": true,// 是否生成聲明文件map文件(便于調(diào)試)
    "moduleResolution": "node",
    "module": "esnext",
    "target": "es5",// 轉(zhuǎn)化成的目標(biāo)語言
    "baseUrl": "./",
    "types": [
      "node",
      "jest"
    ],
    "typeRoots": [
      "./node_modules/@types"
    ],
    "lib": [
      "dom",
      "es2015"
    ],
    "jsx": "react",
    "allowJs": false
  },
  "include": [
    "src/**/*.ts"
  ],// 要打包的文件
  "exclude": [
    "node_modules",
    "*.test.ts"
  ]
}

webpack.config.js

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');

// the path(s) that should be cleaned
let pathsToClean = ['dist'];

// the clean options to use
let cleanOptions = {
    root: path.resolve(__dirname),
    // exclude: ['shared.js'],
    verbose: true,
    dry: false,
};

module.exports = {
    resolve: {
        extensions: ['.js', '.ts', '.json'],
    },
    devtool: 'source-map',// 打包出的js文件是否生成map文件(方便瀏覽器調(diào)試)
    mode: 'production',
    entry: {
        ‘my-ts': './src/index.ts',
    },
    output: {
        filename: '[name].js',// 生成的fiename需要與package.json中的main一致
        path: path.resolve(__dirname, 'dist'),
        libraryTarget: 'commonjs',
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: [
                    {
                        loader: 'tslint-loader',
                        options: {
                            configFile: path.resolve(__dirname, './tslint.json'),
                        },
                    },
                ],
                exclude: /node_modules/,
            },
            {
                test: /\.tsx?$/,
                use: [
                    {
                        loader: 'ts-loader',
                        options: {
                            // 指定特定的ts編譯配置,為了區(qū)分腳本的ts配置
                            configFile: path.resolve(__dirname, './tsconfig.json'),
                        },
                    },
                ],
                exclude: /node_modules/,
            },
        ],
    },
    plugins: [new CleanWebpackPlugin(pathsToClean, cleanOptions)],
};

package.json

{
  "name": "my-ts-project",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/my-ts.js",// 需要與webpack打包出來的內(nèi)容一致
  "types": "./dist/types/index.d.ts",// 需要與types文件的路徑一致
  "private": false,
  "publisher": "wbjqiqi@163.com",
  "scripts": {
    "build": "tsc",
    "test": "jest"
  },
  "files": [
    "dist"
  ],// 最終發(fā)布到npm上時提交的內(nèi)容
  "repository": {
    "type": "",
    "url": ""
  },
  "keywords": [
    "my-ts"
  ],
  "author": "wbjqiqi@163.com",
  "license": "MIT",
  "devDependencies": {
    "@types/jest": "^23.3.1",
    "@types/node": "^10.5.5",
    "clean-webpack-plugin": "^1.0.1",
    "jest": "^23.4.2",
    "prettier": "^1.16.4",// 優(yōu)化代碼格式
    "ts-jest": "^23.0.1",
    "ts-lint": "^4.5.1",
    "ts-loader": "^5.3.3",
    "tslint": "^5.11.0",
    "tslint-loader": "^3.5.4",
    "typescript": "^3.0.1",
    "webpack": "^4.28.1"
  }
}

最終執(zhí)行的時候有兩個方式

  1. 單純的把ts編譯成js ------ 【tsc】
  2. 編譯成js后打包壓縮 ----- 【npm webpack】執(zhí)行webpack.config.js,webpack調(diào)用ts-loader使用tsconfig.json的配置編譯成js后生成bundle文件
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容