From ca46126e98098ca06d09c541833949fd025b15d7 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 3 Feb 2020 17:42:03 -0800 Subject: [PATCH] resourceloader: Support single-file component .vue files Allows .vue files to be used in package modules as if they were .js files: they can be added to the 'packageFiles' array in module definitions, and require()d from JS files. In the load.php output, each .vue file is transformed to a function that contains the JS from the + diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php index bb434e23d88..afe63d70ca8 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php @@ -539,6 +539,13 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase { $base = [ 'localBasePath' => $basePath ]; $commentScript = file_get_contents( "$basePath/script-comment.js" ); $nosemiScript = file_get_contents( "$basePath/script-nosemi.js" ); + $vueComponentDebug = trim( file_get_contents( "$basePath/vue-component-output-debug.js.txt" ) ); + // In PHP 7.2 and below, newlines are inserted between elements even if we try to strip them + // Thankfully this was fixed in PHP 7.3, but it means we need different test cases for 7.2 vs 7.3+ + $vueComponentNonDebugFile = version_compare( PHP_VERSION, '7.3' ) < 0 ? + "$basePath/vue-component-output-nondebug-php72.js.txt" : + "$basePath/vue-component-output-nondebug.js.txt"; + $vueComponentNonDebug = trim( file_get_contents( $vueComponentNonDebugFile ) ); $config = RequestContext::getMain()->getConfig(); return [ [ @@ -738,6 +745,45 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase { 'lang' => 'nl' ] ], + '.vue file in debug mode' => [ + $base + [ + 'packageFiles' => [ + 'vue-component.vue' + ] + ], + [ + 'files' => [ + 'vue-component.vue' => [ + 'type' => 'script', + 'content' => $vueComponentDebug + ] + ], + 'main' => 'vue-component.vue', + ], + [ + 'debug' => 'true' + ] + ], + '.vue file in non-debug mode' => [ + $base + [ + 'packageFiles' => [ + 'vue-component.vue' + ], + 'name' => 'nondebug', + ], + [ + 'files' => [ + 'vue-component.vue' => [ + 'type' => 'script', + 'content' => $vueComponentNonDebug + ] + ], + 'main' => 'vue-component.vue' + ], + [ + 'debug' => 'false' + ] + ], [ $base + [ 'packageFiles' => [ diff --git a/tests/phpunit/includes/resourceloader/VueComponentParserTest.php b/tests/phpunit/includes/resourceloader/VueComponentParserTest.php new file mode 100644 index 00000000000..e5310b81575 --- /dev/null +++ b/tests/phpunit/includes/resourceloader/VueComponentParserTest.php @@ -0,0 +1,193 @@ +expectExceptionMessage( $expectedException ); + } + $actualResult = $parser->parse( $html ); + $this->assertEquals( $expectedResult, $actualResult, $message ); + } + + public static function provideTestParse() { + // @codingStandardsIgnoreStart Generic.Files.LineLength + return [ + [ + '', + [ + 'script' => 'bar', + 'template' => '

{{foo}}

', + 'rawTemplate' => '

{{foo}}

', + 'style' => 'baz', + 'styleLang' => 'css', + ], + 'Basic test' + ], + [ + '', + null, + 'Missing ', + null, + 'Missing