-
Notifications
You must be signed in to change notification settings - Fork 1
/
next.config.js
106 lines (92 loc) · 3.41 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
const path = require('path')
const withWorkers = require('@zeit/next-workers')
const withTM = require('next-transpile-modules')(['pdfjs-dist'])
const csp = require('./csp.config')
const helpers = require('./utils/helpers')
/** Build Target
*
* Supports the following values:
* - 'aws' – Amazon Web Services, production
* - 'now' – ZEIT Now, testing
*
* If not set or empty, means development environment.
*/
const nextConfig = {
env: {
SENTRY_DSN: process.env.SENTRY_DSN,
GA_TRACKING_CODE: process.env.GA_TRACKING_CODE,
BUILD_TARGET: process.env.BUILD_TARGET,
},
assetPrefix: helpers.getAssetPath('', process.env.BUILD_TARGET),
async headers() {
return [
{
source: '/:path(.*)',
headers: [{ key: 'Content-Security-Policy', value: csp }],
},
]
},
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 splitChunks = config.optimization && config.optimization.splitChunks
if (splitChunks) {
const { cacheGroups } = splitChunks
if (cacheGroups.framework) cacheGroups.commons.name = 'framework'
if (cacheGroups.shared) cacheGroups.shared.name = 'framework'
if (cacheGroups.lib) cacheGroups.lib.name = 'framework'
}
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.css'))
) || {}
).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.css') && 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, {
'@sentry/node': config.isServer ? '@sentry/node' : '@sentry/browser',
'react': path.join(__dirname, 'node_modules', 'react'),
'react-dom': path.join(__dirname, 'node_modules', 'react-dom'),
})
// TODO: Remove once https://github.com/zeit/next-plugins/blob/master/packages/next-workers/index.js#L20 is released
config.output.globalObject = 'self'
return config
},
}
nextConfig.workerLoaderOptions = {
publicPath: `${nextConfig.assetPrefix}/_next/`,
name: 'static/[hash].worker.js',
}
module.exports = withTM(withWorkers(nextConfig))