MDL-66192 javascript: Allow subdirs in AMD module names
[moodle.git] / lib / jssourcemap.php
CommitLineData
c53f86d4
RW
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.
13//
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/>.
16
17/**
18 * This file is serving the javascript source map.
19 *
20 * @package core
21 * @copyright 2019 Ryan Wyllie
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25// Disable moodle specific debug messages and any errors in output,
26// comment out when debugging or better look into error log!
27define('NO_DEBUG_DISPLAY', true);
28
73d3dfec
AN
29require('../config.php');
30require_once("$CFG->dirroot/lib/configonlylib.php");
c53f86d4
RW
31require_once("$CFG->dirroot/lib/classes/requirejs.php");
32
33$slashargument = min_get_slash_argument();
34if (!$slashargument) {
35 // The above call to min_get_slash_argument should always work.
36 die('Invalid request');
37}
38
39$slashargument = ltrim($slashargument, '/');
c53f86d4 40// Split into revision and module name.
73d3dfec 41[$file] = explode('/', $slashargument, 1);
c53f86d4
RW
42$file = '/' . min_clean_param($file, 'SAFEPATH');
43
44// Only load js files from the js modules folder from the components.
73d3dfec 45[$unused, $component, $module] = explode('/', $file, 3);
c53f86d4 46
73d3dfec
AN
47// When running a lazy load, we only deal with one file so we can just return the working sourcemap.
48$jsfiles = core_requirejs::find_one_amd_module($component, $module, false);
49$jsfile = reset($jsfiles);
c53f86d4 50
73d3dfec 51$mapfile = $jsfile . '.map';
c53f86d4 52
73d3dfec
AN
53if (file_exists($mapfile)) {
54 $mapdata = file_get_contents($mapfile);
55 $mapdata = json_decode($mapdata, true);
c53f86d4 56
c53f86d4
RW
57 $shortfilename = str_replace($CFG->dirroot, '', $jsfile);
58 $srcfilename = str_replace('/amd/build/', '/amd/src/', $shortfilename);
59 $srcfilename = str_replace('.min.js', '.js', $srcfilename);
73d3dfec
AN
60 $fullsrcfilename = $CFG->wwwroot . $srcfilename;
61 $mapdata['sources'][0] = $fullsrcfilename;
c53f86d4 62
73d3dfec
AN
63 echo json_encode($mapdata);
64} else {
65 // If there is no source map file, then we will not generate one for you, sorry.
66 header('HTTP/1.0 404 not found');
c53f86d4 67}