2020-03-11 09:43:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "discourse_js_processor"
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe DiscourseJsProcessor do
|
2020-03-11 09:43:55 -04:00
|
|
|
describe "should_transpile?" do
|
|
|
|
it "returns false for empty strings" do
|
|
|
|
expect(DiscourseJsProcessor.should_transpile?(nil)).to eq(false)
|
|
|
|
expect(DiscourseJsProcessor.should_transpile?("")).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for a regular js file" do
|
|
|
|
expect(DiscourseJsProcessor.should_transpile?("file.js")).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for deprecated .es6 files" do
|
|
|
|
expect(DiscourseJsProcessor.should_transpile?("file.es6")).to eq(true)
|
|
|
|
expect(DiscourseJsProcessor.should_transpile?("file.js.es6")).to eq(true)
|
|
|
|
expect(DiscourseJsProcessor.should_transpile?("file.js.es6.erb")).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "skip_module?" do
|
|
|
|
it "returns false for empty strings" do
|
|
|
|
expect(DiscourseJsProcessor.skip_module?(nil)).to eq(false)
|
|
|
|
expect(DiscourseJsProcessor.skip_module?("")).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if the header is present" do
|
|
|
|
expect(DiscourseJsProcessor.skip_module?("// cool comment\n// discourse-skip-module")).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if the header is not present" do
|
|
|
|
expect(DiscourseJsProcessor.skip_module?("// just some JS\nconsole.log()")).to eq(false)
|
|
|
|
end
|
2023-04-24 12:39:02 -04:00
|
|
|
|
|
|
|
it "works end-to-end" do
|
|
|
|
source = <<~JS.chomp
|
|
|
|
// discourse-skip-module
|
|
|
|
console.log("hello world");
|
|
|
|
JS
|
|
|
|
expect(DiscourseJsProcessor.transpile(source, "test", "test")).to eq(source)
|
|
|
|
end
|
2020-03-11 09:43:55 -04:00
|
|
|
end
|
2022-08-29 14:53:42 -04:00
|
|
|
|
2023-01-16 12:28:59 -05:00
|
|
|
it "passes through modern JS syntaxes which are supported in our target browsers" do
|
|
|
|
script = <<~JS.chomp
|
|
|
|
optional?.chaining;
|
|
|
|
const template = func`test`;
|
|
|
|
let numericSeparator = 100_000_000;
|
|
|
|
logicalAssignment ||= 2;
|
|
|
|
nullishCoalescing ?? 'works';
|
|
|
|
try {
|
|
|
|
"optional catch binding";
|
|
|
|
} catch {
|
|
|
|
"works";
|
|
|
|
}
|
|
|
|
async function* asyncGeneratorFunction() {
|
|
|
|
yield await Promise.resolve('a');
|
|
|
|
}
|
|
|
|
let a = {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
...spreadRest
|
|
|
|
};
|
|
|
|
JS
|
|
|
|
|
|
|
|
result = DiscourseJsProcessor.transpile(script, "blah", "blah/mymodule")
|
|
|
|
expect(result).to eq <<~JS.strip
|
|
|
|
define("blah/mymodule", [], function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
#{script.indent(2)}
|
|
|
|
});
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
2023-01-16 13:06:46 -05:00
|
|
|
it "supports decorators and class properties without error" do
|
|
|
|
script = <<~JS.chomp
|
|
|
|
class MyClass {
|
|
|
|
classProperty = 1;
|
|
|
|
#privateProperty = 1;
|
|
|
|
#privateMethod() {
|
|
|
|
console.log("hello world");
|
|
|
|
}
|
|
|
|
@decorated
|
|
|
|
myMethod(){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS
|
|
|
|
|
|
|
|
result = DiscourseJsProcessor.transpile(script, "blah", "blah/mymodule")
|
2024-06-10 10:51:48 -04:00
|
|
|
expect(result).to include("static #_ = (() => dt7948.n")
|
2023-01-16 13:06:46 -05:00
|
|
|
end
|
|
|
|
|
2022-08-29 14:53:42 -04:00
|
|
|
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\\"]]",
|
2023-02-27 12:43:28 -05:00
|
|
|
"moduleName": "/blah/mymodule",
|
2022-08-29 14:53:42 -04:00
|
|
|
"isStrictMode": false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
JS
|
|
|
|
end
|
2022-09-01 06:50:46 -04:00
|
|
|
|
|
|
|
describe "Raw template theme transformations" do
|
|
|
|
# For the raw templates, we can easily render them serverside, so let's do that
|
|
|
|
|
|
|
|
let(:compiler) { DiscourseJsProcessor::Transpiler.new }
|
|
|
|
let(:theme_id) { 22 }
|
|
|
|
|
|
|
|
let(:helpers) { <<~JS }
|
|
|
|
Handlebars.registerHelper('theme-prefix', function(themeId, string) {
|
|
|
|
return `theme_translations.${themeId}.${string}`
|
|
|
|
})
|
|
|
|
Handlebars.registerHelper('theme-i18n', function(themeId, string) {
|
|
|
|
return `translated(theme_translations.${themeId}.${string})`
|
|
|
|
})
|
|
|
|
Handlebars.registerHelper('theme-setting', function(themeId, string) {
|
|
|
|
return `setting(${themeId}:${string})`
|
|
|
|
})
|
|
|
|
Handlebars.registerHelper('dummy-helper', function(string) {
|
|
|
|
return `dummy(${string})`
|
|
|
|
})
|
2023-08-24 10:36:22 -04:00
|
|
|
JS
|
2022-09-01 06:50:46 -04:00
|
|
|
|
|
|
|
let(:mini_racer) do
|
|
|
|
ctx = MiniRacer::Context.new
|
2024-09-03 05:51:07 -04:00
|
|
|
ctx.eval(
|
|
|
|
File.open(
|
|
|
|
"#{Rails.root}/app/assets/javascripts/discourse/node_modules/handlebars/dist/handlebars.js",
|
|
|
|
).read,
|
|
|
|
)
|
2022-09-01 06:50:46 -04:00
|
|
|
ctx.eval(helpers)
|
|
|
|
ctx
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2022-09-01 06:50:46 -04:00
|
|
|
|
|
|
|
def render(template)
|
|
|
|
compiled = compiler.compile_raw_template(template, theme_id: theme_id)
|
|
|
|
mini_racer.eval "Handlebars.template(#{compiled.squish})({})"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the theme id to the helpers" do
|
|
|
|
# Works normally
|
|
|
|
expect(render("{{theme-prefix 'translation_key'}}")).to eq(
|
|
|
|
"theme_translations.22.translation_key",
|
|
|
|
)
|
|
|
|
expect(render("{{theme-i18n 'translation_key'}}")).to eq(
|
|
|
|
"translated(theme_translations.22.translation_key)",
|
|
|
|
)
|
|
|
|
expect(render("{{theme-setting 'setting_key'}}")).to eq("setting(22:setting_key)")
|
|
|
|
|
|
|
|
# Works when used inside other statements
|
|
|
|
expect(render("{{dummy-helper (theme-prefix 'translation_key')}}")).to eq(
|
|
|
|
"dummy(theme_translations.22.translation_key)",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't duplicate number parameter inside {{each}}" do
|
|
|
|
expect(
|
|
|
|
compiler.compile_raw_template(
|
|
|
|
"{{#each item as |test test2|}}{{theme-setting 'setting_key'}}{{/each}}",
|
|
|
|
theme_id: theme_id,
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2022-09-01 06:50:46 -04:00
|
|
|
).to include(
|
|
|
|
'{"name":"theme-setting","hash":{},"hashTypes":{},"hashContexts":{},"types":["NumberLiteral","StringLiteral"]',
|
|
|
|
)
|
|
|
|
# Fail would be if theme-setting is defined with types:["NumberLiteral","NumberLiteral","StringLiteral"]
|
|
|
|
end
|
|
|
|
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
|
2022-10-18 13:20:10 -04:00
|
|
|
|
|
|
|
describe "Transpiler#terser" do
|
|
|
|
it "can minify code and provide sourcemaps" do
|
|
|
|
sources = {
|
|
|
|
"multiply.js" => "let multiply = (firstValue, secondValue) => firstValue * secondValue;",
|
|
|
|
"add.js" => "let add = (firstValue, secondValue) => firstValue + secondValue;",
|
|
|
|
}
|
|
|
|
|
|
|
|
result =
|
|
|
|
DiscourseJsProcessor::Transpiler.new.terser(
|
|
|
|
sources,
|
|
|
|
{ sourceMap: { includeSources: true } },
|
|
|
|
)
|
2022-11-24 11:15:46 -05:00
|
|
|
expect(result.keys).to contain_exactly("code", "decoded_map", "map")
|
2022-10-18 13:20:10 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
# Check the code still works
|
|
|
|
ctx = MiniRacer::Context.new
|
|
|
|
ctx.eval(result["code"])
|
|
|
|
expect(ctx.eval("multiply(2, 3)")).to eq(6)
|
|
|
|
expect(ctx.eval("add(2, 3)")).to eq(5)
|
|
|
|
ensure
|
|
|
|
ctx.dispose
|
|
|
|
end
|
|
|
|
|
|
|
|
map = JSON.parse(result["map"])
|
|
|
|
expect(map["sources"]).to contain_exactly(*sources.keys)
|
|
|
|
expect(map["sourcesContent"]).to contain_exactly(*sources.values)
|
|
|
|
end
|
|
|
|
end
|
2020-03-11 09:43:55 -04:00
|
|
|
end
|