4
1

More than 5 years have passed since last update.

zeit の ncc コマンドで node.js アプリを 1 ファイルにコンパイルしてみたメモ

Last updated at Posted at 2018-11-19

ncc なるコマンドの存在を知ったので、ただ気になって、いじったメモ。

Install

これで合っているのかはしらない。

git clone https://github.com/zeit/ncc.git
cd ncc
npm install
npm link

Help

ncc --help でヘルプを確認

ncc <input-file> [opts]
  Options:
    -M, --no-minify       Skip output minification
    -o, --out [file]      Output file (defaults to stdout)
    -h, --help            Show help

Usage

Node.js のアプリを用意する。

テストアプリのフォルダを用意して、その中で下記を実行。

npm init
npm install --save express

下記ファイル (index.js) を用意。express の Hello World コードを利用。

const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(3000, () => console.log('Example app listening on port 3000!'))

node index.js を実行して、ブラウザで http://localhost:3000 へアクセスすると Hello World が表示されるはず。

次に、下記コマンドで、コンパイル する。

ncc index.js -o bundle.js

実行!

node bundle.js

node bundle.js で、package.json が無くたって、node_modules が無くたって、動く。必要なのは node の実行環境と bundle.js のみ。

Impression

  • 利用用途が分かっていないw
  • 感触は良い。
  • db コマンドで、未コンパイルとレイテンシ、スループットを比較したが、差は感じられなかった。読み込んでしまえば一緒か。
  • そんなに大きな差では無いが、ファイルサイズが削減される。
  • あと、ファイルをテキストエディタで開くと。

image.png

image.png

以上

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1