Skip to content

Commit

Permalink
feat: support process and Buffer globals (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish authored Jun 26, 2023
1 parent d7e6226 commit c345cf6
Show file tree
Hide file tree
Showing 13 changed files with 2,270 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

## Description

Polyfills nodejs builtin modules for the browser.
Polyfills nodejs builtin modules and globals for the browser.

## Features

- Written In Typescript
- Offers CJS and ESM builds
- Full TypeScript & JavaScript support
- Supports `node:` protocol
- Optionally injects globals

## Install

Expand All @@ -37,6 +38,21 @@ build({
});
```

Optionally inject globals when detected:

```ts
import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
plugins: [nodeModulesPolyfillPlugin({
globals: {
process: true,
Buffer: true,
}
})],
});
```

## Buy me some doughnuts

If you want to support me by donating, you can do so by using any of the following methods. Thank you very much in advance!
Expand Down
1 change: 1 addition & 0 deletions globals/Buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Buffer } from 'node:buffer';
1 change: 1 addition & 0 deletions globals/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as process } from 'node:process';
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esbuild-plugins-node-modules-polyfill",
"version": "1.0.16",
"description": "Polyfills nodejs builtin modules for the browser.",
"description": "Polyfills nodejs builtin modules and globals for the browser.",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -60,7 +60,8 @@
"files": [
"dist/**/*.js*",
"dist/**/*.mjs*",
"dist/**/*.d*"
"dist/**/*.d*",
"globals/**/*.js"
],
"repository": {
"type": "git",
Expand Down
16 changes: 15 additions & 1 deletion src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type esbuild from 'esbuild';
const NAME = 'node-modules-polyfills';

export interface NodePolyfillsOptions {
globals?: {
Buffer?: boolean;
process?: boolean;
};
name?: string;
namespace?: string;
}
Expand Down Expand Up @@ -47,7 +51,7 @@ const loader = async (args: esbuild.OnLoadArgs): Promise<esbuild.OnLoadResult> =
};

export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): Plugin => {
const { namespace = NAME, name = NAME } = options;
const { globals = {}, namespace = NAME, name = NAME } = options;
if (namespace.endsWith('commonjs')) {
throw new Error(`namespace ${namespace} must not end with commonjs`);
}
Expand All @@ -64,6 +68,16 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P
initialOptions.define = { global: 'globalThis' };
}

initialOptions.inject = initialOptions.inject ?? [];

if (globals.Buffer) {
initialOptions.inject.push(path.resolve(__dirname, '../globals/Buffer.js'));
}

if (globals.process) {
initialOptions.inject.push(path.resolve(__dirname, '../globals/process.js'));
}

onLoad({ filter: /.*/, namespace }, loader);
onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader);
const filter = new RegExp(`(?:node:)?${builtinModules.map(escapeRegex).join('|')}`);
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/input/globalBuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-restricted-globals, n/prefer-global/buffer
console.log(Buffer.from('Hello world'));
6 changes: 6 additions & 0 deletions tests/fixtures/input/globalProcess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// eslint-disable-next-line no-restricted-globals, n/prefer-global/process
console.log(process.version);

// Ensure that environment variables can still be injected via `define`
// eslint-disable-next-line no-restricted-globals, n/prefer-global/process
console.log(process.env.NODE_ENV);
Loading

0 comments on commit c345cf6

Please sign in to comment.