"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViteDriver = void 0;
const path_1 = require("path");
const Base_1 = require("./Base");
/**
 * Resolves entry points and assets path for Vite. Relies
 * on the "manifest.json" and "entrypoints.json" files.
 *
 **********************************************************************
 * The driver assumes following format for the manifest.json file
 **********************************************************************
 *
 * ```json
 *  {
 *    "assetName": {
 *      "file": "path",
 *      "src": "path"
 *    },
 *    ...
 *  }
 * ```
 **********************************************************************
 * The driver assumes following format for the entrypoints.json file
 ***********************************************************************
 *
 * ```json
 *  {
 *    "url": "url"
 *    "entrypoints": {
 *      "entryPointName": {
 *         "js": ["url", "url"],
 *         "css": ["url", "url"],
 *      }
 *    }
 *  }
 * ```
 *
 * Please read the documentation for understanding the format of the files.
 */
class ViteDriver extends Base_1.BaseDriver {
    constructor() {
        super(...arguments);
        this.name = 'vite';
        /**
         * Vite driver has support for entrypoints
         */
        this.hasEntrypoints = true;
        /**
         * Attributes to apply to the script tag. Vite needs to serve
         * source over native ESM
         */
        this.scriptAttributes = { type: 'module' };
    }
    /**
     * If we should use the manifest. We only use the manifest in production.
     */
    shouldUseManifest() {
        return this.application.inProduction;
    }
    /**
     * Get the assets url from the entrypoints.json file
     */
    getAssetUrl() {
        return this.readFileAsJSON((0, path_1.join)(this.publicPath, 'entrypoints.json')).url;
    }
    /**
     * Returns path to a given asset file
     */
    assetPath(filename) {
        if (!this.shouldUseManifest()) {
            return `${this.getAssetUrl()}/${filename}`;
        }
        const manifest = this.manifest();
        if (!manifest[filename]) {
            throw new Error(`Cannot find "${filename}" asset in the manifest file`);
        }
        return `${this.getAssetUrl()}/${manifest[filename].file}`;
    }
    /**
     * Returns the manifest contents as object
     *
     * Note that the manifest file is only available in production.
     * Vite doesn't generate any manifest file in development.
     */
    manifest() {
        if (!this.shouldUseManifest()) {
            throw new Error('Cannot use manifest when not in production');
        }
        return super.manifest();
    }
    /**
     * Returns list for all the javascript files for a given entry point
     */
    entryPointJsFiles(name) {
        const entrypoints = this.entryPoints();
        if (!entrypoints[name]) {
            throw new Error(`Cannot find assets for "${name}" entrypoint. Make sure to define it inside the "entryPoints" vite config`);
        }
        return entrypoints[name].js;
    }
    /**
     * Returns list for all the css files for a given entry point
     */
    entryPointCssFiles(name) {
        const entrypoints = this.entryPoints();
        if (!entrypoints[name]) {
            throw new Error(`Cannot find assets for "${name}" entrypoint. Make sure to define it inside the "entryPoints" vite config`);
        }
        return entrypoints[name].css;
    }
    /**
     * Returns the script needed for the HMR working with React
     */
    getReactHmrScript() {
        if (!this.application.inDev) {
            return '';
        }
        return `
    <script type="module">
      import RefreshRuntime from '${this.getAssetUrl()}/@react-refresh'
      RefreshRuntime.injectIntoGlobalHook(window)
      window.$RefreshReg$ = () => {}
      window.$RefreshSig$ = () => (type) => type
      window.__vite_plugin_react_preamble_installed__ = true
    </script>
    `;
    }
    /**
     * Returns the script needed for the HMR working with Vite
     */
    getViteHmrScript() {
        if (!this.application.inDev) {
            return '';
        }
        return `<script type="module" src="${this.getAssetUrl()}/@vite/client"></script>`;
    }
}
exports.ViteDriver = ViteDriver;
