-
Notifications
You must be signed in to change notification settings - Fork 13
/
next.config.js
87 lines (78 loc) · 2.45 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
const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
const SSRPlugin =
require('next/dist/build/webpack/plugins/nextjs-ssr-import').default;
const {
dirname,
relative,
resolve,
join,
} = require('path');
module.exports = {
webpack(config) {
// Ensures that web workers can import scripts.
config.output.publicPath = '/_next/';
// From https://github.com/rustwasm/wasm-pack/issues/835#issuecomment-772591665
config.experiments = {
syncWebAssembly: true,
};
config.module.rules.push({
test: /\.wasm$/,
type: 'webassembly/sync',
});
// From https://github.com/wasm-tool/wasm-pack-plugin
config.plugins.push(
new WasmPackPlugin({
crateDirectory: resolve('./rust'),
args: '--log-level warn',
})
);
// From https://github.com/vercel/next.js/issues/22581#issuecomment-864476385
const ssrPlugin = config.plugins.find(
(plugin) => plugin instanceof SSRPlugin
);
if (ssrPlugin) {
patchSsrPlugin(ssrPlugin);
}
return config;
},
};
// Patch the NextJsSSRImport plugin to not throw with WASM generated chunks.
function patchSsrPlugin(plugin) {
plugin.apply = function apply(compiler) {
compiler.hooks.compilation.tap(
'NextJsSSRImport',
(compilation) => {
compilation.mainTemplate.hooks.requireEnsure.tap(
'NextJsSSRImport',
(code, chunk) => {
// The patch that we need to ensure this plugin doesn't throw
// with WASM chunks.
if (!chunk.name) {
return;
}
// Update to load chunks from our custom chunks directory
const outputPath = resolve('/');
const pagePath = join('/', dirname(chunk.name));
const relativePathToBaseDir = relative(
pagePath,
outputPath
);
// Make sure even in windows, the path looks like in unix
// Node.js require system will convert it accordingly
const relativePathToBaseDirNormalized =
relativePathToBaseDir.replace(/\\/g, '/');
return code
.replace(
'require("./"',
`require("${relativePathToBaseDirNormalized}/"`
)
.replace(
'readFile(join(__dirname',
`readFile(join(__dirname, "${relativePathToBaseDirNormalized}"`
);
}
);
}
);
};
}