UNPKG

vike

Version:

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

48 lines (47 loc) 2.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pluginModuleBanner = pluginModuleBanner; const magic_string_1 = __importDefault(require("magic-string")); const virtualFiles_js_1 = require("../../../shared/virtualFiles.js"); const isViteServerBuild_js_1 = require("../../shared/isViteServerBuild.js"); // Rollup's banner feature doesn't work with Vite: https://github.com/vitejs/vite/issues/8412 // But, anyways, we want to prepend the banner at the beginning of each module, not at the beginning of each file (I believe that's what Rollup's banner feature does). function pluginModuleBanner() { let config; return { name: 'vike:build:pluginModuleBanner', enforce: 'post', apply: 'build', configResolved(config_) { config = config_; }, transform: { order: 'post', handler(code, id, options) { if (!(0, isViteServerBuild_js_1.isViteServerBuild_safe)(config, options) && // Inject module banners if user sets `build.minify` to `false` for inspecting dist/client/ config.build.minify) { return; } if (id.startsWith('\0')) id = id; id = (0, virtualFiles_js_1.removeVirtualIdTag)(id); if (id.startsWith(config.root)) id = id.slice(config.root.length + 1); id = id.replaceAll('*/', '*\\/'); // https://github.com/vikejs/vike/issues/2377 const magicString = new magic_string_1.default(code); // Use legal comment so that esbuild doesn't remove it. // - Terser still removes the comment, but I guess users use terser to minify JavaScript so I guess it's a good thing that comment is removed. // - https://esbuild.github.io/api/#legal-comments magicString.prepend(`/*! ${id} [vike:pluginModuleBanner] */\n`); return { code: magicString.toString(), map: magicString.generateMap({ hires: true, source: id }), }; }, }, }; }