MDL-78324 core: Convert core/tag to esm
[moodle.git] / lib / jssourcemap.php
blob980d4f111845510bc9f1764221a1ffc8d012b310
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file is serving the javascript source map.
20 * @package core
21 * @copyright 2019 Ryan Wyllie
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 // Disable moodle specific debug messages and any errors in output,
26 // comment out when debugging or better look into error log!
27 define('NO_DEBUG_DISPLAY', true);
29 // We need just the values from config.php and minlib.php.
30 define('ABORT_AFTER_CONFIG', true);
31 require('../config.php'); // This stops immediately at the beginning of lib/setup.php.
32 require_once("$CFG->dirroot/lib/classes/requirejs.php");
34 $slashargument = min_get_slash_argument();
35 if (!$slashargument) {
36 // The above call to min_get_slash_argument should always work.
37 die('Invalid request');
40 $slashargument = ltrim($slashargument, '/');
41 // Split into revision and module name.
42 [$file] = explode('/', $slashargument, 1);
43 $file = '/' . min_clean_param($file, 'SAFEPATH');
45 // Only load js files from the js modules folder from the components.
46 [$unused, $component, $module] = explode('/', $file, 3);
48 // When running a lazy load, we only deal with one file so we can just return the working sourcemap.
49 $jsfiles = core_requirejs::find_one_amd_module($component, $module);
50 $jsfile = reset($jsfiles);
52 $mapfile = $jsfile . '.map';
54 if (file_exists($mapfile)) {
55 $mapdata = file_get_contents($mapfile);
56 $mapdata = json_decode($mapdata, true);
58 $shortfilename = str_replace($CFG->dirroot, '', $jsfile);
59 $srcfilename = str_replace('/amd/build/', '/amd/src/', $shortfilename);
60 $srcfilename = str_replace('.min.js', '.js', $srcfilename);
61 $fullsrcfilename = $CFG->wwwroot . $srcfilename;
62 $mapdata['sources'][0] = $fullsrcfilename;
64 echo json_encode($mapdata);
65 } else {
66 // If there is no source map file, then we will not generate one for you, sorry.
67 header('HTTP/1.0 404 not found');