-
Notifications
You must be signed in to change notification settings - Fork 5
/
next.config.js
133 lines (121 loc) · 4.02 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
const fs = require('fs').promises
const path = require('path')
const withImages = require('next-images')
const yaml = require('js-yaml')
const envConfig = require('./env.config')
let legacyConfig = null
const readLegacyConfig = async (filepath = './legacy.config.yml') => {
if (legacyConfig == null) {
const contents = await fs.readFile(filepath)
const config = await yaml.load(contents)
legacyConfig = config
}
return legacyConfig
}
const isProd = process.env.NODE_ENV === 'production'
const nextConfig = {
env: Object.assign(envConfig, {
GA_CODE: process.env.GA_CODE,
GITHUB_TOKEN: process.env.GITHUB_TOKEN,
}),
assetPrefix: isProd ? 'https://core.ac.uk' : '',
images: {
unoptimized: true,
},
dynamicAssetPrefix: true,
webpack: (config) => {
const originalEntry = config.entry
config.entry = async () => {
const entries = await originalEntry()
if (entries['main.js'] && !entries['main.js'].includes('./polyfills.js'))
entries['main.js'].unshift('./polyfills.js')
return entries
}
const { rules } = config.module
// TODO: Remove once https://github.com/zeit/next.js/issues/10584 is solved and released
// Find the array of "style rules" in the webpack config.
// This is the array of webpack rules that:
// - is inside a 'oneOf' block
// - contains a rule that matches 'file.css'
const styleRules = (
rules.find(
(m) => m.oneOf && m.oneOf.find(({ test: reg }) => reg.test('file.scss'))
) || {}
).oneOf
if (!styleRules) return config
// Find all the webpack rules that handle CSS modules
// Look for rules that match '.module.css'
// but aren't being used to generate
// error messages.
const cssModuleRules = [
styleRules.find(
({ test: reg, use }) =>
reg.test('file.module.scss') && use.loader !== 'error-loader'
),
].filter((n) => n) // remove 'undefined' values
// Add the 'localsConvention' config option to the CSS loader config
// in each of these rules.
cssModuleRules.forEach((cmr) => {
// Find the item inside the 'use' list that defines css-loader
const cssLoaderConfig = cmr.use.find(({ loader }) =>
loader.includes('css-loader')
)
if (cssLoaderConfig && cssLoaderConfig.options) {
// Patch it with the new config
cssLoaderConfig.options.modules.exportLocalsConvention = 'camelCase'
}
})
Object.assign(config.resolve.alias, {
'components': path.resolve(__dirname, 'components'),
'design-v2': path.resolve(__dirname, 'design-v2'),
'templates': path.resolve(__dirname, 'templates'),
'data': path.resolve(__dirname, 'data'),
'content': path.resolve(__dirname, 'content'),
'hooks': path.resolve(__dirname, 'hooks'),
'helpers': path.resolve(__dirname, 'helpers'),
'api': path.resolve(__dirname, 'api'),
'main': path.join(__dirname, 'main'),
'store': path.join(__dirname, 'store'),
'react': path.join(__dirname, 'node_modules', 'react'),
'react-dom': path.join(__dirname, 'node_modules', 'react-dom'),
})
config.module.rules.push(
{
test: /\.ya?ml$/,
include: [path.resolve(__dirname, 'data')],
use: [
'json-loader',
path.resolve('webpack/data-loader.js'),
path.resolve('webpack/data-loader/formap.js'),
{
loader: 'yaml-import-loader',
options: {
output: 'json',
importRawKeyword: 'file',
},
},
],
},
{
test: /\.ya?ml$/,
exclude: [path.resolve(__dirname, 'data')],
use: {
loader: 'yaml-import-loader',
options: {
importNested: false,
},
},
},
{
test: /\.md$/,
use: ['json-loader', 'yaml-frontmatter-loader'],
}
)
return config
},
async redirects() {
const config = await readLegacyConfig()
return config.redirects
},
}
module.exports = withImages(nextConfig)