"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.terserPlugin = void 0;
const okie_1 = require("okie");
function terserPlugin(options) {
    const worker = new okie_1.Worker((basedir, code, options) => {
        // when vite is linked, the worker thread won't share the same resolve
        // root with vite itself, so we have to pass in the basedir and resolve
        // terser first.
        // eslint-disable-next-line
        const terserPath = require.resolve('terser', {
            paths: [basedir]
        });
        return require(terserPath).minify(code, options);
    });
    return {
        name: 'vite:terser',
        async renderChunk(code, _chunk, outputOptions) {
            const res = await worker.run(__dirname, code, {
                ...options,
                sourceMap: !!outputOptions.sourcemap,
                module: outputOptions.format.startsWith('es'),
                toplevel: outputOptions.format === 'cjs',
                safari10: true
            });
            return {
                code: res.code,
                map: res.map
            };
        },
        closeBundle() {
            worker.stop();
        }
    };
}
exports.terserPlugin = terserPlugin;
//# sourceMappingURL=terser.js.map