htmlnano 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -17
- package/dist/_modules/collapseAttributeWhitespace.d.mts +41 -5
- package/dist/_modules/collapseAttributeWhitespace.d.ts +41 -5
- package/dist/_modules/collapseAttributeWhitespace.js +69 -16
- package/dist/_modules/collapseAttributeWhitespace.mjs +67 -17
- package/dist/_modules/collapseBooleanAttributes.d.mts +36 -3
- package/dist/_modules/collapseBooleanAttributes.d.ts +36 -3
- package/dist/_modules/collapseBooleanAttributes.js +18 -11
- package/dist/_modules/collapseBooleanAttributes.mjs +18 -11
- package/dist/_modules/collapseWhitespace.d.mts +36 -3
- package/dist/_modules/collapseWhitespace.d.ts +36 -3
- package/dist/_modules/collapseWhitespace.js +25 -2
- package/dist/_modules/collapseWhitespace.mjs +25 -2
- package/dist/_modules/custom.d.mts +36 -3
- package/dist/_modules/custom.d.ts +36 -3
- package/dist/_modules/deduplicateAttributeValues.d.mts +36 -3
- package/dist/_modules/deduplicateAttributeValues.d.ts +36 -3
- package/dist/_modules/deduplicateAttributeValues.js +20 -5
- package/dist/_modules/deduplicateAttributeValues.mjs +21 -6
- package/dist/_modules/example.d.mts +36 -3
- package/dist/_modules/example.d.ts +36 -3
- package/dist/_modules/mergeScripts.d.mts +36 -3
- package/dist/_modules/mergeScripts.d.ts +36 -3
- package/dist/_modules/mergeScripts.js +99 -24
- package/dist/_modules/mergeScripts.mjs +99 -24
- package/dist/_modules/mergeStyles.d.mts +36 -3
- package/dist/_modules/mergeStyles.d.ts +36 -3
- package/dist/_modules/mergeStyles.js +56 -4
- package/dist/_modules/mergeStyles.mjs +56 -4
- package/dist/_modules/minifyAttributes.d.mts +95 -0
- package/dist/_modules/minifyAttributes.d.ts +95 -0
- package/dist/_modules/minifyAttributes.js +159 -0
- package/dist/_modules/minifyAttributes.mjs +157 -0
- package/dist/_modules/minifyConditionalComments.d.mts +36 -3
- package/dist/_modules/minifyConditionalComments.d.ts +36 -3
- package/dist/_modules/minifyConditionalComments.js +37 -19
- package/dist/_modules/minifyConditionalComments.mjs +37 -19
- package/dist/_modules/minifyCss.d.mts +36 -3
- package/dist/_modules/minifyCss.d.ts +36 -3
- package/dist/_modules/minifyCss.js +13 -27
- package/dist/_modules/minifyCss.mjs +14 -28
- package/dist/_modules/minifyHtmlTemplate.d.mts +91 -0
- package/dist/_modules/minifyHtmlTemplate.d.ts +91 -0
- package/dist/_modules/minifyHtmlTemplate.js +231 -0
- package/dist/_modules/minifyHtmlTemplate.mjs +228 -0
- package/dist/_modules/minifyJs.d.mts +36 -3
- package/dist/_modules/minifyJs.d.ts +36 -3
- package/dist/_modules/minifyJs.js +94 -5
- package/dist/_modules/minifyJs.mjs +95 -6
- package/dist/_modules/minifyJson.d.mts +36 -3
- package/dist/_modules/minifyJson.d.ts +36 -3
- package/dist/_modules/minifyJson.js +8 -11
- package/dist/_modules/minifyJson.mjs +8 -11
- package/dist/_modules/minifySvg.d.mts +36 -3
- package/dist/_modules/minifySvg.d.ts +36 -3
- package/dist/_modules/minifySvg.js +35 -4
- package/dist/_modules/minifySvg.mjs +35 -4
- package/dist/_modules/minifyUrls.d.mts +37 -4
- package/dist/_modules/minifyUrls.d.ts +37 -4
- package/dist/_modules/minifyUrls.js +52 -27
- package/dist/_modules/minifyUrls.mjs +52 -27
- package/dist/_modules/normalizeAttributeValues.d.mts +36 -3
- package/dist/_modules/normalizeAttributeValues.d.ts +36 -3
- package/dist/_modules/normalizeAttributeValues.js +10 -8
- package/dist/_modules/normalizeAttributeValues.mjs +10 -8
- package/dist/_modules/removeAttributeQuotes.d.mts +40 -4
- package/dist/_modules/removeAttributeQuotes.d.ts +40 -4
- package/dist/_modules/removeAttributeQuotes.js +9 -4
- package/dist/_modules/removeAttributeQuotes.mjs +9 -4
- package/dist/_modules/removeComments.d.mts +37 -4
- package/dist/_modules/removeComments.d.ts +37 -4
- package/dist/_modules/removeComments.js +44 -12
- package/dist/_modules/removeComments.mjs +44 -12
- package/dist/_modules/removeEmptyAttributes.d.mts +36 -3
- package/dist/_modules/removeEmptyAttributes.d.ts +36 -3
- package/dist/_modules/removeEmptyAttributes.js +37 -16
- package/dist/_modules/removeEmptyAttributes.mjs +37 -16
- package/dist/_modules/removeEmptyElements.d.mts +95 -0
- package/dist/_modules/removeEmptyElements.d.ts +95 -0
- package/dist/_modules/removeEmptyElements.js +90 -0
- package/dist/_modules/removeEmptyElements.mjs +88 -0
- package/dist/_modules/removeOptionalTags.d.mts +36 -3
- package/dist/_modules/removeOptionalTags.d.ts +36 -3
- package/dist/_modules/removeOptionalTags.js +39 -28
- package/dist/_modules/removeOptionalTags.mjs +39 -28
- package/dist/_modules/removeRedundantAttributes.d.mts +36 -3
- package/dist/_modules/removeRedundantAttributes.d.ts +36 -3
- package/dist/_modules/removeRedundantAttributes.js +43 -28
- package/dist/_modules/removeRedundantAttributes.mjs +43 -28
- package/dist/_modules/removeUnusedCss.d.mts +37 -3
- package/dist/_modules/removeUnusedCss.d.ts +37 -3
- package/dist/_modules/removeUnusedCss.js +38 -13
- package/dist/_modules/removeUnusedCss.mjs +39 -14
- package/dist/_modules/sortAttributes.d.mts +36 -3
- package/dist/_modules/sortAttributes.d.ts +36 -3
- package/dist/_modules/sortAttributes.js +23 -5
- package/dist/_modules/sortAttributes.mjs +23 -5
- package/dist/_modules/sortAttributesWithLists.d.mts +36 -3
- package/dist/_modules/sortAttributesWithLists.d.ts +36 -3
- package/dist/_modules/sortAttributesWithLists.js +30 -8
- package/dist/_modules/sortAttributesWithLists.mjs +31 -9
- package/dist/helpers.d.ts +8 -1
- package/dist/helpers.js +48 -0
- package/dist/helpers.mjs +45 -1
- package/dist/index.d.ts +37 -4
- package/dist/index.js +13 -0
- package/dist/index.mjs +13 -0
- package/dist/presets/ampSafe.d.ts +36 -3
- package/dist/presets/max.d.ts +36 -3
- package/dist/presets/max.js +4 -0
- package/dist/presets/max.mjs +4 -0
- package/dist/presets/safe.d.ts +36 -3
- package/dist/presets/safe.js +6 -0
- package/dist/presets/safe.mjs +6 -0
- package/package.json +21 -13
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { optionalImport, isStyleNode, extractCssFromStyleNode } from '../helpers.mjs';
|
|
1
|
+
import { optionalImport, isStyleNode, isCssStyleType, extractCssFromStyleNode, stripCssCdata, wrapCssCdata } from '../helpers.mjs';
|
|
2
2
|
|
|
3
3
|
const postcssOptions = {
|
|
4
4
|
// Prevent the following warning from being shown:
|
|
@@ -20,7 +20,7 @@ const postcssOptions = {
|
|
|
20
20
|
if (node.attrs && 'integrity' in node.attrs) {
|
|
21
21
|
return node;
|
|
22
22
|
}
|
|
23
|
-
if (isStyleNode(node)) {
|
|
23
|
+
if (isStyleNode(node) && isCssStyleType(node)) {
|
|
24
24
|
p = processStyleNode(node, cssnanoOptions, cssnano, postcss);
|
|
25
25
|
if (p) {
|
|
26
26
|
promises.push(p);
|
|
@@ -38,26 +38,17 @@ const postcssOptions = {
|
|
|
38
38
|
};
|
|
39
39
|
function processStyleNode(styleNode, cssnanoOptions, cssnano, postcss) {
|
|
40
40
|
let css = extractCssFromStyleNode(styleNode);
|
|
41
|
-
if (!css) return;
|
|
42
|
-
// Improve performance by avoiding calling
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const strippedCss = stripCdata(css);
|
|
46
|
-
isCdataWrapped = css !== strippedCss;
|
|
47
|
-
css = strippedCss;
|
|
48
|
-
}
|
|
41
|
+
if (!css || css.trim() === '') return;
|
|
42
|
+
// Improve performance by avoiding calling stripCssCdata again and again
|
|
43
|
+
const { strippedCss, isCdataWrapped } = stripCssCdata(css);
|
|
44
|
+
css = strippedCss;
|
|
49
45
|
return postcss([
|
|
50
46
|
cssnano(cssnanoOptions)
|
|
51
47
|
]).process(css, postcssOptions).then((result)=>{
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} else {
|
|
57
|
-
styleNode.content = [
|
|
58
|
-
result.css
|
|
59
|
-
];
|
|
60
|
-
}
|
|
48
|
+
const minifiedCss = isCdataWrapped ? result.toString() : result.css;
|
|
49
|
+
styleNode.content = [
|
|
50
|
+
wrapCssCdata(minifiedCss, isCdataWrapped)
|
|
51
|
+
];
|
|
61
52
|
});
|
|
62
53
|
}
|
|
63
54
|
function processStyleAttr(node, cssnanoOptions, cssnano, postcss) {
|
|
@@ -65,7 +56,10 @@ function processStyleAttr(node, cssnanoOptions, cssnano, postcss) {
|
|
|
65
56
|
// a{color: red;}
|
|
66
57
|
const wrapperStart = 'a{';
|
|
67
58
|
const wrapperEnd = '}';
|
|
68
|
-
if (!node.attrs || !node.attrs.style) {
|
|
59
|
+
if (!node.attrs || !node.attrs.style || typeof node.attrs.style !== 'string') {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (node.attrs.style.trim() === '') {
|
|
69
63
|
return;
|
|
70
64
|
}
|
|
71
65
|
const wrappedStyle = wrapperStart + (node.attrs.style || '') + wrapperEnd;
|
|
@@ -77,13 +71,5 @@ function processStyleAttr(node, cssnanoOptions, cssnano, postcss) {
|
|
|
77
71
|
node.attrs.style = minifiedCss.substring(wrapperStart.length, minifiedCss.length - wrapperEnd.length);
|
|
78
72
|
});
|
|
79
73
|
}
|
|
80
|
-
function stripCdata(css) {
|
|
81
|
-
const leftStrippedCss = css.replace('<![CDATA[', '');
|
|
82
|
-
if (leftStrippedCss === css) {
|
|
83
|
-
return css;
|
|
84
|
-
}
|
|
85
|
-
const strippedCss = leftStrippedCss.replace(']]>', '');
|
|
86
|
-
return leftStrippedCss === strippedCss ? css : strippedCss;
|
|
87
|
-
}
|
|
88
74
|
|
|
89
75
|
export { mod as default };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import PostHTML from 'posthtml';
|
|
2
|
+
import { MinifyOptions } from 'terser';
|
|
3
|
+
import { Options } from 'cssnano';
|
|
4
|
+
import { Config } from 'svgo';
|
|
5
|
+
import { UserDefinedOptions } from 'purgecss';
|
|
6
|
+
|
|
7
|
+
type PostHTMLTreeLike = [PostHTML.Node] & PostHTML.NodeAPI & {
|
|
8
|
+
options?: {
|
|
9
|
+
quoteAllAttributes?: boolean | undefined;
|
|
10
|
+
quoteStyle?: 0 | 1 | 2 | undefined;
|
|
11
|
+
replaceQuote?: boolean | undefined;
|
|
12
|
+
} | undefined;
|
|
13
|
+
render(): string;
|
|
14
|
+
render(node: PostHTML.Node | PostHTMLTreeLike, renderOptions?: any): string;
|
|
15
|
+
};
|
|
16
|
+
type MaybeArray<T> = T | Array<T>;
|
|
17
|
+
type PostHTMLNodeLike = PostHTML.Node | string;
|
|
18
|
+
type HtmlnanoTemplateRule = {
|
|
19
|
+
tag: string;
|
|
20
|
+
attrs?: Record<string, string | boolean | void>;
|
|
21
|
+
};
|
|
22
|
+
type MinifyHtmlTemplateOptions = boolean | HtmlnanoTemplateRule[];
|
|
23
|
+
interface HtmlnanoOptions {
|
|
24
|
+
skipConfigLoading?: boolean;
|
|
25
|
+
configPath?: string;
|
|
26
|
+
skipInternalWarnings?: boolean;
|
|
27
|
+
collapseAttributeWhitespace?: boolean;
|
|
28
|
+
collapseBooleanAttributes?: {
|
|
29
|
+
amphtml?: boolean;
|
|
30
|
+
};
|
|
31
|
+
collapseWhitespace?: 'conservative' | 'all' | 'aggressive';
|
|
32
|
+
custom?: MaybeArray<(tree: PostHTMLTreeLike, options?: any) => (PostHTML.Node | PostHTMLTreeLike)>;
|
|
33
|
+
deduplicateAttributeValues?: boolean;
|
|
34
|
+
minifyUrls?: URL | string | false;
|
|
35
|
+
mergeStyles?: boolean;
|
|
36
|
+
mergeScripts?: boolean;
|
|
37
|
+
minifyCss?: Options | boolean;
|
|
38
|
+
minifyHtmlTemplate?: MinifyHtmlTemplateOptions;
|
|
39
|
+
minifyConditionalComments?: boolean;
|
|
40
|
+
minifyJs?: MinifyOptions | boolean;
|
|
41
|
+
minifyJson?: boolean;
|
|
42
|
+
minifyAttributes?: boolean | {
|
|
43
|
+
metaContent?: boolean;
|
|
44
|
+
redundantWhitespaces?: 'safe' | 'agressive' | false;
|
|
45
|
+
};
|
|
46
|
+
minifySvg?: Config | boolean;
|
|
47
|
+
normalizeAttributeValues?: boolean;
|
|
48
|
+
removeAttributeQuotes?: boolean | {
|
|
49
|
+
force?: boolean;
|
|
50
|
+
};
|
|
51
|
+
removeComments?: boolean | RegExp | ((comment: string) => boolean) | string;
|
|
52
|
+
removeEmptyAttributes?: boolean;
|
|
53
|
+
removeEmptyElements?: boolean | {
|
|
54
|
+
removeWithAttributes?: boolean;
|
|
55
|
+
};
|
|
56
|
+
removeRedundantAttributes?: boolean;
|
|
57
|
+
removeOptionalTags?: boolean;
|
|
58
|
+
removeUnusedCss?: boolean | ({
|
|
59
|
+
tool: 'purgeCSS';
|
|
60
|
+
} & Omit<UserDefinedOptions, 'content' | 'css' | 'extractors'>) | {
|
|
61
|
+
banner?: boolean;
|
|
62
|
+
csspath?: string;
|
|
63
|
+
htmlroot?: string;
|
|
64
|
+
ignore?: (string | RegExp)[];
|
|
65
|
+
inject?: string;
|
|
66
|
+
jsdom?: object;
|
|
67
|
+
media?: string[];
|
|
68
|
+
report?: boolean;
|
|
69
|
+
strictSSL?: boolean;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
uncssrc?: string;
|
|
72
|
+
userAgent?: string;
|
|
73
|
+
};
|
|
74
|
+
sortAttributes?: boolean | 'alphabetical' | 'frequency';
|
|
75
|
+
sortAttributesWithLists?: boolean | 'alphabetical' | 'frequency';
|
|
76
|
+
}
|
|
77
|
+
type HtmlnanoModuleAttrsHandler = (attrs: Record<string, string | boolean | void>, node: PostHTML.Node) => Record<string, string | boolean | void>;
|
|
78
|
+
type HtmlnanoModuleContentHandler = (content: Array<PostHTMLNodeLike>, node: PostHTML.Node) => MaybeArray<PostHTMLNodeLike>;
|
|
79
|
+
type HtmlnanoModuleNodeHandler = (node: PostHTMLNodeLike) => PostHTML.Node | string;
|
|
80
|
+
type OptionalOptions<T> = T extends boolean | string | Function | number | null | undefined ? T : T extends object ? Partial<T> : T;
|
|
81
|
+
type HtmlnanoModule<Options = any> = {
|
|
82
|
+
onAttrs?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleAttrsHandler;
|
|
83
|
+
onContent?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleContentHandler;
|
|
84
|
+
onNode?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleNodeHandler;
|
|
85
|
+
default?: (tree: PostHTMLTreeLike, options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => PostHTMLTreeLike | Promise<PostHTMLTreeLike>;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
declare const defaultRules: HtmlnanoTemplateRule[];
|
|
89
|
+
declare const mod: HtmlnanoModule<MinifyHtmlTemplateOptions>;
|
|
90
|
+
|
|
91
|
+
export { mod as default, defaultRules };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import PostHTML from 'posthtml';
|
|
2
|
+
import { MinifyOptions } from 'terser';
|
|
3
|
+
import { Options } from 'cssnano';
|
|
4
|
+
import { Config } from 'svgo';
|
|
5
|
+
import { UserDefinedOptions } from 'purgecss';
|
|
6
|
+
|
|
7
|
+
type PostHTMLTreeLike = [PostHTML.Node] & PostHTML.NodeAPI & {
|
|
8
|
+
options?: {
|
|
9
|
+
quoteAllAttributes?: boolean | undefined;
|
|
10
|
+
quoteStyle?: 0 | 1 | 2 | undefined;
|
|
11
|
+
replaceQuote?: boolean | undefined;
|
|
12
|
+
} | undefined;
|
|
13
|
+
render(): string;
|
|
14
|
+
render(node: PostHTML.Node | PostHTMLTreeLike, renderOptions?: any): string;
|
|
15
|
+
};
|
|
16
|
+
type MaybeArray<T> = T | Array<T>;
|
|
17
|
+
type PostHTMLNodeLike = PostHTML.Node | string;
|
|
18
|
+
type HtmlnanoTemplateRule = {
|
|
19
|
+
tag: string;
|
|
20
|
+
attrs?: Record<string, string | boolean | void>;
|
|
21
|
+
};
|
|
22
|
+
type MinifyHtmlTemplateOptions = boolean | HtmlnanoTemplateRule[];
|
|
23
|
+
interface HtmlnanoOptions {
|
|
24
|
+
skipConfigLoading?: boolean;
|
|
25
|
+
configPath?: string;
|
|
26
|
+
skipInternalWarnings?: boolean;
|
|
27
|
+
collapseAttributeWhitespace?: boolean;
|
|
28
|
+
collapseBooleanAttributes?: {
|
|
29
|
+
amphtml?: boolean;
|
|
30
|
+
};
|
|
31
|
+
collapseWhitespace?: 'conservative' | 'all' | 'aggressive';
|
|
32
|
+
custom?: MaybeArray<(tree: PostHTMLTreeLike, options?: any) => (PostHTML.Node | PostHTMLTreeLike)>;
|
|
33
|
+
deduplicateAttributeValues?: boolean;
|
|
34
|
+
minifyUrls?: URL | string | false;
|
|
35
|
+
mergeStyles?: boolean;
|
|
36
|
+
mergeScripts?: boolean;
|
|
37
|
+
minifyCss?: Options | boolean;
|
|
38
|
+
minifyHtmlTemplate?: MinifyHtmlTemplateOptions;
|
|
39
|
+
minifyConditionalComments?: boolean;
|
|
40
|
+
minifyJs?: MinifyOptions | boolean;
|
|
41
|
+
minifyJson?: boolean;
|
|
42
|
+
minifyAttributes?: boolean | {
|
|
43
|
+
metaContent?: boolean;
|
|
44
|
+
redundantWhitespaces?: 'safe' | 'agressive' | false;
|
|
45
|
+
};
|
|
46
|
+
minifySvg?: Config | boolean;
|
|
47
|
+
normalizeAttributeValues?: boolean;
|
|
48
|
+
removeAttributeQuotes?: boolean | {
|
|
49
|
+
force?: boolean;
|
|
50
|
+
};
|
|
51
|
+
removeComments?: boolean | RegExp | ((comment: string) => boolean) | string;
|
|
52
|
+
removeEmptyAttributes?: boolean;
|
|
53
|
+
removeEmptyElements?: boolean | {
|
|
54
|
+
removeWithAttributes?: boolean;
|
|
55
|
+
};
|
|
56
|
+
removeRedundantAttributes?: boolean;
|
|
57
|
+
removeOptionalTags?: boolean;
|
|
58
|
+
removeUnusedCss?: boolean | ({
|
|
59
|
+
tool: 'purgeCSS';
|
|
60
|
+
} & Omit<UserDefinedOptions, 'content' | 'css' | 'extractors'>) | {
|
|
61
|
+
banner?: boolean;
|
|
62
|
+
csspath?: string;
|
|
63
|
+
htmlroot?: string;
|
|
64
|
+
ignore?: (string | RegExp)[];
|
|
65
|
+
inject?: string;
|
|
66
|
+
jsdom?: object;
|
|
67
|
+
media?: string[];
|
|
68
|
+
report?: boolean;
|
|
69
|
+
strictSSL?: boolean;
|
|
70
|
+
timeout?: number;
|
|
71
|
+
uncssrc?: string;
|
|
72
|
+
userAgent?: string;
|
|
73
|
+
};
|
|
74
|
+
sortAttributes?: boolean | 'alphabetical' | 'frequency';
|
|
75
|
+
sortAttributesWithLists?: boolean | 'alphabetical' | 'frequency';
|
|
76
|
+
}
|
|
77
|
+
type HtmlnanoModuleAttrsHandler = (attrs: Record<string, string | boolean | void>, node: PostHTML.Node) => Record<string, string | boolean | void>;
|
|
78
|
+
type HtmlnanoModuleContentHandler = (content: Array<PostHTMLNodeLike>, node: PostHTML.Node) => MaybeArray<PostHTMLNodeLike>;
|
|
79
|
+
type HtmlnanoModuleNodeHandler = (node: PostHTMLNodeLike) => PostHTML.Node | string;
|
|
80
|
+
type OptionalOptions<T> = T extends boolean | string | Function | number | null | undefined ? T : T extends object ? Partial<T> : T;
|
|
81
|
+
type HtmlnanoModule<Options = any> = {
|
|
82
|
+
onAttrs?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleAttrsHandler;
|
|
83
|
+
onContent?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleContentHandler;
|
|
84
|
+
onNode?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleNodeHandler;
|
|
85
|
+
default?: (tree: PostHTMLTreeLike, options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => PostHTMLTreeLike | Promise<PostHTMLTreeLike>;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
declare const defaultRules: HtmlnanoTemplateRule[];
|
|
89
|
+
declare const mod: HtmlnanoModule<MinifyHtmlTemplateOptions>;
|
|
90
|
+
|
|
91
|
+
export { mod as default, defaultRules };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var helpers_js = require('../helpers.js');
|
|
4
|
+
|
|
5
|
+
const defaultRules = [
|
|
6
|
+
{
|
|
7
|
+
tag: 'script',
|
|
8
|
+
attrs: {
|
|
9
|
+
type: 'text/html'
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
tag: 'script',
|
|
14
|
+
attrs: {
|
|
15
|
+
type: 'text/template'
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
tag: 'script',
|
|
20
|
+
attrs: {
|
|
21
|
+
type: 'text/x-template'
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
tag: 'script',
|
|
26
|
+
attrs: {
|
|
27
|
+
type: 'text/x-handlebars-template'
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
tag: 'script',
|
|
32
|
+
attrs: {
|
|
33
|
+
type: 'text/x-handlebars'
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
tag: 'script',
|
|
38
|
+
attrs: {
|
|
39
|
+
type: 'text/x-mustache-template'
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
tag: 'script',
|
|
44
|
+
attrs: {
|
|
45
|
+
type: 'text/x-underscore-template'
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
tag: 'script',
|
|
50
|
+
attrs: {
|
|
51
|
+
type: 'text/x-jsrender'
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
tag: 'script',
|
|
56
|
+
attrs: {
|
|
57
|
+
type: 'text/x-jquery-tmpl'
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
tag: 'script',
|
|
62
|
+
attrs: {
|
|
63
|
+
type: 'text/x-kendo-template'
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
tag: 'script',
|
|
68
|
+
attrs: {
|
|
69
|
+
type: 'text/ng-template'
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
tag: 'template'
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
const mod = {
|
|
77
|
+
async default (tree, options, moduleOptions) {
|
|
78
|
+
const rules = resolveTemplateRules(moduleOptions);
|
|
79
|
+
if (!rules.length) {
|
|
80
|
+
return tree;
|
|
81
|
+
}
|
|
82
|
+
const { default: htmlnano } = await import('../index.js');
|
|
83
|
+
const innerOptions = {
|
|
84
|
+
...options,
|
|
85
|
+
minifyHtmlTemplate: false,
|
|
86
|
+
skipConfigLoading: true
|
|
87
|
+
};
|
|
88
|
+
const promises = [];
|
|
89
|
+
tree.walk((node)=>{
|
|
90
|
+
if (!node.tag) {
|
|
91
|
+
return node;
|
|
92
|
+
}
|
|
93
|
+
if (!matchesTemplateRule(node, rules)) {
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
if (node.attrs && 'integrity' in node.attrs) {
|
|
97
|
+
return node;
|
|
98
|
+
}
|
|
99
|
+
if (node.tag === 'script' && node.attrs && 'src' in node.attrs) {
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
const rawContent = collectNodeContent(tree, node);
|
|
103
|
+
if (rawContent.trim().length === 0) {
|
|
104
|
+
return node;
|
|
105
|
+
}
|
|
106
|
+
const promise = htmlnano.process(rawContent, innerOptions, {}, {}).then((result)=>{
|
|
107
|
+
node.content = [
|
|
108
|
+
result.html
|
|
109
|
+
];
|
|
110
|
+
});
|
|
111
|
+
promises.push(promise);
|
|
112
|
+
return node;
|
|
113
|
+
});
|
|
114
|
+
return Promise.all(promises).then(()=>tree);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
function resolveTemplateRules(moduleOptions) {
|
|
118
|
+
if (!moduleOptions || moduleOptions === true) {
|
|
119
|
+
return normalizeRules(defaultRules);
|
|
120
|
+
}
|
|
121
|
+
if (Array.isArray(moduleOptions)) {
|
|
122
|
+
return normalizeRules(moduleOptions);
|
|
123
|
+
}
|
|
124
|
+
return normalizeRules(defaultRules);
|
|
125
|
+
}
|
|
126
|
+
function normalizeRules(rules) {
|
|
127
|
+
const normalized = [];
|
|
128
|
+
for (const rule of rules){
|
|
129
|
+
const resolved = normalizeRule(rule);
|
|
130
|
+
if (resolved) {
|
|
131
|
+
normalized.push(resolved);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return normalized;
|
|
135
|
+
}
|
|
136
|
+
function normalizeRule(rule) {
|
|
137
|
+
if (rule && typeof rule === 'object') {
|
|
138
|
+
if (typeof rule.tag !== 'string' || !rule.tag.trim()) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const attrs = {};
|
|
142
|
+
if (rule.attrs && typeof rule.attrs === 'object') {
|
|
143
|
+
for (const [name, value] of Object.entries(rule.attrs)){
|
|
144
|
+
if (typeof name !== 'string' || !name.trim()) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
attrs[name.toLowerCase()] = value != null ? value : true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
tag: rule.tag.toLowerCase(),
|
|
152
|
+
attrs: Object.keys(attrs).length ? attrs : undefined
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
function matchesTemplateRule(node, rules) {
|
|
158
|
+
var _node_tag;
|
|
159
|
+
const tag = (_node_tag = node.tag) == null ? void 0 : _node_tag.toLowerCase();
|
|
160
|
+
if (!tag) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const attrs = normalizeNodeAttrs(node);
|
|
164
|
+
for (const rule of rules){
|
|
165
|
+
if (tag !== rule.tag) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (!rule.attrs) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
let match = true;
|
|
172
|
+
for (const [attrName, ruleValue] of Object.entries(rule.attrs)){
|
|
173
|
+
if (!(attrName in attrs)) {
|
|
174
|
+
match = false;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (ruleValue === true) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const nodeValue = normalizeAttrValue(attrName, attrs[attrName]);
|
|
181
|
+
const normalizedRuleValue = normalizeAttrValue(attrName, ruleValue);
|
|
182
|
+
if (nodeValue !== normalizedRuleValue) {
|
|
183
|
+
match = false;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (match) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
function normalizeNodeAttrs(node) {
|
|
194
|
+
if (!node.attrs) {
|
|
195
|
+
return {};
|
|
196
|
+
}
|
|
197
|
+
const normalized = {};
|
|
198
|
+
for (const [key, value] of Object.entries(node.attrs)){
|
|
199
|
+
normalized[key.toLowerCase()] = value;
|
|
200
|
+
}
|
|
201
|
+
return normalized;
|
|
202
|
+
}
|
|
203
|
+
function normalizeAttrValue(attrName, value) {
|
|
204
|
+
if (typeof value !== 'string') {
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
if (attrName === 'type') {
|
|
208
|
+
return helpers_js.normalizeMimeType(value);
|
|
209
|
+
}
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
function collectNodeContent(tree, node) {
|
|
213
|
+
if (!node.content) {
|
|
214
|
+
return '';
|
|
215
|
+
}
|
|
216
|
+
const content = typeof node.content === 'string' ? [
|
|
217
|
+
node.content
|
|
218
|
+
] : node.content;
|
|
219
|
+
let html = '';
|
|
220
|
+
for (const child of content){
|
|
221
|
+
if (typeof child === 'string') {
|
|
222
|
+
html += child;
|
|
223
|
+
} else {
|
|
224
|
+
html += tree.render(child);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return html;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
exports.default = mod;
|
|
231
|
+
exports.defaultRules = defaultRules;
|