Exemplo n.º 1
0
 /**
  * Test requirejs loader
  */
 public function test_requirejs()
 {
     global $CFG;
     // Find a core module.
     $result = core_requirejs::find_one_amd_module('core', 'templates', false);
     $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);
     $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->assertNotContains('.min', $path);
     }
     // Find all modules - debugging.
     $result = core_requirejs::find_all_amd_modules(false);
     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);
         $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->assertContains('.min', $path);
     }
 }
Exemplo n.º 2
0
            $js = implode($replace, explode($search, $js, 2));
            $content .= $js;
        }
        js_write_cache_file_content($candidate, $content);
        // Verify nothing failed in cache file creation.
        clearstatcache();
        if (file_exists($candidate)) {
            js_send_cached($candidate, $etag, 'requirejs.php');
            exit(0);
        }
    }
}
if ($lazyload) {
    $jsfiles = core_requirejs::find_one_amd_module($component, $module, true);
} else {
    $jsfiles = core_requirejs::find_all_amd_modules(true);
}
$content = '';
foreach ($jsfiles as $modulename => $jsfile) {
    $shortfilename = str_replace($CFG->dirroot, '', $jsfile);
    $js = "// ---- {$shortfilename} ----\n";
    $js .= file_get_contents($jsfile) . "\n";
    // Inject the module name into the define.
    $replace = 'define(\'' . $modulename . '\', ';
    $search = 'define(';
    if (strpos($js, $search) === false) {
        // We can't call debugging because we only have minimal config loaded.
        header('HTTP/1.0 500 error');
        die('JS file: ' . $shortfilename . ' does not contain a javascript module in AMD format. "define()" not found.');
    }
    // Replace only the first occurrence.