From 84c650489ab097771b50e6c63d26ffdd001db037 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 24 Feb 2023 09:22:14 +0800 Subject: [PATCH] MDL-75012 js: Drop support for modules built pre-babel We now include more polyfills than we have ever done, and some of these cause false-positives on our code to address modules which do not have a name. We started adding module names to modules in Moodle 3.8 at _build_ time and kept support for modules which were not transpiled using Babel before that point. We no longer need to support this as all pre-3.8 Moodle versions are long out of support. The most recent to go out of support was Moodle 3.5, which went completely out of support in May 2021. We should not support code written and minified without a transpilation phase from this time any longer. --- lib/requirejs.php | 28 +++++++++++----------------- lib/upgrade.txt | 2 ++ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/requirejs.php b/lib/requirejs.php index 6da961ba892..4c9824130c4 100644 --- a/lib/requirejs.php +++ b/lib/requirejs.php @@ -104,13 +104,12 @@ if ($rev > 0 and $rev < (time() + 60 * 60)) { $js = rtrim($js); $js .= "\n"; - if (preg_match('/define\(\s*(\[|function)/', $js)) { - // If the JavaScript module has been defined without specifying a name then we'll - // add the Moodle module name now. - $replace = 'define(\'' . $modulename . '\', '; - $search = 'define('; - // Replace only the first occurrence. - $js = implode($replace, explode($search, $js, 2)); + if (!preg_match('/define\(\s*["\']/', $js)) { + $shortfilename = str_replace($CFG->dirroot, '', $jsfile); + error_log( + "JS file: '{$shortfilename}' cannot be loaded, or does not contain a javascript" . + ' module in AMD format. "define()" not found.' + ); } $content .= $js; @@ -157,16 +156,11 @@ if (!empty($jsfiles)) { $js = rtrim($js); } - if (preg_match('/define\(\s*(\[|function)/', $js)) { - // If the JavaScript module has been defined without specifying a name then we'll - // add the Moodle module name now. - $replace = 'define(\'' . $modulename . '\', '; - - // Replace only the first occurrence. - $js = implode($replace, explode('define(', $js, 2)); - } else if (!preg_match('/define\s*\(/', $js)) { - debugging('JS file: ' . $shortfilename . ' cannot be loaded, or does not contain a javascript' . - ' module in AMD format. "define()" not found.', DEBUG_DEVELOPER); + if (!preg_match('/define\(\s*["\']/', $js)) { + error_log( + "JS file: '{$shortfilename}' cannot be loaded, or does not contain a javascript" . + ' module in AMD format. "define()" not found.' + ); } js_send_uncached($js, 'requirejs.php'); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index f790a3bd2a8..db0a3a01535 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -72,6 +72,8 @@ information provided here is intended especially for developers. * The core/modal module and all their versions (SAVE_CANCEL, DELETE_CANCEL...) now has a setButtonDisable to disable or enable specific modal action buttons. This function allows developers to have modals that could only be submited if the user do some action in the modal body like ticking a checkbox or selecting an element. +* Support for serving of AMD modules built in really old versions of Moodle (<= 3.8) has been removed. + Please ensure that your AMD modules have been rebuilt with a supported Moodle version. === 4.1 === -- 2.43.0