Skip to content

Commit

Permalink
MDL-73183 js: Stop supporting debug mode for amd
Browse files Browse the repository at this point in the history
This option was used to return the source files, but we do not support
this any more because we use source maps instead and require babel
transformation to support ES2015.

It does not make sense to support this code path any longer.
  • Loading branch information
andrewnicols committed Feb 8, 2022
1 parent 6652ec1 commit 5d66721
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 42 deletions.
15 changes: 4 additions & 11 deletions lib/classes/requirejs.php
Expand Up @@ -40,11 +40,11 @@ class core_requirejs {
*
* @param string $component The component determines the folder the js file should be in.
* @param string $jsfilename The filename for the module (with the js extension).
* @param boolean $debug If true, returns the paths to the original (unminified) source files.
* @param bool $unused
* @return array $files An array of mappings from module names to file paths.
* Empty array if the file does not exist.
*/
public static function find_one_amd_module($component, $jsfilename, $debug = false) {
public static function find_one_amd_module($component, $jsfilename, $unused = false) {
$jsfileroot = core_component::get_component_directory($component);
if (!$jsfileroot) {
return array();
Expand All @@ -54,10 +54,6 @@ public static function find_one_amd_module($component, $jsfilename, $debug = fal

$srcdir = $jsfileroot . '/amd/build';
$minpart = '.min';
if ($debug) {
$srcdir = $jsfileroot . '/amd/src';
$minpart = '';
}

$filename = $srcdir . '/' . $module . $minpart . '.js';
if (!file_exists($filename)) {
Expand All @@ -74,11 +70,11 @@ public static function find_one_amd_module($component, $jsfilename, $debug = fal
* The expected location for amd modules is:
* <componentdir>/amd/src/modulename.js
*
* @param boolean $debug If true, returns the paths to the original (unminified) source files.
* @param boolean $unused
* @param boolean $includelazy If true, includes modules with the -lazy suffix.
* @return array $files An array of mappings from module names to file paths.
*/
public static function find_all_amd_modules($debug = false, $includelazy = false) {
public static function find_all_amd_modules($unused = false, $includelazy = false) {
global $CFG;

$jsdirs = array();
Expand Down Expand Up @@ -106,9 +102,6 @@ public static function find_all_amd_modules($debug = false, $includelazy = false

foreach ($jsdirs as $component => $dir) {
$srcdir = $dir . '/build';
if ($debug) {
$srcdir = $dir . '/src';
}
if (!is_dir($srcdir) || !is_readable($srcdir)) {
// This is probably an empty amd directory without src or build.
// Skip it - RecursiveDirectoryIterator fatals if the directory is not readable as an iterator.
Expand Down
2 changes: 1 addition & 1 deletion lib/jssourcemap.php
Expand Up @@ -45,7 +45,7 @@
[$unused, $component, $module] = explode('/', $file, 3);

// When running a lazy load, we only deal with one file so we can just return the working sourcemap.
$jsfiles = core_requirejs::find_one_amd_module($component, $module, false);
$jsfiles = core_requirejs::find_one_amd_module($component, $module);
$jsfile = reset($jsfiles);

$mapfile = $jsfile . '.map';
Expand Down
2 changes: 1 addition & 1 deletion lib/requirejs.php
Expand Up @@ -128,7 +128,7 @@

// If we've made it here then we're in "dev mode" where everything is lazy loaded.
// So all files will be served one at a time.
$jsfiles = core_requirejs::find_one_amd_module($component, $module, false);
$jsfiles = core_requirejs::find_one_amd_module($component, $module);

if (!empty($jsfiles)) {
$modulename = array_keys($jsfiles)[0];
Expand Down
33 changes: 4 additions & 29 deletions lib/tests/requirejs_test.php
Expand Up @@ -42,42 +42,17 @@ public function test_requirejs() {
global $CFG;

// Find a core module.
$result = core_requirejs::find_one_amd_module('core', 'templates', false);
$result = core_requirejs::find_one_amd_module('core', 'templates');
$expected = ['core/templates' => $CFG->dirroot . '/lib/amd/build/templates.min.js'];
$this->assertEquals($expected, $result);

$result = core_requirejs::find_one_amd_module('core', 'templates', true);
$expected = ['core/templates' => $CFG->dirroot . '/lib/amd/src/templates.js'];
$this->assertEquals($expected, $result);

// Find a subsystem module (none exist yet).
$result = core_requirejs::find_one_amd_module('core_group', 'doesnotexist', false);
$result = core_requirejs::find_one_amd_module('core_group', 'doesnotexist');
$expected = [];
$this->assertEquals($expected, $result);

// Find a plugin module.
$result = core_requirejs::find_one_amd_module('mod_assign', 'grading_panel', true);
$expected = ['mod_assign/grading_panel' => $CFG->dirroot . '/mod/assign/amd/src/grading_panel.js'];
$this->assertEquals($expected, $result);

// Find all modules - no debugging.
$result = core_requirejs::find_all_amd_modules(true);
foreach ($result as $key => $path) {
// Lets verify the first part of the key is a valid component name and the second part correctly contains "min" or not.
list($component, $template) = explode('/', $key, 2);
// Can we resolve it to a valid dir?
$dir = core_component::get_component_directory($component);
$this->assertNotEmpty($dir);

// Only "core" is allowed to have no _ in component names.
if (strpos($component, '_') === false) {
$this->assertEquals('core', $component);
}
$this->assertStringNotContainsString('.min', $path);
}

// Find all modules - debugging.
$result = core_requirejs::find_all_amd_modules(false);
// Find all modules.
$result = core_requirejs::find_all_amd_modules();
foreach ($result as $key => $path) {
// Lets verify the first part of the key is a valid component name and the second part correctly contains "min" or not.
list($component, $template) = explode('/', $key, 2);
Expand Down

0 comments on commit 5d66721

Please sign in to comment.