DEV: Replace mini-loader with Ember's `loader.js` library (#18643)
We already have this as a dependency, so it makes sense to use it rather than using our own not-quite-spec-compliant implementation
This commit is contained in:
parent
43c2841a69
commit
f7fdaa97d1
|
@ -1,233 +0,0 @@
|
|||
// Used by pretty-text in MiniRacer context
|
||||
|
||||
let define, requirejs;
|
||||
|
||||
(function () {
|
||||
let JS_MODULES = {};
|
||||
|
||||
let registry = {};
|
||||
let seen = {};
|
||||
let FAILED = false;
|
||||
|
||||
let uuid = 0;
|
||||
|
||||
function tryFinally(tryable, finalizer) {
|
||||
try {
|
||||
return tryable();
|
||||
} finally {
|
||||
finalizer();
|
||||
}
|
||||
}
|
||||
|
||||
function unsupportedModule(length) {
|
||||
throw new Error(
|
||||
"an unsupported module was defined, expected `define(name, deps, module)` instead got: `" +
|
||||
length +
|
||||
"` arguments to define`"
|
||||
);
|
||||
}
|
||||
|
||||
let defaultDeps = ["require", "exports", "module"];
|
||||
|
||||
function Module(name, deps, callback, exports) {
|
||||
this.id = uuid++;
|
||||
this.name = name;
|
||||
this.deps = !deps.length && callback.length ? defaultDeps : deps;
|
||||
this.exports = exports || {};
|
||||
this.callback = callback;
|
||||
this.state = undefined;
|
||||
this._require = undefined;
|
||||
}
|
||||
|
||||
Module.prototype.makeRequire = function () {
|
||||
if (this._require) {
|
||||
return this._require;
|
||||
}
|
||||
this._require = (dep) => {
|
||||
return requirejs(resolve(dep, this.name));
|
||||
};
|
||||
this._require.has = (dep) => {
|
||||
const moduleName = resolve(dep, this.name);
|
||||
return require.has(moduleName);
|
||||
};
|
||||
return this._require;
|
||||
};
|
||||
|
||||
define = function (name, deps, callback) {
|
||||
if (arguments.length < 2) {
|
||||
unsupportedModule(arguments.length);
|
||||
}
|
||||
|
||||
if (!Array.isArray(deps)) {
|
||||
callback = deps;
|
||||
deps = [];
|
||||
}
|
||||
|
||||
registry[name] = new Module(name, deps, callback);
|
||||
};
|
||||
|
||||
// we don't support all of AMD
|
||||
// define.amd = {};
|
||||
// we will support petals...
|
||||
define.petal = {};
|
||||
|
||||
function Alias(path) {
|
||||
this.name = path;
|
||||
}
|
||||
|
||||
define.alias = function (path) {
|
||||
return new Alias(path);
|
||||
};
|
||||
|
||||
function reify(mod, name, rseen) {
|
||||
let deps = mod.deps;
|
||||
let length = deps.length;
|
||||
let reified = new Array(length);
|
||||
let dep;
|
||||
// TODO: new Module
|
||||
// TODO: seen refactor
|
||||
let module = {};
|
||||
|
||||
for (let i = 0, l = length; i < l; i++) {
|
||||
dep = deps[i];
|
||||
if (dep === "exports") {
|
||||
module.exports = reified[i] = rseen;
|
||||
} else if (dep === "require") {
|
||||
reified[i] = mod.makeRequire();
|
||||
} else if (dep === "module") {
|
||||
mod.exports = rseen;
|
||||
module = reified[i] = mod;
|
||||
} else {
|
||||
reified[i] = requireFrom(resolve(dep, name), name);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
deps: reified,
|
||||
module,
|
||||
};
|
||||
}
|
||||
|
||||
function requireFrom(name, origin) {
|
||||
let mod = JS_MODULES[name] || registry[name];
|
||||
|
||||
if (!mod) {
|
||||
name = name + "/index";
|
||||
mod = registry[name];
|
||||
}
|
||||
|
||||
if (!mod) {
|
||||
throw new Error(
|
||||
"Could not find module `" + name + "` imported from `" + origin + "`"
|
||||
);
|
||||
}
|
||||
return requirejs(name);
|
||||
}
|
||||
|
||||
function missingModule(name) {
|
||||
throw new Error("Could not find module " + name);
|
||||
}
|
||||
|
||||
requirejs = require = function (name) {
|
||||
if (JS_MODULES[name]) {
|
||||
return JS_MODULES[name];
|
||||
}
|
||||
|
||||
let mod = registry[name];
|
||||
|
||||
if (mod && mod.callback instanceof Alias) {
|
||||
mod = registry[mod.callback.name];
|
||||
}
|
||||
|
||||
if (!mod) {
|
||||
name = name + "/index";
|
||||
mod = registry[name];
|
||||
}
|
||||
|
||||
if (!mod) {
|
||||
missingModule(name);
|
||||
}
|
||||
|
||||
if (mod.state !== FAILED && seen.hasOwnProperty(name)) {
|
||||
return seen[name];
|
||||
}
|
||||
|
||||
let reified;
|
||||
let module;
|
||||
let loaded = false;
|
||||
|
||||
seen[name] = {}; // placeholder for run-time cycles
|
||||
|
||||
tryFinally(
|
||||
function () {
|
||||
reified = reify(mod, name, seen[name]);
|
||||
module = mod.callback.apply(this, reified.deps);
|
||||
loaded = true;
|
||||
},
|
||||
function () {
|
||||
if (!loaded) {
|
||||
mod.state = FAILED;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let obj;
|
||||
if (module === undefined && reified.module.exports) {
|
||||
obj = reified.module.exports;
|
||||
} else {
|
||||
obj = seen[name] = module;
|
||||
}
|
||||
|
||||
if (
|
||||
obj !== null &&
|
||||
(typeof obj === "object" || typeof obj === "function") &&
|
||||
obj["default"] === undefined
|
||||
) {
|
||||
obj["default"] = obj;
|
||||
}
|
||||
|
||||
return (seen[name] = obj);
|
||||
};
|
||||
window.requireModule = requirejs;
|
||||
|
||||
function resolve(child, name) {
|
||||
if (child.charAt(0) !== ".") {
|
||||
return child;
|
||||
}
|
||||
|
||||
let parts = child.split("/");
|
||||
let nameParts = name.split("/");
|
||||
let parentBase = nameParts.slice(0, -1);
|
||||
|
||||
for (let i = 0, l = parts.length; i < l; i++) {
|
||||
let part = parts[i];
|
||||
|
||||
if (part === "..") {
|
||||
if (parentBase.length === 0) {
|
||||
throw new Error("Cannot access parent module of root");
|
||||
}
|
||||
parentBase.pop();
|
||||
} else if (part === ".") {
|
||||
continue;
|
||||
} else {
|
||||
parentBase.push(part);
|
||||
}
|
||||
}
|
||||
|
||||
return parentBase.join("/");
|
||||
}
|
||||
|
||||
requirejs.entries = requirejs._eak_seen = registry;
|
||||
requirejs.clear = function () {
|
||||
requirejs.entries = requirejs._eak_seen = registry = {};
|
||||
seen = {};
|
||||
};
|
||||
require.has = function (moduleName) {
|
||||
return (
|
||||
Boolean(registry[moduleName]) || Boolean(registry[moduleName + "/index"])
|
||||
);
|
||||
};
|
||||
|
||||
globalThis.define = define;
|
||||
globalThis.require = require;
|
||||
})();
|
|
@ -142,7 +142,7 @@ class DiscourseJsProcessor
|
|||
JS
|
||||
|
||||
# define/require support
|
||||
load_file_in_context(ctx, "mini-loader.js")
|
||||
load_file_in_context(ctx, "node_modules/loader.js/dist/loader/loader.js")
|
||||
|
||||
# Babel
|
||||
load_file_in_context(ctx, "node_modules/@babel/standalone/babel.js")
|
||||
|
|
|
@ -10,7 +10,7 @@ class Barber::Precompiler
|
|||
|
||||
def precompiler
|
||||
if !@precompiler
|
||||
loader = File.read("#{Rails.root}/app/assets/javascripts/mini-loader.js")
|
||||
loader = File.read("#{Rails.root}/app/assets/javascripts/node_modules/loader.js/dist/loader/loader.js")
|
||||
source = File.read("#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js")
|
||||
|
||||
transpiled = DiscourseJsProcessor.transpile(source, "#{Rails.root}/app/assets/javascripts/", "discourse-common/lib/raw-handlebars")
|
||||
|
|
|
@ -94,7 +94,7 @@ module PrettyText
|
|||
end
|
||||
|
||||
root_path = "#{Rails.root}/app/assets/javascripts/"
|
||||
ctx_load(ctx, "#{root_path}/mini-loader.js")
|
||||
ctx_load(ctx, "#{root_path}/node_modules/loader.js/dist/loader/loader.js")
|
||||
ctx_load(ctx, "#{root_path}/handlebars-shim.js")
|
||||
ctx_load(ctx, "#{root_path}/node_modules/xss/dist/xss.js")
|
||||
ctx.load("#{Rails.root}/lib/pretty_text/vendor-shims.js")
|
||||
|
|
Loading…
Reference in New Issue