From fd0e5e0602a1d661fb6b08a4bf40112c1963cace Mon Sep 17 00:00:00 2001 From: Konstantin Shutkin Date: Sun, 18 Jun 2023 16:04:29 +0200 Subject: [PATCH] fix: file path for asset factories --- .changeset/green-bags-brush.md | 5 +++++ plugin-html/src/index.ts | 15 +++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 .changeset/green-bags-brush.md diff --git a/.changeset/green-bags-brush.md b/.changeset/green-bags-brush.md new file mode 100644 index 0000000..241917b --- /dev/null +++ b/.changeset/green-bags-brush.md @@ -0,0 +1,5 @@ +--- +"@rollup-extras/plugin-html": patch +--- + +fixed correct path to file when using multiple dist folders and asset factories diff --git a/plugin-html/src/index.ts b/plugin-html/src/index.ts index 0d77fd8..53724d6 100644 --- a/plugin-html/src/index.ts +++ b/plugin-html/src/index.ts @@ -83,7 +83,7 @@ export default function(options: HtmlPluginOptions = {}) { instance.renderStart = function (this: PluginContext, outputOptions: NormalizedOutputOptions, inputOptions: NormalizedInputOptions) { logger('started collecting information', LogLevel.verbose); - initialDir = outputOptions.dir || ''; + initialDir = outputOptions.dir ?? ''; fileNameInInitialDir = path.join(initialDir, outputFile); return (baseRenderStart as (this: PluginContext, outputOptions: NormalizedOutputOptions, inputOptions: NormalizedInputOptions) => void | Promise).call(this, outputOptions, inputOptions); }; @@ -119,7 +119,7 @@ export default function(options: HtmlPluginOptions = {}) { .map(({ key, count }) => `${key}: ${count}`) .join(', '); logger(`assets collected: [${statistics}], remaining: ${remainingOutputsCount} outputs, ${remainingConfigsCount} configs`, LogLevel.verbose); - const dir = options.dir || '', fileName = path.relative(dir, fileNameInInitialDir); + const dir = options.dir ?? '', fileName = path.relative(dir, fileNameInInitialDir); if (fileName in bundle) { if (useEmittedTemplate) { logger(`using exiting emitted ${fileName} as an input for out templateFactory`, LogLevel.verbose); @@ -170,7 +170,7 @@ export default function(options: HtmlPluginOptions = {}) { async function generateBundle(this: PluginContext, options: NormalizedOutputOptions) { logger.start('generating html', logLevel); try { - const dir = options.dir || '', fileName = path.relative(dir, fileNameInInitialDir); + const dir = options.dir ?? '', fileName = path.relative(dir, fileNameInInitialDir); const depromisifiedTemplateString = await templateString, source = await templateFactory(depromisifiedTemplateString, assets, defaultTemplateFactory); @@ -197,19 +197,19 @@ export default function(options: HtmlPluginOptions = {}) { } async function getAssets(options: NormalizedOutputOptions, bundle: OutputBundle) { - const dir = options.dir || ''; + const dir = options.dir ?? ''; for (const fileName of Object.keys(bundle)) { const relativeToRootAssetPath = path.join(dir, fileName); + const assetPath = path.relative(initialDir, relativeToRootAssetPath); if (ignore(relativeToRootAssetPath) || processedFiles.has(relativeToRootAssetPath)) { continue; } processedFiles.add(relativeToRootAssetPath); if ((bundle[fileName] as OutputAsset | OutputChunk).type === 'asset') { - if (await useAssetFactory(fileName, relativeToRootAssetPath, (bundle[fileName] as OutputAsset).source, 'asset')) { + if (await useAssetFactory(assetPath, relativeToRootAssetPath, (bundle[fileName] as OutputAsset).source, 'asset')) { continue; } if (fileName.endsWith(cssExtention)) { - const assetPath = path.relative(initialDir, relativeToRootAssetPath); (assets.asset as AssetDescriptor[]).push({ html: getLinkElement(assetPath), head: injectIntoHead(relativeToRootAssetPath), @@ -220,11 +220,10 @@ export default function(options: HtmlPluginOptions = {}) { } else if ((bundle[fileName] as OutputAsset | OutputChunk).type === 'chunk') { const chunk = bundle[fileName] as OutputChunk; if (chunk.isEntry) { - if (await useAssetFactory(fileName, relativeToRootAssetPath, chunk.code, options.format)) { + if (await useAssetFactory(assetPath, relativeToRootAssetPath, chunk.code, options.format)) { continue; } if (options.format === 'es' || options.format === 'iife' || options.format === 'umd') { - const assetPath = path.relative(initialDir, relativeToRootAssetPath); (assets[options.format] as AssetDescriptor[]).push({ html: (assets: Assets) => { let useConditionalLoading = conditionalLoading;