Skip to content

Commit

Permalink
Don't prefix files with '/'
Browse files Browse the repository at this point in the history
Fix #4
  • Loading branch information
mentaljam committed Jul 30, 2020
1 parent adfb1aa commit e5e5387
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -198,7 +207,7 @@ const html2: RollupPluginHTML2 = ({
modules,
nomodule,
minify: minifyOptions,
onlinePath = '',
onlinePath,
...options
}) => ({
name: 'html2',
Expand Down Expand Up @@ -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<string>).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}"`))
}
}
})
Expand Down

0 comments on commit e5e5387

Please sign in to comment.