ncc 是一个简单的 CLI,可以将 Node.js 项目编译为单个文件,包括项目的依赖与 gcc-style。
go
npm i -g @zeit/ncc
$ ncc build input.js -o dist
输出的Node.js的身体结实input.js成dist/index.js。
input.js
dist/index.js
对于测试和调试,可以将文件构建到临时目录中,并使用以下命令执行完整的源映射支持:
$ ncc run input.js
唯一的要求是指ncc到.ts或.tsx文件。一个tsconfig.json 文件是必要的。您很可能希望表明es2015支持:
ncc
.ts
.tsx
tsconfig.json
es2015
{ "compilerOptions": { "target": "es2015", "moduleResolution": "node" } }
require('@zeit/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"], minify: false, // default sourceMap: 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 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) })
当watch: true设置,构建产物不是一个promise,但具有以下特征:
watch: true
{ // 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 (); }