forked from dfinity/motoko-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
craco.config.js
80 lines (70 loc) · 2.25 KB
/
craco.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
const { addBeforeLoader, loaderByName } = require("@craco/craco");
const webpack = require("webpack");
// Patch unsupported MD4 hash function for Node >= 17.x
const crypto = require("crypto");
const { createHash } = crypto;
crypto.createHash = (algorithm) =>
createHash(algorithm === "md4" ? "sha256" : algorithm);
let canisterEnv;
function initCanisterIds() {
let localCanisters, prodCanisters, canisters;
try {
localCanisters = require("./.dfx/local/canister_ids.json");
} catch (error) {
console.log("No local canister_ids.json found. Continuing production");
}
try {
prodCanisters = require("./canister_ids.json");
} catch (error) {
console.log("No production canister_ids.json found. Continuing with local");
}
const localNetwork = "local";
const network = process.env.DFX_NETWORK || localNetwork;
canisters = network === localNetwork ? localCanisters : prodCanisters;
for (const canister in canisters) {
const canisterName = canister.toUpperCase() + "_CANISTER_ID";
process.env[canisterName] = canisters[canister][network];
canisterEnv = {
...canisterEnv,
[canisterName]: canisters[canister][network],
};
}
}
initCanisterIds();
const overrideWebpackConfig = ({ webpackConfig }) => {
webpackConfig.resolve.plugins = webpackConfig.resolve.plugins.filter(
(plugin) =>
// Removes ModuleScopePlugin so `dfx-generated/` aliases work correctly
!Object.keys(plugin).includes("appSrcs")
);
webpackConfig.plugins.push(new webpack.EnvironmentPlugin(canisterEnv));
// Load WASM modules
webpackConfig.resolve.extensions.push(".wasm");
webpackConfig.module.rules.forEach((rule) => {
(rule.oneOf || []).forEach((oneOf) => {
if (oneOf.loader && oneOf.loader.indexOf("file-loader") >= 0) {
oneOf.exclude.push(/\.wasm$/);
}
});
});
addBeforeLoader(webpackConfig, loaderByName("file-loader"), {
test: /\.wasm$/,
exclude: /node_modules/,
loaders: ["wasm-loader"],
});
return webpackConfig;
};
module.exports = {
plugins: [
{
plugin: { overrideWebpackConfig },
},
{
// Fixes a Babel error encountered on Node 16.x / 18.x
plugin: require("craco-babel-loader"),
options: {
includes: [/(\.dfx)/],
},
},
],
};