import { RollupPluginHtml } from '@web/rollup-plugin-html';
import { Plugin } from 'rollup';
import { GeneratedFile, injectPolyfillsLoader, File, fileTypes } from '@web/polyfills-loader';
import path from 'path';
import { RollupPluginPolyfillsLoaderConfig } from './types';
import { createError, shouldInjectLoader } from './utils';
import { createPolyfillsLoaderConfig, formatToFileType } from './createPolyfillsLoaderConfig';
export function polyfillsLoader(pluginOptions: RollupPluginPolyfillsLoaderConfig = {}): Plugin {
let generatedFiles: GeneratedFile[] | undefined;
return {
name: '@web/rollup-plugin-polyfills-loader',
buildStart(options) {
generatedFiles = undefined;
if (!options.plugins) {
throw createError('Could not find any installed plugins');
}
const htmlPlugins = options.plugins.filter(
p => p.name === '@web/rollup-plugin-html',
) as RollupPluginHtml[];
const [htmlPlugin] = htmlPlugins;
if (!htmlPlugin) {
throw createError(
'Could not find any instance of @web/rollup-plugin-html in rollup build.',
);
}
htmlPlugin.api.disableDefaultInject();
htmlPlugin.api.addHtmlTransformer(async (html, { bundle, bundles, htmlFileName }) => {
const config = createPolyfillsLoaderConfig(pluginOptions, bundle, bundles);
const relativePathToPolyfills = path.relative(
path.dirname(htmlFileName),
path.dirname(pluginOptions.polyfillsDir || './polyfills'),
);
let htmlString = html;
if (shouldInjectLoader(config)) {
const result = await injectPolyfillsLoader(html, { ...config, relativePathToPolyfills });
htmlString = result.htmlString;
generatedFiles = result.polyfillFiles;
} else {
// we don't need to inject a polyfills loader, so we just inject the scripts directly
const scripts = config
.modern!.files.map((f: File) => {
let attributes = '';
if (f.attributes && f.attributes.length > 0) {
attributes = ' ';
attributes += f.attributes
.map(attribute => `${attribute.name}="${attribute.value}"`)
.join(' ');
}
return `\n`;
})
.join('');
htmlString = htmlString.replace('