-
Notifications
You must be signed in to change notification settings - Fork 2
/
next.config.js
151 lines (139 loc) · 4.61 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
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
150
151
const withTM = require("next-transpile-modules")([
"library-simplified-webpub-viewer"
]);
const {
BugsnagBuildReporterPlugin,
BugsnagSourceMapUploaderPlugin
} = require("webpack-bugsnag-plugins");
const withSourceMaps = require("@zeit/next-source-maps");
const chalk = require("chalk");
const package = require("./package.json");
const APP_VERSION = package.version;
const { NODE_ENV, CONFIG_FILE, REACT_AXE } = process.env;
const log = (...message) =>
console.log(chalk.blue("app info") + " -", ...message);
// Compute some git info by running commands in a child process
const execSync = require("child_process").execSync;
const GIT_COMMIT_SHA = execSync("git rev-parse HEAD").toString().trim();
const GIT_BRANCH = execSync("git rev-parse --abbrev-ref HEAD")
.toString()
.trim();
// compute the release stage of the app
const RELEASE_STAGE =
NODE_ENV === "production" && GIT_BRANCH === "production"
? "production"
: NODE_ENV === "production" && GIT_BRANCH === "qa"
? "qa"
: "development";
const BUILD_ID = `${APP_VERSION}-${GIT_BRANCH}.${GIT_COMMIT_SHA}`;
// fetch the config file synchronously. This will wait until the command exits to continue.
const APP_CONFIG = JSON.parse(
execSync("node --unhandled-rejections=strict src/config/fetch-config.js", {
encoding: "utf-8"
})
);
/**
* Set the AXISNOW_DECRYPT variable based on whether the package is available.
*/
let AXISNOW_DECRYPT = false;
try {
const Decryptor = require("@nypl-simplified-packages/axisnow-access-control-web");
if (Decryptor) AXISNOW_DECRYPT = true;
log("AxisNow Decryptor package is available.");
} catch (e) {
log("AxisNow Decryptor package is not available.");
}
// log some info to the console for the record.
log(`Instance Name: ${APP_CONFIG.instanceName}`);
log(`CONFIG_FILE: ${CONFIG_FILE}`);
log(`GIT_BRANCH: ${GIT_BRANCH}`);
log(`APP_VERSION: ${APP_VERSION}`);
log(`NODE_ENV: ${NODE_ENV}`);
log(`RELEASE_STAGE: ${RELEASE_STAGE}`);
log(`BUILD_ID: ${BUILD_ID}`);
log(
`AXISNOW_DECRYPT: ${AXISNOW_DECRYPT} (based on availability of decryptor package)`
);
log(`Companion App: ${APP_CONFIG.companionApp}`);
log(`Show Medium: ${APP_CONFIG.showMedium ? "enabled" : "disabled"}`);
log(
`Google Tag Manager: ${
APP_CONFIG.gtmId
? `enabled - ${APP_CONFIG.gtmId}`
: "disabled (no gtm_id in config file)"
}`
);
log(
`Bugsnag: ${
APP_CONFIG.bugsnagApiKey
? `enabled - ${APP_CONFIG.bugsnagApiKey}`
: "disabled (no bugsnag_api_key in config file)"
}`
);
log(`Open eBooks Config: `, APP_CONFIG.openebooks);
log(`Media Support: `, APP_CONFIG.mediaSupport);
log(`Libraries: `, APP_CONFIG.libraries);
const config = {
env: {
CONFIG_FILE: CONFIG_FILE,
REACT_AXE: REACT_AXE,
APP_VERSION,
BUILD_ID,
GIT_BRANCH,
GIT_COMMIT_SHA,
RELEASE_STAGE,
AXISNOW_DECRYPT,
APP_CONFIG: JSON.stringify(APP_CONFIG)
},
productionBrowserSourceMaps: true,
generateBuildId: async () => BUILD_ID,
webpack: (config, { dev, isServer, _defaultLoaders, webpack }) => {
console.log(
chalk.cyan("info -"),
`Building ${isServer ? "server" : "client"} files.`
);
// Note: we provide webpack above so you should not `require` it
// Perform customizations to webpack config
// Important: return the modified config
!isServer && config.plugins.push(new webpack.IgnorePlugin(/jsdom$/));
// react-axe should only be bundled when REACT_AXE=true
!REACT_AXE === "true" &&
config.plugins.push(new webpack.IgnorePlugin(/react-axe$/));
// Fixes dependency on "fs" module.
// we don't (and can't) depend on this in client-side code.
if (!isServer) {
config.node = {
fs: "empty"
};
}
// ignore the axisnow decryptor if we don't have access
if (!AXISNOW_DECRYPT) {
config.plugins.push(
new webpack.NormalModuleReplacementPlugin(
/@nypl-simplified-packages\/axisnow-access-control-web/,
"utils/mockDecryptor.ts"
)
);
}
// upload sourcemaps to bugsnag if we are not in dev
if (!dev && APP_CONFIG.bugsnagApiKey) {
const bugsnagConfig = {
apiKey: APP_CONFIG.bugsnagApiKey,
appVersion: BUILD_ID
};
config.plugins.push(new BugsnagBuildReporterPlugin(bugsnagConfig));
config.plugins.push(
new BugsnagSourceMapUploaderPlugin({
...bugsnagConfig,
publicPath: isServer ? "" : "*/_next",
overwrite: true
})
);
}
return config;
}
};
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true"
});
module.exports = withSourceMaps(withTM(withBundleAnalyzer(config)));