From f86b980ba8afb9ac0f9d0a914d0fc10de9e3c2aa Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 25 Jun 2021 17:13:12 +0300 Subject: [PATCH] fix inline svg buggy optimization temporary workaround for https://github.com/gatsbyjs/gatsby/issues/31878 --- gatsby-node.js | 110 +++++++++++++++++++++--- jsconfig.json | 2 +- src/components/Header/style.module.scss | 2 +- 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 8ad65c51..3c11cfb3 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -4,20 +4,102 @@ * See: https://www.gatsbyjs.com/docs/node-apis/ */ -// You can delete this file if you're not using it - const path = require('path') -exports.onCreateWebpackConfig = ({ actions }) => { - actions.setWebpackConfig({ - resolve: { - alias: { - '~components': path.resolve(__dirname, 'src/components'), - // '~containers': path.resolve(__dirname, 'src/containers'), - '~styles': path.resolve(__dirname, 'src/styles'), - '~img': path.resolve(__dirname, 'src/assets/img'), - '~fonts': path.resolve(__dirname, 'src/assets/fonts'), - '~images': path.resolve(__dirname, 'src/images'), + +const ALIASES = { + '~components': path.resolve(__dirname, 'src/components'), + // '~containers': path.resolve(__dirname, 'src/containers'), + '~styles': path.resolve(__dirname, 'src/styles'), + '~img': path.resolve(__dirname, 'src/assets/img'), + '~fonts': path.resolve(__dirname, 'src/assets/fonts'), + '~images': path.resolve(__dirname, 'src/images'), +} + +// TODO: temporary workaround for https://github.com/gatsbyjs/gatsby/issues/31878 +exports.onCreateWebpackConfig = ({ actions, plugins, stage, getConfig }) => { + // override config only during production JS & CSS build + if (stage === 'build-javascript') { + // get current webpack config + const config = getConfig() + + const options = { + minimizerOptions: { + preset: [ + `default`, + { + svgo: { + full: true, + plugins: [ + // potentially destructive plugins removed - see https://github.com/gatsbyjs/gatsby/issues/15629 + // use correct config format and remove plugins requiring specific params - see https://github.com/gatsbyjs/gatsby/issues/31619 + `removeUselessDefs`, + `cleanupAttrs`, + `cleanupEnableBackground`, + `cleanupIDs`, + `cleanupListOfValues`, + `cleanupNumericValues`, + `collapseGroups`, + `convertColors`, + `convertPathData`, + `convertStyleToAttrs`, + `convertTransform`, + `inlineStyles`, + `mergePaths`, + `minifyStyles`, + `moveElemsAttrsToGroup`, + `moveGroupAttrsToElems`, + `prefixIds`, + `removeAttrs`, + `removeComments`, + `removeDesc`, + `removeDimensions`, + `removeDoctype`, + `removeEditorsNSData`, + `removeEmptyAttrs`, + `removeEmptyContainers`, + `removeEmptyText`, + `removeHiddenElems`, + `removeMetadata`, + `removeNonInheritableGroupAttrs`, + `removeOffCanvasPaths`, + `removeRasterImages`, + `removeScriptElement`, + `removeStyleElement`, + `removeTitle`, + `removeUnknownsAndDefaults`, + `removeUnusedNS`, + `removeUselessStrokeAndFill`, + `removeXMLProcInst`, + `reusePaths`, + `sortAttrs`, + ], + }, + }, + ], + }, + } + // find CSS minimizer + const minifyCssIndex = config.optimization.minimizer.findIndex( + (minimizer) => minimizer.constructor.name === 'CssMinimizerPlugin' + ) + // if found, overwrite existing CSS minimizer with the new one + if (minifyCssIndex > -1) { + config.optimization.minimizer[minifyCssIndex] = plugins.minifyCss(options) + } + const modifiedAliases = { + ...config.resolve.alias, + ...ALIASES, + } + + config.resolve.alias = modifiedAliases + + // replace webpack config with the modified object + actions.replaceWebpackConfig(config) + } else { + actions.setWebpackConfig({ + resolve: { + alias: ALIASES, }, - }, - }) + }) + } } diff --git a/jsconfig.json b/jsconfig.json index 2b4c8819..5ca4ec2c 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -2,7 +2,7 @@ "description": "This is to config VS Code to recongnise our aliases, aka https://medium.com/@justintulk/solve-module-import-aliasing-for-webpack-jest-and-vscode-74007ce4adc9", "compilerOptions": { "baseUrl": ".", - "jsx": "preserve", + // "jsx": "preserve", "paths": { // "api": ["api"], // "globalstore": ["store.js"], diff --git a/src/components/Header/style.module.scss b/src/components/Header/style.module.scss index 546a160c..3b17b5d2 100644 --- a/src/components/Header/style.module.scss +++ b/src/components/Header/style.module.scss @@ -9,6 +9,6 @@ justify-self: start; } @include b-d(sm) { - margin-bottom: 30px; + // Code... } }