A platform for community discussion. Free, open, simple.
DEV: Update plugin JS loading in Ember CLI testem environment
David Taylorff5a6edb71a2
diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.jsindex 07071809dd3..3e989603399 100644--- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js+++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js@@ -7,6 +7,7 @@ const cleanBaseURL = require("clean-base-url");const path = require("path");const { promises: fs } = require("fs");const { JSDOM } = require("jsdom");+const { shouldLoadPluginTestJs } = require("discourse/lib/plugin-js");// via https://stackoverflow.com/a/6248722/165668function generateUID() {@@ -298,6 +299,14 @@ module.exports = {return true;},+ contentFor: function (type, config) {+ if (shouldLoadPluginTestJs() && type === "test-plugin-js") {+ return `<script src="${config.rootURL}assets/discourse/tests/active-plugins.js"></script>`;+ } else if (shouldLoadPluginTestJs() && type === "test-plugin-tests-js") {+ return `<script id="plugin-test-script" src="${config.rootURL}assets/discourse/tests/plugin-tests.js"></script>`;+ }+ },+serverMiddleware(config) {const app = config.app;let { proxy, rootURL, baseURL } = config.options;diff --git a/app/assets/javascripts/discourse/lib/plugin-js.js b/app/assets/javascripts/discourse/lib/plugin-js.jsnew file mode 100644index 00000000000..5d741fc4ffe--- /dev/null+++ b/app/assets/javascripts/discourse/lib/plugin-js.js@@ -0,0 +1,7 @@+const EmberApp = require("ember-cli/lib/broccoli/ember-app");++module.exports = {+ shouldLoadPluginTestJs() {+ return EmberApp.env() === "development" || process.env.LOAD_PLUGINS === "1";+ },+};diff --git a/app/assets/javascripts/discourse/lib/translation-plugin.js b/app/assets/javascripts/discourse/lib/translation-plugin.jsindex a8551d0b9ee..d1e6543b335 100644--- a/app/assets/javascripts/discourse/lib/translation-plugin.js+++ b/app/assets/javascripts/discourse/lib/translation-plugin.js@@ -6,6 +6,7 @@ const mergeTrees = require("broccoli-merge-trees");const MessageFormat = require("messageformat");const deepmerge = require("deepmerge");const glob = require("glob");+const { shouldLoadPluginTestJs } = require("discourse/lib/plugin-js");let built = false;@@ -92,11 +93,15 @@ module.exports = function translatePlugin(...params) {};module.exports.createI18nTree = function (discourseRoot, vendorJs) {- let translations = [discourseRoot + "/config/locales"].concat(- glob- .sync(discourseRoot + "/plugins/*/config/locales/client.en.yml")- .map((f) => f.replace(/\/client\.en\.yml$/, ""))- );+ let translations = [discourseRoot + "/config/locales"];++ if (shouldLoadPluginTestJs()) {+ translations = translations.concat(+ glob+ .sync(discourseRoot + "/plugins/*/config/locales/client.en.yml")+ .map((f) => f.replace(/\/client\.en\.yml$/, ""))+ );+ }let en = new TranslationPlugin(translations, "client.en.yml");diff --git a/app/assets/javascripts/discourse/testem.js b/app/assets/javascripts/discourse/testem.jsindex 0c5b5d437e7..1fd4b7132e7 100644--- a/app/assets/javascripts/discourse/testem.js+++ b/app/assets/javascripts/discourse/testem.js@@ -1,4 +1,5 @@const TapReporter = require("testem/lib/reporters/tap_reporter");+const { shouldLoadPluginTestJs } = require("discourse/lib/plugin-js");class Reporter {constructor() {@@ -25,8 +26,8 @@ class Reporter {module.exports = {test_page: "tests/index.html?hidepassed",disable_watching: true,- launch_in_ci: ["Chrome", "Firefox", "Headless Firefox"], // Firefox is old ESR version, Headless Firefox is up-to-date evergreen version- launch_in_dev: ["Chrome"],+ launch_in_ci: ["Chrome"],+ // launch_in_dev: ["Chrome"] // Ember-CLI always launches testem in 'CI' modetap_failed_tests_only: process.env.CI,parallel: 1, // disable parallel tests for stabilitybrowser_start_timeout: 120,@@ -51,3 +52,15 @@ module.exports = {},reporter: Reporter,};++if (shouldLoadPluginTestJs()) {+ const target = `http://localhost:${process.env.UNICORN_PORT || "3000"}`;+ module.exports.proxies = {+ "/assets/discourse/tests/active-plugins.js": {+ target,+ },+ "/assets/discourse/tests/plugin-tests.js": {+ target,+ },+ };+}diff --git a/app/assets/javascripts/discourse/tests/index.html b/app/assets/javascripts/discourse/tests/index.htmlindex 1c9ac2bb559..def3e68131e 100644--- a/app/assets/javascripts/discourse/tests/index.html+++ b/app/assets/javascripts/discourse/tests/index.html@@ -50,11 +50,11 @@<script src="{{rootURL}}assets/test-support.js"></script><script src="{{rootURL}}assets/discourse.js"></script><script src="{{rootURL}}assets/discourse-markdown.js"></script>- <script src="{{rootURL}}assets/discourse/tests/active-plugins.js"></script>+ {{content-for "test-plugin-js"}}<script src="{{rootURL}}assets/admin.js"></script><script src="{{rootURL}}assets/test-helpers.js"></script><script src="{{rootURL}}assets/core-tests.js"></script>- <script src="{{rootURL}}assets/discourse/tests/plugin-tests.js"></script>+ {{content-for "test-plugin-tests-js"}}<script>require('discourse/tests/test-boot-ember-cli');</script>diff --git a/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js b/app/assets/javascripts/discourse/tests/test-boot-ember-cli.jsindex cfbc0b9ff8c..e58bc312aac 100644--- a/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js+++ b/app/assets/javascripts/discourse/tests/test-boot-ember-cli.js@@ -8,6 +8,13 @@ import { setup } from "qunit-dom";setEnvironment("testing");document.addEventListener("discourse-booted", () => {+ const script = document.getElementById("plugin-test-script");+ if (script && !requirejs.entries["discourse/tests/active-plugins"]) {+ throw new Error(+ `Plugin JS payload failed to load from ${script.src}. Is the Rails server running?`+ );+ }+let setupTests = require("discourse/tests/setup-tests").default;const skippingCore =new URLSearchParams(window.location.search).get("qunit_skip_core") === "1";