2014-10-22 05:32:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
2014-10-10 00:07:14 +00:00
|
|
|
/**
|
2015-04-01 07:35:32 +00:00
|
|
|
* @group Database
|
2014-10-10 00:07:14 +00:00
|
|
|
* @group ResourceLoader
|
|
|
|
|
*/
|
2014-10-22 05:32:13 +00:00
|
|
|
class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
|
|
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
// The return value of the closure shouldn't matter since this test should
|
|
|
|
|
// never call it
|
|
|
|
|
SkinFactory::getDefaultInstance()->register(
|
|
|
|
|
'fakeskin',
|
|
|
|
|
'FakeSkin',
|
|
|
|
|
function () {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 03:57:45 +00:00
|
|
|
private static function getModules() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$base = [
|
2015-11-12 18:58:20 +00:00
|
|
|
'localBasePath' => realpath( __DIR__ ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-10-10 00:07:14 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'noTemplateModule' => [],
|
2014-10-10 00:07:14 +00:00
|
|
|
|
2016-07-14 22:09:06 +00:00
|
|
|
'deprecatedModule' => $base + [
|
|
|
|
|
'deprecated' => true,
|
|
|
|
|
],
|
|
|
|
|
'deprecatedTomorrow' => $base + [
|
2017-03-18 00:22:34 +00:00
|
|
|
'deprecated' => 'Will be removed tomorrow.'
|
2016-07-14 22:09:06 +00:00
|
|
|
],
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
'htmlTemplateModule' => $base + [
|
|
|
|
|
'templates' => [
|
2014-10-10 00:07:14 +00:00
|
|
|
'templates/template.html',
|
|
|
|
|
'templates/template2.html',
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
2014-10-10 00:07:14 +00:00
|
|
|
|
2017-06-27 04:21:36 +00:00
|
|
|
'htmlTemplateUnknown' => $base + [
|
|
|
|
|
'templates' => [
|
|
|
|
|
'templates/notfound.html',
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
'aliasedHtmlTemplateModule' => $base + [
|
|
|
|
|
'templates' => [
|
2014-10-10 00:07:14 +00:00
|
|
|
'foo.html' => 'templates/template.html',
|
|
|
|
|
'bar.html' => 'templates/template2.html',
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
2014-10-10 00:07:14 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
'templateModuleHandlebars' => $base + [
|
|
|
|
|
'templates' => [
|
2014-10-10 00:07:14 +00:00
|
|
|
'templates/template_awesome.handlebars',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
2014-12-17 20:30:54 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
'aliasFooFromBar' => $base + [
|
|
|
|
|
'templates' => [
|
2014-12-17 20:30:54 +00:00
|
|
|
'foo.foo' => 'templates/template.bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
2014-12-17 20:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function providerTemplateDependencies() {
|
|
|
|
|
$modules = self::getModules();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
$modules['noTemplateModule'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
|
|
|
|
],
|
|
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
$modules['htmlTemplateModule'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
'mediawiki.template',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
$modules['templateModuleHandlebars'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
'mediawiki.template',
|
|
|
|
|
'mediawiki.template.handlebars',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
$modules['aliasFooFromBar'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-12-17 20:30:54 +00:00
|
|
|
'mediawiki.template',
|
|
|
|
|
'mediawiki.template.foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
2014-10-10 00:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-17 20:30:54 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider providerTemplateDependencies
|
|
|
|
|
* @covers ResourceLoaderFileModule::__construct
|
|
|
|
|
* @covers ResourceLoaderFileModule::getDependencies
|
|
|
|
|
*/
|
|
|
|
|
public function testTemplateDependencies( $module, $expected ) {
|
|
|
|
|
$rl = new ResourceLoaderFileModule( $module );
|
2016-09-12 00:07:52 +00:00
|
|
|
$rl->setName( 'testing' );
|
2014-12-17 20:30:54 +00:00
|
|
|
$this->assertEquals( $rl->getDependencies(), $expected );
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 22:09:06 +00:00
|
|
|
public static function providerDeprecatedModules() {
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'deprecatedModule',
|
|
|
|
|
'mw.log.warn("This page is using the deprecated ResourceLoader module \"deprecatedModule\".");',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'deprecatedTomorrow',
|
|
|
|
|
'mw.log.warn(' .
|
|
|
|
|
'"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\\n' .
|
|
|
|
|
"Will be removed tomorrow." .
|
|
|
|
|
'");'
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providerDeprecatedModules
|
2016-08-06 08:04:21 +00:00
|
|
|
* @covers ResourceLoaderFileModule::getScript
|
2016-07-14 22:09:06 +00:00
|
|
|
*/
|
|
|
|
|
public function testDeprecatedModules( $name, $expected ) {
|
|
|
|
|
$modules = self::getModules();
|
2017-06-27 05:04:21 +00:00
|
|
|
$module = new ResourceLoaderFileModule( $modules[$name] );
|
|
|
|
|
$module->setName( $name );
|
2016-12-03 00:46:53 +00:00
|
|
|
$ctx = $this->getResourceLoaderContext();
|
2017-06-27 05:04:21 +00:00
|
|
|
$this->assertEquals( $module->getScript( $ctx ), $expected );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoaderFileModule::getScript
|
2018-06-26 01:52:40 +00:00
|
|
|
* @covers ResourceLoaderFileModule::getScriptFiles
|
|
|
|
|
* @covers ResourceLoaderFileModule::readScriptFiles
|
2017-06-27 05:04:21 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetScript() {
|
|
|
|
|
$module = new ResourceLoaderFileModule( [
|
|
|
|
|
'localBasePath' => __DIR__ . '/../../data/resourceloader',
|
|
|
|
|
'scripts' => [ 'script-nosemi.js', 'script-comment.js' ],
|
|
|
|
|
] );
|
|
|
|
|
$module->setName( 'testing' );
|
|
|
|
|
$ctx = $this->getResourceLoaderContext();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
"/* eslint-disable */\nmw.foo()\n" .
|
|
|
|
|
"\n" .
|
|
|
|
|
"/* eslint-disable */\nmw.foo()\n// mw.bar();\n" .
|
|
|
|
|
"\n",
|
|
|
|
|
$module->getScript( $ctx ),
|
|
|
|
|
'scripts are concatenated with a new-line'
|
|
|
|
|
);
|
2016-07-14 22:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-22 05:32:13 +00:00
|
|
|
/**
|
2015-03-23 03:57:45 +00:00
|
|
|
* @covers ResourceLoaderFileModule::getAllStyleFiles
|
2014-10-22 05:32:13 +00:00
|
|
|
* @covers ResourceLoaderFileModule::getAllSkinStyleFiles
|
2015-03-23 03:57:45 +00:00
|
|
|
* @covers ResourceLoaderFileModule::getSkinStyleFiles
|
2014-10-22 05:32:13 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetAllSkinStyleFiles() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$baseParams = [
|
|
|
|
|
'scripts' => [
|
2014-10-22 05:32:13 +00:00
|
|
|
'foo.js',
|
|
|
|
|
'bar.js',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'styles' => [
|
2014-10-22 05:32:13 +00:00
|
|
|
'foo.css',
|
2016-02-17 09:09:32 +00:00
|
|
|
'bar.css' => [ 'media' => 'print' ],
|
|
|
|
|
'screen.less' => [ 'media' => 'screen' ],
|
|
|
|
|
'screen-query.css' => [ 'media' => 'screen and (min-width: 400px)' ],
|
|
|
|
|
],
|
|
|
|
|
'skinStyles' => [
|
2014-10-22 05:32:13 +00:00
|
|
|
'default' => 'quux-fallback.less',
|
2016-02-17 09:09:32 +00:00
|
|
|
'fakeskin' => [
|
2014-10-22 05:32:13 +00:00
|
|
|
'baz-vector.css',
|
|
|
|
|
'quux-vector.less',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'messages' => [
|
2014-10-22 05:32:13 +00:00
|
|
|
'hello',
|
|
|
|
|
'world',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-10-22 05:32:13 +00:00
|
|
|
|
|
|
|
|
$module = new ResourceLoaderFileModule( $baseParams );
|
2016-09-12 00:07:52 +00:00
|
|
|
$module->setName( 'testing' );
|
2014-10-22 05:32:13 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-10-22 05:32:13 +00:00
|
|
|
'foo.css',
|
|
|
|
|
'baz-vector.css',
|
|
|
|
|
'quux-vector.less',
|
|
|
|
|
'quux-fallback.less',
|
|
|
|
|
'bar.css',
|
|
|
|
|
'screen.less',
|
|
|
|
|
'screen-query.css',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-10-22 05:32:13 +00:00
|
|
|
array_map( 'basename', $module->getAllStyleFiles() )
|
|
|
|
|
);
|
|
|
|
|
}
|
2014-10-10 00:07:14 +00:00
|
|
|
|
2015-03-23 03:57:45 +00:00
|
|
|
/**
|
|
|
|
|
* Strip @noflip annotations from CSS code.
|
|
|
|
|
* @param string $css
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function stripNoflip( $css ) {
|
|
|
|
|
return str_replace( '/*@noflip*/ ', '', $css );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* What happens when you mix @embed and @noflip?
|
|
|
|
|
* This really is an integration test, but oh well.
|
|
|
|
|
*
|
|
|
|
|
* @covers ResourceLoaderFileModule::getStyles
|
|
|
|
|
* @covers ResourceLoaderFileModule::getStyleFiles
|
2018-06-26 01:52:40 +00:00
|
|
|
* @covers ResourceLoaderFileModule::readStyleFiles
|
|
|
|
|
* @covers ResourceLoaderFileModule::readStyleFile
|
2015-03-23 03:57:45 +00:00
|
|
|
*/
|
2015-06-16 19:06:19 +00:00
|
|
|
public function testMixedCssAnnotations() {
|
2015-03-23 03:57:45 +00:00
|
|
|
$basePath = __DIR__ . '/../../data/css';
|
2016-02-17 09:09:32 +00:00
|
|
|
$testModule = new ResourceLoaderFileModule( [
|
2015-03-23 03:57:45 +00:00
|
|
|
'localBasePath' => $basePath,
|
2016-02-17 09:09:32 +00:00
|
|
|
'styles' => [ 'test.css' ],
|
|
|
|
|
] );
|
2016-09-12 00:07:52 +00:00
|
|
|
$testModule->setName( 'testing' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$expectedModule = new ResourceLoaderFileModule( [
|
2015-03-23 03:57:45 +00:00
|
|
|
'localBasePath' => $basePath,
|
2016-02-17 09:09:32 +00:00
|
|
|
'styles' => [ 'expected.css' ],
|
|
|
|
|
] );
|
2016-09-12 00:07:52 +00:00
|
|
|
$expectedModule->setName( 'testing' );
|
2015-03-23 03:57:45 +00:00
|
|
|
|
2016-12-03 00:46:53 +00:00
|
|
|
$contextLtr = $this->getResourceLoaderContext( [
|
|
|
|
|
'lang' => 'en',
|
|
|
|
|
'dir' => 'ltr',
|
|
|
|
|
] );
|
|
|
|
|
$contextRtl = $this->getResourceLoaderContext( [
|
|
|
|
|
'lang' => 'he',
|
|
|
|
|
'dir' => 'rtl',
|
|
|
|
|
] );
|
2015-03-23 03:57:45 +00:00
|
|
|
|
|
|
|
|
// Since we want to compare the effect of @noflip+@embed against the effect of just @embed, and
|
|
|
|
|
// the @noflip annotations are always preserved, we need to strip them first.
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedModule->getStyles( $contextLtr ),
|
|
|
|
|
self::stripNoflip( $testModule->getStyles( $contextLtr ) ),
|
|
|
|
|
"/*@noflip*/ with /*@embed*/ gives correct results in LTR mode"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedModule->getStyles( $contextLtr ),
|
|
|
|
|
self::stripNoflip( $testModule->getStyles( $contextRtl ) ),
|
|
|
|
|
"/*@noflip*/ with /*@embed*/ gives correct results in RTL mode"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function providerGetTemplates() {
|
|
|
|
|
$modules = self::getModules();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
$modules['noTemplateModule'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
|
|
|
|
],
|
|
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
$modules['templateModuleHandlebars'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
'templates/template_awesome.handlebars' => "wow\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
$modules['htmlTemplateModule'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
'templates/template.html' => "<strong>hello</strong>\n",
|
|
|
|
|
'templates/template2.html' => "<div>goodbye</div>\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
$modules['aliasedHtmlTemplateModule'],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-03-23 03:57:45 +00:00
|
|
|
'foo.html' => "<strong>hello</strong>\n",
|
|
|
|
|
'bar.html' => "<div>goodbye</div>\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
2017-06-27 04:21:36 +00:00
|
|
|
[
|
|
|
|
|
$modules['htmlTemplateUnknown'],
|
|
|
|
|
false,
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-03-23 03:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-10 00:07:14 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider providerGetTemplates
|
|
|
|
|
* @covers ResourceLoaderFileModule::getTemplates
|
|
|
|
|
*/
|
|
|
|
|
public function testGetTemplates( $module, $expected ) {
|
|
|
|
|
$rl = new ResourceLoaderFileModule( $module );
|
2016-09-12 00:07:52 +00:00
|
|
|
$rl->setName( 'testing' );
|
2014-10-10 00:07:14 +00:00
|
|
|
|
2017-06-27 04:21:36 +00:00
|
|
|
if ( $expected === false ) {
|
|
|
|
|
$this->setExpectedException( MWException::class );
|
|
|
|
|
$rl->getTemplates();
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( $rl->getTemplates(), $expected );
|
|
|
|
|
}
|
2014-10-10 00:07:14 +00:00
|
|
|
}
|
2016-05-11 11:53:59 +00:00
|
|
|
|
2017-06-27 04:44:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoaderFileModule::stripBom
|
|
|
|
|
*/
|
2016-05-11 11:53:59 +00:00
|
|
|
public function testBomConcatenation() {
|
|
|
|
|
$basePath = __DIR__ . '/../../data/css';
|
|
|
|
|
$testModule = new ResourceLoaderFileModule( [
|
|
|
|
|
'localBasePath' => $basePath,
|
|
|
|
|
'styles' => [ 'bom.css' ],
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
] );
|
2016-09-12 00:07:52 +00:00
|
|
|
$testModule->setName( 'testing' );
|
2016-05-11 11:53:59 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
substr( file_get_contents( "$basePath/bom.css" ), 0, 10 ),
|
|
|
|
|
"\xef\xbb\xbf.efbbbf",
|
|
|
|
|
'File has leading BOM'
|
|
|
|
|
);
|
|
|
|
|
|
2016-12-03 00:46:53 +00:00
|
|
|
$context = $this->getResourceLoaderContext();
|
2016-05-11 11:53:59 +00:00
|
|
|
$this->assertEquals(
|
2016-12-03 00:46:53 +00:00
|
|
|
$testModule->getStyles( $context ),
|
2016-05-11 11:53:59 +00:00
|
|
|
[ 'all' => ".efbbbf_bom_char_at_start_of_file {}\n" ],
|
|
|
|
|
'Leading BOM removed when concatenating files'
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-07-27 01:53:37 +00:00
|
|
|
|
2018-08-09 15:09:56 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoaderFileModule::compileLessFile
|
|
|
|
|
*/
|
|
|
|
|
public function testLessFileCompilation() {
|
|
|
|
|
$context = $this->getResourceLoaderContext();
|
|
|
|
|
$basePath = __DIR__ . '/../../data/less/module';
|
|
|
|
|
$module = new ResourceLoaderFileTestModule( [
|
|
|
|
|
'localBasePath' => $basePath,
|
|
|
|
|
'styles' => [ 'styles.less' ],
|
|
|
|
|
], [
|
|
|
|
|
'lessVars' => [ 'foo' => '2px', 'Foo' => '#eeeeee' ]
|
|
|
|
|
] );
|
|
|
|
|
$module->setName( 'test.less' );
|
|
|
|
|
$styles = $module->getStyles( $context );
|
|
|
|
|
$this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 01:53:37 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoaderFileModule::getDefinitionSummary
|
2018-06-26 01:52:40 +00:00
|
|
|
* @covers ResourceLoaderFileModule::getFileHashes
|
2017-07-27 01:53:37 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetVersionHash() {
|
|
|
|
|
$context = $this->getResourceLoaderContext();
|
|
|
|
|
|
|
|
|
|
// Less variables
|
|
|
|
|
$module = new ResourceLoaderFileTestModule();
|
|
|
|
|
$version = $module->getVersionHash( $context );
|
|
|
|
|
$module = new ResourceLoaderFileTestModule( [], [
|
|
|
|
|
'lessVars' => [ 'key' => 'value' ],
|
|
|
|
|
] );
|
|
|
|
|
$this->assertNotEquals(
|
|
|
|
|
$version,
|
|
|
|
|
$module->getVersionHash( $context ),
|
|
|
|
|
'Using less variables is significant'
|
|
|
|
|
);
|
|
|
|
|
}
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
|
|
|
|
|
public function providerGetScriptPackageFiles() {
|
|
|
|
|
$basePath = __DIR__ . '/../../data/resourceloader';
|
|
|
|
|
$base = [ 'localBasePath' => $basePath ];
|
|
|
|
|
$commentScript = file_get_contents( "$basePath/script-comment.js" );
|
|
|
|
|
$nosemiScript = file_get_contents( "$basePath/script-nosemi.js" );
|
|
|
|
|
$config = RequestContext::getMain()->getConfig();
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
|
|
|
|
'script-comment.js',
|
|
|
|
|
'script-nosemi.js'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'files' => [
|
|
|
|
|
'script-comment.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => $commentScript,
|
|
|
|
|
],
|
|
|
|
|
'script-nosemi.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => $nosemiScript
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'main' => 'script-comment.js'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
|
|
|
|
'script-comment.js',
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'script-nosemi.js', 'main' => true ]
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
],
|
|
|
|
|
'deprecated' => 'Deprecation test',
|
|
|
|
|
'name' => 'test-deprecated'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'files' => [
|
|
|
|
|
'script-comment.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => $commentScript,
|
|
|
|
|
],
|
|
|
|
|
'script-nosemi.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => 'mw.log.warn(' .
|
|
|
|
|
'"This page is using the deprecated ResourceLoader module \"test-deprecated\".\\n' .
|
|
|
|
|
"Deprecation test" .
|
|
|
|
|
'");' .
|
|
|
|
|
$nosemiScript
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'main' => 'script-nosemi.js'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'init.js', 'file' => 'script-comment.js', 'main' => true ],
|
|
|
|
|
[ 'name' => 'nosemi.js', 'file' => 'script-nosemi.js' ],
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'files' => [
|
|
|
|
|
'init.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => $commentScript,
|
|
|
|
|
],
|
|
|
|
|
'nosemi.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => $nosemiScript
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'main' => 'init.js'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ] ],
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
'sample.json',
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'bar.js', 'content' => "console.log('Hello');" ],
|
|
|
|
|
[ 'name' => 'data.json', 'callback' => function ( $context ) {
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
return [ 'langCode' => $context->getLanguage() ];
|
|
|
|
|
} ],
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'config.json', 'config' => [
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
'Sitename',
|
|
|
|
|
'wgVersion' => 'Version',
|
|
|
|
|
] ],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'files' => [
|
|
|
|
|
'foo.json' => [
|
|
|
|
|
'type' => 'data',
|
|
|
|
|
'content' => [ 'Hello' => 'world' ],
|
|
|
|
|
],
|
|
|
|
|
'sample.json' => [
|
|
|
|
|
'type' => 'data',
|
|
|
|
|
'content' => (object)[ 'foo' => 'bar', 'answer' => 42 ],
|
|
|
|
|
],
|
|
|
|
|
'bar.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => "console.log('Hello');",
|
|
|
|
|
],
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
'data.json' => [
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
'type' => 'data',
|
|
|
|
|
'content' => [ 'langCode' => 'fy' ]
|
|
|
|
|
],
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
'config.json' => [
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
'type' => 'data',
|
|
|
|
|
'content' => [
|
|
|
|
|
'Sitename' => $config->get( 'Sitename' ),
|
|
|
|
|
'wgVersion' => $config->get( 'Version' ),
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'main' => 'bar.js'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'lang' => 'fy'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
|
|
|
|
[ 'file' => 'script-comment.js' ]
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'foo.json', 'callback' => 'functionThatDoesNotExist142857' ]
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
'foo.json' => [ 'type' => 'script', 'config' => [ 'Sitename' ] ]
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'foo.js', 'config' => 'Sitename' ]
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
|
|
|
|
'foo.js' => [ 'garbage' => 'data' ]
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
|
|
|
|
'filethatdoesnotexist142857.js'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
$base + [
|
|
|
|
|
'packageFiles' => [
|
|
|
|
|
'script-nosemi.js',
|
resourceloader: Change 'packageFiles' format to be JSON-compatible
The module definition format for 'packageFiles', as initially designed,
mixes sequential and associative arrays. This works in PHP, but not in
JSON. To make the format JSON compatible, use a 'name' key instead of
using the key in the main array.
Leave backwards compatibility in place so that extensions using the old
format can be migrated. This will be removed in the next commit.
Before:
'packageFiles' => [
'script.js',
'script2.js',
'config.json' => [ 'config' => [ 'Foo', 'Bar' ] ],
'data.json' => [ 'callback' => function () { ... } ],
],
After:
'packageFiles' => [
'script.js',
'script2.js',
[ 'name' => 'config.json', 'config' => [ 'Foo', 'Bar' ] ],
[ 'name' => 'data.json', 'callback' => function () { ... } ],
],
This can then be written in extension.json as:
"packageFiles": [
"script.js",
"script2.js",
[ "name": "config.json", "config": [ "Foo", "Bar" ] ],
[ "name": "data.json", "callback: [ "MyExtHooks", "getData" ] ]
]
Change-Id: Ic566a1cd7efd075c380bc50ba0cc2c329a2041d7
2019-02-22 01:26:23 +00:00
|
|
|
[ 'name' => 'foo.json', 'content' => [ 'Hello' => 'world' ], 'main' => true ]
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
false
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providerGetScriptPackageFiles
|
|
|
|
|
* @covers ResourceLoaderFileModule::getScript
|
|
|
|
|
* @covers ResourceLoaderFileModule::getPackageFiles
|
|
|
|
|
* @covers ResourceLoaderFileModule::expandPackageFiles
|
|
|
|
|
*/
|
|
|
|
|
public function testGetScriptPackageFiles( $moduleDefinition, $expected, $contextOptions = [] ) {
|
|
|
|
|
$module = new ResourceLoaderFileModule( $moduleDefinition );
|
|
|
|
|
$context = $this->getResourceLoaderContext( $contextOptions );
|
|
|
|
|
if ( isset( $moduleDefinition['name'] ) ) {
|
|
|
|
|
$module->setName( $moduleDefinition['name'] );
|
|
|
|
|
}
|
|
|
|
|
if ( $expected === false ) {
|
|
|
|
|
$this->setExpectedException( MWException::class );
|
|
|
|
|
$module->getScript( $context );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( $expected, $module->getScript( $context ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-22 05:32:13 +00:00
|
|
|
}
|