"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyBuilderOutputPlugin = void 0;
const path_1 = require("path");
const fs_1 = require("../fs");
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const utils_2 = require("@modern-js/utils");
function applyBuilderOutputPlugin(api) {
    api.modifyBundlerChain(async (chain, { isProd, isServer, isServiceWorker }) => {
        const config = api.getNormalizedConfig();
        const jsPath = (0, fs_1.getDistPath)(config.output, 'js');
        const publicPath = getPublicPath({
            config,
            isProd,
            context: api.context,
        });
        // js output
        const jsFilename = (0, fs_1.getFilename)(config.output, 'js', isProd);
        chain.output
            .path(api.context.distPath)
            .filename(path_1.posix.join(jsPath, jsFilename))
            .chunkFilename(path_1.posix.join(jsPath, `async/${jsFilename}`))
            .publicPath(publicPath)
            // disable pathinfo to improve compile performance
            // the path info is useless in most cases
            // see: https://webpack.js.org/guides/build-performance/#output-without-path-info
            .pathinfo(false)
            // since webpack v5.54.0+, hashFunction supports xxhash64 as a faster algorithm
            // which will be used as default when experiments.futureDefaults is enabled.
            .hashFunction('xxhash64');
        if (isServer) {
            const serverPath = (0, fs_1.getDistPath)(config.output, 'server');
            const filename = path_1.posix.join(serverPath, `[name].js`);
            chain.output
                .filename(filename)
                .chunkFilename(filename)
                .libraryTarget('commonjs2');
        }
        if (isServiceWorker) {
            const workerPath = (0, fs_1.getDistPath)(config.output, 'worker');
            const filename = path_1.posix.join(workerPath, `[name].js`);
            chain.output
                .filename(filename)
                .chunkFilename(filename)
                .libraryTarget('commonjs2');
        }
    });
}
exports.applyBuilderOutputPlugin = applyBuilderOutputPlugin;
function getPublicPath({ config, isProd, context, }) {
    var _a, _b, _c;
    const { dev, output } = config;
    let publicPath = '/';
    if (isProd) {
        if (output.assetPrefix) {
            publicPath = output.assetPrefix;
        }
    }
    else if (typeof dev.assetPrefix === 'string') {
        publicPath = dev.assetPrefix;
    }
    else if (dev.assetPrefix === true) {
        const protocol = ((_a = context.devServer) === null || _a === void 0 ? void 0 : _a.https) ? 'https' : 'http';
        const hostname = ((_b = context.devServer) === null || _b === void 0 ? void 0 : _b.hostname) || utils_2.DEFAULT_DEV_HOST;
        const port = ((_c = context.devServer) === null || _c === void 0 ? void 0 : _c.port) || constants_1.DEFAULT_PORT;
        if (hostname === utils_2.DEFAULT_DEV_HOST) {
            const localHostname = `localhost`;
            // If user not specify the hostname, it would use 0.0.0.0
            // The http://0.0.0.0:port can't visit in windows, so we shouldn't set publicPath as `//0.0.0.0:${port}/`;
            // Relative to docs:
            // - https://github.com/quarkusio/quarkus/issues/12246
            publicPath = `${protocol}://${localHostname}:${port}/`;
        }
        else {
            publicPath = `${protocol}://${hostname}:${port}/`;
        }
    }
    return (0, utils_1.addTrailingSlash)(publicPath);
}
