Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 1.44 KB

bundling.md

File metadata and controls

66 lines (53 loc) · 1.44 KB

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.

ncc

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.

Webpack

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 create a CommonJS bundle with the following webpack.config.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',
  },
};