albertyw/albertyw.com

View on GitHub
app/notes/20190401-0735.md

Summary

Maintainability
Test Coverage
Browserify, Mochify, Nyc, Envify, and Dotenv

browserify-mochify-nyc-envify-dotenv

1554104128

I found that it's possible to mix several javascript libraries together to
generate production and test bundles for browsers.  I was specifically
looking for a way to use

- [browserify](https://github.com/browserify/browserify) - bundling multiple
   javascript files that `require` each other together.
- [envify](https://github.com/hughsk/envify) - Interpolating environment
   variables into javascript code
- [dotenv](https://github.com/motdotla/dotenv) - Setting environment variables
   from a file
- [nyc](https://github.com/istanbuljs/nyc) - An istanbul CLI for getting
   instrumenting code and getting test coverage
- [mochify](https://github.com/mantoni/mochify.js/) - A pipeline to run
   mocha tests in a headless browser with browserify

For a production build with a browserify js script, you can use:

```javascript
const browserify = require('browserify');
require('dotenv').config();

browserify('target.js')
  .transform('envify')
  .bundle()
  .pipe(process.stdout);
```

which will generate a bundled javascript output with dotenv variables
interpolated with envify.  For a test build directly as a shell command, you
can use:

```bash
nyc --require dotenv/config mochify --transform envify
```

which will run in-browser tests after applying browserify and envify (with
dotenv variables).