From a0805eee05a2eb8d2b95802ad7fffb6706846939 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Thu, 17 Nov 2022 20:46:27 -0500 Subject: [PATCH] Bug/issue 763 refine import rule handling (#999) * setting up test cases for additional import rule examples * support non relative @import url rule * support arbitrary @ rules * @supports and single declaration handling * test for more @ rules * update related specs --- .../plugins/resource/plugin-standard-css.js | 18 +++++--- .../fixtures/expected.css | 42 ++++++++++++------- .../src/styles/main.css | 32 ++++++++++++++ .../src/styles/theme.css | 7 +++- .../build.config-optimization-inline.spec.js | 2 +- ...uild.config-optimization-overrides.spec.js | 2 +- ...d.default.workspace-javascript-css.spec.js | 4 +- ...lt.workspace-template-page-and-app.spec.js | 8 ++-- .../test/cases/default/default.spec.js | 2 +- .../options.extend-config.spec.js | 2 +- 10 files changed, 89 insertions(+), 30 deletions(-) diff --git a/packages/cli/src/plugins/resource/plugin-standard-css.js b/packages/cli/src/plugins/resource/plugin-standard-css.js index 262263b68..34bd2769a 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-css.js +++ b/packages/cli/src/plugins/resource/plugin-standard-css.js @@ -28,6 +28,8 @@ function bundleCss(body, url) { const importContents = fs.readFileSync(path.resolve(path.dirname(url), value), 'utf-8'); optimizedCss += bundleCss(importContents, url); + } else { + optimizedCss += `@import url('${value}');`; } } else if (type === 'Atrule' && name !== 'import') { optimizedCss += `@${name} `; @@ -58,6 +60,8 @@ function bundleCss(body, url) { optimizedCss += `${name}(`; } else if (type === 'MediaFeature') { optimizedCss += ` (${name}:`; + } else if (type === 'Parentheses') { + optimizedCss += '('; } else if (type === 'PseudoElementSelector') { optimizedCss += `::${name}`; } else if (type === 'Block') { @@ -124,19 +128,20 @@ function bundleCss(body, url) { } } }, - leave: function(node, item) { + leave: function(node, item) { // eslint-disable-line complexity switch (node.type) { case 'Atrule': - if (node.name !== 'import') { - optimizedCss += '}'; + if (!node.block && node.name !== 'import') { + optimizedCss += ';'; } break; - case 'Rule': + case 'Block': optimizedCss += '}'; break; case 'Function': case 'MediaFeature': + case 'Parentheses': optimizedCss += ')'; break; case 'PseudoClassSelector': @@ -160,7 +165,10 @@ function bundleCss(body, url) { optimizedCss += '!important'; } - optimizedCss += ';'; + if (item.next || (item.prev && !item.next)) { + optimizedCss += ';'; + } + break; case 'Selector': if (item.next) { diff --git a/packages/cli/test/cases/build.config.optimization-default/fixtures/expected.css b/packages/cli/test/cases/build.config.optimization-default/fixtures/expected.css index c99671ff2..131b5f07f 100644 --- a/packages/cli/test/cases/build.config.optimization-default/fixtures/expected.css +++ b/packages/cli/test/cases/build.config.optimization-default/fixtures/expected.css @@ -1,43 +1,57 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + :root,:host{--primary-color:#16f;--secondary-color:#ff7;} @font-face {font-family:'Source Sans Pro';font-style:normal;font-weight:400;font-display:swap;src:local('Source Sans Pro Regular'),local('SourceSansPro-Regular'),url('/assets/fonts/source-sans-pro-v13-latin-regular.woff2')format('woff2'),url('/assets/fonts/source-sans-pro-v13-latin-regular.woff')format('woff'),url('/assets/fonts/source-sans-pro-v13-latin-regular.ttf')format('truetype');} +@import url('https://fonts.googleapis.com/css?family=Raleway&display=swap'); + *{margin:0;padding:0;font-family:'Comic Sans',sans-serif;} -body{background-color:green;} +body{background-color:green} h1,h2{color:var(--primary-color);border:0.5px solid #dddde1;} -#foo,.bar{color:var(--secondary-color);} +#foo,.bar{color:var(--secondary-color)} -div>p{display:none;} +div>p{display:none} -a[title]{color:purple;} +a[title]{color:purple} -@media screen and (max-width:992px){body{background-color:blue;}} +@media screen and (max-width:992px){body{background-color:blue}} p::first-line{color:blue;width:100%!important;} pre[class*='language-']{color:#ccc;background:none;} -dd:only-of-type{background-color:bisque;} +dd:only-of-type{background-color:bisque} -:not(pre)>code[class*='language-']{background:#2d2d2d;} +:not(pre)>code[class*='language-']{background:#2d2d2d} li:nth-child(-n+3){border:2px solid orange;margin-bottom:1px;} -li:nth-child(even){background-color:lightyellow;} +li:nth-child(even){background-color:lightyellow} li:nth-last-child(5n){border:2px solid orange;margin-top:1px;} -dd:nth-last-of-type(odd){border:2px solid orange;} +dd:nth-last-of-type(odd){border:2px solid orange} + +p:nth-of-type(2n+1){color:red} + +*:lang(en-US){outline:2px solid deeppink} + +p~ul{font-weight:bold} + +a[href*='greenwood'],a[href$='.pdf']{color:orange} -p:nth-of-type(2n+1){color:red;} +[title~=flower],a[href^='https'],[lang|=en]{text-decoration:underline} -*:lang(en-US){outline:2px solid deeppink;} +@keyframes slidein{from{transform:translateX(0%)}to{transform:translateX(100%)}} -p~ul{font-weight:bold;} +@supports (display:flex){.flex-container>*{text-shadow:0px 0px 2px blue;float:none;}.flex-container{display:flex}} -a[href*='greenwood'],a[href$='.pdf']{color:orange;} +@page {size:8.5in 9in;margin-top:4in;} -[title~=flower],a[href^='https'],[lang|=en]{text-decoration:underline;} \ No newline at end of file +@font-feature-values Font One{@styleset {nice-style:12}} \ No newline at end of file diff --git a/packages/cli/test/cases/build.config.optimization-default/src/styles/main.css b/packages/cli/test/cases/build.config.optimization-default/src/styles/main.css index 3dbae9c37..a0868cae7 100644 --- a/packages/cli/test/cases/build.config.optimization-default/src/styles/main.css +++ b/packages/cli/test/cases/build.config.optimization-default/src/styles/main.css @@ -87,4 +87,36 @@ a[href*="greenwood"], a[href$=".pdf"] { [title~=flower], a[href^="https"], [lang|=en] { text-decoration: underline; +} + +@keyframes slidein { + from { + transform: translateX(0%); + } + + to { + transform: translateX(100%); + } +} + +@supports (display: flex) { + .flex-container > * { + text-shadow: 0px 0px 2px blue; + float: none; + } + + .flex-container { + display: flex; + } +} + +@page { + size: 8.5in 9in; + margin-top: 4in; +} + +@font-feature-values Font One { + @styleset { + nice-style: 12; + } } \ No newline at end of file diff --git a/packages/cli/test/cases/build.config.optimization-default/src/styles/theme.css b/packages/cli/test/cases/build.config.optimization-default/src/styles/theme.css index 36b39ca02..120e7130c 100644 --- a/packages/cli/test/cases/build.config.optimization-default/src/styles/theme.css +++ b/packages/cli/test/cases/build.config.optimization-default/src/styles/theme.css @@ -1 +1,6 @@ -@import url('../system/variables.css'); \ No newline at end of file +@tailwind base; +@tailwind components; +@tailwind utilities; + +@import url('../system/variables.css'); +@import url('https://fonts.googleapis.com/css?family=Raleway&display=swap'); \ No newline at end of file diff --git a/packages/cli/test/cases/build.config.optimization-inline/build.config-optimization-inline.spec.js b/packages/cli/test/cases/build.config.optimization-inline/build.config-optimization-inline.spec.js index e5fc05f75..789440089 100644 --- a/packages/cli/test/cases/build.config.optimization-inline/build.config-optimization-inline.spec.js +++ b/packages/cli/test/cases/build.config.optimization-inline/build.config-optimization-inline.spec.js @@ -144,7 +144,7 @@ describe('Build Greenwood With: ', function() { it('should contain the expected CSS content inlined for page.css', function() { const styleTags = dom.window.document.querySelectorAll('head style'); - expect(styleTags[1].textContent).to.contain('body{color:red;}'); + expect(styleTags[1].textContent).to.contain('body{color:red}'); }); }); }); diff --git a/packages/cli/test/cases/build.config.optimization-overrides/build.config-optimization-overrides.spec.js b/packages/cli/test/cases/build.config.optimization-overrides/build.config-optimization-overrides.spec.js index 253f89e62..714adeb2e 100644 --- a/packages/cli/test/cases/build.config.optimization-overrides/build.config-optimization-overrides.spec.js +++ b/packages/cli/test/cases/build.config.optimization-overrides/build.config-optimization-overrides.spec.js @@ -153,7 +153,7 @@ describe('Build Greenwood With: ', function() { it('should have an inline