-
Notifications
You must be signed in to change notification settings - Fork 3
/
next.config.js
86 lines (76 loc) · 2.66 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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require("fs");
const path = require("path");
const { stringify, parse } = require("flatted");
const memoize = require("fast-memoize");
const {
PHASE_PRODUCTION_BUILD,
PHASE_PRODUCTION_SERVER,
PHASE_DEVELOPMENT_SERVER,
} = require("next/constants");
const scenariosFilePath = path.join(".", "scenarios.json");
// See https://github.com/vercel/next.js/issues/10142#issuecomment-648974042
const hackStylesToSupportNonPureDeclarations = (config) => {
const oneOf = config.module.rules.find((rule) => typeof rule.oneOf === "object");
const fixUse = (use) => {
const { loader, options } = use;
if (options && options.modules && loader && loader.indexOf("css-loader") >= 0) {
options.modules.mode = "local";
}
};
if (oneOf) {
oneOf.oneOf.forEach((rule) => {
if (Array.isArray(rule.use)) {
rule.use.map(fixUse);
} else if (rule.use && rule.use.loader) {
fixUse(rule.use);
}
});
}
};
const loadScenariosJsonStringFromFile = () => {
return fs.readFileSync(scenariosFilePath);
};
const createScenariosJsonString = memoize(() => {
// eslint-disable-next-line no-global-assign
require = require("esm")(module);
require("tsconfig-paths").register({ baseUrl: `${process.cwd()}/src`, paths: {} });
require("ts-node").register({
compilerOptions: {
baseUrl: `${process.cwd()}/src`,
},
});
return stringify(require("./src/server/_loadScenarios").loadScenarios());
});
const createWriteScenariosPlugin = () => {
const createFile = () => {
fs.writeFileSync(scenariosFilePath, createScenariosJsonString());
};
return {
apply: (compiler) => {
compiler.hooks.done.tap("CreateWriteScenariosWebpack", createFile);
},
};
};
const getServerRuntimeConfig = (phase) => {
if (phase === PHASE_PRODUCTION_SERVER) {
return { scenarios: parse(loadScenariosJsonStringFromFile()) };
} else if (phase === PHASE_DEVELOPMENT_SERVER) {
return { scenariosString: createScenariosJsonString() };
}
return {};
};
module.exports = (phase) => {
const shouldWriteScenariosWithWebpack = phase === PHASE_PRODUCTION_BUILD;
const serverRuntimeConfig = getServerRuntimeConfig(phase);
return {
serverRuntimeConfig,
webpack: (config) => {
if (shouldWriteScenariosWithWebpack) {
config.plugins.push(createWriteScenariosPlugin());
}
hackStylesToSupportNonPureDeclarations(config);
return config;
},
};
};