Skip to content

Commit

Permalink
Require Node.js 12
Browse files Browse the repository at this point in the history
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
Richienb committed Jun 24, 2022
1 parent 14bceee commit 110208c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 152 deletions.
8 changes: 4 additions & 4 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require("assert")
const assert = require('assert');

assert(true)
assert(true);

console.clear()
console.clear();

module.exports = Buffer.from("Hello World").toString()
module.exports = Buffer.from('Hello World').toString();
102 changes: 50 additions & 52 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
import { MergeExclusive } from "type-fest"
import { Compiler } from "webpack"
import {MergeExclusive} from 'type-fest';
import {Compiler} from 'webpack';

declare namespace NodePolyfillPlugin {
export type Alias =
| "Buffer"
| "console"
| "process"
| "assert"
| "buffer"
| "console"
| "constants"
| "crypto"
| "domain"
| "events"
| "http"
| "https"
| "os"
| "path"
| "punycode"
| "process"
| "querystring"
| "stream"
| "_stream_duplex"
| "_stream_passthrough"
| "_stream_readable"
| "_stream_transform"
| "_stream_writable"
| "string_decoder"
| "sys"
| "timers"
| "tty"
| "url"
| "util"
| "vm"
| "zlib"
export type Alias =
| 'Buffer'
| 'console'
| 'process'
| 'assert'
| 'buffer'
| 'console'
| 'constants'
| 'crypto'
| 'domain'
| 'events'
| 'http'
| 'https'
| 'os'
| 'path'
| 'punycode'
| 'process'
| 'querystring'
| 'stream'
| '_stream_duplex'
| '_stream_passthrough'
| '_stream_readable'
| '_stream_transform'
| '_stream_writable'
| 'string_decoder'
| 'sys'
| 'timers'
| 'tty'
| 'url'
| 'util'
| 'vm'
| 'zlib';

interface IncludeOptions {
/**
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, you can choose to only include certain aliases. For example, you can only have `console` polyfilled.
*/
includeAliases?: readonly Alias[]
}

interface ExcludeOptions {
/**
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, if you don't want a module like `console` to be polyfilled you can specify alises to be skipped here.
*/
excludeAliases?: readonly Alias[]
}
interface IncludeOptions {
/**
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, you can choose to only include certain aliases. For example, you can only have `console` polyfilled.
*/
includeAliases?: readonly Alias[];
}

export type Options = MergeExclusive<IncludeOptions, ExcludeOptions>
interface ExcludeOptions {
/**
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, if you don't want a module like `console` to be polyfilled you can specify alises to be skipped here.
*/
excludeAliases?: readonly Alias[];
}

export type Options = MergeExclusive<IncludeOptions, ExcludeOptions>;

declare class NodePolyfillPlugin {
constructor(options?: NodePolyfillPlugin.Options)
constructor(options?: Options);

apply(compiler: InstanceType<typeof Compiler>): void
apply(compiler: InstanceType<typeof Compiler>): void;
}

export = NodePolyfillPlugin
export = NodePolyfillPlugin;
86 changes: 43 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict"
const { ProvidePlugin } = require("webpack")
const filterObject = require("filter-obj")
'use strict';
const {ProvidePlugin} = require('webpack');
const filterObject = require('filter-obj');

function createAliasFilter({ includeAliases, excludeAliases }) {
function createAliasFilter({includeAliases, excludeAliases}) {
if (includeAliases.length > 0) {
return object => filterObject(object, key => includeAliases.includes(key))
return object => filterObject(object, key => includeAliases.includes(key));
}

return object => filterObject(object, key => !excludeAliases.includes(key))
return object => filterObject(object, key => !excludeAliases.includes(key));
}

module.exports = class NodePolyfillPlugin {
Expand All @@ -16,56 +16,56 @@ module.exports = class NodePolyfillPlugin {
excludeAliases: [],
includeAliases: [],
...options
}
};

if (this.options.includeAliases.length > 0 && this.options.excludeAliases.length > 0) {
throw new Error("excludeAliases and includeAliases are mutually exclusive")
throw new Error('excludeAliases and includeAliases are mutually exclusive');
}
}

apply(compiler) {
const filter = createAliasFilter(this.options)
const filter = createAliasFilter(this.options);

compiler.options.plugins.push(new ProvidePlugin(filter({
Buffer: [require.resolve("buffer/"), "Buffer"],
console: require.resolve("console-browserify"),
process: require.resolve("process/browser")
})))
Buffer: [require.resolve('buffer/'), 'Buffer'],
console: require.resolve('console-browserify'),
process: require.resolve('process/browser')
})));

