-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.config.js
198 lines (194 loc) · 6.92 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs');
const path = require('path');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const { StatsWriterPlugin } = require('webpack-stats-plugin');
const ThreeMinifierPlugin = require('@yushijinhun/three-minifier-webpack');
const transformShaderChunk = require('three-minify-shaderchunk');
const shadersToInclude = require('./src/util/three/three_shaders');
const genCssTypings = require('css-modules-typescript-generator')({
stylesDir: path.join(__dirname, 'src', 'styles'),
});
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
const SRC = path.join(__dirname, 'src');
const STYLES = path.join(SRC, 'styles');
const UTIL = path.join(SRC, 'util');
const THREE_EXPORTS = path.join(UTIL, 'three', 'three_exports.ts');
// Ensure aliased, relative imported files exist
// TODO move this into a separate module
const ALIASED_FILES = [
'node_modules/three/src/renderers/WebGLRenderer.js',
'node_modules/three/src/renderers/webxr/WebXRManager.js',
'node_modules/three/src/renderers/webgl/WebGLCubeMaps.js',
'node_modules/three/src/renderers/webgl/WebGLCubeUVMaps.js',
'node_modules/three/src/renderers/webgl/WebGLMorphtargets.js',
'node_modules/three/src/renderers/webgl/WebGLAnimation.js',
'node_modules/three/src/renderers/webgl/WebGLShadowMap.js',
];
for (const file of ALIASED_FILES) {
const filePath = path.join(__dirname, file);
try {
if (!fs.existsSync(filePath)) {
throw new Error(`File ${filePath} doesn't exist. Please update next.config.js`);
}
} catch (err) {
console.error(err);
}
}
// * Have to set 'unsafe-eval' to enable WASM
const ContentSecurityPolicy = `
default-src 'self';
base-uri 'none';
object-src 'none';
style-src 'self' 'unsafe-inline';
img-src 'self' data: blob:;
worker-src 'self' blob:;
script-src 'self' 'unsafe-eval';
script-src-attr 'none';
connect-src 'self' data: https://www.gstatic.com/draco/versioned/decoders/1.4.3/;
frame-ancestors 'self';
form-action 'self';
`;
// TODO update postcss config
/** @type {import('next').NextConfig} */
module.exports = withBundleAnalyzer({
reactStrictMode: true,
swcMinify: false, // TODO currently outputs larger bundle sizes
images: {
formats: ['image/avif', 'image/webp'],
},
sassOptions: {
includePaths: [STYLES],
additionalData: (content, { resourcePath }) => {
genCssTypings(content, resourcePath);
return null;
},
},
webpack: (config, { isServer, dev }) => {
config.performance.hints = 'warning';
config.plugins.push(
new StyleLintPlugin({
configFile: '.stylelintrc',
context: STYLES,
emitError: true,
emitWarning: true,
})
);
if (process.env.ANALYZE === 'true') {
config.profile = true;
config.plugins.push(
new StatsWriterPlugin({
filename: 'stats.json',
stats: {
context: './src',
assets: true,
entrypoints: true,
chunks: true,
chunkModules: true,
modules: true,
children: true,
cached: true,
reasons: true,
},
})
);
}
config.module.rules.push(
{
test: /\.(glsl|vs|fs|vert|frag)$/,
exclude: /node_modules/,
type: 'asset/source',
use: 'glslify-loader',
},
{
test: /\.worker\.(js|cjs|mjs|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: 'worker-loader',
},
{
test: /ShaderChunk.js$/,
loader: 'string-replace-loader',
include: path.resolve('./node_modules/three/src/renderers/shaders'),
options: {
search: /[\s\S]*/,
replace: transformShaderChunk(shadersToInclude),
strict: true,
},
}
);
if (!isServer) {
if (!dev) {
config.cache = false;
// const threeMinifier = new ThreeMinifierPlugin();
// config.plugins.unshift(threeMinifier);
// config.resolve.plugins.unshift(threeMinifier.resolver);
}
config.resolve.alias = {
...config.resolve.alias,
three$: THREE_EXPORTS,
'./webxr/WebXRManager.js': THREE_EXPORTS,
'./webgl/WebGLCubeMaps.js': THREE_EXPORTS,
'./webgl/WebGLCubeUVMaps.js': THREE_EXPORTS,
'./webgl/WebGLMorphtargets.js': THREE_EXPORTS,
'./webgl/WebGLAnimation.js': THREE_EXPORTS,
'./webgl/WebGLShadowMap.js': THREE_EXPORTS,
// TODO ...
};
}
return config;
},
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'X-DNS-Prefetch-Control',
value: 'on',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
{
key: 'X-Permitted-Cross-Domain-Policies',
value: 'none',
},
{
key: 'X-XSS-Protection',
value: '0',
},
{
key: 'Referrer-Policy',
value: 'strict-origin-when-cross-origin',
},
{
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
},
{
key: 'Content-Security-Policy',
value: ContentSecurityPolicy.replace(/\s{2,}/g, ' ').trim(),
},
{
key: 'Origin-Agent-Cluster',
value: '?1',
},
{
key: 'cross-origin-opener-policy',
value: 'same-origin',
},
{
key: 'cross-origin-resource-policy',
value: 'same-origin',
},
{
key: 'cross-origin-embedder-policy',
value: 'require-corp',
},
],
},
];
},
});