diff --git a/app/assets/javascripts/discourse/ember-cli-build.js b/app/assets/javascripts/discourse/ember-cli-build.js index 9d61616daca..0230f6298a3 100644 --- a/app/assets/javascripts/discourse/ember-cli-build.js +++ b/app/assets/javascripts/discourse/ember-cli-build.js @@ -7,6 +7,7 @@ const concat = require("broccoli-concat"); const prettyTextEngine = require("./lib/pretty-text-engine"); const { createI18nTree } = require("./lib/translation-plugin"); const discourseScss = require("./lib/discourse-scss"); +const generateScriptsTree = require("./lib/scripts"); const funnel = require("broccoli-funnel"); module.exports = function (defaults) { @@ -138,5 +139,6 @@ module.exports = function (defaults) { headerFiles: [`start-app.js`], inputFiles: [`discourse-boot.js`], }), + generateScriptsTree(app), ]); }; diff --git a/app/assets/javascripts/discourse/lib/scripts.js b/app/assets/javascripts/discourse/lib/scripts.js new file mode 100644 index 00000000000..3c4572b113c --- /dev/null +++ b/app/assets/javascripts/discourse/lib/scripts.js @@ -0,0 +1,39 @@ +const mergeTrees = require("broccoli-merge-trees"); +const funnel = require("broccoli-funnel"); +const concat = require("broccoli-concat"); +const fs = require("fs"); + +// Each file under `scripts/{name}.js` is run through babel, sourcemapped, and then output to `/assets/{name}.js +module.exports = function scriptsTree(app) { + let babelAddon = app.project.findAddonByName("ember-cli-babel"); + let babelConfig = { + babel: { sourceMaps: "inline" }, + "ember-cli-babel": { compileModules: false }, + }; + + const trees = []; + + const scripts = fs + .readdirSync("scripts", { withFileTypes: true }) + .filter((dirent) => dirent.isFile()); + + for (let script of scripts) { + let source = funnel(`scripts`, { + files: [script.name], + destDir: "scripts", + }); + + // Babel will append a base64 sourcemap to the file + let transpiled = babelAddon.transpileTree(source, babelConfig); + + // We don't actually need to concat any source files... but this will move the base64 + // source map into its own file + let transpiledWithDecodedSourcemap = concat(transpiled, { + outputFile: `assets/${script.name}`, + }); + + trees.push(transpiledWithDecodedSourcemap); + } + + return mergeTrees(trees); +}; diff --git a/app/assets/javascripts/activate-account.js b/app/assets/javascripts/discourse/scripts/activate-account.js similarity index 96% rename from app/assets/javascripts/activate-account.js rename to app/assets/javascripts/discourse/scripts/activate-account.js index 3de61ebccd6..2d1d7428c20 100644 --- a/app/assets/javascripts/activate-account.js +++ b/app/assets/javascripts/discourse/scripts/activate-account.js @@ -1,4 +1,3 @@ -// discourse-skip-module (function () { setTimeout(function () { const $activateButton = $("#activate-account-button"); diff --git a/app/assets/javascripts/auto-redirect.js b/app/assets/javascripts/discourse/scripts/auto-redirect.js similarity index 87% rename from app/assets/javascripts/auto-redirect.js rename to app/assets/javascripts/discourse/scripts/auto-redirect.js index f9c2f3ded81..ae6255bb63f 100644 --- a/app/assets/javascripts/auto-redirect.js +++ b/app/assets/javascripts/discourse/scripts/auto-redirect.js @@ -1,4 +1,3 @@ -// discourse-skip-module (function () { const path = document.getElementById("data-auto-redirect").dataset.path; setTimeout(function () { diff --git a/app/assets/javascripts/browser-detect.js b/app/assets/javascripts/discourse/scripts/browser-detect.js similarity index 100% rename from app/assets/javascripts/browser-detect.js rename to app/assets/javascripts/discourse/scripts/browser-detect.js diff --git a/app/assets/javascripts/browser-update.js b/app/assets/javascripts/discourse/scripts/browser-update.js similarity index 100% rename from app/assets/javascripts/browser-update.js rename to app/assets/javascripts/discourse/scripts/browser-update.js diff --git a/app/assets/javascripts/embed-application.js b/app/assets/javascripts/discourse/scripts/embed-application.js similarity index 98% rename from app/assets/javascripts/embed-application.js rename to app/assets/javascripts/discourse/scripts/embed-application.js index f71ac900ef2..0d231d6b5c3 100644 --- a/app/assets/javascripts/embed-application.js +++ b/app/assets/javascripts/discourse/scripts/embed-application.js @@ -1,4 +1,3 @@ -// discourse-skip-module (function () { const referer = document.getElementById("data-embedded").dataset.referer; diff --git a/app/assets/javascripts/google-tag-manager.js b/app/assets/javascripts/discourse/scripts/google-tag-manager.js similarity index 97% rename from app/assets/javascripts/google-tag-manager.js rename to app/assets/javascripts/discourse/scripts/google-tag-manager.js index 90cc8ccaaa8..39787e99934 100644 --- a/app/assets/javascripts/google-tag-manager.js +++ b/app/assets/javascripts/discourse/scripts/google-tag-manager.js @@ -1,4 +1,3 @@ -// discourse-skip-module (function () { const gtmDataElement = document.getElementById("data-google-tag-manager"); const dataLayerJson = JSON.parse(gtmDataElement.dataset.dataLayer); diff --git a/app/assets/javascripts/google-universal-analytics-v3.js b/app/assets/javascripts/discourse/scripts/google-universal-analytics-v3.js similarity index 97% rename from app/assets/javascripts/google-universal-analytics-v3.js rename to app/assets/javascripts/discourse/scripts/google-universal-analytics-v3.js index b0e06a51c69..efafd4884c7 100644 --- a/app/assets/javascripts/google-universal-analytics-v3.js +++ b/app/assets/javascripts/discourse/scripts/google-universal-analytics-v3.js @@ -1,4 +1,3 @@ -// discourse-skip-module /* eslint-disable */ // prettier-ignore (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ diff --git a/app/assets/javascripts/google-universal-analytics-v4.js b/app/assets/javascripts/discourse/scripts/google-universal-analytics-v4.js similarity index 96% rename from app/assets/javascripts/google-universal-analytics-v4.js rename to app/assets/javascripts/discourse/scripts/google-universal-analytics-v4.js index d16f3de9eb6..ce313454679 100644 --- a/app/assets/javascripts/google-universal-analytics-v4.js +++ b/app/assets/javascripts/discourse/scripts/google-universal-analytics-v4.js @@ -1,4 +1,3 @@ -// discourse-skip-module (function () { const gaDataElement = document.getElementById("data-ga-universal-analytics"); window.dataLayer = window.dataLayer || []; diff --git a/app/assets/javascripts/onpopstate-handler.js b/app/assets/javascripts/discourse/scripts/onpopstate-handler.js similarity index 89% rename from app/assets/javascripts/onpopstate-handler.js rename to app/assets/javascripts/discourse/scripts/onpopstate-handler.js index 9748c1b886e..031c60dd893 100644 --- a/app/assets/javascripts/onpopstate-handler.js +++ b/app/assets/javascripts/discourse/scripts/onpopstate-handler.js @@ -1,4 +1,3 @@ -// discourse-skip-module window.onpopstate = function (event) { // check if Discourse object exists if not take care of back navigation if (event.state && !window.hasOwnProperty("Discourse")) { diff --git a/app/assets/javascripts/print-page.js b/app/assets/javascripts/discourse/scripts/print-page.js similarity index 100% rename from app/assets/javascripts/print-page.js rename to app/assets/javascripts/discourse/scripts/print-page.js diff --git a/lib/ember_cli.rb b/lib/ember_cli.rb index 64690949d73..efae6be1697 100644 --- a/lib/ember_cli.rb +++ b/lib/ember_cli.rb @@ -9,7 +9,7 @@ module EmberCli pretty-text-bundle.js start-discourse.js vendor.js - ) + ) + Dir.glob("app/assets/javascripts/discourse/scripts/*.js").map { |f| File.basename(f) } def self.script_chunks return @@chunk_infos if defined? @@chunk_infos