File: README.md
ncc
Simple CLI for compiling a Node.js module into a single file,
together with all its dependencies, gcc-style.
Motivation
- Publish minimal packages to npm
- Only ship relevant app code to serverless environments
- Don't waste time configuring bundlers
- Generally faster bootup time and less I/O overhead
- Compiled language-like experience (e.g.:
go)
Design goals
- Zero configuration
- TypeScript built-in
- Only supports Node.js programs as input / output
- Support all Node.js patterns and npm modules
Usage
Installation
npm i @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.
Outputs the Node.js compact build of input.js into 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.
Commands:
build [opts]
run [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:
{
"compilerOptions": {
"target": "es2015",
"moduleResolution": "node"
}
}If typescript is found in devDependencies, that version will be used.
Package Support
Some packages may need some extra options for ncc support in order to better work with the static analysis.
See package-support.md for some common packages and their usage with ncc.
Programmatically From 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:
{
// 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 ();
}Caveats
- Files / assets are relocated based on a static evaluator. Dynamic non-statically analyzable asset loads may not work out correctly
File: Readme.md
ncc
Simple CLI for compiling a Node.js module into a single file,
together with all its dependencies, gcc-style.
Motivation
- Publish minimal packages to npm
- Only ship relevant app code to serverless environments
- Don't waste time configuring bundlers
- Generally faster bootup time and less I/O overhead
- Compiled language-like experience (e.g.:
go)
Design goals
- Zero configuration
- TypeScript built-in
- Only supports Node.js programs as input / output
- Support all Node.js patterns and npm modules
Usage
Installation
npm i @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.
Outputs the Node.js compact build of input.js into 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.
Commands:
build [opts]
run [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:
{
"compilerOptions": {
"target": "es2015",
"moduleResolution": "node"
}
}If typescript is found in devDependencies, that version will be used.
Package Support
Some packages may need some extra options for ncc support in order to better work with the static analysis.
See package-support.md for some common packages and their usage with ncc.
Programmatically From 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:
{
// 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 ();
}Caveats
- Files / assets are relocated based on a static evaluator. Dynamic non-statically analyzable asset loads may not work out correctly
File: readme.md
ncc
Simple CLI for compiling a Node.js module into a single file,
together with all its dependencies, gcc-style.
Motivation
- Publish minimal packages to npm
- Only ship relevant app code to serverless environments
- Don't waste time configuring bundlers
- Generally faster bootup time and less I/O overhead
- Compiled language-like experience (e.g.:
go)
Design goals
- Zero configuration
- TypeScript built-in
- Only supports Node.js programs as input / output
- Support all Node.js patterns and npm modules
Usage
Installation
npm i @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.
Outputs the Node.js compact build of input.js into 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.
Commands:
build [opts]
run [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:
{
"compilerOptions": {
"target": "es2015",
"moduleResolution": "node"
}
}If typescript is found in devDependencies, that version will be used.
Package Support
Some packages may need some extra options for ncc support in order to better work with the static analysis.
See package-support.md for some common packages and their usage with ncc.
Programmatically From 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:
{
// 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 ();
}Caveats
- Files / assets are relocated based on a static evaluator. Dynamic non-statically analyzable asset loads may not work out correctly
File: examples/typescript/README.md
TypeScript
How to use
Within this directory, run yarn to install dependencies.
To execute the Node.js program with ncc, run yarn dev.
To build the program with ncc, run yarn build.
File: examples/programmatic/README.md
Programmatic
This example shows how you can programmatically build multiple bundles. The build script (scripts/build.js) finds .js files in src/routes/, builds each one with ncc, and then writes the resulting file and its sourcemap to a new directory in dist/.
How to use
Within this directory, run yarn to install dependencies.
To run a development server with ncc, run yarn dev. You can then test the routes:
To build all of the bundles, run yarn build.
โฏ yarn build
yarn run v1.12.3
$ node ./scripts/build.js
โ dist/dog/index.js (289.17KB)
โ dist/dog/index.map.js (234.37KB)
โ dist/fox/index.js (289.18KB)
โ dist/fox/index.map.js (234.43KB)
โ dist/cat/index.js (289.17KB)
โ dist/cat/index.map.js (234.43KB)
โจ Done in 2.79s.Each subdirectory of dist/ could be deployed to a serverless environment to run independent of each other!
Thanks
Special thanks to random.cat, AdenFlorian/random.dog, and xinitrc-dev/randomfox.ca for their open APIs to make this example more fun and realistic!
File: examples/multiple-bundles/README.md
Multiple Bundles
This example shows how you could build a simple module along with a CLI.
How to use
Within this directory, run yarn to install dependencies.
To build the main module, run yarn build:main. You'll notice the resulting dist/main/index.js only includes main.js and its chalk dependency.
To build the CLI, run yarn build:cli. You'll notice the resulting dist/cli/index.js includes its args dependency in addition to everything required for main.js.
To build both concurrently, run yarn build.
To test the main module, run yarn test. You'll notice it uses a strip-ansi dependency that is not included in any of the builds.
To use the built CLI, run ./dist/cli/index.js color or ./dist/cli/index.js number. If this were published to npm as "lucky", you would be able to run lucky color or lucky number because of the bin declaration in package.json.
File: examples/hello-world/README.md
Hello World
How to use
Within this directory, run yarn to install dependencies.
To execute the Node.js program with ncc, run yarn dev.
To build the program with ncc, run yarn build.