forked from payloadcms/next-payload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withPayloadPlugin.js
149 lines (132 loc) · 4.35 KB
/
withPayloadPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const FilterWarningsPlugin = require("webpack-filter-warnings-plugin");
const { getBaseConfig } = require("payload/dist/bundlers/webpack/configs/base");
const path = require("path");
const loadPayloadConfig = require("./loadPayloadConfig");
const mockModulePath = path.resolve(__dirname, "./mocks/emptyModule.js");
const customCSSMockPath = path.resolve(__dirname, "./mocks/custom.css");
const withPayload = async (config, paths) => {
const {
cssPath,
payloadPath,
configPath,
adminRoute: adminRouteArg = "/admin",
} = paths || {};
let payloadConfig = await loadPayloadConfig(configPath);
payloadConfig = {
...payloadConfig,
admin: {
...payloadConfig.admin,
css: cssPath || customCSSMockPath,
},
paths: {
...payloadConfig.paths,
rawConfig: configPath,
}
}
const payloadWebpackConfig = getBaseConfig(payloadConfig);
const configRequiresSharp = payloadConfig.collections.find(({ upload }) => {
if (typeof upload === "object" && upload !== null) {
if (upload.imageSizes || upload.resizeOptions || upload.formatOptions) {
return true;
}
}
return false;
});
const outputFileTracingExcludes = {
"**/*": [
"node_modules/@swc/core-linux-x64-gnu",
"node_modules/@swc/core-linux-x64-musl",
"node_modules/@swc/wasm",
"node_modules/webpack/**/*",
...(config.experimental &&
config.experimental.outputFileTracingExcludes &&
config.experimental.outputFileTracingExcludes["**/*"]
? config.experimental.outputFileTracingExcludes["**/*"]
: []),
],
...(config.experimental && config.experimental.outputFileTracingExcludes
? config.experimental.outputFileTracingExcludes
: {}),
};
if (!configRequiresSharp) {
outputFileTracingExcludes["**/*"].push("node_modules/sharp/**/*");
}
return {
...config,
experimental: {
...config.experimental,
appDir: true,
outputFileTracingExcludes,
},
webpack: (webpackConfig, webpackOptions) => {
const incomingWebpackConfig =
typeof config.webpack === "function"
? config.webpack(webpackConfig, webpackOptions)
: webpackConfig;
incomingWebpackConfig.module.rules.push({
oneOf: [
{
test: /node_modules[\\\/]payload[\\\/].*\.(?:ico|gif|png|jpg|jpeg|woff(2)?|eot|ttf|otf|svg)/,
type: "asset/resource",
},
],
});
let newWebpackConfig = {
...incomingWebpackConfig,
plugins: [
...(incomingWebpackConfig.plugins || []),
new FilterWarningsPlugin({
exclude: [/Critical dependency/, /require.extensions/],
}),
],
resolve: {
...incomingWebpackConfig.resolve,
alias: {
...incomingWebpackConfig.resolve.alias,
"@payloadcms/next-payload/getPayload":
payloadPath ||
path.resolve(process.cwd(), "./payload/payloadClient.ts"),
...payloadWebpackConfig.resolve.alias,
// [path.resolve(process.cwd(), "./node_modules/payload/dist/bundlers/webpack/bundler.js")]: mockModulePath,
// ^ useful for development with `yarn link`
},
},
};
if (!configRequiresSharp) {
newWebpackConfig.resolve.alias.sharp = mockModulePath;
}
if (typeof payloadConfig.admin.webpack === "function") {
return payloadConfig.admin.webpack(newWebpackConfig);
}
return newWebpackConfig;
},
transpilePackages: [
...(config.transpilePackages || []),
"@payloadcms/next-payload",
"payload",
"mongoose",
],
rewrites: async () => {
let userRewrites = config.rewrites;
if (typeof config.rewrites === "function") {
userRewrites = await config.rewrites();
}
const adminRoute = adminRouteArg.split("/").filter(Boolean).join("/");
const payloadAdminRewrite = {
source: `/${adminRoute}/:path*`,
destination: `/${adminRoute}`,
};
if (Array.isArray(userRewrites)) {
return [...userRewrites, payloadAdminRewrite];
}
if (userRewrites) {
return {
...userRewrites,
afterFiles: [...(userRewrites.afterFiles || []), payloadAdminRewrite],
};
}
return [payloadAdminRewrite];
},
};
};
module.exports = withPayload;