Skip to content

Commit

Permalink
Add timers/promises, and improve process, querystring and url
Browse files Browse the repository at this point in the history
Fixes #51, fixes #58, fixes #55

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
Richienb committed Nov 20, 2024
1 parent fc4e0a9 commit 399f2ec
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 63 deletions.
2 changes: 1 addition & 1 deletion fixture.js → fixtures/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable unicorn/prefer-node-protocol, n/prefer-global/buffer */
/* eslint-disable unicorn/prefer-node-protocol */
const assert = require('assert');

assert(true);
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare namespace NodePolyfillPlugin {
| 'string_decoder'
| 'sys'
| 'timers'
| 'timers/promises'
| 'tty'
| 'url'
| 'util'
Expand Down
66 changes: 35 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
const stdLibBrowser = require('node-stdlib-browser');

// https://github.com/sindresorhus/filter-obj/blob/58086b537bb622166387216bfb7da6e8184996ba/index.js#L1-L25
function includeKeys(object, predicate) {
const result = {};
Expand Down Expand Up @@ -37,6 +39,7 @@ const defaultPolyfills = new Set([
'string_decoder',
'sys',
'timers',
'timers/promises',
'tty',
'url',
'util',
Expand Down Expand Up @@ -94,44 +97,45 @@ module.exports = class NodePolyfillPlugin {
const filter = createAliasFilter(this.options);

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

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/'),
assert: stdLibBrowser.assert,
buffer: stdLibBrowser.buffer,
console: stdLibBrowser.console,
constants: stdLibBrowser.constants,
crypto: stdLibBrowser.crypto,
domain: stdLibBrowser.domain,
events: stdLibBrowser.events,
fs: false,
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'),
http: stdLibBrowser.http,
https: stdLibBrowser.https,
os: stdLibBrowser.os,
path: stdLibBrowser.path,
punycode: stdLibBrowser.punycode,
process: stdLibBrowser.process,
querystring: stdLibBrowser.querystring,
stream: stdLibBrowser.stream,
/* 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: stdLibBrowser._stream_duplex,
_stream_passthrough: stdLibBrowser._stream_passthrough,
_stream_readable: stdLibBrowser._stream_readable,
_stream_transform: stdLibBrowser._stream_transform,
_stream_writable: stdLibBrowser._stream_writable,
string_decoder: stdLibBrowser.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: stdLibBrowser.sys,
timers: stdLibBrowser.timers,
'timers/promises': stdLibBrowser['timers/promises'],
tty: stdLibBrowser.tty,
url: stdLibBrowser.url,
util: stdLibBrowser.util,
vm: stdLibBrowser.vm,
zlib: stdLibBrowser.zlib,
}),
...compiler.options.resolve.fallback,
};
Expand Down
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 - 2022 Richie Bendall
Copyright (c) 2020 - 2024 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
32 changes: 5 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,14 @@
"polyfill"
],
"dependencies": {
"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": "^5.7.0",
"events": "^3.3.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"punycode": "^2.3.1",
"querystring-es3": "^0.2.1",
"readable-stream": "^4.5.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": "^4.18.2",
"url": "^0.11.3",
"util": "^0.12.5",
"vm-browserify": "^1.1.2"
"node-stdlib-browser": "^1.2.1",
"type-fest": "^4.27.0"
},
"devDependencies": {
"ava": "^5.3.1",
"ava": "^6.2.0",
"p-webpack": "^1.0.1",
"webpack": "^5.88.2",
"xo": "^0.56.0"
"webpack": "^5.96.1",
"xo": "^0.59.3"
},
"peerDependencies": {
"webpack": ">=5"
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {promises: fs} = require('node:fs');
const {promises: fs} = require('node:fs'); // eslint-disable-line n/no-unsupported-features/node-builtins
const test = require('ava');
const webpack = require('p-webpack');
const NodePolyfillPlugin = require('./index.js');

test('main', async t => {
await webpack({
entry: './fixture',
entry: './fixtures/main',
output: {
library: {
type: 'commonjs-module',
Expand Down Expand Up @@ -33,7 +33,7 @@ test('main', async t => {

test('includeAliases', async t => {
await webpack({
entry: './fixture',
entry: './fixtures/main',
output: {
library: {
type: 'commonjs-module',
Expand Down

0 comments on commit 399f2ec

Please sign in to comment.