From e5e538779825f02154e389be9ab02c9241b4efff Mon Sep 17 00:00:00 2001 From: Petr Tsymbarovich Date: Thu, 30 Jul 2020 14:41:21 +0300 Subject: [PATCH] Don't prefix files with '/' Fix #4 --- src/index.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 34b22cc..30d8e55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,6 +51,15 @@ const normalizePreload = ( return preload } +const normalizePrefix = ( + prefix = '', +) => { + if (prefix && !prefix.endsWith('/')) { + prefix += '/' + } + return prefix +} + const extensionToType = ( ext: InjectType | string, ): string | null => { @@ -198,7 +207,7 @@ const html2: RollupPluginHTML2 = ({ modules, nomodule, minify: minifyOptions, - onlinePath = '', + onlinePath, ...options }) => ({ name: 'html2', @@ -351,16 +360,18 @@ const html2: RollupPluginHTML2 = ({ } as IReducesBundle) // Now process all files and inject only entries and preload files preload = normalizePreload(preload) + const prefix = normalizePrefix(onlinePath) files.forEach(({fileName}) => { const {name, ext} = path.parse(fileName) - const injectType = ext.slice(1) + const injectType = ext.slice(1) + const filePath = prefix + fileName if (name in entries) { - injectCSSandJS(`${onlinePath}/${fileName}`, injectType, inject) + injectCSSandJS(filePath, injectType, inject) } else if (name in dynamicEntries && (preload as Set).has(dynamicEntries[name])) { const linkType = extensionToType(injectType) if (linkType) { addNewLine(head) - head.appendChild(new HTMLElement('link', {}, `rel="preload" href="${onlinePath}/${fileName}" as="${linkType}"`)) + head.appendChild(new HTMLElement('link', {}, `rel="preload" href="${filePath}" as="${linkType}"`)) } } })