DEV: Support inline-hbs compilation in themes (#18112)

This commit makes a number of improvements to the DiscourseJsProcessor:

1. Remove dependence on the out-of-date Ember template compiler from the ember-rails gem; switch to modern template compiler
2. Refactor to make use of a proper module system with `define`/`require`
3. Introduce `babel-plugin-ember-template-compilation` to enable inline hbs compilation

The `mini-loader` is upgraded to support relative lookup and `require.has`, so that these new JS packages work correctly.
This commit is contained in:
David Taylor
2022-08-29 19:53:42 +01:00
committed by GitHub
parent 1bd1664ae0
commit e16c8ea2e7
6 changed files with 147 additions and 27 deletions

View File

@ -35,4 +35,47 @@ RSpec.describe DiscourseJsProcessor do
expect(DiscourseJsProcessor.skip_module?("// just some JS\nconsole.log()")).to eq(false)
end
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,[34,0]]],[],false,[\\"somevalue\\"]]",
"moduleName": "(unknown template module)",
"isStrictMode": false
});
});
JS
end
end