From 3140a4b2cef2dff0c83f139c68a38c4df3e58694 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 10 Jul 2023 15:11:49 -0700 Subject: [PATCH] DEV: invert pretty-text build logic (#22524) Generally follows the same pattern as #22520 There are some changes here, notably it uses the addon's babel settings rather than the app's, and it goes through the same treatment as the rest of the addon code (which may include more than just babel). However, this probably brings us closer to the normal expectations you have around developing addon code, and in any case, does not seem to have any effect on the final output: ``` $ diff dist/assets/markdown-it-bundle.js /tmp/dist-before/assets/markdown-it-bundle.js ``` --- .../javascripts/discourse/ember-cli-build.js | 12 +++++- .../discourse/lib/pretty-text-engine.js | 28 -------------- app/assets/javascripts/discourse/package.json | 1 - app/assets/javascripts/pretty-text/index.js | 37 +++++++++++++++++++ .../javascripts/pretty-text/package.json | 1 + 5 files changed, 48 insertions(+), 31 deletions(-) delete mode 100644 app/assets/javascripts/discourse/lib/pretty-text-engine.js diff --git a/app/assets/javascripts/discourse/ember-cli-build.js b/app/assets/javascripts/discourse/ember-cli-build.js index bdc90104bdc..c981472629d 100644 --- a/app/assets/javascripts/discourse/ember-cli-build.js +++ b/app/assets/javascripts/discourse/ember-cli-build.js @@ -4,7 +4,6 @@ const EmberApp = require("ember-cli/lib/broccoli/ember-app"); const resolve = require("path").resolve; const mergeTrees = require("broccoli-merge-trees"); const concat = require("broccoli-concat"); -const prettyTextEngine = require("./lib/pretty-text-engine"); const { createI18nTree } = require("./lib/translation-plugin"); const { parsePluginClientSettings } = require("./lib/site-settings-plugin"); const discourseScss = require("./lib/discourse-scss"); @@ -186,6 +185,10 @@ module.exports = function (defaults) { const wizardTree = app.project.findAddonByName("wizard").treeForAddonBundle(); + const markdownItBundleTree = app.project + .findAddonByName("pretty-text") + .treeForMarkdownItBundle(); + const terserPlugin = app.project.findAddonByName("ember-cli-terser"); const applyTerser = (tree) => terserPlugin.postprocessTree("all", tree); @@ -211,7 +214,12 @@ module.exports = function (defaults) { outputFile: `assets/wizard.js`, }) ), - applyTerser(prettyTextEngine(app)), + applyTerser( + concat(markdownItBundleTree, { + inputFiles: ["**/*.js"], + outputFile: `assets/markdown-it-bundle.js`, + }) + ), generateScriptsTree(app), applyTerser(discoursePluginsTree), ]); diff --git a/app/assets/javascripts/discourse/lib/pretty-text-engine.js b/app/assets/javascripts/discourse/lib/pretty-text-engine.js deleted file mode 100644 index 08d91f1877a..00000000000 --- a/app/assets/javascripts/discourse/lib/pretty-text-engine.js +++ /dev/null @@ -1,28 +0,0 @@ -const mergeTrees = require("broccoli-merge-trees"); -const funnel = require("broccoli-funnel"); -const concat = require("broccoli-concat"); -const WatchedDir = require("broccoli-source").WatchedDir; -const Funnel = require("broccoli-funnel"); - -module.exports = function prettyTextEngine(app) { - let babelAddon = app.project.findAddonByName("ember-cli-babel"); - - const sourceTree = new WatchedDir( - "../pretty-text/engines/discourse-markdown" - ); - const namespacedTree = new Funnel(sourceTree, { - getDestinationPath: function (relativePath) { - return `pretty-text/engines/discourse-markdown/${relativePath}`; - }, - }); - - const engineTree = babelAddon.transpileTree(namespacedTree); - - let markdownIt = funnel("../node_modules/markdown-it/dist", { - files: ["markdown-it.js"], - }); - return concat(mergeTrees([engineTree, markdownIt]), { - inputFiles: ["**/*.js"], - outputFile: `assets/markdown-it-bundle.js`, - }); -}; diff --git a/app/assets/javascripts/discourse/package.json b/app/assets/javascripts/discourse/package.json index 6e373f4b0f9..5a1168cde1f 100644 --- a/app/assets/javascripts/discourse/package.json +++ b/app/assets/javascripts/discourse/package.json @@ -21,7 +21,6 @@ "discourse-widget-hbs": "1.0.0", "ember-source": "~3.28.12", "handlebars": "^4.7.7", - "markdown-it": "^13.0.1", "pretty-text": "1.0.0" }, "devDependencies": { diff --git a/app/assets/javascripts/pretty-text/index.js b/app/assets/javascripts/pretty-text/index.js index d4a9ef20380..7b77a4dfbf6 100644 --- a/app/assets/javascripts/pretty-text/index.js +++ b/app/assets/javascripts/pretty-text/index.js @@ -1,7 +1,44 @@ "use strict"; +const Funnel = require("broccoli-funnel"); +const mergeTrees = require("broccoli-merge-trees"); +const path = require("path"); + module.exports = { name: require("./package").name, + + // custom method to produce the tree for markdown-it-bundle.js + // called by ember-cli-build.js in discourse core + // + // code in here is only needed by the editor and we do not want them included + // into the main addon/vendor bundle; instead, it'll be included via a script + // tag as needed + treeForMarkdownItBundle() { + return mergeTrees([this._treeForEngines(), this._treeForMarkdownIt()]); + }, + + // treat the JS code in /engines like any other JS code in the /addon folder + _treeForEngines() { + let enginesTreePath = path.resolve(this.root, "engines"); + let enginesTree = this.treeGenerator(enginesTreePath); + + // we started at /engines, if we just call treeForAddon, the modules will + // be under pretty-text/*, but we want pretty-text/engines/* + let namespacedTree = new Funnel(enginesTree, { + destDir: "engines", + }); + + return this.treeForAddon.call(this, namespacedTree); + }, + + _treeForMarkdownIt() { + let markdownIt = require.resolve("markdown-it/dist/markdown-it.js"); + + return new Funnel(path.dirname(markdownIt), { + files: ["markdown-it.js"], + }); + }, + isDevelopingAddon() { return true; }, diff --git a/app/assets/javascripts/pretty-text/package.json b/app/assets/javascripts/pretty-text/package.json index e5ad0547d5b..8f310b7ce28 100644 --- a/app/assets/javascripts/pretty-text/package.json +++ b/app/assets/javascripts/pretty-text/package.json @@ -37,6 +37,7 @@ "ember-source": "~3.28.12", "ember-source-channel-url": "^3.0.0", "loader.js": "^4.7.0", + "markdown-it": "^13.0.1", "webpack": "^5.88.1" }, "engines": {