$ npm install --save-dev webpack webpack-cli yargs
Additional dependencies for typescript users:
$ npm install --save-dev ts-loader typescript @types/yargs
Create src/index.js
:
const yargs = require('yargs')
console.log(yargs.parse())
Or for typescript users, src/index.ts
:
import * as yargs from 'yargs';
console.log(yargs.parse());
along with its tsconfig.json
:
{
"compilerOptions": {
"sourceMap": true
}
}
Create webpack.config.js
:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
stats: {
// Ignore warnings due to yarg's dynamic module loading
warningsFilter: [/node_modules\/yargs/]
},
target: 'node'
}
For typescript users, replace :
module.exports = {
entry: './src/index.js',
...
}
by:
module.exports = {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.ts$/,
use: [
'ts-loader',
]
}
]
},
resolve: {
extensions: ['.ts', '.js'],
},
...
}
$ ./node_modules/.bin/webpack --mode=production
$ rm -rf node_modules
$ node dist/index.js
{ _: [], '$0': 'dist/index.js' }