2024-01-18 13:20:10 +02:00
|
|
|
import { RenderResultPluginAsset } from './types';
|
|
|
|
|
|
|
|
interface Options {
|
|
|
|
asHtml: boolean;
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:05:23 +02:00
|
|
|
// Utility function to convert the plugin assets to a list of LINK or SCRIPT tags
|
|
|
|
// that can be included in the HEAD tag.
|
2024-01-18 13:20:10 +02:00
|
|
|
const assetsToHeaders = (pluginAssets: RenderResultPluginAsset[], options: Options|null = null) => {
|
2023-06-01 13:02:36 +02:00
|
|
|
options = { asHtml: false, ...options };
|
2020-02-08 13:11:04 +02:00
|
|
|
|
2024-01-18 13:20:10 +02:00
|
|
|
const headers: Record<string, string> = {};
|
2020-01-30 23:05:23 +02:00
|
|
|
for (let i = 0; i < pluginAssets.length; i++) {
|
|
|
|
const asset = pluginAssets[i];
|
|
|
|
if (asset.mime === 'text/css') {
|
2020-02-08 13:11:04 +02:00
|
|
|
headers[asset.name] = `<link rel="stylesheet" href="pluginAssets/${asset.name}">`;
|
2020-01-30 23:05:23 +02:00
|
|
|
} else if (asset.mime === 'application/javascript') {
|
|
|
|
// NOT TESTED!!
|
2020-02-08 13:11:04 +02:00
|
|
|
headers[asset.name] = `<script type="application/javascript" src="pluginAssets/${asset.name}"></script>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.asHtml) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = [];
|
|
|
|
for (const name in headers) {
|
2020-02-08 13:11:04 +02:00
|
|
|
output.push(headers[name]);
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|
2020-02-08 13:11:04 +02:00
|
|
|
return output.join('');
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-08 13:11:04 +02:00
|
|
|
return headers;
|
2024-01-18 13:20:10 +02:00
|
|
|
};
|
2020-01-30 23:05:23 +02:00
|
|
|
|
2024-01-18 13:20:10 +02:00
|
|
|
export default assetsToHeaders;
|