2022-03-09 08:28:34 +00:00
|
|
|
import { helper } from '@ember/component/helper';
|
2022-07-04 10:22:14 +00:00
|
|
|
import require from 'require';
|
2022-03-09 08:28:34 +00:00
|
|
|
|
|
|
|
import { css } from '@lit/reactive-element';
|
|
|
|
import resolve from 'consul-ui/utils/path/resolve';
|
|
|
|
|
2022-03-15 12:54:56 +00:00
|
|
|
|
2022-07-04 10:22:14 +00:00
|
|
|
const appName = 'consul-ui';
|
2022-03-09 08:28:34 +00:00
|
|
|
|
|
|
|
const container = new Map();
|
|
|
|
|
|
|
|
// `css` already has a caching mechanism under the hood so rely on that, plus
|
|
|
|
// we get the advantage of laziness here, i.e. we only call css as and when we
|
|
|
|
// need to
|
2022-07-07 17:23:26 +00:00
|
|
|
export default helper(([path = ''], options) => {
|
|
|
|
let fullPath = resolve(`${appName}${options.from}`, path);
|
|
|
|
if(path.charAt(0) === '/') {
|
|
|
|
fullPath = `${appName}${fullPath}`;
|
|
|
|
}
|
2022-07-04 10:22:14 +00:00
|
|
|
|
|
|
|
let module;
|
|
|
|
if(require.has(fullPath)) {
|
2022-07-07 17:23:26 +00:00
|
|
|
module = require(fullPath)[options.export || 'default'];
|
2022-07-04 10:22:14 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unable to resolve '${fullPath}' does the file exist?`)
|
|
|
|
}
|
|
|
|
|
2022-03-09 08:28:34 +00:00
|
|
|
switch(true) {
|
|
|
|
case fullPath.endsWith('.css'):
|
2022-07-04 10:22:14 +00:00
|
|
|
return module(css);
|
|
|
|
case fullPath.endsWith('.xstate'):
|
|
|
|
return module;
|
2022-07-07 17:23:26 +00:00
|
|
|
case fullPath.endsWith('.element'): {
|
2022-03-09 08:28:34 +00:00
|
|
|
if(container.has(fullPath)) {
|
|
|
|
return container.get(fullPath);
|
|
|
|
}
|
2022-07-04 10:22:14 +00:00
|
|
|
const component = module(HTMLElement);
|
|
|
|
container.set(fullPath, component);
|
|
|
|
return component;
|
2022-03-09 08:28:34 +00:00
|
|
|
}
|
2022-07-07 17:23:26 +00:00
|
|
|
default:
|
|
|
|
return module;
|
2022-03-09 08:28:34 +00:00
|
|
|
}
|
|
|
|
});
|