-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev): make browser Node polyfills opt-in (#7269)
- Loading branch information
1 parent
65f0b29
commit fda1df5
Showing
20 changed files
with
244 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
"@remix-run/dev": minor | ||
--- | ||
|
||
The `serverNodeBuiltinsPolyfill` option (along with the newly added `browserNodeBuiltinsPolyfill`) now supports defining global polyfills in addition to module polyfills. | ||
|
||
For example, to polyfill Node's `Buffer` global: | ||
|
||
```js | ||
module.exports = { | ||
serverNodeBuiltinsPolyfill: { | ||
globals: { | ||
Buffer: true, | ||
}, | ||
// You'll probably need to polyfill the "buffer" module | ||
// too since the global polyfill imports this: | ||
modules: { | ||
buffer: true, | ||
}, | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
"@remix-run/dev": major | ||
--- | ||
|
||
Remove default Node.js polyfills. | ||
|
||
Any Node.js polyfills (or empty polyfills) that are required for your browser code must be configured via the `browserNodeBuiltinsPolyfill` option in `remix.config.js`. | ||
|
||
```js | ||
exports.browserNodeBuiltinsPolyfill = { | ||
modules: { | ||
buffer: true, | ||
fs: "empty", | ||
}, | ||
globals: { | ||
Buffer: true, | ||
}, | ||
}; | ||
``` | ||
|
||
If you're targeting a non-Node.js server platform, any Node.js polyfills (or empty polyfills) that are required for your server code must be configured via the `serverNodeBuiltinsPolyfill` option in `remix.config.js`. | ||
|
||
```js | ||
exports.serverNodeBuiltinsPolyfill = { | ||
modules: { | ||
buffer: true, | ||
fs: "empty", | ||
}, | ||
globals: { | ||
Buffer: true, | ||
}, | ||
}; | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@remix-run/dev": patch | ||
--- | ||
|
||
Restart dev server when Remix config changes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { create as createCompiler } from "./compiler"; | ||
export { write } from "./write"; |
37 changes: 37 additions & 0 deletions
37
packages/remix-dev/compiler/js/plugins/browserNodeBuiltinsPolyfill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { nodeModulesPolyfillPlugin } from "esbuild-plugins-node-modules-polyfill"; | ||
|
||
import type { Context } from "../../context"; | ||
|
||
export const browserNodeBuiltinsPolyfillPlugin = (ctx: Context) => | ||
nodeModulesPolyfillPlugin({ | ||
// Rename plugin to improve error message attribution | ||
name: "browser-node-builtins-polyfill-plugin", | ||
// Only pass through the "modules" and "globals" options to ensure we | ||
// don't leak the full plugin API to Remix consumers. | ||
modules: ctx.config.browserNodeBuiltinsPolyfill?.modules ?? {}, | ||
globals: ctx.config.browserNodeBuiltinsPolyfill?.globals ?? {}, | ||
// Mark any unpolyfilled Node builtins in the build output as errors. | ||
fallback: "error", | ||
formatError({ moduleName, importer, polyfillExists }) { | ||
let normalizedModuleName = moduleName.replace("node:", ""); | ||
let modulesConfigKey = /^[a-z_]+$/.test(normalizedModuleName) | ||
? normalizedModuleName | ||
: JSON.stringify(normalizedModuleName); | ||
|
||
return { | ||
text: (polyfillExists | ||
? [ | ||
`Node builtin "${moduleName}" (imported by "${importer}") must be polyfilled for the browser. `, | ||
`You can enable this polyfill in your Remix config, `, | ||
`e.g. \`browserNodeBuiltinsPolyfill: { modules: { ${modulesConfigKey}: true } }\``, | ||
] | ||
: [ | ||
`Node builtin "${moduleName}" (imported by "${importer}") doesn't have a browser polyfill available. `, | ||
`You can stub it out with an empty object in your Remix config `, | ||
`e.g. \`browserNodeBuiltinsPolyfill: { modules: { ${modulesConfigKey}: "empty" } }\` `, | ||
"but note that this may cause runtime errors if the module is used in your browser code.", | ||
] | ||
).join(""), | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as path from "node:path"; | ||
import type { OutputFile } from "esbuild"; | ||
import fse from "fs-extra"; | ||
|
||
import type { RemixConfig } from "../../config"; | ||
|
||
export async function write(config: RemixConfig, outputFiles: OutputFile[]) { | ||
await fse.ensureDir(path.dirname(config.assetsBuildDirectory)); | ||
|
||
for (let file of outputFiles) { | ||
await fse.ensureDir(path.dirname(file.path)); | ||
await fse.writeFile(file.path, file.contents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.