Skip to content

Commit

Permalink
Shimming Builtin Node Globals (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Apr 21, 2022
1 parent 1a3e869 commit 940f568
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/features/node-emulation.md
Expand Up @@ -54,7 +54,7 @@ The `.env.local` file is also supported for local overrides of environment varia

## Polyfilling & Excluding Builtin Node Modules

When your code, or more likely a dependency, imports builtin Node modules such as `crypto`, `fs` or `process`, Parcel will automatically use one of the following polyfills. If no polyfill is available, then an empty module will be used instead. You can also use [aliases](/features/dependency-resolution/#aliases) to override these.
When targetting the browser and your code, or more likely a dependency, imports builtin Node modules such as `crypto`, `fs` or `process`, Parcel will automatically use one of the following polyfills. If no polyfill is available, then an empty module will be used instead. You can also use [aliases](/features/dependency-resolution/#aliases) to override these.

| native module | npm replacement | native module | npm replacement |
| ------------- | -------------------------- | -------------- | -------------------- |
Expand All @@ -71,6 +71,19 @@ When your code, or more likely a dependency, imports builtin Node modules such a
| path | `path-browserify` | vm | `vm-browserify` |
| zlib | `browserify-zlib` |

## Shimming Builtin Node Globals

When targetting the browser, usages of global variables that are available in Node are replaced to not break code written for Node:

- `process` is imported automatically from the `process` module, unless it's part of a `process.browser` or `process.env.FOO` expression which is replaced by a boolean or the value of the environment variable.

- `Buffer` is imported automatically from the `buffer` module.

- `__filename` and `dirname` are replaced by string literals of the asset's filepath (or the parent folder) relative to the project root.

- `global` is replaced with a reference to the global variable (behaving like the newer `globalThis`).


## Inlining fs.readFileSync

Calls to `fs.readFileSync` are replaced with the file's contents if the filepath is statically determinable and inside the project root.
Expand Down

1 comment on commit 940f568

@vercel
Copy link

@vercel vercel bot commented on 940f568 Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.