blob: 277d9d9f7a98c7b97f0c15626426bad34853974f [file] [log] [blame]
Tim van der Lippe652ccb72021-05-27 17:07:12 +01001"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.minifyHTMLLiterals = exports.defaultValidation = exports.defaultShouldMinifyCSS = exports.defaultShouldMinify = exports.defaultGenerateSourceMap = void 0;
4const magic_string_1 = require("magic-string");
5const parse_literals_1 = require("parse-literals");
6const strategy_1 = require("./strategy");
7/**
8 * The default method to generate a SourceMap. It will generate the SourceMap
9 * from the provided MagicString instance using "fileName.map" as the file and
10 * "fileName" as the source.
11 *
12 * @param ms the MagicString instance with code modifications
13 * @param fileName the name of the source file
14 * @returns a v3 SourceMap
15 */
16function defaultGenerateSourceMap(ms, fileName) {
17 return ms.generateMap({
18 file: `${fileName}.map`,
19 source: fileName,
20 hires: true
21 });
22}
23exports.defaultGenerateSourceMap = defaultGenerateSourceMap;
24/**
25 * The default method to determine whether or not to minify a template. It will
26 * return true for all tagged templates whose tag name contains "html" (case
27 * insensitive).
28 *
29 * @param template the template to check
30 * @returns true if the template should be minified
31 */
32function defaultShouldMinify(template) {
33 const tag = template.tag && template.tag.toLowerCase();
34 return !!tag && (tag.includes('html') || tag.includes('svg'));
35}
36exports.defaultShouldMinify = defaultShouldMinify;
37/**
38 * The default method to determine whether or not to minify a CSS template. It
39 * will return true for all tagged templates whose tag name contains "css" (case
40 * insensitive).
41 *
42 * @param template the template to check
43 * @returns true if the template should be minified
44 */
45function defaultShouldMinifyCSS(template) {
46 return !!template.tag && template.tag.toLowerCase().includes('css');
47}
48exports.defaultShouldMinifyCSS = defaultShouldMinifyCSS;
49/**
50 * The default validation.
51 */
52exports.defaultValidation = {
53 ensurePlaceholderValid(placeholder) {
54 if (typeof placeholder !== 'string' || !placeholder.length) {
55 throw new Error('getPlaceholder() must return a non-empty string');
56 }
57 },
58 ensureHTMLPartsValid(parts, htmlParts) {
59 if (parts.length !== htmlParts.length) {
60 throw new Error('splitHTMLByPlaceholder() must return same number of strings as template parts');
61 }
62 }
63};
64function minifyHTMLLiterals(source, options = {}) {
65 options.minifyOptions = {
66 ...strategy_1.defaultMinifyOptions,
67 ...(options.minifyOptions || {})
68 };
69 if (!options.MagicString) {
70 options.MagicString = magic_string_1.default;
71 }
72 if (!options.parseLiterals) {
73 options.parseLiterals = parse_literals_1.parseLiterals;
74 }
75 if (!options.shouldMinify) {
76 options.shouldMinify = defaultShouldMinify;
77 }
78 if (!options.shouldMinifyCSS) {
79 options.shouldMinifyCSS = defaultShouldMinifyCSS;
80 }
81 options.parseLiteralsOptions = {
82 ...{ fileName: options.fileName },
83 ...(options.parseLiteralsOptions || {})
84 };
85 const templates = options.parseLiterals(source, options.parseLiteralsOptions);
86 const strategy = options.strategy || strategy_1.defaultStrategy;
87 const { shouldMinify, shouldMinifyCSS } = options;
88 let validate;
89 if (options.validate !== false) {
90 validate = options.validate || exports.defaultValidation;
91 }
92 const ms = new options.MagicString(source);
93 templates.forEach(template => {
94 const minifyHTML = shouldMinify(template);
95 const minifyCSS = !!strategy.minifyCSS && shouldMinifyCSS(template);
96 if (minifyHTML || minifyCSS) {
97 const placeholder = strategy.getPlaceholder(template.parts);
98 if (validate) {
99 validate.ensurePlaceholderValid(placeholder);
100 }
101 const combined = strategy.combineHTMLStrings(template.parts, placeholder);
102 let min;
103 if (minifyCSS) {
104 const minifyCSSOptions = (options.minifyOptions || {}).minifyCSS;
105 if (typeof minifyCSSOptions === 'function') {
106 min = minifyCSSOptions(combined);
107 }
108 else if (minifyCSSOptions === false) {
109 min = combined;
110 }
111 else {
112 const cssOptions = typeof minifyCSSOptions === 'object' ? minifyCSSOptions : undefined;
113 min = strategy.minifyCSS(combined, cssOptions);
114 }
115 }
116 else {
117 min = strategy.minifyHTML(combined, options.minifyOptions);
118 }
119 const minParts = strategy.splitHTMLByPlaceholder(min, placeholder);
120 if (validate) {
121 validate.ensureHTMLPartsValid(template.parts, minParts);
122 }
123 template.parts.forEach((part, index) => {
124 if (part.start < part.end) {
125 // Only overwrite if the literal part has text content
126 ms.overwrite(part.start, part.end, minParts[index]);
127 }
128 });
129 }
130 });
131 const sourceMin = ms.toString();
132 if (source === sourceMin) {
133 return null;
134 }
135 else {
136 let map;
137 if (options.generateSourceMap !== false) {
138 const generateSourceMap = options.generateSourceMap || defaultGenerateSourceMap;
139 map = generateSourceMap(ms, options.fileName || '');
140 }
141 return {
142 map,
143 code: sourceMin
144 };
145 }
146}
147exports.minifyHTMLLiterals = minifyHTMLLiterals;
148//# sourceMappingURL=minifyHTMLLiterals.js.map