@jsenv/plugin-minification
Version:
32 lines (29 loc) • 897 B
JavaScript
// https://github.com/terser-js/terser#minify-options
export const minifyJs = async (jsUrlInfo, options) => {
const url = jsUrlInfo.url;
const content = jsUrlInfo.content;
const sourcemap = jsUrlInfo.sourcemap;
const isJsModule = jsUrlInfo.type === "js_module";
const { minify } = await import("terser");
const terserResult = await minify(
{
[url]: content,
},
{
sourceMap: {
...(sourcemap ? { content: JSON.stringify(sourcemap) } : {}),
asObject: true,
includeSources: true,
},
module: isJsModule,
// We need to preserve "new __InlineContent__()" calls to be able to recognize them
// after minification in order to version urls inside inline content text
keep_fnames: /__InlineContent__/,
...options,
},
);
return {
content: terserResult.code,
sourcemap: terserResult.map,
};
};