From 704f2d7250a4d6e41802de10743e1f5831c21cb5 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Wed, 30 Jun 2021 13:39:55 -0400 Subject: [PATCH 01/13] initial support for JS static overrides --- packages/cli/src/config/rollup.config.js | 6 +- ...uild.config-optimization-overrides.spec.js | 85 +++++++++++++++++++ .../greenwood.config.js | 3 + .../src/components/header.js | 19 +++++ .../src/pages/index.html | 12 +++ 5 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 packages/cli/test/cases/build.config.optimization-overrides/build.config-optimization-overrides.spec.js create mode 100644 packages/cli/test/cases/build.config.optimization-overrides/greenwood.config.js create mode 100644 packages/cli/test/cases/build.config.optimization-overrides/src/components/header.js create mode 100644 packages/cli/test/cases/build.config.optimization-overrides/src/pages/index.html diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index e36a55df3..c173810f3 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -179,8 +179,8 @@ function greenwoodHtmlPlugin(compilation) { // handle if (!isRemoteUrl(parsedAttributes.src) && parsedAttributes.type === 'module' && parsedAttributes.src && !mappedScripts.get(parsedAttributes.src)) { - if (optimization === 'static') { - // console.debug('dont emit ', parsedAttributes.src); + if (optimization === 'static' || parsedAttributes['data-gwd-opt'] === 'static') { + // dont need to bundle / emit this one } else { const { src } = parsedAttributes; const absoluteSrc = `${path.normalize(src.replace(/\.\.\//g, '').replace('./', ''))}`; @@ -357,7 +357,7 @@ function greenwoodHtmlPlugin(compilation) { `); } - } else if (optimization === 'static' && newHtml.indexOf(pathToMatch) > 0) { + } else if ((parsedAttributes['data-gwd-opt'] === 'static' || optimization === 'static') && newHtml.indexOf(pathToMatch) > 0) { newHtml = newHtml.replace(scriptTag, ''); } } 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 new file mode 100644 index 000000000..50a51353a --- /dev/null +++ b/packages/cli/test/cases/build.config.optimization-overrides/build.config-optimization-overrides.spec.js @@ -0,0 +1,85 @@ +/* + * Use Case + * Run Greenwood build command with various override settings for optimization settings. + * + * User Result + * Should generate a Greenwood build that respects optimization setting overrides for all + + + + + + + \ No newline at end of file From 29c25a4699fe0816419f613242f0638322a4c2b1 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Wed, 30 Jun 2021 19:50:47 -0400 Subject: [PATCH 02/13] add support for inline overrides --- packages/cli/src/config/rollup.config.js | 8 +-- ...uild.config-optimization-overrides.spec.js | 70 ++++++++++++++++--- .../src/components/footer.js | 16 +++++ .../src/components/header.js | 1 - .../src/pages/index.html | 2 + 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 packages/cli/test/cases/build.config.optimization-overrides/src/components/footer.js diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index c173810f3..8c54c7196 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -295,8 +295,8 @@ function greenwoodHtmlPlugin(compilation) { })); }, - // crawl through all entry HTML files and map bundled JavaScript and CSS filenames - // back to original + + \ No newline at end of file From 789322d56382cf636ac5ec278a84a88ea54672fb Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Thu, 1 Jul 2021 11:38:00 -0400 Subject: [PATCH 03/13] fix typo --- .../build.config-optimization-none.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js b/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js index ad2d2a294..e00ebd566 100644 --- a/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js +++ b/packages/cli/test/cases/build.config.optimization-none/build.config-optimization-none.spec.js @@ -68,7 +68,7 @@ describe('Build Greenwood With: ', function() { }); describe(' if ((parsedAttributes['data-gwd-opt'] === 'inline' || optimization === 'inline') && isScriptSrcTag && !isRemoteUrl(parsedAttributes.src)) { const src = parsedAttributes.src; const basePath = src.indexOf(tokenNodeModules) >= 0 From c8700cd8ca37d85ae08231d876647c5f78f8b8c8 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Thu, 1 Jul 2021 12:15:37 -0400 Subject: [PATCH 05/13] support style inline override --- packages/cli/src/config/rollup.config.js | 42 +++++++++--------- ...uild.config-optimization-overrides.spec.js | 43 +++++++++++++++++-- .../src/pages/index.html | 1 + .../src/styles/theme.css | 3 ++ 4 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 packages/cli/test/cases/build.config.optimization-overrides/src/styles/theme.css diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index 92e98535e..1db0aafad 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -378,7 +378,8 @@ function greenwoodHtmlPlugin(compilation) { newHtml = newHtml.replace(href, newHref); - if (optimization !== 'none' && optimization !== 'inline') { + // TODO remove negatives + if (!parsedAttributes['data-gwd-opt'] && (optimization !== 'none' && optimization !== 'inline')) { newHtml = newHtml.replace('', ` @@ -465,27 +466,24 @@ function greenwoodHtmlPlugin(compilation) { } }); - if (optimization === 'inline') { - headLinks - .forEach((linkTag) => { - const linkTagAttributes = parseTagForAttributes(linkTag); - const isLocalLinkTag = linkTagAttributes.rel === 'stylesheet' - && !isRemoteUrl(linkTagAttributes.href); - - if (isLocalLinkTag) { - const href = linkTagAttributes.href; - const outputPath = path.join(outputDir, href); - const css = fs.readFileSync(outputPath, 'utf-8'); - - html = html.replace(``, ` - - `); - } - }); - } - + headLinks + .forEach((linkTag) => { + const parsedAttributes = parseTagForAttributes(linkTag); + const isLocalLinkTag = parsedAttributes.rel === 'stylesheet' + && !isRemoteUrl(parsedAttributes.href); + + if (isLocalLinkTag && (parsedAttributes['data-gwd-opt'] === 'inline' || optimization === 'inline')) { + const href = parsedAttributes.href; + const outputPath = path.join(outputDir, href); + const css = fs.readFileSync(outputPath, 'utf-8'); + + html = html.replace(``, ` + + `); + } + }); // mark each HTML's sourcemap file (generated by Rollup) to be cleaned up // would be nice if we could just prevent Rollup from generating sourcemaps for just our input files in the first place (GFI) scratchFiles[`${htmlPath.replace(outputDir, '')}.map`] = true; 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 11047bc2a..4e9c21348 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 @@ -47,9 +47,9 @@ describe('Build Greenwood With: ', function() { describe('Cumulative output based on all settings', function() { // TODO - // total JS files emitted - // total script tags - // total preload tags + // total JS / CSS files expected + // total script / style tags expected + // total preload tags expected }); describe('JavaScript + diff --git a/packages/cli/test/cases/build.config.optimization-overrides/src/styles/theme.css b/packages/cli/test/cases/build.config.optimization-overrides/src/styles/theme.css new file mode 100644 index 000000000..9a7b45f93 --- /dev/null +++ b/packages/cli/test/cases/build.config.optimization-overrides/src/styles/theme.css @@ -0,0 +1,3 @@ +* { + color: blue; +} \ No newline at end of file From f140645ffcdb25eb4aa326a16567a08599e032f0 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Fri, 2 Jul 2021 10:48:32 -0400 Subject: [PATCH 06/13] add specs for cumulative optimization overrides output --- ...uild.config-optimization-overrides.spec.js | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) 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 4e9c21348..2d2b99920 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 @@ -14,9 +14,12 @@ * Custom Workspace * src/ * components/ + * footer.js * header.js * pages/ * index.html + * styles/ + * theme.css */ const expect = require('chai').expect; const glob = require('glob-promise'); @@ -45,11 +48,43 @@ describe('Build Greenwood With: ', function() { await runner.runCommand(cliPath, 'build'); }); - describe('Cumulative output based on all settings', function() { - // TODO - // total JS / CSS files expected - // total script / style tags expected - // total preload tags expected + describe('Cumulative output based on all override settings', function() { + let dom; + + before(async function() { + dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, './index.html')); + }); + + it('should emit one Javascript file to the output directory', async function() { + const jsFiles = await glob.promise(path.join(this.context.publicDir, '**/*.js')); + + expect(jsFiles).to.have.lengthOf(1); + }); + + it('should emit one CSS file to the output directory', async function() { + const cssFiles = await glob.promise(path.join(this.context.publicDir, '**/*.css')); + + expect(cssFiles).to.have.lengthOf(1); + }); + + it('should have one - + From aed55410ed4e830b4bcdee06aed4a20c91d5585d Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Fri, 2 Jul 2021 11:39:37 -0400 Subject: [PATCH 08/13] add overrides section to optimization docs --- www/pages/docs/configuration.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/www/pages/docs/configuration.md b/www/pages/docs/configuration.md index 274c4c890..71a8339fe 100644 --- a/www/pages/docs/configuration.md +++ b/www/pages/docs/configuration.md @@ -141,7 +141,19 @@ module.exports = { } ``` -> _These settings are currently expiremental, and more fine grained control and intelligent based defaults will be coming soon!_ +#### Overrides +Additionally, you can apply overrides on a per `` or ` + + + + +``` + +> _These settings are currently considered expiremental. Additional improvements and considerations include adding [`none` override support](https://github.com/ProjectEvergreen/greenwood/discussions/545#discussioncomment-957320), [SSR + hydration](https://github.com/ProjectEvergreen/greenwood/discussions/576), and [side effect free templates and pages](https://github.com/ProjectEvergreen/greenwood/discussions/644)._ ### Prerender From b26b8cee018aa76f1c576237fc3b8ec0fd7da977 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Fri, 2 Jul 2021 12:18:44 -0400 Subject: [PATCH 09/13] apply static overrides for website --- www/components/footer/footer.css | 2 +- www/pages/index.html | 2 +- www/templates/app.html | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/www/components/footer/footer.css b/www/components/footer/footer.css index 3ab8d4c26..5f1e0dbf5 100644 --- a/www/components/footer/footer.css +++ b/www/components/footer/footer.css @@ -8,7 +8,7 @@ & h4 { width: 90%; - margin: 0 auto; + margin: 0 auto!important; padding: 0; text-align: center; } diff --git a/www/pages/index.html b/www/pages/index.html index e0cffc00a..0e7d7ad41 100644 --- a/www/pages/index.html +++ b/www/pages/index.html @@ -3,7 +3,7 @@ - + diff --git a/www/templates/app.html b/www/templates/app.html index c0ded2903..a7b3b0e96 100644 --- a/www/templates/app.html +++ b/www/templates/app.html @@ -22,8 +22,8 @@ import '@evergreen-wc/eve-container'; - - + + From 23c17c62df11039bcd5d610f3f3bac3fefdeb99c Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Fri, 2 Jul 2021 12:27:59 -0400 Subject: [PATCH 10/13] remove unused config file --- .../build.config.optimization-overrides/greenwood.config.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 packages/cli/test/cases/build.config.optimization-overrides/greenwood.config.js diff --git a/packages/cli/test/cases/build.config.optimization-overrides/greenwood.config.js b/packages/cli/test/cases/build.config.optimization-overrides/greenwood.config.js deleted file mode 100644 index 97dacb7fa..000000000 --- a/packages/cli/test/cases/build.config.optimization-overrides/greenwood.config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - // optimization: 'static' -}; \ No newline at end of file From 20fe59de6f929521f2a6dd00f584861bd7f37d25 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Fri, 2 Jul 2021 12:47:23 -0400 Subject: [PATCH 11/13] document global styles for CSS in JS --- www/pages/docs/configuration.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/www/pages/docs/configuration.md b/www/pages/docs/configuration.md index 71a8339fe..acc5f4522 100644 --- a/www/pages/docs/configuration.md +++ b/www/pages/docs/configuration.md @@ -134,6 +134,8 @@ Greenwood provides a number of different ways to send hints to Greenwood as to h |`none` | With this setting, _none_ of your JS or CSS will be minified or hinted at all. | The best choice if you want to handle everything yourself through custom [Resource plugins](/plugins/resource/). | |`static` | Only for ``, `