-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
108 lines (97 loc) · 2.89 KB
/
next.config.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
/** @type {import("next").NextConfig} */
const path = require("path");
const { IgnorePlugin } = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const { devDependencies } = require("./package.json");
// Build up your externals object from devDependencies (only for the server build).
const externals = {};
for (const devDependency of Object.keys(devDependencies)) {
externals[devDependency] = `commonjs ${devDependency}`;
}
// Optional: Add any optional modules that you want to be externalized
const optionalModules = new Set([
...Object.keys(require("knex/package.json").browser),
...Object.keys(
require("@mikro-orm/core/package.json").peerDependencies || {}
),
...Object.keys(require("@mikro-orm/core/package.json").devDependencies || {}),
]);
// Externalize all MikroORM modules
externals["@mikro-orm/core"] = "commonjs @mikro-orm/core";
externals["@mikro-orm/mariadb"] = "commonjs @mikro-orm/mariadb";
externals["@mikro-orm/migrations"] = "commonjs @mikro-orm/migrations";
externals["@mikro-orm/reflection"] = "commonjs @mikro-orm/reflection";
externals["@mikro-orm/seeder"] = "commonjs @mikro-orm/seeder";
// Externalize knex
externals["knex"] = "commonjs knex";
module.exports = {
output: "standalone",
webpack: (config, { isServer, dev, webpack }) => {
//
// 1) Externals (only do this on the server build)
//
if (isServer) {
config.externals = [
...(config.externals || []),
function ({ request }, callback) {
if (externals[request]) {
return callback(null, externals[request]);
}
callback();
},
];
}
config.plugins.push(new webpack.EnvironmentPlugin({ WEBPACK: true }));
// IgnorePlugin
config.plugins.push(
new IgnorePlugin({
checkResource: (resource) => {
const baseResource = resource
.split("/", resource[0] === "@" ? 2 : 1)
.join("/");
if (optionalModules.has(baseResource)) {
try {
require.resolve(resource);
return false;
} catch {
return true;
}
}
return false;
},
})
);
// TerserPlugin
if (!config.optimization) {
config.optimization = {};
}
config.optimization.minimizer = config.optimization.minimizer || [];
config.optimization.minimizer.push(
new TerserPlugin({
terserOptions: {
mangle: false,
compress: {
keep_classnames: true,
keep_fnames: true,
},
},
})
);
//
// 4) Custom module rules
//
config.module.rules.push(
{
test: /\.node$/,
use: "node-loader",
},
{
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto",
}
);
// Return the modified config to Next
return config;
},
};