Skip to content

Commit 6f6b5cf

Browse files
committed
fix: alias @faker-js/faker to false in webpack to reduce bundle size
The Postman code generator dependency includes @faker-js/faker, which isn't needed in the browser but adds significant bundle weight. This change uses a webpack alias and IgnorePlugin to exclude it from the client bundle.
1 parent afa156a commit 6f6b5cf

File tree

1 file changed

+42
-34
lines changed
  • packages/docusaurus-theme-openapi-docs/src

1 file changed

+42
-34
lines changed

packages/docusaurus-theme-openapi-docs/src/index.ts

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
import path from "path";
99

10-
import type { Plugin } from "@docusaurus/types";
10+
import type { Plugin, ConfigureWebpackUtils } from "@docusaurus/types";
11+
import type { Configuration } from "webpack";
1112

1213
export default function docusaurusThemeOpenAPI(): Plugin<void> {
1314
return {
1415
name: "docusaurus-theme-openapi",
1516

1617
getClientModules() {
17-
const modules = [
18+
return [
1819
require.resolve(
1920
path.join(__dirname, "..", "lib", "theme", "styles.scss")
2021
),
2122
];
22-
return modules;
2323
},
2424

2525
getThemePath() {
@@ -30,26 +30,45 @@ export default function docusaurusThemeOpenAPI(): Plugin<void> {
3030
return path.resolve(__dirname, "..", "src", "theme");
3131
},
3232

33-
configureWebpack(_, isServer, utils) {
34-
const rules: any = _.module?.rules ?? [];
35-
const sassLoaderRule = rules.filter((r: any) => {
36-
return String(r.test) === String(/\.s[ca]ss$/);
33+
configureWebpack(
34+
config: Configuration,
35+
isServer: boolean,
36+
utils: ConfigureWebpackUtils
37+
): Configuration {
38+
const { getStyleLoaders, currentBundler } = utils;
39+
40+
// --- Drop-in replacement for @faker-js/faker --------------------------
41+
const fakerAlias = { "@faker-js/faker": false } as const;
42+
43+
const ignoreFaker = new currentBundler.instance.IgnorePlugin({
44+
resourceRegExp: /^@faker-js\/faker$/,
3745
});
38-
const { getStyleLoaders } = utils;
39-
// Avoid conflicts with docusaurus-plugin-sass
40-
if (sassLoaderRule.length === 0) {
41-
return {
42-
resolve: {
43-
fallback: {
44-
buffer: require.resolve("buffer/"),
45-
},
46+
// ----------------------------------------------------------------------
47+
48+
const existingRules: any[] = config.module?.rules ?? [];
49+
const hasSassRule = existingRules.some(
50+
(r) => String(r.test) === String(/\.s[ca]ss$/)
51+
);
52+
53+
const baseConfig: Configuration = {
54+
resolve: {
55+
alias: fakerAlias,
56+
fallback: {
57+
buffer: require.resolve("buffer/"),
4658
},
47-
plugins: [
48-
new utils.currentBundler.instance.ProvidePlugin({
49-
process: require.resolve("process/browser"),
50-
Buffer: ["buffer", "Buffer"],
51-
}),
52-
],
59+
},
60+
plugins: [
61+
ignoreFaker,
62+
new currentBundler.instance.ProvidePlugin({
63+
process: require.resolve("process/browser"),
64+
Buffer: ["buffer", "Buffer"],
65+
}),
66+
],
67+
};
68+
69+
if (!hasSassRule) {
70+
return {
71+
...baseConfig,
5372
module: {
5473
rules: [
5574
{
@@ -67,19 +86,8 @@ export default function docusaurusThemeOpenAPI(): Plugin<void> {
6786
},
6887
};
6988
}
70-
return {
71-
resolve: {
72-
fallback: {
73-
buffer: require.resolve("buffer/"),
74-
},
75-
},
76-
plugins: [
77-
new utils.currentBundler.instance.ProvidePlugin({
78-
process: require.resolve("process/browser"),
79-
Buffer: ["buffer", "Buffer"],
80-
}),
81-
],
82-
};
89+
90+
return baseConfig;
8391
},
8492
};
8593
}

0 commit comments

Comments
 (0)