compiler.options.resolve.fallback = {
...filter({
assert: require.resolve("assert/"),
buffer: require.resolve("buffer/"),
console: require.resolve("console-browserify"),
constants: require.resolve("constants-browserify"),
crypto: require.resolve("crypto-browserify"),
domain: require.resolve("domain-browser"),
events: require.resolve("events/"),
http: require.resolve("stream-http"),
https: require.resolve("https-browserify"),
os: require.resolve("os-browserify/browser"),
path: require.resolve("path-browserify"),
punycode: require.resolve("punycode/"),
process: require.resolve("process/browser"),
querystring: require.resolve("querystring-es3"),
stream: require.resolve("stream-browserify"),
assert: require.resolve('assert/'),
buffer: require.resolve('buffer/'),
console: require.resolve('console-browserify'),
constants: require.resolve('constants-browserify'),
crypto: require.resolve('crypto-browserify'),
domain: require.resolve('domain-browser'),
events: require.resolve('events/'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
punycode: require.resolve('punycode/'),
process: require.resolve('process/browser'),
querystring: require.resolve('querystring-es3'),
stream: require.resolve('stream-browserify'),
/* eslint-disable camelcase */
_stream_duplex: require.resolve("readable-stream/lib/_stream_duplex"),
_stream_passthrough: require.resolve("readable-stream/lib/_stream_passthrough"),
_stream_readable: require.resolve("readable-stream/lib/_stream_readable"),
_stream_transform: require.resolve("readable-stream/lib/_stream_transform"),
_stream_writable: require.resolve("readable-stream/lib/_stream_writable"),
string_decoder: require.resolve("string_decoder/"),
_stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),
_stream_passthrough: require.resolve('readable-stream/lib/_stream_passthrough'),
_stream_readable: require.resolve('readable-stream/lib/_stream_readable'),
_stream_transform: require.resolve('readable-stream/lib/_stream_transform'),
_stream_writable: require.resolve('readable-stream/lib/_stream_writable'),
string_decoder: require.resolve('string_decoder/'),
/* eslint-enable camelcase */
sys: require.resolve("util/"),
timers: require.resolve("timers-browserify"),
tty: require.resolve("tty-browserify"),
url: require.resolve("url/"),
util: require.resolve("util/"),
vm: require.resolve("vm-browserify"),
zlib: require.resolve("browserify-zlib")
sys: require.resolve('util/'),
timers: require.resolve('timers-browserify'),
tty: require.resolve('tty-browserify'),
url: require.resolve('url/'),
util: require.resolve('util/'),
vm: require.resolve('vm-browserify'),
zlib: require.resolve('browserify-zlib')
}),
...compiler.options.resolve.fallback
}
};
}
}
};
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 - 2021 Richie Bendall
Copyright (c) 2020 - 2022 Richie Bendall

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 11 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
"name": "node-polyfill-webpack-plugin",
"version": "1.1.4",
"description": "Polyfill Node.js core modules in Webpack.",
"repository": "https://github.com/Richienb/node-polyfill-webpack-plugin.git",
"author": "Richie Bendall <richiebendall@gmail.com>",
"repository": "Richienb/node-polyfill-webpack-plugin",
"author": {
"name": "Richie Bendall",
"email": "richiebendall@gmail.com"
},
"license": "MIT",
"main": "index.js",
"files": [
"index.js",
"index.d.ts"
],
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"lint": "xo",
"test": "xo && ava"
},
"keywords": [
Expand All @@ -30,7 +32,7 @@
"console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0",
"domain-browser": "^4.19.0",
"domain-browser": "^4.22.0",
"events": "^3.3.0",
"filter-obj": "^2.0.2",
"https-browserify": "^1.0.0",
Expand All @@ -39,28 +41,24 @@
"process": "^0.11.10",
"punycode": "^2.1.1",
"querystring-es3": "^0.2.1",
"readable-stream": "^3.6.0",
"readable-stream": "^4.0.0",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"string_decoder": "^1.3.0",
"timers-browserify": "^2.0.12",
"tty-browserify": "^0.0.1",
"type-fest": "^2.13.1",
"type-fest": "^2.14.0",
"url": "^0.11.0",
"util": "^0.12.4",
"vm-browserify": "^1.1.2"
},
"devDependencies": {
"ava": "^3.15.0",
"eslint-config-richienb": "^0.6.2",
"p-webpack": "^1.0.1",
"webpack": "^5.36.2",
"xo": "^0.38.2"
"webpack": "^5.73.0",
"xo": "^0.39.1"
},
"peerDependencies": {
"webpack": ">=5"
},
"xo": {
"extends": "richienb"
}
}
18 changes: 8 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Polyfill Node.js core modules in Webpack.

This module is only needed for [webpack 5+](https://github.com/webpack/changelog-v5#automatic-nodejs-polyfills-removed).

[![NPM Badge](https://nodei.co/npm/node-polyfill-webpack-plugin.png)](https://npmjs.com/package/node-polyfill-webpack-plugin)

## Install

```sh
Expand All @@ -17,14 +15,14 @@ npm install node-polyfill-webpack-plugin
Add the following to your `webpack.config.js`:

```js
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

module.exports = {
// Other rules...
plugins: [
new NodePolyfillPlugin()
]
}
};
```

## API
Expand All @@ -42,33 +40,33 @@ Type: `object`
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, if you don't want a module like `console` to be polyfilled you can specify alises to be skipped here.

```js
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

module.exports = {
// Other rules...
plugins: [
new NodePolyfillPlugin({
excludeAliases: ["console"]
excludeAliases: ['console']
})
]
}
};
```

#### includeAliases

Alternatively, you can choose to only include certain aliases. For example, you can only have `console` polyfilled.

```js
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

module.exports = {
// Other rules...
plugins: [
new NodePolyfillPlugin({
includeAliases: ["console"]
includeAliases: ['console']
})
]
}
};
```

## Aliases
Expand Down
Loading

0 comments on commit 110208c

Please sign in to comment.