This document outlines how to bundle your libraries that use yargs into standalone distributions.
Newer releases of yargs can run directly in modern browsers, take a look at Running yargs in the browser.
If you are targeting Node.js with your bundle, we recommend using
@vercel/ncc
.
Given a CommonJS file, index.js:
const yargs = require('yargs/yargs')
const chalk = require('chalk')
require('yargs/yargs')(process.argv.slice(2))
.option('awesome-opt', {
describe: `my awesome ${chalk.green('option')}`
})
.parse()
You can simply run: ncc build index.js
.
The following is tested with webpack 5.x
In a new project (see: npm-init):
npm install yargs assert path-browserify
.- Create the following index.js:
const yargs = require('yargs/yargs') require('yargs/yargs')(process.argv.slice(2)) .option('awesome-opt', { describe: `my awesome option` }) .parse()
- Create the following webpack.config.js:
const path = require('path'); module.exports = { mode: 'development', devtool: 'source-map', entry: './index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, resolve: { fallback: { assert: require.resolve("assert"), fs: false, path: require.resolve("path-browserify") }, }, };
- Run:
npx webpack@5 --config webpack.config.js
. - You can now execute
dist/bundle.js
.