From 6c90ae31104fa754dae621cc19798a3ccb3f17b8 Mon Sep 17 00:00:00 2001 From: Afonso Jorge Ramos Date: Tue, 10 Oct 2023 14:03:56 +0100 Subject: [PATCH] Require Node.js 14 and update dependencies (#46) Co-authored-by: Richie Bendall --- .github/workflows/main.yml | 8 ++++---- fixture.js | 1 + index.d.ts | 19 ++++++++---------- index.js | 40 +++++++++++++++++++++++++++++++------- package.json | 26 ++++++++++++++----------- test.js | 26 ++++++++++++------------- 6 files changed, 74 insertions(+), 46 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 48143fc..9d58fb3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/fixture.js b/fixture.js index c393b4f..672cfaf 100644 --- a/fixture.js +++ b/fixture.js @@ -1,3 +1,4 @@ +/* eslint-disable unicorn/prefer-node-protocol, n/prefer-global/buffer */ const assert = require('assert'); assert(true); diff --git a/index.d.ts b/index.d.ts index 89598e3..3e9e3c7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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' @@ -18,8 +15,8 @@ declare namespace NodePolyfillPlugin { | 'https' | 'os' | 'path' - | 'punycode' | 'process' + | 'punycode' | 'querystring' | 'stream' | '_stream_duplex' @@ -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; } diff --git a/index.js b/index.js index a5faf72..2c997e7 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,38 @@ '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 { @@ -14,7 +40,7 @@ module.exports = class NodePolyfillPlugin { this.options = { excludeAliases: [], includeAliases: [], - ...options + ...options, }; if (this.options.includeAliases.length > 0 && this.options.excludeAliases.length > 0) { @@ -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 = { @@ -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, }; } }; diff --git a/package.json b/package.json index c1797cd..538f9b8 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "index.d.ts" ], "engines": { - "node": ">=12" + "node": ">=14" }, "scripts": { "test": "xo && ava" @@ -26,7 +26,7 @@ "polyfill" ], "dependencies": { - "assert": "^2.0.0", + "assert": "^2.1.0", "browserify-zlib": "^0.2.0", "buffer": "^6.0.3", "console-browserify": "^1.2.0", @@ -34,31 +34,35 @@ "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" + } } } diff --git a/test.js b/test.js index 3173c3d..204f2ae 100644 --- a/test.js +++ b/test.js @@ -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'); @@ -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'); @@ -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}); });