使用webpack/gulp构建TypeScript项目

2,867 阅读2分钟

总体来看,TypeScript项目构建主要分两步:

  1. ts项目整体转换为js项目
  2. 按常规套路,对该js项目进行打包构建

构建过程中,对ts文件的转换不再使用命令行方式,所以tsc的配置参数,需要通过tsconfig.json文件设置。

初始化 tsconfig.json

tsc --init

之后,我们会在项目目录中得到一个完整冗长的 tsconfig.json 配置文件。这个文件暂且不必改动。

{
  "compilerOptions": {
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,  ...
  }
}

使用 webpack 构建

全局安装 webpack

npm i -g webpack webpack-cli

本地安装 ts-loadertypescript

npm i -D ts-loader typescript

创建webpack.config.js

const path = require('path')
module.exports = {
    mode: 'production',
    entry: {
        main: './index.ts'
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    output: {
        filename: 'webpack-bundle.js',
        path: path.resolve(__dirname, 'dist'),
        libraryTarget: 'commonjs',
    },
    resolve: {
        extensions: ['.ts']
    }
}

运行 webpack

经过上述配置之后,在控制台项目路径内,中直接运行 webpack

% webpack   
Hash: 1c028195d238a71fe1c7
Version: webpack 4.41.3
Time: 726ms
Built at: 2019/12/17 下午2:56:12
   Asset      Size  Chunks             Chunk Names
index.js  1.61 KiB       0  [emitted]  main
Entrypoint main = index.js
[0] ./a.ts 147 bytes {0} [built]
[1] ./b.ts 147 bytes {0} [built]
[2] ./index.ts 318 bytes {0} [built]
[3] ./c.ts 378 bytes {0} [built]

dist 中,生成了一个转换且合并完成的 webpack-bundle.js 文件。

使用 gule 构建

全局安装 gule

npm i -g gulp

本地安装

  • gulp
  • browserify
  • tsify
  • vinyl-source-stream
npm i -D gulp browserify tsify vinyl-source-stream

创建 gulpfile.js 文件

const gulp = require('gulp')
const tsify = require('tsify')
const browserify = require('browserify')
const source = require('vinyl-source-stream')

gulp.task('default', () => {
    return browserify({
        basedir: '.',
        debug: true,
        entries: ['index.ts'],
        cache: {},
        packageCache: {}
    }).plugin(tsify).bundle()
    .pipe(source('gulp-bundle.js'))
    .pipe(gulp.dest('dist'))
})

运行 gulp

经过上述配置之后,在控制台项目路径内,中直接运行 gulp

% gulp
[15:37:30] Using gulpfile ~/ts-learn/bundle/gulpfile.js
[15:37:30] Starting 'default'...
[15:37:32] Finished 'default' after 1.4 s

dist 中,生成了一个转换且合并完成的 gulp-bundle.js 文件。

配置 npm 指令

我们将这两个指令整合到项目指令中:

"scripts": {
    "test": "ts-node test",
    "build-webpack": "webpack",
    "build-gulp": "gulp",
    "build": "npm run build-webpack"
}

这里分别针对 webpack / gulp 添加了构建指令,并将 build 指令设置为默认使用 webpack 构建。


就酱