From ce63c414c791434eab4e91fba27fdd54bf991abd Mon Sep 17 00:00:00 2001 From: Alex Winkler Date: Fri, 27 Sep 2024 18:17:31 +0200 Subject: [PATCH 1/4] add scss parsing to the extension --- composer.json | 3 +- extension.json | 4 +- i18n/en.json | 2 +- src/Hooks/MainHookHandler.php | 5 +- .../ResourceLoaderArticlesModule.php | 70 ++++++++++++------- .../SpecialResourceLoaderArticles.php | 13 +++- 6 files changed, 62 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index 4f1aaa3..0ac76a8 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "require": { - "mck89/peast": "^1.16" + "mck89/peast": "^1.16", + "scssphp/scssphp": "^1.13.0" }, "require-dev": { "liquipedia/sqllint": "*", diff --git a/extension.json b/extension.json index 68e3044..20ea608 100644 --- a/extension.json +++ b/extension.json @@ -1,6 +1,6 @@ { "name": "ResourceLoaderArticles", - "version": "2.3.0", + "version": "2.4.0", "author": [ "[https://fo-nttax.de Alex Winkler]" ], @@ -57,4 +57,4 @@ } }, "manifest_version": 2 -} \ No newline at end of file +} diff --git a/i18n/en.json b/i18n/en.json index 4f8542d..3745629 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -32,5 +32,5 @@ "resourceloaderarticles-success-delete": "Resource successfully deleted", "resourceloaderarticles-help-page": "Page name of resource (excl. `MediaWiki:Common.[js|css]/`). Valid JS ends with `.js` and valid styling ends with `.css` or `.less`.", - "resourceloaderarticles-help-priority": "Priority for loading order of the resource (higher first), falls back to alphabetic order within a single priority class." + "resourceloaderarticles-help-priority": "Priority for loading order of the resource (higher first), falls back to alphabetic order within a single priority class. Priority is done in two groups: Less/CSS and SCSS. The first group will always be before the second." } diff --git a/src/Hooks/MainHookHandler.php b/src/Hooks/MainHookHandler.php index 40e1bc8..fadfb5a 100644 --- a/src/Hooks/MainHookHandler.php +++ b/src/Hooks/MainHookHandler.php @@ -83,7 +83,7 @@ public function onBeforePageDisplay( $out, $skin ): void { } /** - * Set the CONTENT_MODEL_CSS content handler for less files + * Set the CONTENT_MODEL_CSS content handler for less and scss files * * @param Title $title * @param string &$model @@ -94,6 +94,9 @@ public function onContentHandlerDefaultModelFor( $title, &$model ) { if ( str_ends_with( $title->getText(), '.less' ) ) { $model = CONTENT_MODEL_CSS; return true; + } elseif ( str_ends_with( $title->getText(), '.scss' ) ) { + $model = CONTENT_MODEL_CSS; + return true; } } } diff --git a/src/ResourceLoader/ResourceLoaderArticlesModule.php b/src/ResourceLoader/ResourceLoaderArticlesModule.php index e5b3e7c..06ba95b 100644 --- a/src/ResourceLoader/ResourceLoaderArticlesModule.php +++ b/src/ResourceLoader/ResourceLoaderArticlesModule.php @@ -16,6 +16,7 @@ use ResourceLoader; use ResourceLoaderContext; use ResourceLoaderWikiModule; +use ScssPhp\ScssPhp\Compiler as SCSSCompiler; class ResourceLoaderArticlesModule extends ResourceLoaderWikiModule { @@ -38,7 +39,11 @@ protected function getPages( ResourceLoaderContext $context ) { foreach ( $articles as $article ) { if ( substr( $article, -3 ) === '.js' ) { $pages[ 'MediaWiki:Common.js/' . $article ] = [ 'type' => 'script' ]; - } elseif ( substr( $article, -4 ) === '.css' || substr( $article, -5 ) === '.less' ) { + } elseif ( + substr( $article, -4 ) === '.css' + || substr( $article, -5 ) === '.less' + || substr( $article, -5 ) === '.scss' + ) { $pages[ 'MediaWiki:Common.css/' . $article ] = [ 'type' => 'style' ]; } } @@ -99,12 +104,13 @@ static function () use ( $contents, $fileName ) { * @return array */ public function getStyles( ResourceLoaderContext $context ) { - $styles = []; + $styles = [ 'all' => [ '' ] ]; + $less = ''; + $scss = ''; foreach ( $this->getPages( $context ) as $titleText => $options ) { if ( $options[ 'type' ] !== 'style' ) { continue; } - $media = isset( $options[ 'media' ] ) ? $options[ 'media' ] : 'all'; $style = $this->getContent( $titleText, $context ); if ( strval( $style ) === '' ) { continue; @@ -114,33 +120,43 @@ public function getStyles( ResourceLoaderContext $context ) { $style = '/* using @import is forbidden */'; } - if ( !isset( $styles[ $media ] ) ) { - $styles[ $media ] = []; - $styles[ $media ][ 0 ] = ''; - } $style = ResourceLoader::makeComment( $titleText ) . $style; - $styles[ $media ][ 0 ] .= $style; - } - foreach ( $styles as $media => $styleItem ) { - /* start of less parser */ - try { - $lessc = new Less_Parser; - $lessc->parse( $styleItem[ 0 ] ); - $style = $lessc->getCss(); - } catch ( exception $e ) { - $style = '/* invalid less: ' . $e->getMessage() . ' */'; - } - /* end of less parser */ - if ( $this->getFlip( $context ) ) { - $style = CSSJanus::transform( $style, true, false ); + if ( substr( $titleText, -5 ) === '.scss' ) { + $scss .= $style; + } else { + $less .= $style; } - $style = MemoizedCallable::call( - 'CSSMin::remap', - [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] - ); - $styles[ $media ][ 0 ] = $style; } - return $styles; + /* start of less parser */ + try { + $lessc = new Less_Parser; + $lessc->parse( $less ); + $compiledLess = $lessc->getCss(); + } catch ( \Exception $e ) { + $compiledLess = '/* invalid less: ' . $e->getMessage() . ' */'; + } + /* end of less parser */ + + /* start of scss parser */ + try { + $compiler = new SCSSCompiler(); + $compiledScss = $compiler->compileString( $scss )->getCss(); + } catch ( \Exception $e ) { + $compiledLess = '/* invalid less: ' . $e->getMessage() . ' */'; + } + /* end of scss parser */ + + $css = $compiledLess . $compiledScss; + + if ( $this->getFlip( $context ) ) { + $css = CSSJanus::transform( $css, true, false ); + } + $css = MemoizedCallable::call( + 'CSSMin::remap', + [ $css, false, $this->getConfig()->get( 'ScriptPath' ), true ] + ); + + return [ 'all' => [ $css ] ]; } /** diff --git a/src/SpecialPage/SpecialResourceLoaderArticles.php b/src/SpecialPage/SpecialResourceLoaderArticles.php index 407fef8..9bf55f1 100644 --- a/src/SpecialPage/SpecialResourceLoaderArticles.php +++ b/src/SpecialPage/SpecialResourceLoaderArticles.php @@ -346,11 +346,18 @@ public function trimValueCB( $value ) { */ public function validatePageCB( $value, $alldata ) { if ( - ( $alldata[ 'Type' ] === 'style' - && !( ( strlen( $value ) > 4 && substr( $value, -4 ) === '.css' ) + ( + $alldata[ 'Type' ] === 'style' + && !( + ( strlen( $value ) > 4 && substr( $value, -4 ) === '.css' ) || ( strlen( $value ) > 5 && substr( $value, -5 ) === '.less' ) + || ( strlen( $value ) > 5 && substr( $value, -5 ) === '.scss' ) ) - ) || ( $alldata[ 'Type' ] === 'script' && !( strlen( $value ) > 3 && substr( $value, -3 ) === '.js' ) ) + ) + || ( + $alldata[ 'Type' ] === 'script' + && !( strlen( $value ) > 3 && substr( $value, -3 ) === '.js' ) + ) ) { return $this->msg( 'resourceloaderarticles-error-page-invalid' )->text(); } From 90a7f6902a7c180ce743f2098b5afea3f7339f22 Mon Sep 17 00:00:00 2001 From: Alex Winkler Date: Fri, 27 Sep 2024 18:19:58 +0200 Subject: [PATCH 2/4] add scss parsing to the extension --- src/ResourceLoader/ResourceLoaderArticlesModule.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ResourceLoader/ResourceLoaderArticlesModule.php b/src/ResourceLoader/ResourceLoaderArticlesModule.php index 06ba95b..4b42690 100644 --- a/src/ResourceLoader/ResourceLoaderArticlesModule.php +++ b/src/ResourceLoader/ResourceLoaderArticlesModule.php @@ -104,7 +104,6 @@ static function () use ( $contents, $fileName ) { * @return array */ public function getStyles( ResourceLoaderContext $context ) { - $styles = [ 'all' => [ '' ] ]; $less = ''; $scss = ''; foreach ( $this->getPages( $context ) as $titleText => $options ) { From 6b61303a83b953e1cf4ff71b4699cba27a2a3558 Mon Sep 17 00:00:00 2001 From: Alex Winkler Date: Fri, 27 Sep 2024 19:28:54 +0200 Subject: [PATCH 3/4] fix typo'd variable name --- src/ResourceLoader/ResourceLoaderArticlesModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ResourceLoader/ResourceLoaderArticlesModule.php b/src/ResourceLoader/ResourceLoaderArticlesModule.php index 4b42690..35e871a 100644 --- a/src/ResourceLoader/ResourceLoaderArticlesModule.php +++ b/src/ResourceLoader/ResourceLoaderArticlesModule.php @@ -141,7 +141,7 @@ public function getStyles( ResourceLoaderContext $context ) { $compiler = new SCSSCompiler(); $compiledScss = $compiler->compileString( $scss )->getCss(); } catch ( \Exception $e ) { - $compiledLess = '/* invalid less: ' . $e->getMessage() . ' */'; + $compiledScss = '/* invalid less: ' . $e->getMessage() . ' */'; } /* end of scss parser */ From bd39172c5c90490ce7b2123dd3c8e9edd0a122ac Mon Sep 17 00:00:00 2001 From: Alex Winkler Date: Fri, 27 Sep 2024 19:35:00 +0200 Subject: [PATCH 4/4] another typo --- src/ResourceLoader/ResourceLoaderArticlesModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ResourceLoader/ResourceLoaderArticlesModule.php b/src/ResourceLoader/ResourceLoaderArticlesModule.php index 35e871a..365341b 100644 --- a/src/ResourceLoader/ResourceLoaderArticlesModule.php +++ b/src/ResourceLoader/ResourceLoaderArticlesModule.php @@ -141,7 +141,7 @@ public function getStyles( ResourceLoaderContext $context ) { $compiler = new SCSSCompiler(); $compiledScss = $compiler->compileString( $scss )->getCss(); } catch ( \Exception $e ) { - $compiledScss = '/* invalid less: ' . $e->getMessage() . ' */'; + $compiledScss = '/* invalid scss: ' . $e->getMessage() . ' */'; } /* end of scss parser */