consul/ui/packages/consul-ui/app/helpers/require.js

46 lines
1.8 KiB
JavaScript
Raw Normal View History

import { helper } from '@ember/component/helper';
import { css } from '@lit/reactive-element';
import resolve from 'consul-ui/utils/path/resolve';
2022-03-15 12:54:56 +00:00
import panel from 'consul-ui/components/panel/index.css';
import distributionMeter from 'consul-ui/components/distribution-meter/index.css';
import distributionMeterMeter from 'consul-ui/components/distribution-meter/meter/index.css';
import distributionMeterMeterElement from 'consul-ui/components/distribution-meter/meter/element';
import visuallyHidden from 'consul-ui/styles/base/decoration/visually-hidden.css';
2022-03-15 12:54:56 +00:00
import baseKeyframes from 'consul-ui/styles/base/icons/base-keyframes.css';
import chevronDown from 'consul-ui/styles/base/icons/icons/chevron-down/index.css';
const fs = {
2022-03-15 12:54:56 +00:00
['/components/panel/index.css']: panel,
['/components/distribution-meter/index.css']: distributionMeter,
['/components/distribution-meter/meter/index.css']: distributionMeterMeter,
['/components/distribution-meter/meter/element']: distributionMeterMeterElement,
2022-03-15 12:54:56 +00:00
['/styles/base/decoration/visually-hidden.css']: visuallyHidden,
['/styles/base/icons/base-keyframes.css']: baseKeyframes,
['/styles/base/icons/icons/chevron-down/index.css']: chevronDown
};
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
export default helper(([path = ''], { from }) => {
const fullPath = resolve(from, path);
switch(true) {
case fullPath.endsWith('.css'):
return fs[fullPath](css)
default: {
if(container.has(fullPath)) {
return container.get(fullPath);
}
const module = fs[fullPath](HTMLElement);
container.set(fullPath, module);
return module;
}
}
});