From 258961d79fcb3fdc54e7483bf4e34ed4c22f8666 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Fri, 20 Dec 2019 10:49:31 +1300 Subject: [PATCH] BUGFIX: Fix purged table css --- gatsby-config.js | 44 ++------------------------- purgecss.config.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 purgecss.config.js diff --git a/gatsby-config.js b/gatsby-config.js index 06b1dda..e974042 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -1,4 +1,4 @@ -const path = require('path'); +const purgeCSSConfig = require('./purgecss.config'); const sources = process.env.DOCS_CONTEXT === 'user' ? require('./sources-user') : require('./sources-docs'); @@ -66,47 +66,7 @@ module.exports = { { resolve: `gatsby-plugin-purgecss`, options: { - printRejected: false, - whitelist: ['algolia-autocomplete', 'pre', 'code'], - ignore: ['prismjs/','docsearch.js/', 'src/theme/assets/search/algolia.css'], - content: [ - // All the markdown in the git repos - path.join(process.cwd(), '.cache/gatsby-source-git/**/*.md'), - // Components - path.join(process.cwd(), 'src/components/!(*.d).{ts,js,jsx,tsx}'), - // Static pages (e.g. 404) - path.join(process.cwd(), 'src/pages/!(*.d).{ts,js,jsx,tsx}'), - // Page templates - path.join(process.cwd(), 'src/templates/!(*.d).{ts,js,jsx,tsx}'), - ], - extractors: [ - { - // Simple extractor just matches against components and templates (e.g. JSX) - extractor: class { - static extract(content) { - return content.match(/[A-Za-z0-9-_:\/]+/g) || []; - } - }, - extensions: ['js', 'ts', 'jsx', 'tsx'] - }, - { - // Match markdown files for icon classes (icon, iconBrand). Add each one to the - // allowed selectors defined in FontAwesome. Everything else in FA should be removed. - extractor: class { - static extract(content) { - const selectors = [`fa-file-alt`] - const matches = content.match(/icon(Brand)?: ([a-zA-Z0-9_-]+)/); - if (matches) { - const isBrand = typeof matches[1] !== 'undefined'; - selectors.push(isBrand ? `fab` : `fas`); - selectors.push(`fa-${matches[2]}`); - } - return selectors; - } - }, - extensions: ['md'] - }, - ] + ...purgeCSSConfig, }, }, `gatsby-plugin-remove-serviceworker`, diff --git a/purgecss.config.js b/purgecss.config.js new file mode 100644 index 0000000..3b55584 --- /dev/null +++ b/purgecss.config.js @@ -0,0 +1,74 @@ +const path = require('path'); + +// Standard keyword match for React components +class DefaultExtractor { + static extract(content) { + return content.match(/[A-Za-z0-9-_:\/]+/g) || []; + } +} + +// Scan frontmatter in Markdown docs for icons that they use, e.g. "icon: database" +class IconExtractor { + static extract(content) { + const selectors = [`fa-file-alt`] + const matches = content.match(/icon(Brand)?: ([a-zA-Z0-9_-]+)/); + if (matches) { + const isBrand = typeof matches[1] !== 'undefined'; + selectors.push(isBrand ? `fab` : `fas`); + selectors.push(`fa-${matches[2]}`); + } + return selectors; + } +} + +// Classes that are added at build time need to be explicitly ignored +const whitelist = [ + // Algolia + 'algolia-autocomplete', + + // Syntax highlighting + 'pre', + 'code', +]; + +const whitelistPatterns = [ + /^callout-/, +]; + +const whitelistPatternsChildren = [ + /^table/, +]; + +module.exports = { + printRejected: false, + whitelist, + whitelistPatterns, + whitelistPatternsChildren, + // Don't try to purge any CSS in the thirdparty libraries that do their own rendering + // (syntax highlight, Algolia) + ignore: [ + 'prismjs/', + 'docsearch.js/', + 'src/theme/assets/search/algolia.css', + ], + content: [ + // All the markdown in the git repos + path.join(process.cwd(), '.cache/gatsby-source-git/**/*.md'), + // Components + path.join(process.cwd(), 'src/components/!(*.d).{ts,js,jsx,tsx}'), + // Static pages (e.g. 404) + path.join(process.cwd(), 'src/pages/!(*.d).{ts,js,jsx,tsx}'), + // Page templates + path.join(process.cwd(), 'src/templates/!(*.d).{ts,js,jsx,tsx}'), + ], + extractors : [ + { + extractor: DefaultExtractor, + extensions: ['js', 'ts', 'jsx', 'tsx'], + }, + { + extractor: IconExtractor, + extensions: ['md'] + } + ], +}; \ No newline at end of file