Skip to content

A platform for community discussion. Free, open, simple.

DEV: Remove template compilation from theme transpiler (#33862)

Jarek Radosz5a42bdc6b459
diff --git a/app/assets/javascripts/theme-transpiler/transpiler.js b/app/assets/javascripts/theme-transpiler/transpiler.js
index 00248c9977a..aa5b5f0f3ac 100644
--- a/app/assets/javascripts/theme-transpiler/transpiler.js
+++ b/app/assets/javascripts/theme-transpiler/transpiler.js
@@ -2,67 +2,14 @@ import "./shims";
import "./postcss";
import "./theme-rollup";
import { transform as babelTransform } from "@babel/standalone";
-import HTMLBarsInlinePrecompile from "babel-plugin-ember-template-compilation";
import DecoratorTransforms from "decorator-transforms";
-import colocatedBabelPlugin from "ember-cli-htmlbars/lib/colocated-babel-plugin";
-import { precompile } from "ember-source/dist/ember-template-compiler";
-import EmberThisFallback from "ember-this-fallback";
import { minify as terserMinify } from "terser";
-import { WidgetHbsCompiler } from "discourse-widget-hbs/lib/widget-hbs-compiler";
import { browsers } from "../discourse/config/targets";
-import { Preprocessor } from "./content-tag";
-import buildEmberTemplateManipulatorPlugin from "./theme-hbs-ast-transforms";
-
-const thisFallbackPlugin = EmberThisFallback._buildPlugin({
- enableLogging: false,
- isTheme: true,
-}).plugin;
-
-function buildTemplateCompilerBabelPlugins({ extension, themeId }) {
- const compiler = { precompile };
-
- if (themeId && extension !== "gjs") {
- compiler.precompile = (src, opts) => {
- return precompile(src, {
- ...opts,
- plugins: {
- ast: [
- buildEmberTemplateManipulatorPlugin(themeId),
- thisFallbackPlugin,
- ],
- },
- });
- };
- }
-
- return [
- colocatedBabelPlugin,
- WidgetHbsCompiler,
- [
- HTMLBarsInlinePrecompile,
- {
- compiler,
- enableLegacyModules: [
- "ember-cli-htmlbars",
- "ember-cli-htmlbars-inline-precompile",
- "htmlbars-inline-precompile",
- ],
- },
- ],
- ];
-}
globalThis.transpile = function (source, options = {}) {
- const { moduleId, filename, extension, skipModule, themeId, generateMap } =
- options;
-
- if (extension === "gjs") {
- const preprocessor = new Preprocessor();
- source = preprocessor.process(source).code;
- }
+ const { moduleId, filename, skipModule, generateMap } = options;
const plugins = [];
- plugins.push(...buildTemplateCompilerBabelPlugins({ extension, themeId }));
if (moduleId && !skipModule) {
plugins.push(["transform-modules-amd", { noInterop: true }]);
}
diff --git a/spec/lib/discourse_js_processor_spec.rb b/spec/lib/discourse_js_processor_spec.rb
index 5ddf02c7152..3d82e51606c 100644
--- a/spec/lib/discourse_js_processor_spec.rb
+++ b/spec/lib/discourse_js_processor_spec.rb
@@ -78,90 +78,6 @@ RSpec.describe DiscourseJsProcessor do
expect(result).to include("static #_ = (() => dt7948.n")
end
- it "correctly transpiles widget hbs" do
- result = DiscourseJsProcessor.transpile(<<~JS, "blah", "blah/mymodule")
- import hbs from "discourse/widgets/hbs-compiler";
- const template = hbs`{{somevalue}}`;
- JS
- expect(result).to eq <<~JS.strip
- define("blah/mymodule", [], function () {
- "use strict";
-
- const template = function (attrs, state) {
- var _r = [];
- _r.push(somevalue);
- return _r;
- };
- });
- JS
- end
-
- it "correctly transpiles ember hbs" do
- result = DiscourseJsProcessor.transpile(<<~JS, "blah", "blah/mymodule")
- import { hbs } from 'ember-cli-htmlbars';
- const template = hbs`{{somevalue}}`;
- JS
- expect(result).to eq <<~JS.strip
- define("blah/mymodule", ["@ember/template-factory"], function (_templateFactory) {
- "use strict";
-
- const template = (0, _templateFactory.createTemplateFactory)(
- /*
- {{somevalue}}
- */
- {
- "id": null,
- "block": "[[[1,[35,0]]],[],false,[\\"somevalue\\"]]",
- "moduleName": "/blah/mymodule",
- "isStrictMode": false
- });
- });
- JS
- end
-
- describe "Ember template transformations" do
- # For the Ember (Glimmer) templates, serverside rendering is not trivial,
- # so we compile the expected result with the standard compiler and compare to the theme compiler
- let(:theme_id) { 22 }
-
- def theme_compile(template)
- script = <<~JS
- import { hbs } from 'ember-cli-htmlbars';
- export default hbs(#{template.to_json});
- JS
- result = DiscourseJsProcessor.transpile(script, "", "theme/blah", theme_id: theme_id)
- result.gsub(%r{/\*(.*)\*/}m, "/* (js comment stripped) */")
- end
-
- def standard_compile(template)
- script = <<~JS
- import { hbs } from 'ember-cli-htmlbars';
- export default hbs(#{template.to_json});
- JS
- result = DiscourseJsProcessor.transpile(script, "", "theme/blah")
- result.gsub(%r{/\*(.*)\*/}m, "/* (js comment stripped) */")
- end
-
- it "adds the theme id to the helpers" do
- expect(theme_compile "{{theme-prefix 'translation_key'}}").to eq(
- standard_compile "{{theme-prefix #{theme_id} 'translation_key'}}"
- )
-
- expect(theme_compile "{{theme-i18n 'translation_key'}}").to eq(
- standard_compile "{{theme-i18n #{theme_id} 'translation_key'}}"
- )
-
- expect(theme_compile "{{theme-setting 'setting_key'}}").to eq(
- standard_compile "{{theme-setting #{theme_id} 'setting_key'}}"
- )
-
- # Works when used inside other statements
- expect(theme_compile "{{dummy-helper (theme-prefix 'translation_key')}}").to eq(
- standard_compile "{{dummy-helper (theme-prefix #{theme_id} 'translation_key')}}"
- )
- end
- end
-
describe "Transpiler#terser" do
it "can minify code and provide sourcemaps" do
sources = {