Skip to content

Latest commit

 

History

History
103 lines (82 loc) · 1.4 KB

webpack.md

File metadata and controls

103 lines (82 loc) · 1.4 KB

Webpack usage examples

Install dependencies

$ npm install --save-dev webpack webpack-cli yargs

Additional dependencies for typescript users:

$ npm install --save-dev ts-loader typescript @types/yargs

Sample program

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
  }
}

Webpack configuration

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'],
  },
  ...
}

Build

$ ./node_modules/.bin/webpack --mode=production

Run

$ rm -rf node_modules
$ node dist/index.js
{ _: [], '$0': 'dist/index.js' }