Skip to content

Commit

Permalink
perf: use native fetch for node >= 18 to reduce %40 of bundle size (
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 13, 2023
1 parent 08f0152 commit 058bacf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import { loadConfig, watchConfig, WatchConfigOptions } from "c12";
import { klona } from "klona/full";
import { camelCase } from "scule";
import { defu } from "defu";
import {
resolveModuleExportNames,
resolvePath as resolveModule,
parseNodeModulePath,
} from "mlly";
import { resolveModuleExportNames } from "mlly";
import escapeRE from "escape-string-regexp";
import { withLeadingSlash, withoutTrailingSlash, withTrailingSlash } from "ufo";
import { isTest, isDebug } from "std-env";
import { isTest, isDebug, nodeMajorVersion, provider } from "std-env";
import { findWorkspaceDir } from "pkg-types";
import consola from "consola";
import {
resolvePath,
resolveFile,
Expand Down Expand Up @@ -378,6 +375,23 @@ export async function loadOptions(
});
}

// Native fetch
if (options.experimental.nodeFetchCompat === undefined) {
options.experimental.nodeFetchCompat = nodeMajorVersion < 18;
if (options.experimental.nodeFetchCompat && provider !== "stackblitz") {
consola.warn(
"Node fetch compatibility is enabled. Please consider upgrading to Node.js >= 18."
);
}
}
if (!options.experimental.nodeFetchCompat) {
options.alias = {
"node-fetch-native/polyfill": "unenv/runtime/mock/empty",
"node-fetch-native": "node-fetch-native/native",
...options.alias,
};
}

return options;
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ export interface NitroOptions extends PresetOptions {
* Disable Experimental Sourcemap Minification
*/
sourcemapMinify?: false;
/**
* Backward compatibility support for Node fetch (required for Node < 18)
*/
nodeFetchCompat?: boolean;
};
future: {
nativeSWR: boolean;
Expand Down

0 comments on commit 058bacf

Please sign in to comment.