# Chatopera Bless

Node.js packaging utility to bundle javascript and resource files.

<img src="https://gitlab.chatopera.com/hain/npk/uploads/b98afc1da7590806d26152f1625cfc98/image.png" width="500" />

_source:_ [nodejs-binary](https://www.slideshare.net/cfsghost/nodejs-binary)

# Usage

## Install

via npmjs registry

https://www.npmjs.com/package/@chatopera/bless

```
npm install -g nan @chatopera/bless
```

Use below command to verifiy installation.

```
Usage: dist [options]

Options:
  -p, --projectdir <value>  Project Path, *required
  -l, --level [value]       Mangling Level [union|minify|binary], optional, default binary
  --clean [value]           Clean up build files after generation, optional, default true
  -h, --help                output usage information
```

## Project Structure

To package your project's source code to binary, define `bless.json` under the project base dir, imagine the project has a structure like this.

```
purejs
├── main.js
├── bless.json
├── data
│   └── data.txt
├── lib
│   ├── a.js
│   ├── b.js
│   └── c.js
└── package.json
```

Note, `purejs` is basedir, `main.js` is main entry, `bless.json` is configuration for `bless` toolset.

## Configure

A sample configuration file `bless.json`.

```
{
  "dist": {
    "sources": ["main.js", "lib/**/*.js"],
    "data": ["data/**/*", "data/**/.*"]
  }
}
```

`dist` is for target name, it is used as directory name to store Release files under `bless-gen`.

`sources` is a bunch of java script sources, the first element is for main entry of this application/library.

`data` is the assets to copy into `dist` folder.

## Build

```
bless -p purejs
```

After the generation is done, check out `purejs/bless-gen/$TARGET_NAME` for the release file.

Note, during the generation, some `build files` are generated first for compiling and linking, then package the `sources` as `binary`, `build files` are deleted by default. In order to keep these files, `--clean false` should be present, use command like `bless dist --clean false -p $PROJRCT_PATH`.

There are three levels to hide the source codes, specify `--level` with `union`, `minify` or `binary`.

- `union`, make all source code into a solitary javascript file, the names for variables are not changed.

- `minify`, make all source code into a solitary javascript file, the codes are uglified and minified.

- `binary`, make all source code into a binary file, the codes are uglified and minified.

Checkout [sample script](./admin/dev.sh).

## Run

```
cd ${TARGET_NAME}/bless-gen/dist
npm i
node app.js
```

## Test

```
app/tests/complex
app/tests/purejs
```

## Known Issues

### empty folder in `data` is not copyed

empty folder in `data` section would be omitted.

### require.main is not assigned value in main entry

So, this piece of code does not work to check main entry .

```
// on main entry
if (require.main === module) {
  (async function() {
    await main();
  })();
}
```

# Give credits to

- [npk](https://github.com/cfsghost/npk)

# License

Copyright 2019 <a href="https://www.chatopera.com/" target="_blank">北京华夏春松科技有限公司</a>. All rights reserved.

This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.

[![chatoper banner][co-banner-image]][co-url]

[co-banner-image]: https://user-images.githubusercontent.com/3538629/42383104-da925942-8168-11e8-8195-868d5fcec170.png
[co-url]: https://www.chatopera.com
