Skip to content

Commit e37fa62

Browse files
committed
Proxy process with additional exports
Closes #33.
1 parent 4032e50 commit e37fa62

File tree

8 files changed

+157
-4
lines changed

8 files changed

+157
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const b = browserify(
271271
| `net` | | [net](mock/net.js) |
272272
| `os` | [os-browserify](https://github.com/CoderPuppy/os-browserify) | |
273273
| `path` | [path-browserify](https://github.com/browserify/path-browserify) | |
274-
| `process` | [process](https://github.com/defunctzombie/node-process) | [process](mock/process.js) |
274+
| `process` | [process](https://github.com/defunctzombie/node-process) | [process](mock/process.js) | Contains additional exports from newer Node |
275275
| `punycode` | [punycode](https://github.com/bestiejs/punycode.js) | | `punycode@1` for browser support |
276276
| `querystring` | [querystring-es3](https://github.com/mike-spainhower/querystring) | | Contains additional exports from newer Node versions |
277277
| `readline` | | |

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const net = resolvePath('./mock/empty.js');
4040
const os = resolvePath('os-browserify/browser.js');
4141
const path = resolvePath('path-browserify');
4242
const punycode = resolvePath('punycode/');
43-
const _process = resolvePath('process/browser.js');
43+
const _process = resolvePath('./proxy/process').replace('.js', '');
4444
const querystring = resolvePath('./proxy/querystring.js');
4545
const readline = resolvePath('./mock/empty.js');
4646
const repl = resolvePath('./mock/empty.js');

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"README.md"
3737
],
3838
"scripts": {
39-
"build": "rollup --config rollup.config.js && babel helpers/esbuild/shim.src.js --out-file=helpers/esbuild/shim.js",
39+
"build": "del '{esm/,cjs/}' && rollup --config rollup.config.js && babel helpers/esbuild/shim.src.js --out-file=helpers/esbuild/shim.js",
4040
"lint": "eslint '{index,lib/**/*,test/**/*,helpers/**/*,example/**/*}.{js,mjs}'",
4141
"lint:types": "tsc",
4242
"module-check": "node -e 'require(\"node-stdlib-browser\"); require(\"node-stdlib-browser/helpers/esbuild/plugin\");' && node --input-type=module -e 'import \"node-stdlib-browser\"; import \"node-stdlib-browser/helpers/esbuild/plugin\";'",
@@ -94,6 +94,7 @@
9494
"core-js": "^2.6.5",
9595
"cpy": "^8.1.2",
9696
"del": "^6.0.0",
97+
"del-cli": "^3.0.1",
9798
"esbuild": "^0.13.14",
9899
"eslint": "^7.31.0",
99100
"eslint-config-niksy": "^10.0.0",

proxy/process.js

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import {
2+
nextTick,
3+
title,
4+
env as environment,
5+
argv,
6+
version,
7+
versions,
8+
on,
9+
addListener,
10+
once,
11+
off,
12+
removeListener,
13+
removeAllListeners,
14+
emit,
15+
prependListener,
16+
prependOnceListener,
17+
listeners,
18+
cwd,
19+
chdir,
20+
umask,
21+
// @ts-ignore
22+
browser as _browser,
23+
// @ts-ignore
24+
binding as _binding
25+
} from 'process/browser.js';
26+
27+
function noop() {}
28+
29+
const browser = /** @type {boolean} */ (_browser);
30+
const emitWarning = noop;
31+
const binding = /** @type {Function} */ (_binding);
32+
const exit = noop;
33+
const pid = 1;
34+
const features = {};
35+
const kill = noop;
36+
const dlopen = noop;
37+
const uptime = noop;
38+
const memoryUsage = noop;
39+
const uvCounters = noop;
40+
const platform = 'browser';
41+
const arch = 'browser';
42+
const execPath = 'browser';
43+
const execArgv = /** @type {string[]} */ ([]);
44+
45+
const api = {
46+
nextTick,
47+
title,
48+
browser,
49+
env: environment,
50+
argv,
51+
version,
52+
versions,
53+
on,
54+
addListener,
55+
once,
56+
off,
57+
removeListener,
58+
removeAllListeners,
59+
emit,
60+
emitWarning,
61+
prependListener,
62+
prependOnceListener,
63+
listeners,
64+
binding,
65+
cwd,
66+
chdir,
67+
umask,
68+
exit,
69+
pid,
70+
features,
71+
kill,
72+
dlopen,
73+
uptime,
74+
memoryUsage,
75+
uvCounters,
76+
platform,
77+
arch,
78+
execPath,
79+
execArgv
80+
};
81+
82+
export default api;
83+
84+
export {
85+
nextTick,
86+
title,
87+
browser,
88+
environment as env,
89+
argv,
90+
version,
91+
versions,
92+
on,
93+
addListener,
94+
once,
95+
off,
96+
removeListener,
97+
removeAllListeners,
98+
emit,
99+
emitWarning,
100+
prependListener,
101+
prependOnceListener,
102+
listeners,
103+
binding,
104+
cwd,
105+
chdir,
106+
umask,
107+
exit,
108+
pid,
109+
features,
110+
kill,
111+
dlopen,
112+
uptime,
113+
memoryUsage,
114+
uvCounters,
115+
platform,
116+
arch,
117+
execPath,
118+
execArgv
119+
};

proxy/process/browser.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import api from '../process.js';
2+
3+
export default api;
4+
5+
export * from '../process.js';

rollup.config.js

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ function getConfig(filename, options = {}) {
3434
if (source === 'url') {
3535
return require.resolve('url/');
3636
}
37+
if (source === 'process/browser.js') {
38+
return require.resolve('process/browser.js');
39+
}
3740
if (
3841
source === 'path' &&
3942
importer.includes('proxy/url.js')
@@ -163,6 +166,14 @@ module.exports = [
163166
[
164167
'proxy/querystring.js',
165168
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
169+
],
170+
[
171+
'proxy/process.js',
172+
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
173+
],
174+
[
175+
'proxy/process/browser.js',
176+
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
166177
]
167178
].map((entry) => {
168179
const [filename, options = {}] = [].concat(entry);

test/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import parseNodeVersion from 'parse-node-version';
77
import api from '../index';
88
import url from '../proxy/url';
99
import qs from '../proxy/querystring';
10+
import _process from '../proxy/process';
1011

1112
/** @typedef {import('../index').PackageNames} PackageNames */
1213

@@ -37,7 +38,7 @@ const packages = {
3738
net: 'mock/empty.js',
3839
os: 'node_modules/os-browserify',
3940
path: 'node_modules/path-browserify',
40-
process: 'node_modules/process',
41+
process: 'proxy/process',
4142
punycode: 'node_modules/punycode',
4243
querystring: 'proxy/querystring.js',
4344
readline: 'mock/empty.js',
@@ -415,6 +416,17 @@ describe('`querystring` additional exports', function () {
415416
});
416417
});
417418

419+
describe('`process` additional exports', function () {
420+
it('has exports for browser environment', function () {
421+
assert.equal(_process.title, 'browser');
422+
assert.equal(_process.browser, true);
423+
assert.equal(_process.arch, 'browser');
424+
assert.equal(_process.platform, 'browser');
425+
assert.ok(Array.isArray(_process.execArgv));
426+
assert.ok(typeof _process.emitWarning !== 'undefined');
427+
});
428+
});
429+
418430
const nodeVersion = parseNodeVersion(process.version);
419431
const shouldBundle = nodeVersion.major >= 12;
420432
const shouldBundleESM = nodeVersion.major >= 16;

types/lib.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ declare module 'querystring-es3' {
22
import { decode, encode, parse, stringify } from 'querystring';
33
export { decode, encode, parse, stringify };
44
};
5+
6+
declare module 'process/browser.js' {
7+
const process: NodeJS.Process;
8+
export = process;
9+
};

0 commit comments

Comments
 (0)