This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/rfc176-shims/index.js
David Taylor 569fa8a135
DEV: Improve Ember module shims under Ember CLI (#15795) (#15806)
In our legacy environment, Ember RFC176 shims are included in `discourse-loader.js` which is part of the `vendor.js` bundle. This meant that the module shims were available as soon as the vendor.js asset was loaded.

Under Ember CLI, we were defining these shims in `discourse-boot.js`. This is loaded by the browser much later, and meant that the shims were not available to themes/plugins that call `require()` before Discourse has booted. This was causing errors under some circumstances.

This commit refactors the Ember CLI implementation so that the shims are included in the vendor.js bundle. This is done via an addon which leans on the ember-rfc176-data NPM package. This will ensure we have all the definitions, without the need for manual copy/paste.
2022-02-03 17:36:32 +00:00

58 lines
1.3 KiB
JavaScript

"use strict";
// In core, babel-plugin-ember-modules-api-polyfill takes care of re-writing the new module
// syntax to the legacy Ember globals. For themes and plugins, we need to manually set up
// the modules.
//
// Eventually, Ember RFC176 will be implemented, and we can drop these shims.
const RFC176Data = require("ember-rfc176-data");
module.exports = {
name: require("./package").name,
isDevelopingAddon() {
return true;
},
contentFor: function (type) {
if (type !== "vendor-suffix") {
return;
}
const modules = {};
for (const entry of RFC176Data) {
// Entries look like:
// {
// global: 'Ember.expandProperties',
// module: '@ember/object/computed',
// export: 'expandProperties',
// deprecated: false
// },
if (entry.deprecated) {
continue;
}
let m = modules[entry.module];
if (!m) {
m = modules[entry.module] = [];
}
m.push(entry);
}
let output = "";
for (const moduleName of Object.keys(modules)) {
const exports = modules[moduleName];
const rawExports = exports
.map((e) => `${e.export}:${e.global}`)
.join(",");
output += `define("${moduleName}", () => {return {${rawExports}}});\n`;
}
return output;
},
};