webpack.config.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const path = require('path');
  2. module.exports = ({ config, mode }) => {
  3. // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
  4. config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/];
  5. // use installed babel-loader which is v8.0-beta (which is meant to work with @babel/core@7)
  6. config.module.rules[0].use[0].loader = require.resolve('babel-loader');
  7. // use @babel/preset-react for JSX and env (instead of staged presets)
  8. config.module.rules[0].use[0].options.presets = [
  9. require.resolve('@babel/preset-react'),
  10. require.resolve('@babel/preset-env'),
  11. ];
  12. config.module.rules[0].use[0].options.plugins = [
  13. // use @babel/plugin-proposal-class-properties for class arrow functions
  14. require.resolve('@babel/plugin-proposal-class-properties'),
  15. // use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
  16. require.resolve('babel-plugin-remove-graphql-queries'),
  17. ];
  18. // Prefer Gatsby ES6 entrypoint (module) over commonjs (main) entrypoint
  19. config.resolve.mainFields = ['browser', 'module', 'main'];
  20. config.module.rules.push({
  21. test: /\.scss$/,
  22. use: ['style-loader', 'css-loader', 'sass-loader'],
  23. include: path.resolve(__dirname, '../'),
  24. });
  25. config.module.rules = config.module.rules.map(rule => {
  26. if (!rule.test.test('.svg')) {
  27. return rule;
  28. }
  29. const newRule = rule;
  30. // Changes existing default rule to not handle SVG files
  31. newRule.test = /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/;
  32. return newRule;
  33. });
  34. // Adds new SVG loader
  35. config.module.rules.push({
  36. test: /\.svg$/,
  37. use: ['@svgr/webpack', 'url-loader'],
  38. });
  39. config.module.rules.push({
  40. test: /\.stories\.jsx?$/,
  41. loaders: [require.resolve('@storybook/addon-storysource/loader')],
  42. enforce: 'pre',
  43. });
  44. return config;
  45. };