UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

31 lines (30 loc) 1.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getViteDevScript = getViteDevScript; const utils_js_1 = require("../../utils.js"); const picocolors_1 = __importDefault(require("@brillout/picocolors")); const reachOutCTA = 'Create a new GitHub issue to discuss a solution.'; async function getViteDevScript(pageContext) { const globalContext = pageContext._globalContext; if (globalContext._isProduction) { return ''; } const { _viteDevServer: viteDevServer } = globalContext; const fakeHtmlBegin = '<html> <head>'; // White space to test whether user is using a minifier const fakeHtmlEnd = '</head><body></body></html>'; let fakeHtml = fakeHtmlBegin + fakeHtmlEnd; fakeHtml = await viteDevServer.transformIndexHtml('/', fakeHtml); (0, utils_js_1.assertUsage)(!fakeHtml.includes('vite-plugin-pwa'), `The HTML transformer of ${picocolors_1.default.cyan('vite-plugin-pwa')} cannot be applied, see workaround at https://github.com/vikejs/vike/issues/388#issuecomment-1199280084`); (0, utils_js_1.assertUsage)(!fakeHtml.startsWith(fakeHtmlBegin.replace(' ', '')), `Vite plugins that minify the HTML cannot be applied, see https://github.com/vikejs/vike/issues/224`); (0, utils_js_1.assertUsage)(fakeHtml.startsWith(fakeHtmlBegin) && fakeHtml.endsWith(fakeHtmlEnd), `You are using a Vite Plugin that transforms the HTML in a way that conflicts with Vike. ${reachOutCTA}`); const viteInjection = fakeHtml.slice(fakeHtmlBegin.length, -1 * fakeHtmlEnd.length); (0, utils_js_1.assert)(viteInjection.includes('script')); (0, utils_js_1.assertWarning)(!viteInjection.includes('import('), `Unexpected Vite injected HMR code. ${reachOutCTA}`, { onlyOnce: true, }); const viteDevScript = viteInjection; return viteDevScript; }