Node 打包工具 @vercel/ncc
网址: https://github.com/vercel/ncc
Simple CLI for compiling a Node.js module into a single file, together with all its dependencies, gcc-style. 用于将 Node.js 模块及其所有依赖项编译成 gcc 风格的单个文件的简单 CLI。
Motivation
- Publish minimal packages to npm 将最小的包发布到 npm
- Only ship relevant app code to serverless environments 仅将相关应用程序代码发送到无服务器环境
- Don't waste time configuring bundlers 不要浪费时间配置捆绑器
- Generally faster bootup time and less I/O overhead 通常启动时间更快,I/O 开销更少
- Compiled language-like experience (e.g.:
go) 编译类似语言的经验(例如:go)
Design goals
- Zero configuration 零配置
- TypeScript built-in 内置 TypeScript
- Only supports Node.js programs as input / output 仅支持 Node.js 程序作为输入/输出
- Support all Node.js patterns and npm modules 支持所有 Node.js 模式和 npm 模块
Usage
Installation 安装
npm i -g @vercel/nccUsage 用法
ncc <cmd> <opts>Eg: 例如:
ncc build input.js -o distIf building an .mjs or .js module inside a "type": "module" package boundary, an ES module output will be created automatically. 如果在"type": "module"包边界内构建.mjs或.js模块,则会自动创建 ES 模块输出。
Outputs the Node.js compact build of input.js into dist/index.js. 将input.js的 Node.js 紧凑构建输出到dist/index.js中。
Note: If the input file is using a
.cjsextension, then so will the corresponding output file. This is useful for packages that want to use.jsfiles as modules in native Node.js using a"type": "module"in the package.json file. 注意:如果输入文件使用.cjs扩展名,则相应的输出文件也将使用 .cjs 扩展名。这对于想要使用 package.json 文件中的"type": "module"将.js文件用作本机 Node.js 中的模块的包非常有用。
Commands: 命令
build <input-file> [opts]
run <input-file> [opts]
cache clean|dir|size
help
versionOptions: 选项
-o, --out [dir] Output directory for build (defaults to dist)
-m, --minify Minify output
-C, --no-cache Skip build cache population
-s, --source-map Generate source map
-a, --asset-builds Build nested JS assets recursively, useful for
when code is loaded as an asset eg for workers.
--no-source-map-register Skip source-map-register source map support
-e, --external [mod] Skip bundling 'mod'. Can be used many times
-q, --quiet Disable build summaries / non-error outputs
-w, --watch Start a watched build
-t, --transpile-only Use transpileOnly option with the ts-loader
--v8-cache Emit a build using the v8 compile cache
--license [file] Adds a file containing licensing information to the output
--stats-out [file] Emit webpack stats as json to the specified output file
--target [es] ECMAScript target to use for output (default: es2015)
Learn more: https://webpack.js.org/configuration/target
-d, --debug Show debug logsExecution Testing 执行测试
For testing and debugging, a file can be built into a temporary directory and executed with full source maps support with the command: 为了测试和调试,可以将文件构建到临时目录中,并使用以下命令在完整的源映射支持下执行:
ncc run input.jsWith TypeScript 使用打字稿
The only requirement is to point ncc to .ts or .tsx files. A tsconfig.json file is necessary. Most likely you want to indicate es2015 support: 唯一的要求是将ncc指向.ts或.tsx文件。 tsconfig.json文件是必需的。您很可能想表明es2015支持:
{
"compilerOptions": {
"target": "es2015",
"moduleResolution": "node"
}
}If typescript is found in devDependencies, that version will be used. 如果在devDependencies中找到 typescript,则将使用该版本。
Package Support 套餐支持
Some packages may need some extra options for ncc support in order to better work with the static analysis. 某些软件包可能需要一些额外的 ncc 支持选项,以便更好地进行静态分析。
See package-support.md for some common packages and their usage with ncc. 请参阅package-support.md了解一些常见软件包及其与 ncc 的用法。
Programmatically From Node.js 以编程方式从 Node.js
require('@vercel/ncc')('/path/to/input', {
// provide a custom cache path or disable caching
cache: "./custom/cache/path" | false,
// externals to leave as requires of the build
externals: ["externalpackage"],
// directory outside of which never to emit assets
filterAssetBase: process.cwd(), // default
minify: false, // default
sourceMap: false, // default
assetBuilds: false, // default
sourceMapBasePrefix: '../', // default treats sources as output-relative
// when outputting a sourcemap, automatically include
// source-map-support in the output file (increases output by 32kB).
sourceMapRegister: true, // default
watch: false, // default
license: '', // default does not generate a license file
target: 'es2015', // default
v8cache: false, // default
quiet: false, // default
debugLog: false // default
}).then(({ code, map, assets }) => {
console.log(code);
// Assets is an object of asset file names to { source, permissions, symlinks }
// expected relative to the output code (if any)
})When watch: true is set, the build object is not a promise, but has the following signature: 当watch: true设置时,构建对象不是一个 Promise,而是具有以下签名:
{
// handler re-run on each build completion
// watch errors are reported on "err"
handler (({ err, code, map, assets }) => { ... })
// handler re-run on each rebuild start
rebuild (() => {})
// close the watcher
void close ();
}