如何从单体Node TypeScript仓库构建独立打包脚本?
问题描述
我有一个单体Node TypeScript项目,项目内包含scripts文件夹,其中每个文件作为独立服务/脚本使用,且共享公共库、方法与类。例如,我有一个在多文件中复用的HttpRequest类,其内部基于axios库实现。
需求是:开发阶段保持单体结构以减少代码冗余,但编译项目到/dist文件夹时,希望scripts文件夹中的每个文件都成为可独立执行的单文件打包JavaScript,即每个构建后的文件包含运行所需的全部依赖。
曾尝试使用Webpack,但感觉它要么过于复杂,要么不匹配我的特定需求,想找更简单或更合适的实现方式。
示例文件夹结构
> scripts/ -------->test1.ts -------->test2.ts -------->test3.ts > shared/ -------->shared1.ts -------->shared2.ts -------->httpRequestAgnostic.ts
测试用Webpack配置
import path from "path"; import TsconfigPathsPlugin from "tsconfig-paths-webpack-plugin"; import TerserPlugin from "terser-webpack-plugin"; export default { mode: "production", target: "node", entry: { test1: "./src/scripts/test1.ts", test2: "./src/scripts/test2.ts", test3: "./src/scripts/test3.ts", }, module: { rules: [ { test: /\.tsx?$/, use: [ { loader: "ts-loader", options: { transpileOnly: true, }, }, ], exclude: /node_modules/, }, ], }, resolve: { extensions: [".tsx", ".ts", ".js"], plugins: [new TsconfigPathsPlugin({ configFile: "./tsconfig.json" })], }, optimization: { minimize: true, minimizer: [ new TerserPlugin({ terserOptions: { format: { comments: false, }, }, extractComments: false, }), ], }, experiments: { outputModule: true, }, output: { module: true, filename: "[name].js", path: path.resolve("microservices"), library: { type: "module", }, chunkFormat: "module", clean: true, }, };
推荐方案
1. 使用tsup(极简零配置)
tsup基于esbuild打造,是专门针对TypeScript项目的打包工具,配置门槛极低,完全满足你的需求。
操作步骤:
- 安装依赖:
npm install tsup --save-dev - 在
package.json中添加构建脚本:{ "scripts": { "build": "tsup src/scripts/*.ts --format cjs --out-dir dist --clean" } } - 执行构建:
npm run build
关键参数说明:
--format cjs:输出Node.js兼容的CommonJS格式,若需ES模块可替换为--format esm--out-dir dist:指定输出目录为dist--clean:构建前自动清空输出目录- tsup会自动处理TypeScript编译、依赖打包,每个
scripts下的文件都会被打包成包含所有依赖(shared代码、axios等第三方库)的独立单文件。
2. 使用esbuild(速度优先,灵活可控)
esbuild是目前最快的JavaScript/TypeScript打包工具,配置简单且可定制化程度高。
操作步骤:
- 安装依赖:
npm install esbuild --save-dev - 创建构建脚本
build.js:const esbuild = require('esbuild'); const fs = require('fs'); const path = require('path'); // 自动获取scripts目录下的所有ts文件作为入口 const scriptsDir = path.join(__dirname, 'src', 'scripts'); const entryPoints = fs.readdirSync(scriptsDir) .filter(file => file.endsWith('.ts')) .map(file => path.join(scriptsDir, file)); esbuild.build({ entryPoints: entryPoints, bundle: true, // 开启打包,整合所有依赖 platform: 'node', // 目标平台为Node.js outdir: 'dist', // 输出目录 format: 'cjs', // 输出CommonJS格式 minify: true, // 开启代码压缩 sourcemap: false, // 无需sourcemap可关闭 clean: true, // 构建前清空输出目录 }).catch(() => process.exit(1)); - 在
package.json中添加脚本:{ "scripts": { "build": "node build.js" } } - 执行构建:
npm run build
3. 使用pkg(打包为二进制可执行文件,可选)
如果需要生成无需Node.js环境即可运行的独立可执行文件,可搭配pkg工具。
操作步骤:
- 先通过tsup或esbuild完成TypeScript到JavaScript的打包(得到
dist目录下的单文件) - 安装pkg:
npm install pkg --save-dev - 在
package.json中添加配置与脚本:{ "scripts": { "build:bin": "pkg dist/*.js --out-path dist/bin" }, "pkg": { "targets": ["node18-linux-x64", "node18-win-x64", "node18-macos-x64"] // 指定目标平台 } } - 执行二进制打包:
npm run build:bin
内容的提问来源于stack exchange,提问作者N4pst3r
相关产品推荐
相关产品推荐

