gatsby-node.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import path from 'path';
  2. import dotenv from 'dotenv';
  3. dotenv.config({
  4. path: `.env.${process.env.NODE_ENV}`,
  5. });
  6. export const onCreateWebpackConfig = function addPathMapping({
  7. stage,
  8. actions,
  9. getConfig,
  10. }) {
  11. actions.setWebpackConfig({
  12. resolve: {
  13. alias: {
  14. '~': path.resolve(__dirname, 'src'),
  15. },
  16. },
  17. });
  18. // TODO: make sure this only runs in dev
  19. actions.setWebpackConfig({
  20. devtool: 'eval-source-map',
  21. });
  22. const wasmExtensionRegExp = /\.wasm$/;
  23. actions.setWebpackConfig({
  24. module: {
  25. rules: [
  26. {
  27. test: wasmExtensionRegExp,
  28. include: /node_modules\/(bridge|token-bridge|nft)/,
  29. use: ['wasm-loader'],
  30. type: "javascript/auto"
  31. }
  32. ]
  33. }
  34. });
  35. if (stage === 'build-html') {
  36. // exclude wasm from SSR
  37. actions.setWebpackConfig({
  38. externals: getConfig().externals.concat(function (context, request, callback) {
  39. const regex = wasmExtensionRegExp;
  40. // exclude wasm from being bundled in SSR html, it will be loaded async at runtime.
  41. if (regex.test(request)) {
  42. return callback(null, 'commonjs ' + request); // use commonjs for wasm modules
  43. }
  44. const bridge = new RegExp('/wormhole-sdk/')
  45. if (bridge.test(request)) {
  46. return callback(null, 'commonjs ' + request);
  47. }
  48. callback();
  49. })
  50. });
  51. }
  52. // Attempt to improve webpack vender code splitting
  53. if (stage === 'build-javascript') {
  54. const config = getConfig();
  55. config.optimization.splitChunks.cacheGroups = {
  56. ...config.optimization.splitChunks.cacheGroups,
  57. vendors: {
  58. test: /[\\/]node_modules[\\/]/,
  59. enforce: true,
  60. chunks: 'all',
  61. priority: 1,
  62. },
  63. };
  64. // Ensure Gatsby does not do any css code splitting
  65. config.optimization.splitChunks.cacheGroups.styles.priority = 10;
  66. actions.replaceWebpackConfig(config);
  67. }
  68. };