Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use empty polyfill if browser field is false #151

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,20 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P
if (initialOptions.platform === 'browser') {
const packageJson = await loadPackageJSON(args.resolveDir);
const browserFieldValue = packageJson?.browser?.[args.path];
if (browserFieldValue !== undefined) return;

// This is here to support consumers who have used the
// "external" option to exclude all Node builtins (e.g.
// Remix v1 does this), otherwise the import/require is left
// in the output and throws an error at runtime. Ideally we
// would just return undefined for any browser field value,
// and we can safely switch to this in a major version.
if (browserFieldValue === false) {
return emptyResult;
}

if (browserFieldValue !== undefined) {
return;
}
}

const moduleName = normalizeNodeBuiltinPath(args.path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ exports[`Browser Field False Test > GIVEN a file in a browser target build that
mod
));

// (disabled):constants
// node-modules-polyfills-empty:constants
var require_constants = __commonJS({
\\"(disabled):constants\\"() {
\\"node-modules-polyfills-empty:constants\\"(exports, module) {
module.exports = {};
}
});

Expand Down