forked from yargs/yargs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: document how to bundle yargs with webpack/ncc (yargs#1757)
- Loading branch information
Showing
4 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## Bundling yargs | ||
|
||
This document outlines how to bundle your libraries that use yargs into | ||
standalone distributions. | ||
|
||
### You might not need to bundle | ||
|
||
Newer releases of yargs can run directly in modern browsers, take a look at | ||
[Running yargs in the browser](https://github.com/yargs/yargs/blob/master/docs/browser.md). | ||
|
||
## ncc | ||
|
||
If you are targetting Node.js with your bundle, we recommend using | ||
[`@vercel/ncc`](https://www.npmjs.com/package/@vercel/ncc). | ||
|
||
Given a CommonJS file, **index.js**: | ||
|
||
```js | ||
const yargs = require('yargs') | ||
const chalk = require('chalk') | ||
yargs | ||
.option('awesome-opt', { | ||
describe: `my awesome ${chalk.green('option')}` | ||
}) | ||
.parse() | ||
``` | ||
|
||
You can simply run: `ncc build index.js`. | ||
|
||
### Webpack | ||
|
||
Given a CommonJS file, **index.js**: | ||
|
||
```js | ||
const yargs = require('yargs') | ||
const chalk = require('chalk') | ||
yargs | ||
.option('awesome-opt', { | ||
describe: `my awesome ${chalk.green('option')}` | ||
}) | ||
.parse() | ||
``` | ||
|
||
You can create a CommonJS bundle with the following `webpack.config.js`: | ||
|
||
```js | ||
module.exports = { | ||
mode: "development", | ||
entry: { | ||
index: "./index.js", | ||
}, | ||
output: { | ||
filename: './index.js' | ||
}, | ||
resolve: { | ||
extensions: ['.js', '.cjs', '.json'] | ||
}, | ||
target: 'node', | ||
devtool: "source-map-inline", | ||
externals: { | ||
'cliui': 'commonjs2 cliui', | ||
'y18n': 'commonjs2 y18n', | ||
'yargs-parser': 'commonjs2 yargs-parser', | ||
}, | ||
}; | ||
``` |