deno.land / x / lume@v2.1.4 / plugins / terser.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { minify } from "../deps/terser.ts";import { merge } from "../core/utils/object.ts";import { Page } from "../core/file.ts";import { prepareAsset, saveAsset } from "./source_maps.ts";
import type Site from "../core/site.ts";import type { MinifyOptions } from "../deps/terser.ts";
export interface Options { /** The list of extensions this plugin applies to */ extensions?: string[];
/** * Options passed to `terser` * @see https://terser.org/docs/api-reference/#minify-options */ options?: MinifyOptions;}
// Default optionsexport const defaults: Options = { extensions: [".js"], options: { module: true, compress: true, mangle: true, },};
/** A plugin to load all JavaScript files and minify them using Terser */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site.loadAssets(options.extensions); site.process(options.extensions, (pages) => pages.forEach(terser)); site.filter("terser", filter, true);
async function terser(page: Page) { const { content, filename, sourceMap, enableSourceMap } = prepareAsset( site, page, );
const terserOptions = { ...options.options, sourceMap: enableSourceMap ? { content: JSON.stringify(sourceMap), filename: filename, } : undefined, };
try { const output = await minify({ [filename]: content }, terserOptions); saveAsset( site, page, output.code!, // @ts-expect-error: terser uses @jridgewell/gen-mapping, which incorrectly has typed some types as nullable: https://github.com/jridgewell/gen-mapping/pull/9 output.map, ); } catch (cause) { throw new Error( `Error processing the file: ${filename} by the Terser plugin.`, { cause }, ); } }
async function filter(code: string): Promise<string | undefined> { const output = await minify(code, options.options); return output.code; } };}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/terser/#the-terser-filter */ terser: (code: string) => Promise<string | undefined>; } }}
lume

Version Info

Tagged at
6 months ago