framework/js/CustomLoader.js
2021-04-28 15:57:42 -07:00

41 lines
1.1 KiB
JavaScript

// Hi early reviewer! I'm a temporary file and
// will be moved to the Flarum webpack config soon!
const loaderUtils = require('loader-utils');
// Custom loader logic
module.exports = function (source) {
// Get the type of the module to be exported
const location = loaderUtils.interpolateName(this, '[folder]/', {
context: this.rootContext || this.context,
});
// Get the name of module to be exported
const moduleName = loaderUtils.interpolateName(this, '[name]', {
context: this.rootContext || this.context,
});
// Don't export low level files
if (/.*\/(admin|forum)$/.test(location) || /(index|app|compat|FlarumRegistry)$/.test(moduleName)) {
return source;
}
let addition = "";
// Find the export names
const matches = [...source.matchAll(/export\s+?(?:default\s?|function|abstract\s?|class)+?\s([^(\s<;]*)/gm)];
matches.map(match => {
let name = match[1]
if (!name || name === 'interface') {
return;
}
// Add code at the end of the file to add the file to registry
addition += `\nwindow.flreg.add('${location}${name}', ${name})`
});
return source + addition;
}