Skip to content

Commit

Permalink
Require Node.js 14 and update dependencies (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
afonsojramos and Richienb authored Oct 10, 2023
1 parent 3474abf commit 6c90ae3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 46 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 20
- 18
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
1 change: 1 addition & 0 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable unicorn/prefer-node-protocol, n/prefer-global/buffer */
const assert = require('assert');

assert(true);
Expand Down
19 changes: 8 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
/* eslint-disable no-redeclare */
import {MergeExclusive} from 'type-fest';
import {Compiler} from 'webpack';
import {type MergeExclusive} from 'type-fest';
import {type Compiler} from 'webpack';

declare namespace NodePolyfillPlugin {
export type Alias =
| 'Buffer'
| 'console'
| 'process'
| 'assert'
| 'buffer'
| 'Buffer'
| 'console'
| 'constants'
| 'crypto'
Expand All @@ -18,8 +15,8 @@ declare namespace NodePolyfillPlugin {
| 'https'
| 'os'
| 'path'
| 'punycode'
| 'process'
| 'punycode'
| 'querystring'
| 'stream'
| '_stream_duplex'
Expand All @@ -36,19 +33,19 @@ declare namespace NodePolyfillPlugin {
| 'vm'
| 'zlib';

export interface IncludeOptions {
export type 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 interface ExcludeOptions {
export type 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>;
}
Expand Down
40 changes: 33 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
'use strict';
const filterObject = require('filter-obj');
// https://github.com/sindresorhus/filter-obj/blob/58086b537bb622166387216bfb7da6e8184996ba/index.js#L1-L25
function includeKeys(object, predicate) {
const result = {};

if (Array.isArray(predicate)) {
for (const key of predicate) {
result[key] = object[key];
}
} else {
for (const key of Object.keys(object)) {
const value = object[key];

if (predicate(key, value, object)) {
result[key] = value;
}
}
}

return result;
}

// https://github.com/sindresorhus/filter-obj/blob/58086b537bb622166387216bfb7da6e8184996ba/index.js#L27-L34
function excludeKeys(object, keys) {
const set = new Set(keys);

return includeKeys(object, key => !set.has(key));
}

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

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

module.exports = class NodePolyfillPlugin {
constructor(options = {}) {
this.options = {
excludeAliases: [],
includeAliases: [],
...options
...options,
};

if (this.options.includeAliases.length > 0 && this.options.excludeAliases.length > 0) {
Expand All @@ -28,7 +54,7 @@ module.exports = class NodePolyfillPlugin {
compiler.options.plugins.push(new compiler.webpack.ProvidePlugin(filter({
Buffer: [require.resolve('buffer/'), 'Buffer'],
console: require.resolve('console-browserify'),
process: require.resolve('process/browser')
process: require.resolve('process/browser'),
})));

compiler.options.resolve.fallback = {
Expand Down Expand Up @@ -62,9 +88,9 @@ module.exports = class NodePolyfillPlugin {
url: require.resolve('url/'),
util: require.resolve('util/'),
vm: require.resolve('vm-browserify'),
zlib: require.resolve('browserify-zlib')
zlib: require.resolve('browserify-zlib'),
}),
...compiler.options.resolve.fallback
...compiler.options.resolve.fallback,
};
}
};
26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"index.d.ts"
],
"engines": {
"node": ">=12"
"node": ">=14"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -26,39 +26,43 @@
"polyfill"
],
"dependencies": {
"assert": "^2.0.0",
"assert": "^2.1.0",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0",
"domain-browser": "^4.22.0",
"events": "^3.3.0",
"filter-obj": "^2.0.2",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"punycode": "^2.1.1",
"punycode": "^2.3.0",
"querystring-es3": "^0.2.1",
"readable-stream": "^4.0.0",
"readable-stream": "^4.4.2",
"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.14.0",
"url": "^0.11.0",
"util": "^0.12.4",
"type-fest": "^4.4.0",
"url": "^0.11.3",
"util": "^0.12.5",
"vm-browserify": "^1.1.2"
},
"devDependencies": {
"ava": "^3.15.0",
"ava": "^5.3.1",
"p-webpack": "^1.0.1",
"webpack": "^5.73.0",
"xo": "^0.39.1"
"webpack": "^5.88.2",
"xo": "^0.56.0"
},
"peerDependencies": {
"webpack": ">=5"
},
"xo": {
"rules": {
"unicorn/prefer-module": "off"
}
}
}
26 changes: 13 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const fs = require('fs');
const fs = require('node:fs');
const test = require('ava');
const webpack = require('p-webpack');
const NodePolyfillPlugin = require('.');
const NodePolyfillPlugin = require('./index.js');

test('main', async t => {
await webpack({
entry: './fixture',
output: {
library: {
type: 'commonjs-module'
type: 'commonjs-module',
},
filename: '1.js'
filename: '1.js',
},
plugins: [
new NodePolyfillPlugin({
excludeAliases: ['console']
})
]
excludeAliases: ['console'],
}),
],
});

t.is(require('./dist/1.js'), 'Hello World');
Expand All @@ -33,15 +33,15 @@ test('includeAliases', async t => {
entry: './fixture',
output: {
library: {
type: 'commonjs-module'
type: 'commonjs-module',
},
filename: '2.js'
filename: '2.js',
},
plugins: [
new NodePolyfillPlugin({
includeAliases: ['console']
})
]
includeAliases: ['console'],
}),
],
});

t.is(require('./dist/2.js'), 'Hello World');
Expand All @@ -56,6 +56,6 @@ test('includeAliases', async t => {
test('includeAliases and excludeAliases used at the same time', t => {
t.throws(() => new NodePolyfillPlugin({
includeAliases: ['console'],
excludeAliases: ['crypto']
excludeAliases: ['crypto'],
}), {instanceOf: Error});
});

0 comments on commit 6c90ae3

Please sign in to comment.