"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveImport = exports.rewriteImports = exports.moduleRewritePlugin = void 0; const path_1 = __importDefault(require("path")); const lru_cache_1 = __importDefault(require("lru-cache")); const magic_string_1 = __importDefault(require("magic-string")); const es_module_lexer_1 = require("es-module-lexer"); const resolver_1 = require("../resolver"); const serverPluginHmr_1 = require("./serverPluginHmr"); const serverPluginClient_1 = require("./serverPluginClient"); const utils_1 = require("../utils"); const chalk_1 = __importDefault(require("chalk")); const cssUtils_1 = require("../utils/cssUtils"); const serverPluginEnv_1 = require("./serverPluginEnv"); const fs_extra_1 = __importDefault(require("fs-extra")); const debug = require('debug')('vite:rewrite'); const rewriteCache = new lru_cache_1.default({ max: 1024 }); // Plugin for rewriting served js. // - Rewrites named module imports to `/@modules/:id` requests, e.g. // "vue" => "/@modules/vue" // - Rewrites files containing HMR code (reference to `import.meta.hot`) to // inject `import.meta.hot` and track HMR boundary accept whitelists. // - Also tracks importer/importee relationship graph during the rewrite. // The graph is used by the HMR plugin to perform analysis on file change. exports.moduleRewritePlugin = ({ root, app, watcher, resolver }) => { app.use(async (ctx, next) => { await next(); if (ctx.status === 304) { return; } // we are doing the js rewrite after all other middlewares have finished; // this allows us to post-process javascript produced by user middlewares // regardless of the extension of the original files. const publicPath = ctx.path; if (ctx.body && ctx.response.is('js') && !cssUtils_1.isCSSRequest(ctx.path) && !ctx.url.endsWith('.map') && !resolver.isPublicRequest(ctx.path) && // skip internal client publicPath !== serverPluginClient_1.clientPublicPath && // need to rewrite for