From 0258e4f466f6353d7788ee184044081d55a3cf0d Mon Sep 17 00:00:00 2001 From: Daniel Shields Date: Thu, 24 Feb 2022 22:00:58 +0000 Subject: [PATCH] fix: asset paths were borked --- index.ts | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/index.ts b/index.ts index e3970f1..9914842 100644 --- a/index.ts +++ b/index.ts @@ -2,13 +2,19 @@ import path from 'path' import fs from 'fs' import cheerio from 'cheerio' import type { InputOptions } from 'rollup' -import type { ResolvedConfig, UserConfig, Manifest } from 'vite' +import type { ResolvedConfig, UserConfig, Manifest, Plugin } from 'vite' + +export default function viteNodeCGPlugin(): Plugin { + const bundleName = path.basename(process.cwd()) + + let graphicTemplate = fs.readFileSync(path.join(process.cwd(), 'src/graphics/template.html')) + let dashboardTemplate = fs.readFileSync(path.join(process.cwd(), 'src/dashboard/template.html')) -export default function viteNodeCGPlugin() { let config: ResolvedConfig - let graphicTemplate: Buffer - let dashboardTemplate: Buffer let assetManifest: Manifest + let protocol: string + let socketAddr: string + let inputOptions: InputOptions function injectAssets(html: string | Buffer, entry: string) { @@ -17,18 +23,18 @@ export default function viteNodeCGPlugin() { const assets = [] if (config.mode === 'development') { - assets.push('') - assets.push(``) + assets.push(``) + assets.push(``) } else if (config.mode === 'production' && assetManifest) { let entryManifest = assetManifest[entry] if (entryManifest.css) { entryManifest.css.forEach(function (cssAsset) { - assets.push(``) - }); + assets.push(``) + }) } - assets.push(``) + assets.push(``) } $('head').append(assets.join('\n')) @@ -74,29 +80,30 @@ export default function viteNodeCGPlugin() { return { name: 'nodecg', - config: (): UserConfig => { - const bundleName = path.basename(process.cwd()) + config: (_config, {mode}): UserConfig => { + protocol = _config?.server?.https ? 'https' : 'http' + socketAddr = `${typeof _config?.server?.host === 'string' ? _config?.server?.host : 'localhost'}:${_config?.server?.port?.toString() ?? '3000'}` return { build: { - manifest: true + manifest: true, + outDir: 'shared/dist' }, - base: `/bundles/${bundleName}/` + server: { + origin: `${protocol}://${socketAddr}` + }, + base: `/bundles/${bundleName}/${mode === 'development' ? '' : 'shared/dist/'}` } }, configResolved(resolvedConfig: ResolvedConfig) { config = resolvedConfig - - graphicTemplate = fs.readFileSync(path.join(process.cwd(), 'src/graphics/template.html')) - dashboardTemplate = fs.readFileSync(path.join(process.cwd(), 'src/dashboard/template.html')) }, buildStart(options: InputOptions) { - // dev inject - inputOptions = options - + + // dev inject if (!inputOptions?.input || config.mode !== 'development') return generateHTMLFiles()