From 50081377969dc72b60950d4d5593cb426341a82b Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 26 Dec 2017 12:24:27 -0500 Subject: [PATCH 1/6] Better purge CSS pipeline Signed-off-by: Andrew Welch --- gulpfile.js | 58 +++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index c6f0bd4..38e6844 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -65,6 +65,16 @@ gulp.task("tailwind", () => { $.tailwindcss(pkg.paths.tailwindcss.conf), require("autoprefixer"), ])) + .pipe($.if(process.env.NODE_ENV === "production", + $.purgecss({ + extractors: [{ + extractor: TailwindExtractor, + extensions: ["html", "twig", "css", "js"] + }], + whitelist: ["menuOpen", "open", "main-logo-svg", "sub-logo-svg", "scaling-svg"], + content: pkg.globs.purgecss + }) + )) .pipe(gulp.dest(pkg.paths.build.css)); }); @@ -78,48 +88,26 @@ class TailwindExtractor { } } -// purgecss task -gulp.task("purgecss", ["tailwind", "scss"], () => { - switch (process.env.NODE_ENV) { - case "development": - $.fancyLog("-> Copying CSS"); - return gulp.src(pkg.globs.distCss) - .pipe(gulp.dest(pkg.paths.build.css)); - break; - - case "production": - $.fancyLog("-> Running purgecss"); - return gulp.src(pkg.globs.distCss) - .pipe($.purgecss({ - extractors: [{ - extractor: TailwindExtractor, - extensions: ["html", "twig", "css", "js"] - }], - content: pkg.globs.purgecss - })) - .pipe(gulp.dest(pkg.paths.build.css)); - break; - } -}); - // css task - combine & minimize any distribution CSS into the public css folder, and add our banner to it -gulp.task("css", ["purgecss"], () => { +gulp.task("css", ["tailwind", "scss"], () => { $.fancyLog("-> Building css"); - return gulp.src(pkg.paths.build.css + "**/*.{css,.min.css}") + return gulp.src(pkg.globs.distCss) .pipe($.plumber({errorHandler: onError})) .pipe($.newer({dest: pkg.paths.dist.css + pkg.vars.siteCssName})) .pipe($.print()) .pipe($.sourcemaps.init({loadMaps: true})) .pipe($.concat(pkg.vars.siteCssName)) - .pipe($.cssnano({ - discardComments: { - removeAll: true - }, - discardDuplicates: true, - discardEmpty: true, - minifyFontValues: true, - minifySelectors: true - })) + .pipe($.if(process.env.NODE_ENV === "production", + $.cssnano({ + discardComments: { + removeAll: true + }, + discardDuplicates: true, + discardEmpty: true, + minifyFontValues: true, + minifySelectors: true + }) + )) .pipe($.header(banner, {pkg: pkg})) .pipe($.sourcemaps.write("./")) .pipe($.size({gzip: true, showFiles: true})) From d0de31d384fcb9fa2bedf8a7e76cbea5390212f8 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 26 Dec 2017 12:32:17 -0500 Subject: [PATCH 2/6] PurgeCSS whitelist in package.json Signed-off-by: Andrew Welch --- gulpfile.js | 2 +- package.json | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/gulpfile.js b/gulpfile.js index 38e6844..5275730 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -71,7 +71,7 @@ gulp.task("tailwind", () => { extractor: TailwindExtractor, extensions: ["html", "twig", "css", "js"] }], - whitelist: ["menuOpen", "open", "main-logo-svg", "sub-logo-svg", "scaling-svg"], + whitelist: pkg.globs.purgecssWhitelist, content: pkg.globs.purgecss }) )) diff --git a/package.json b/package.json index ff10d10..a2bf1b9 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,8 @@ "purgecss": [ "./templates/**/*.{html,twig}" ], + "purgecssWhitelist": [ + ], "critical": [ { "url": "", From da7c83450b344da9c939d7ceac6b6f9f59fecedd Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 1 Jan 2018 12:33:56 -0500 Subject: [PATCH 3/6] Add critical whitelist & JS execution Signed-off-by: Andrew Welch --- gulpfile.js | 4 ++++ package.json | 2 ++ 2 files changed, 6 insertions(+) diff --git a/gulpfile.js b/gulpfile.js index 5275730..32a9b5d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -244,6 +244,10 @@ function processCriticalCSS(element, i, callback) { $.critical.generate({ src: criticalSrc, dest: criticalDest, + penthouse: { + blockJSRequests: false, + forceInclude: pkg.globs.criticalWhitelist + }, inline: false, ignore: [], css: [ diff --git a/package.json b/package.json index a2bf1b9..9c7c791 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,8 @@ "template": "404" } ], + "criticalWhitelist": [ + ], "download": [ { "url": "https://www.google-analytics.com/analytics.js", From e523e2b9c15556a6cdb3f41921a4e1b8110b188d Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 1 Jan 2018 12:51:38 -0500 Subject: [PATCH 4/6] Added `post-update-cmd` Signed-off-by: Andrew Welch --- composer.json | 8 ++++++++ nys-setup | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/composer.json b/composer.json index 2515018..565f130 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,11 @@ "nystudio107/craft3-minify": "^1.2.5", "nystudio107/craft3-typogrify": "^1.1.1" }, + "autoload": { + "psr-4": { + "modules\\": "modules/" + } + }, "config": { "optimize-autoloader": true }, @@ -41,6 +46,9 @@ "post-create-project-cmd": [ "@php craft setup/welcome", "@php nys-setup welcome" + ], + "post-update-cmd": [ + "@php nys-setup update" ] } } diff --git a/nys-setup b/nys-setup index d4282d4..03e69e2 100755 --- a/nys-setup +++ b/nys-setup @@ -192,6 +192,11 @@ if (empty($argv[1])) { welcomeNysCraft(); break; + case 'update': + // Display a welcome message + updateNysCraft(); + break; + default: // Set up all the things! setupNysCraft(); @@ -213,6 +218,19 @@ function welcomeNysCraft() outputString(PHP_EOL.'Your setup is not complete until you run these two commands.', Console::FG_YELLOW); } +/** + * Update our Craft-Scripts symlinks as needed! + */ +function updateNysCraft() +{ + // Say hello + outputString(PHP_EOL.'Updating nys-setup', Console::FG_YELLOW); + // Set up Craft-Scripts + setupCraftScripts(); + // Say goodbye + outputString(PHP_EOL.'Update complete. Have a nice day!', Console::FG_YELLOW); +} + /** * Set up all the things! */ From 9812aea29cf628d17ecb849384f7cd40d3181b43 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 1 Jan 2018 12:51:55 -0500 Subject: [PATCH 5/6] Added site-module Signed-off-by: Andrew Welch --- config/app.php | 4 +- modules/Module.php | 32 --------- modules/nystudio107/sitemodule/SiteModule.php | 66 +++++++++++++++++++ .../sitemodule/SiteModuleAsset.php | 48 ++++++++++++++ .../sitemodule/dist/css/SiteModule.css | 9 +++ .../assetbundles/sitemodule/dist/img/.gitkeep | 0 .../sitemodule/dist/js/SiteModule.js | 9 +++ 7 files changed, 134 insertions(+), 34 deletions(-) delete mode 100644 modules/Module.php create mode 100644 modules/nystudio107/sitemodule/SiteModule.php create mode 100644 modules/nystudio107/sitemodule/assetbundles/sitemodule/SiteModuleAsset.php create mode 100644 modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/css/SiteModule.css create mode 100644 modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/img/.gitkeep create mode 100644 modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/js/SiteModule.js diff --git a/config/app.php b/config/app.php index 95cb293..bc733b7 100644 --- a/config/app.php +++ b/config/app.php @@ -18,9 +18,9 @@ // All environments '*' => [ 'modules' => [ - 'my-module' => \modules\Module::class, + 'site-module' => \modules\nystudio107\sitemodule\SiteModule::class, ], - //'bootstrap' => ['my-module'], + 'bootstrap' => ['site-module'], ], // Live (production) environment diff --git a/modules/Module.php b/modules/Module.php deleted file mode 100644 index 275c1de..0000000 --- a/modules/Module.php +++ /dev/null @@ -1,32 +0,0 @@ -getModule('my-module')`. - * - * You can change its module ID ("my-module") to something else from - * config/app.php. - * - * If you want the module to get loaded on every request, uncomment this line - * in config/app.php: - * - * 'bootstrap' => ['my-module'] - * - * Learn more about Yii module development in Yii's documentation: - * http://www.yiiframework.com/doc-2.0/guide-structure-modules.html - */ -class Module extends \yii\base\Module -{ - /** - * Initializes the module. - */ - public function init() - { - parent::init(); - - // Custom initialization code goes here... - } -} diff --git a/modules/nystudio107/sitemodule/SiteModule.php b/modules/nystudio107/sitemodule/SiteModule.php new file mode 100644 index 0000000..8719728 --- /dev/null +++ b/modules/nystudio107/sitemodule/SiteModule.php @@ -0,0 +1,66 @@ +getBasePath()); + parent::init(); + + // Only respond to AdminCP requests + $request = Craft::$app->getRequest(); + if ($request->getIsCpRequest()) { + // Handler: View::EVENT_BEFORE_RENDER_TEMPLATE + Event::on( + View::class, + View::EVENT_BEFORE_RENDER_TEMPLATE, + function (TemplateEvent $event) { + Craft::trace( + 'View::EVENT_BEFORE_RENDER_TEMPLATE', + __METHOD__ + ); + // Add our SiteModule AssetBundle + $view = Craft::$app->getView(); + try { + $view->registerAssetBundle(SiteModuleAsset::class); + } catch (InvalidConfigException $e) { + Craft::error( + 'Error registering AssetBundle - '.$e->getMessage(), + __METHOD__ + ); + } + } + ); + } + } +} diff --git a/modules/nystudio107/sitemodule/assetbundles/sitemodule/SiteModuleAsset.php b/modules/nystudio107/sitemodule/assetbundles/sitemodule/SiteModuleAsset.php new file mode 100644 index 0000000..980cc6c --- /dev/null +++ b/modules/nystudio107/sitemodule/assetbundles/sitemodule/SiteModuleAsset.php @@ -0,0 +1,48 @@ +sourcePath = '@site-module/assetbundles/sitemodule/dist'; + // Declare AssetBundles that must be loaded first + $this->depends = [ + CpAsset::class, + ]; + // Add in our CSS + $this->css = [ + 'css/SiteModule.css', + ]; + // Add in our JS + $this->js = [ + 'js/SiteModule.js', + ]; + + parent::init(); + } +} diff --git a/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/css/SiteModule.css b/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/css/SiteModule.css new file mode 100644 index 0000000..2315a92 --- /dev/null +++ b/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/css/SiteModule.css @@ -0,0 +1,9 @@ +/** + * SiteModule CSS + * + * @author nystudio107 + * @copyright Copyright (c) 2017 nystudio107 + * @link https://nystudio107.com + * @package SiteModule + * @since 1.0.0 + */ diff --git a/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/img/.gitkeep b/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/img/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/js/SiteModule.js b/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/js/SiteModule.js new file mode 100644 index 0000000..ba89f2f --- /dev/null +++ b/modules/nystudio107/sitemodule/assetbundles/sitemodule/dist/js/SiteModule.js @@ -0,0 +1,9 @@ +/** + * SiteModule JS + * + * @author nystudio107 + * @copyright Copyright (c) 2017 nystudio107 + * @link https://nystudio107.com + * @package SiteModule + * @since 1.0.0 + */ From 4e7e2f9e123903cac7d882b9483b3be1296cb179 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 1 Jan 2018 12:52:05 -0500 Subject: [PATCH 6/6] Version 1.0.8 Signed-off-by: Andrew Welch --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eaf9eb..2ba28e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # nystudio107/craft Change Log +## 1.0.8 - 2018.01.01 +### Added +* Added a better PurgeCSS pipeline +* Added a `purgecssWhitelist` to `package.json` +* Execute JavaScript when doing Critical CSS +* Added a `criticalWhitelist` to `package.json` +* Added SiteModule framework to nystudio107/craft +* Added a `post-update-cmd` to `composer.json` to recreate any symlinks that may have been removed after a `composer update` or `composer install` + ## 1.0.7 - 2017.12.16 ### Added * Added `purgecss` to production builds