From fdad34b09534392cd6009b4f1a9eaec41c37eb6b Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Thu, 24 Dec 2020 17:39:04 +0330 Subject: [PATCH] feat(styling): keep Nuxt style when `css=false` (#218) * fix: improve inline style detection * feat: disable amp specific styles This is useful on pure amp project. With this developer can use SFC styling. * docs: update docs --- docs/api/options.md | 2 +- lib/module.js | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/api/options.md b/docs/api/options.md index 7665f9fb..3d11f91e 100644 --- a/docs/api/options.md +++ b/docs/api/options.md @@ -6,7 +6,7 @@ General options of amp module | Option | Default | Valid values | Description | | ------ | ------- | ------------ | ----------- | | cdnBase | `https://cdn.ampproject.org/v0/` | Any String | A CDN Domain to load AMP elements scripts | -| css | `undefined` | Path to CSS style | Custom styles for AMP pages, eg. `~/assets/style/amp-custom.css` | +| css | `undefined` | `false` or Path to CSS style | Custom styles for AMP pages, eg. `~/assets/style/amp-custom.css`. By setting this option to `false`, the module will not remove inline styles, instead it will try to merge all inline styles into a single style tag.| | tags | `{}` | Object of tag options | Define new tags or modify current tags, for instance if you want to use `amp-mustache` version 0.1, tags value must be `{ 'amp-mustache': { version: '0.1' } }` | | origin | `` | Any String | Main domain of website. Using this AMP modules tries to add missing canonical link for pages. | | mode | `hybrid` | `only\|hybrid\|false` | Default behaviour of amp module. (`only` all pages serve in AMP mode by default, `hybrid` pages serves in both normal and AMP mode, `false` pages does not serve AMP by default ) | diff --git a/lib/module.js b/lib/module.js index 6cabcc68..d363af0a 100644 --- a/lib/module.js +++ b/lib/module.js @@ -159,11 +159,25 @@ function registerRendererHook (options) { params.HEAD = params.HEAD .replace(scriptPattern, v => (v.includes('custom-element') || v.includes('application/ld+json')) ? v : '') + + if (options.css !== false) { + /** + * Remove inline styles + */ + params.HEAD = params.HEAD + .replace(/]*>[.\S\s]*?<\/style>/g, '') + } else { /** - * Remove external styles + * Merge inline styles */ - .replace(/]*>.*?<\/style>/g, '') + params.HEAD = params.HEAD.replace(/<\/style>\S*