Skip to content

Commit

Permalink
add example with multiple bundles (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeatty authored and rauchg committed Dec 31, 2018
1 parent 69b6031 commit ea37034
Show file tree
Hide file tree
Showing 6 changed files with 709 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/multiple-bundles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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`.
17 changes: 17 additions & 0 deletions examples/multiple-bundles/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

const args = require("args");

const { color, number } = require("./main");

args
.command("color", "Receive a lucky color", () => {
console.log(color());
})
.command("number", "Receive a lucky number", () => {
console.log(number());
});

args.parse(process.argv, {
name: "lucky"
});
16 changes: 16 additions & 0 deletions examples/multiple-bundles/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const chalk = require("chalk");

exports.color = function color() {
const color = Math.random() > 0.5 ? "red" : "blue";

return `Your lucky color is ${chalk[color](color)}`;
};

exports.number = function number(min = 0, max = 100) {
min = Math.ceil(min);
max = Math.floor(max);
//The maximum is exclusive and the minimum is inclusive
const num = Math.floor(Math.random() * (max - min)) + min;

return `Your lucky number is ${chalk.bold(num)}`;
};
29 changes: 29 additions & 0 deletions examples/multiple-bundles/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const assert = require("assert");
const chalk = require("chalk");
const stripAnsi = require("strip-ansi");

const { color, number } = require("./main");

function getValue(fn) {
return stripAnsi(
fn()
.split(/\s/)
.pop()
);
}

// color is a function
assert(typeof color === "function");

// color is red or blue for this example
const c = getValue(color);
assert(c === "red" || c === "blue");

// number is a function
assert(typeof number === "function");

// number returns an integer between 0 and 100 by default
const n = getValue(number);
assert(n > 0 && n <= 100);

console.log(chalk.green("Tests pass!"));
25 changes: 25 additions & 0 deletions examples/multiple-bundles/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "multiple-bundles",
"private": true,
"version": "0.0.0",
"license": "MIT",
"main": "./dist/main/index.js",
"bin": {
"lucky": "./dist/cli/index.js"
},
"scripts": {
"build:cli": "ncc build cli.js -o dist/cli",
"build:main": "ncc build main.js -o dist/main",
"build": "concurrently 'npm:build:cli' 'npm:build:main'",
"test": "node main.test.js"
},
"dependencies": {
"args": "^5.0.0",
"chalk": "^2.4.1"
},
"devDependencies": {
"@zeit/ncc": "latest",
"concurrently": "^4.1.0",
"strip-ansi": "^5.0.0"
}
}
Loading

0 comments on commit ea37034

Please sign in to comment.