From a9bc732837963807d45253540da14d67492ea997 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 27 Nov 2023 14:01:27 +0000 Subject: [PATCH] DEV: Prepare jquery integration for Ember upgrade (#24566) - Update optional-features to tie the `jquery-integration` flag to the current ember version - Wrap ember-4-specific logic in ember-cli-build with a version check - Update global-compat.js to add the jquery global if it doesn't exist (i.e. if we're on a modern ember version) Extracted from https://github.com/discourse/discourse/pull/21720. This is a no-op under our current Ember 3.28 version. --- .../javascripts/discourse/app/global-compat.js | 5 +++++ .../discourse/config/optional-features.json | 6 ------ .../discourse/config/optional-features.json.js | 11 +++++++++++ app/assets/javascripts/discourse/ember-cli-build.js | 12 +++++++----- 4 files changed, 23 insertions(+), 11 deletions(-) delete mode 100644 app/assets/javascripts/discourse/config/optional-features.json create mode 100644 app/assets/javascripts/discourse/config/optional-features.json.js diff --git a/app/assets/javascripts/discourse/app/global-compat.js b/app/assets/javascripts/discourse/app/global-compat.js index 97270c55b71..eddc30a2798 100644 --- a/app/assets/javascripts/discourse/app/global-compat.js +++ b/app/assets/javascripts/discourse/app/global-compat.js @@ -1,3 +1,4 @@ +import jQuery from "jquery"; import virtualDom from "virtual-dom"; import widgetHelpers from "discourse-widget-hbs/helpers"; @@ -5,3 +6,7 @@ window.__widget_helpers = widgetHelpers; // TODO: Eliminate this global window.virtualDom = virtualDom; + +if (!window.$) { + window.$ = window.jQuery = jQuery; +} diff --git a/app/assets/javascripts/discourse/config/optional-features.json b/app/assets/javascripts/discourse/config/optional-features.json deleted file mode 100644 index d0d7c47f937..00000000000 --- a/app/assets/javascripts/discourse/config/optional-features.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "application-template-wrapper": false, - "default-async-observers": true, - "jquery-integration": true, - "template-only-glimmer-components": true -} diff --git a/app/assets/javascripts/discourse/config/optional-features.json.js b/app/assets/javascripts/discourse/config/optional-features.json.js new file mode 100644 index 00000000000..1f6dd89d55c --- /dev/null +++ b/app/assets/javascripts/discourse/config/optional-features.json.js @@ -0,0 +1,11 @@ +const EMBER_MAJOR_VERSION = parseInt( + require("ember-source/package.json").version.split(".")[0], + 10 +); + +module.exports = { + "application-template-wrapper": false, + "default-async-observers": true, + "jquery-integration": EMBER_MAJOR_VERSION < 4, + "template-only-glimmer-components": true, +}; diff --git a/app/assets/javascripts/discourse/ember-cli-build.js b/app/assets/javascripts/discourse/ember-cli-build.js index c7667f23dd1..6d7a2f33706 100644 --- a/app/assets/javascripts/discourse/ember-cli-build.js +++ b/app/assets/javascripts/discourse/ember-cli-build.js @@ -99,10 +99,12 @@ module.exports = function (defaults) { }, }); - // TODO: remove me - // Ember 3.28 still has some internal dependency on jQuery being a global, - // for the time being we will bring it in vendor.js - app.import("node_modules/jquery/dist/jquery.js", { prepend: true }); + if (EMBER_MAJOR_VERSION < 4) { + // TODO: remove me + // Ember 3.28 still has some internal dependency on jQuery being a global, + // for the time being we will bring it in vendor.js + app.import("node_modules/jquery/dist/jquery.js", { prepend: true }); + } // WARNING: We should only import scripts here if they are not in NPM. app.import(vendorJs + "bootbox.js"); @@ -177,7 +179,7 @@ module.exports = function (defaults) { if ( !request.includes("-embroider-implicit") && // TODO: delete special case for jquery when removing app.import() above - (request === "jquery" || + ((EMBER_MAJOR_VERSION < 4 && request === "jquery") || request.startsWith("admin/") || request.startsWith("wizard/") || request.startsWith("discourse/plugins/") ||