From 67b5fc10b775c8c4a4d51c229005eac12f713fb1 Mon Sep 17 00:00:00 2001 From: Walker Knapp Date: Mon, 20 Jan 2020 16:43:30 -0500 Subject: [PATCH] Added onlinePath argument --- README.md | 17 +++++++++++++++++ src/index.ts | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1eddcd1..97ce5d3 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,23 @@ minify: { }, ``` +### onlinePath + +```js +string +``` + +A path to append to file references injected. This is useful for putting files on a CDN after building. + +#### Example +```js +onlinePath: '//www.example.com/foo', +``` +Which will generate: +```html + +``` + ## License [MIT](LICENSE) © [Petr Tsymbarovich](mailto:petr@tsymbarovich.ru) diff --git a/src/index.ts b/src/index.ts index 718ba82..aa60d71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,6 +58,7 @@ interface IPluginOptions { preload?: string[] | Set modules?: boolean minify?: false | MinifyOptions + onlinePath?: string } const enum Cache { @@ -175,6 +176,7 @@ export default ({ preload, modules, minify: minifyOptions, + onlinePath: onlinePath, ...options }: IPluginOptions): Plugin => ({ name: 'html2', @@ -329,12 +331,12 @@ consider to use the esm format or switch off the option`) const {name, ext} = path.parse(fileName) const injectType = ext.slice(1) if (name in entries) { - injectCSSandJS('/' + fileName, injectType, inject) + injectCSSandJS((onlinePath === undefined ? '' : onlinePath) + '/' + fileName, 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="/${fileName}" as="${linkType}"`)) + head.appendChild(new HTMLElement('link', {}, `rel="preload" href="${(onlinePath === undefined ? '' : onlinePath)}/${fileName}" as="${linkType}"`)) } } })