From 110208cad00952c43b19b617632ed266bfe81a5f Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sat, 25 Jun 2022 02:21:30 +1200 Subject: [PATCH] Require Node.js 12 Signed-off-by: Richie Bendall --- fixture.js | 8 ++-- index.d.ts | 102 +++++++++++++++++++++++++-------------------------- index.js | 86 +++++++++++++++++++++---------------------- license | 2 +- package.json | 24 ++++++------ readme.md | 18 ++++----- test.js | 60 +++++++++++++++--------------- 7 files changed, 148 insertions(+), 152 deletions(-) diff --git a/fixture.js b/fixture.js index 7a0b680..c393b4f 100644 --- a/fixture.js +++ b/fixture.js @@ -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(); diff --git a/index.d.ts b/index.d.ts index 077379b..01da7c9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 +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; + declare class NodePolyfillPlugin { - constructor(options?: NodePolyfillPlugin.Options) + constructor(options?: Options); - apply(compiler: InstanceType): void + apply(compiler: InstanceType): void; } -export = NodePolyfillPlugin +export = NodePolyfillPlugin; diff --git a/index.js b/index.js index c58bfa3..fb7c93b 100644 --- a/index.js +++ b/index.js @@ -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 { @@ -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 - } + }; } -} +}; diff --git a/license b/license index 491056b..a759e34 100644 --- a/license +++ b/license @@ -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 diff --git a/package.json b/package.json index 0a23502..210bec4 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,11 @@ "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 ", + "repository": "Richienb/node-polyfill-webpack-plugin", + "author": { + "name": "Richie Bendall", + "email": "richiebendall@gmail.com" + }, "license": "MIT", "main": "index.js", "files": [ @@ -11,10 +14,9 @@ "index.d.ts" ], "engines": { - "node": ">=10" + "node": ">=12" }, "scripts": { - "lint": "xo", "test": "xo && ava" }, "keywords": [ @@ -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", @@ -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" } } diff --git a/readme.md b/readme.md index 91c465e..0b34772 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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 @@ -42,16 +40,16 @@ 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 @@ -59,16 +57,16 @@ module.exports = { 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 diff --git a/test.js b/test.js index e04c4f9..3173c3d 100644 --- a/test.js +++ b/test.js @@ -1,59 +1,61 @@ -const fs = require("fs") -const test = require("ava") -const webpack = require("p-webpack") -const NodePolyfillPlugin = require(".") +const fs = require('fs'); +const test = require('ava'); +const webpack = require('p-webpack'); +const NodePolyfillPlugin = require('.'); -test.serial("main", async t => { +test('main', async t => { await webpack({ - entry: "./fixture", + entry: './fixture', output: { library: { - type: "commonjs-module" - } + type: 'commonjs-module' + }, + filename: '1.js' }, plugins: [ new NodePolyfillPlugin({ - excludeAliases: ["console"] + excludeAliases: ['console'] }) ] - }) + }); - t.is(require("./dist/main.js"), "Hello World") + t.is(require('./dist/1.js'), 'Hello World'); // https://github.com/browserify/console-browserify/blob/f7eefc7c908c29d2e94954e5c6c1098e8c1028b4/index.js#L63 - t.false(fs.readFileSync("./dist/main.js").toString().includes("No such label: ")) + t.false(fs.readFileSync('./dist/1.js').toString().includes('No such label: ')); // https://github.com/feross/buffer/blob/master/index.js#L80 - t.true(fs.readFileSync("./dist/main.js").toString().includes("is invalid for option \"size\"")) -}) + t.true(fs.readFileSync('./dist/1.js').toString().includes('is invalid for option "size"')); +}); -test.serial("includeAliases", async t => { +test('includeAliases', async t => { await webpack({ - entry: "./fixture", + entry: './fixture', output: { library: { - type: "commonjs-module" - } + type: 'commonjs-module' + }, + filename: '2.js' }, plugins: [ new NodePolyfillPlugin({ - includeAliases: ["console"] + includeAliases: ['console'] }) ] - }) + }); - t.is(require("./dist/main.js"), "Hello World") + t.is(require('./dist/2.js'), 'Hello World'); // https://github.com/browserify/console-browserify/blob/f7eefc7c908c29d2e94954e5c6c1098e8c1028b4/index.js#L63 - t.true(fs.readFileSync("./dist/main.js").toString().includes("No such label: ")) + t.true(fs.readFileSync('./dist/2.js').toString().includes('No such label: ')); // https://github.com/feross/buffer/blob/master/index.js#L80 - t.false(fs.readFileSync("./dist/main.js").toString().includes("is invalid for option \"size\"")) -}) + t.false(fs.readFileSync('./dist/2.js').toString().includes('is invalid for option "size"')); +}); -test("includeAliases and excludeAliases used at the same time", t => { +test('includeAliases and excludeAliases used at the same time', t => { t.throws(() => new NodePolyfillPlugin({ - includeAliases: ["console"], - excludeAliases: ["crypto"] - }), { instanceOf: Error }) -}) + includeAliases: ['console'], + excludeAliases: ['crypto'] + }), {instanceOf: Error}); +});