-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.ts
More file actions
79 lines (73 loc) · 1.95 KB
/
Copy pathindex.ts
File metadata and controls
79 lines (73 loc) · 1.95 KB
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
/// <reference path="declarations.d.ts" />
import * as minify from 'minify-html-literals';
import {
Plugin,
SourceDescription,
TransformHook,
PluginContext
} from 'rollup';
import { createFilter } from 'rollup-pluginutils';
/**
* Plugin options.
*/
export interface Options {
/**
* Pattern or array of patterns of files to minify.
*/
include?: string | string[];
/**
* Pattern or array of patterns of files not to minify.
*/
exclude?: string | string[];
/**
* Minify options, see
* https://www.npmjs.com/package/minify-html-literals#options.
*/
options?: Partial<minify.Options>;
/**
* If true, any errors while parsing or minifying will abort the bundle
* process. Defaults to false, which will only show a warning.
*/
failOnError?: boolean;
/**
* Override minify-html-literals function.
*/
minifyHTMLLiterals?: typeof minify.minifyHTMLLiterals;
/**
* Override include/exclude filter.
*/
filter?: (id: string) => boolean;
}
export default function(
options: Options = {}
): Plugin & { transform: TransformHook } {
if (!options.minifyHTMLLiterals) {
options.minifyHTMLLiterals = minify.minifyHTMLLiterals;
}
if (!options.filter) {
options.filter = createFilter(options.include, options.exclude);
}
const minifyOptions = <minify.DefaultOptions>options.options || {};
return {
name: 'minify-html-literals',
transform(this: PluginContext, code: string, id: string) {
if (options.filter!(id)) {
try {
return <SourceDescription>options.minifyHTMLLiterals!(code, {
...minifyOptions,
fileName: id
});
} catch (error) {
// check if Error ese treat as string
const message =
error instanceof Error ? error.message : (error as string);
if (options.failOnError) {
this.error(message);
} else {
this.warn(message);
}
}
}
}
};
}