Skip to content

Commit

Permalink
docs(webpck): add updated docs on webpack (#1900)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Apr 7, 2021
1 parent 4b91c76 commit b71254f
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions docs/bundling.md
Expand Up @@ -29,38 +29,40 @@ You can simply run: `ncc build index.js`.

### Webpack

Given a CommonJS file, **index.js**:
_The following is tested with webpack 5.x_

```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()
```
In a new project (see: [npm-init](https://docs.npmjs.com/cli/v7/commands/npm-init)):

You can create a CommonJS bundle with the following `webpack.config.js`:
1. `npm install yargs assert path-browserify`.
2. Create the following **index.js**:
```js
const yargs = require('yargs/yargs')
require('yargs/yargs')(process.argv.slice(2))
.option('awesome-opt', {
describe: `my awesome option`
})
.parse()
```
3. Create the following **webpack.config.js**:
```js
const path = require('path');

```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',
},
};
```
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")
},
},
};
```
4. Run: `npx webpack@5 --config webpack.config.js`.
5. You can now execute `dist/bundle.js`.

0 comments on commit b71254f

Please sign in to comment.