@ohos-ports/html-minifier-next 8.4.2-beta.0 → 8.10.8-beta.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 +147 -33
- package/cli.js +70 -54
- package/dist/types/htmlminifier.d.ts +87 -33
- package/dist/types/htmlminifier.d.ts.map +1 -1
- package/dist/types/htmlparser.d.ts +4 -1
- package/dist/types/htmlparser.d.ts.map +1 -1
- package/dist/types/lib/attributes.d.ts +12 -10
- package/dist/types/lib/attributes.d.ts.map +1 -1
- package/dist/types/lib/constants.d.ts +38 -3
- package/dist/types/lib/constants.d.ts.map +1 -1
- package/dist/types/lib/content.d.ts.map +1 -1
- package/dist/types/lib/elements.d.ts.map +1 -1
- package/dist/types/lib/fragments.d.ts +1 -0
- package/dist/types/lib/fragments.d.ts.map +1 -1
- package/dist/types/lib/option-definitions.d.ts.map +1 -1
- package/dist/types/lib/options.d.ts +14 -46
- package/dist/types/lib/options.d.ts.map +1 -1
- package/dist/types/lib/unused-css.d.ts +34 -4
- package/dist/types/lib/unused-css.d.ts.map +1 -1
- package/dist/types/lib/urls.d.ts.map +1 -1
- package/dist/types/lib/utils.d.ts +31 -1
- package/dist/types/lib/utils.d.ts.map +1 -1
- package/dist/types/lib/whitespace.d.ts +57 -5
- package/dist/types/lib/whitespace.d.ts.map +1 -1
- package/dist/types/presets.d.ts +1 -2
- package/dist/types/presets.d.ts.map +1 -1
- package/dist/types/tokenchain.d.ts.map +1 -1
- package/html-minifier-next.schema.json +15 -7
- package/package.json +17 -18
- package/src/htmlminifier.js +1103 -392
- package/src/htmlparser.js +452 -200
- package/src/lib/attributes.js +201 -112
- package/src/lib/constants.js +130 -19
- package/src/lib/content.js +1 -6
- package/src/lib/elements.js +17 -12
- package/src/lib/file-pool.js +18 -2
- package/src/lib/file-write.js +87 -0
- package/src/lib/fragments.js +74 -8
- package/src/lib/option-definitions.js +17 -10
- package/src/lib/options.js +151 -71
- package/src/lib/unused-css.js +145 -27
- package/src/lib/urls.js +0 -9
- package/src/lib/utils.js +210 -45
- package/src/lib/whitespace.js +173 -63
- package/src/presets.js +1 -2
- package/src/tokenchain.js +1 -4
package/src/lib/options.js
CHANGED
|
@@ -3,24 +3,22 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { createUrlMinifier } from './urls.js';
|
|
6
|
-
import { LRU, MAX_CACHE_ENTRY_SIZE, stableStringify, hashContent, identity, lowercase, paramCase, replaceAsync, parseRegExp, describeQuantifierRisk, lostFlag } from './utils.js';
|
|
7
|
-
import { RE_TRAILING_SEMICOLON } from './constants.js';
|
|
6
|
+
import { LRU, MAX_CACHE_ENTRY_SIZE, stableStringify, hashContent, identity, isThenable, lowercase, paramCase, replaceAsync, parseRegExp, describeQuantifierRisk, lostFlag } from './utils.js';
|
|
7
|
+
import { RE_TRAILING_SEMICOLON, MISSING_DEPENDENCY, svgoPluginsInline } from './constants.js';
|
|
8
8
|
import { canCollapseWhitespace, canTrimWhitespace } from './whitespace.js';
|
|
9
9
|
import { wrapCSS, unwrapCSS } from './content.js';
|
|
10
|
-
import { findUnusedSymbols, normalizeUnusedCSSOptions } from './unused-css.js';
|
|
10
|
+
import { createSelectorPruner, findUnusedSymbols, normalizeUnusedCSSOptions } from './unused-css.js';
|
|
11
11
|
import { getPreset, getPresetNames } from '../presets.js';
|
|
12
12
|
import { optionDefinitions, optionDefaults } from './option-definitions.js';
|
|
13
13
|
|
|
14
14
|
/** @import { MinifierOptions, HTMLAttribute } from '../htmlminifier.js' */
|
|
15
15
|
|
|
16
|
-
// Type definitions
|
|
17
|
-
|
|
18
16
|
/**
|
|
19
17
|
* Per-document state handed to `minifyCSS`. Its closure hangs off the memoized
|
|
20
18
|
* options object that every `minify()` call with those options shares, so state
|
|
21
19
|
* belonging to one document has to be passed in rather than captured.
|
|
22
20
|
*
|
|
23
|
-
* @typedef {{usedSymbols?: Set<string>, warned: Set<string>}} CSSContext
|
|
21
|
+
* @typedef {{usedSymbols?: Set<string>, usedElements?: Set<string>, usedElementsKey?: string, warned: Set<string>}} CSSContext
|
|
24
22
|
*/
|
|
25
23
|
|
|
26
24
|
/**
|
|
@@ -47,18 +45,23 @@ import { optionDefinitions, optionDefaults } from './option-definitions.js';
|
|
|
47
45
|
* minifyJS: (text: string, inline?: boolean, isModule?: boolean) => string | Promise<string>,
|
|
48
46
|
* minifyURLs: (text: string) => string | Promise<string>,
|
|
49
47
|
* minifySVG: ((svgContent: string) => string | Promise<string>) | null,
|
|
48
|
+
* canMinifyCSS: ((text: string, type?: 'inline' | 'media') => boolean) | null,
|
|
49
|
+
* canMinifyJS: ((text: string, inline?: boolean) => boolean) | null,
|
|
50
|
+
* canMinifySVG: ((text: string) => boolean) | null,
|
|
50
51
|
* removeUnusedCSS: {safelist: Array<string | RegExp>, scripts: boolean} | null,
|
|
51
52
|
* cssContext?: CSSContext,
|
|
53
|
+
* documentState?: {hasMathML: boolean},
|
|
52
54
|
* parallelJS?: boolean,
|
|
53
55
|
* nameParent?: (name: string) => string,
|
|
54
|
-
*
|
|
56
|
+
* namesAsWritten?: boolean,
|
|
55
57
|
* keepClosingSlashHTML?: boolean,
|
|
56
58
|
* insideSVG?: boolean,
|
|
57
59
|
* insideForeignContent?: boolean
|
|
58
60
|
* }} ProcessedOptions
|
|
59
61
|
*/
|
|
60
62
|
|
|
61
|
-
|
|
63
|
+
const RE_SCOPE_AT_RULE = /@scope\b/i;
|
|
64
|
+
const RE_SVGO_EMPTY_ATTRIBUTE = / ([^\s"'<>/=]+)=""/g;
|
|
62
65
|
|
|
63
66
|
/** @param {ProcessedOptions} options */
|
|
64
67
|
function shouldMinifyInnerHTML(options) {
|
|
@@ -97,7 +100,7 @@ function getUrlMinifyCache(site) {
|
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
// User-facing option keys that are valid but not listed in `optionDefinitions`
|
|
100
|
-
const optionKeysExtra = new Set(['preset', 'log', 'canCollapseWhitespace', 'canTrimWhitespace']);
|
|
103
|
+
const optionKeysExtra = new Set(['preset', 'log', 'canCollapseWhitespace', 'canTrimWhitespace', 'canMinifyCSS', 'canMinifyJS', 'canMinifySVG']);
|
|
101
104
|
|
|
102
105
|
// Unknown option keys and preset names already warned about—warn once per
|
|
103
106
|
// key per process, so repeated `minify` calls (e.g., batch runs) don’t flood STDERR
|
|
@@ -112,6 +115,10 @@ const unusedCSSWarned = new Set();
|
|
|
112
115
|
// Object-valued options handed a string, warned about once per distinct value
|
|
113
116
|
const stringValuesWarned = new Set();
|
|
114
117
|
|
|
118
|
+
// Clause for options that only the built-in minifier behind `key` reads
|
|
119
|
+
const replacedByOwnFunction = (/** @type {string} */ key) =>
|
|
120
|
+
(/** @type {Record<string, any>} */ input) => typeof input[key] === 'function' && 'which a function of your own replaces';
|
|
121
|
+
|
|
115
122
|
/**
|
|
116
123
|
* Options that do nothing on their own, and the option each one needs to take effect
|
|
117
124
|
*
|
|
@@ -127,7 +134,11 @@ const stringValuesWarned = new Set();
|
|
|
127
134
|
* @type {OptionDependency[]}
|
|
128
135
|
*/
|
|
129
136
|
const optionDependencies = [
|
|
137
|
+
{ option: 'canMinifyCSS', requires: 'minifyCSS', unusable: replacedByOwnFunction('minifyCSS'), clear: true },
|
|
138
|
+
{ option: 'canMinifyJS', requires: 'minifyJS', unusable: replacedByOwnFunction('minifyJS'), clear: true },
|
|
139
|
+
{ option: 'canMinifySVG', requires: 'minifySVG', unusable: replacedByOwnFunction('minifySVG'), clear: true },
|
|
130
140
|
{ option: 'collapseInlineTagWhitespace', requires: 'collapseWhitespace' },
|
|
141
|
+
{ option: 'collapseNoBreakSpaces', requires: 'collapseWhitespace' },
|
|
131
142
|
{ option: 'conservativeCollapse', requires: 'collapseWhitespace' },
|
|
132
143
|
{ option: 'customEventAttributes', requires: 'minifyJS' },
|
|
133
144
|
{ option: 'inlineCustomElements', requires: 'collapseWhitespace' },
|
|
@@ -138,7 +149,7 @@ const optionDependencies = [
|
|
|
138
149
|
option: 'removeUnusedCSS',
|
|
139
150
|
requires: 'minifyCSS',
|
|
140
151
|
// Removal rides along with Lightning CSS, which a function of one’s own replaces
|
|
141
|
-
unusable: (
|
|
152
|
+
unusable: replacedByOwnFunction('minifyCSS'),
|
|
142
153
|
clear: true
|
|
143
154
|
},
|
|
144
155
|
{ option: 'trimCustomFragments', requires: 'collapseWhitespace' }
|
|
@@ -152,7 +163,60 @@ function isRequested(value) {
|
|
|
152
163
|
return Array.isArray(value) ? value.length > 0 : Boolean(value);
|
|
153
164
|
}
|
|
154
165
|
|
|
155
|
-
//
|
|
166
|
+
// A `canMinify…` hook that returns false leaves the content as is, as does one that
|
|
167
|
+
// throws under `continueOnMinifyError`; hooks are synchronous, so a promise counts as an error
|
|
168
|
+
/**
|
|
169
|
+
* @param {ProcessedOptions} options - Processed options
|
|
170
|
+
* @param {'canMinifyCSS' | 'canMinifyJS' | 'canMinifySVG'} name - Hook to consult
|
|
171
|
+
* @param {...any} args - Arguments for the hook
|
|
172
|
+
* @returns {boolean}
|
|
173
|
+
*/
|
|
174
|
+
function declinesMinify(options, name, ...args) {
|
|
175
|
+
const hook = /** @type {((...args: any[]) => unknown) | null} */ (options[name]);
|
|
176
|
+
if (!hook) return false;
|
|
177
|
+
try {
|
|
178
|
+
const result = hook(...args);
|
|
179
|
+
if (isThenable(result)) {
|
|
180
|
+
// Keep a later rejection from surfacing as unhandled
|
|
181
|
+
/** @type {Promise<unknown>} */ (result).catch(() => {});
|
|
182
|
+
throw new TypeError(`\`${name}\` must return a boolean, not a promise`);
|
|
183
|
+
}
|
|
184
|
+
return !result;
|
|
185
|
+
} catch (err) {
|
|
186
|
+
if (!options.continueOnMinifyError) {
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
options.log && options.log(err);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** @param {unknown} err - Error raised while minifying */
|
|
195
|
+
function isMissingDependency(err) {
|
|
196
|
+
return Boolean(err) && /** @type {any} */ (err).code === MISSING_DEPENDENCY;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// An unset (falsy) engine takes the fallback and names match lowercase; anything else
|
|
200
|
+
// passes through untouched so that validation reports it instead of a type error
|
|
201
|
+
/**
|
|
202
|
+
* @param {unknown} engine - Configured engine name
|
|
203
|
+
* @param {string} fallback - Engine to use when none is configured
|
|
204
|
+
*/
|
|
205
|
+
function normalizeEngine(engine, fallback) {
|
|
206
|
+
if (!engine) return fallback;
|
|
207
|
+
return typeof engine === 'string' ? engine.toLowerCase() : engine;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** @param {unknown} engine - Engine name as it should read in an error message */
|
|
211
|
+
function describeEngine(engine) {
|
|
212
|
+
if (typeof engine === 'string') return engine;
|
|
213
|
+
try {
|
|
214
|
+
return JSON.stringify(engine) ?? String(engine);
|
|
215
|
+
} catch {
|
|
216
|
+
// BigInt and cyclic values do not serialize
|
|
217
|
+
return String(engine);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
156
220
|
|
|
157
221
|
/**
|
|
158
222
|
* @param {MinifierOptions} inputOptions - User-provided options
|
|
@@ -163,6 +227,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
163
227
|
/** @type {ProcessedOptions} */
|
|
164
228
|
const options = {
|
|
165
229
|
name: lowercase,
|
|
230
|
+
namesAsWritten: false,
|
|
166
231
|
canCollapseWhitespace,
|
|
167
232
|
canTrimWhitespace,
|
|
168
233
|
...optionDefaults,
|
|
@@ -171,6 +236,9 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
171
236
|
minifyJS: identity,
|
|
172
237
|
minifyURLs: identity,
|
|
173
238
|
minifySVG: null,
|
|
239
|
+
canMinifyCSS: null,
|
|
240
|
+
canMinifyJS: null,
|
|
241
|
+
canMinifySVG: null,
|
|
174
242
|
removeUnusedCSS: null
|
|
175
243
|
};
|
|
176
244
|
|
|
@@ -182,11 +250,9 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
182
250
|
const parseNestedRegExpArray = (/** @type {unknown} */ arr) => {
|
|
183
251
|
if (!Array.isArray(arr)) return [];
|
|
184
252
|
return arr.map(item => {
|
|
185
|
-
// If item is an array (a pair), recursively convert each element
|
|
186
253
|
if (Array.isArray(item)) {
|
|
187
254
|
return item.map(parseRegExp);
|
|
188
255
|
}
|
|
189
|
-
// Otherwise, convert single item
|
|
190
256
|
return parseRegExp(item);
|
|
191
257
|
});
|
|
192
258
|
};
|
|
@@ -202,7 +268,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
202
268
|
// its consumer already knows the source.
|
|
203
269
|
const warn = typeof inputOptions.log === 'function' ? inputOptions.log : console.warn;
|
|
204
270
|
|
|
205
|
-
// Warn about unrecognized options—catches typos
|
|
271
|
+
// Warn about unrecognized options—catches typos and options removed in earlier versions
|
|
206
272
|
Object.keys(inputOptions).forEach(function (key) {
|
|
207
273
|
if (!Object.hasOwn(optionDefinitions, key) && !optionKeysExtra.has(key) && !optionKeysWarned.has(key)) {
|
|
208
274
|
optionKeysWarned.add(key);
|
|
@@ -210,8 +276,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
210
276
|
}
|
|
211
277
|
});
|
|
212
278
|
|
|
213
|
-
// Merge preset with user options so all values go through normalization
|
|
214
|
-
// User options take precedence over preset values
|
|
279
|
+
// Merge preset with user options (which take precedence), so all values go through normalization
|
|
215
280
|
let effectiveInput = inputOptions;
|
|
216
281
|
if (inputOptions.preset) {
|
|
217
282
|
const preset = getPreset(inputOptions.preset);
|
|
@@ -250,8 +315,9 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
250
315
|
}
|
|
251
316
|
|
|
252
317
|
if (key === 'caseSensitive') {
|
|
318
|
+
// HTML names are read in lowercase, like browsers do, and only written as in the source
|
|
253
319
|
if (option) {
|
|
254
|
-
options.
|
|
320
|
+
options.namesAsWritten = true;
|
|
255
321
|
}
|
|
256
322
|
} else if (key === 'removeUnusedCSS') {
|
|
257
323
|
optionsDynamic.removeUnusedCSS = normalizeUnusedCSSOptions(option, message => {
|
|
@@ -264,6 +330,10 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
264
330
|
if (typeof option === 'function') {
|
|
265
331
|
options.log = option;
|
|
266
332
|
}
|
|
333
|
+
} else if (key === 'canMinifyCSS' || key === 'canMinifyJS' || key === 'canMinifySVG') {
|
|
334
|
+
if (typeof option === 'function') {
|
|
335
|
+
optionsDynamic[key] = option;
|
|
336
|
+
}
|
|
267
337
|
} else if (key === 'minifyCSS' && typeof option !== 'function') {
|
|
268
338
|
if (!option || !getLightningCSS || !cssMinifyCache) {
|
|
269
339
|
return;
|
|
@@ -280,12 +350,14 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
280
350
|
return text;
|
|
281
351
|
}
|
|
282
352
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
//
|
|
288
|
-
//
|
|
353
|
+
if (declinesMinify(options, 'canMinifyCSS', text, type)) {
|
|
354
|
+
return text;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Warnings are cached with the result and replayed on every hit, so a second document with
|
|
358
|
+
// the same defect hears about it, too (`context.warned` keeps one document from repeating
|
|
359
|
+
// itself), and the output doesn’t depend on cache size or eviction. They are cached even
|
|
360
|
+
// with the no-op `log`, so a later document with a `log` hook still gets them.
|
|
289
361
|
const report = (/** @type {string[]} */ messages) => {
|
|
290
362
|
if (!messages.length || options.log === identity) {
|
|
291
363
|
return;
|
|
@@ -302,8 +374,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
302
374
|
}
|
|
303
375
|
};
|
|
304
376
|
|
|
305
|
-
//
|
|
306
|
-
// This avoids expensive `replaceAsync` when URL minification is disabled
|
|
377
|
+
// Skip the costly `replaceAsync` when URL minification is off
|
|
307
378
|
if (options.minifyURLs !== identity) {
|
|
308
379
|
text = await replaceAsync(
|
|
309
380
|
text,
|
|
@@ -330,16 +401,25 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
330
401
|
const unusedSymbols = (unusedCSSConfig && context?.usedSymbols)
|
|
331
402
|
? findUnusedSymbols(text, context.usedSymbols, unusedCSSConfig.safelist)
|
|
332
403
|
: undefined;
|
|
333
|
-
|
|
334
|
-
//
|
|
335
|
-
//
|
|
336
|
-
|
|
404
|
+
// Selectors are pruned one by one, too—unless a `visitor` of the caller’s own is
|
|
405
|
+
// set, or the sheet uses `@scope`, whose preludes the visitor reaches as well and
|
|
406
|
+
// where a dropped limit would widen the scope
|
|
407
|
+
const pruneSelectors = Boolean(unusedSymbols && context?.usedElements) &&
|
|
408
|
+
!('visitor' in lightningCssOptions) && !RE_SCOPE_AT_RULE.test(text);
|
|
409
|
+
|
|
410
|
+
// Cache key: Content + type + options signature; large inputs are hashed to avoid
|
|
411
|
+
// huge Map keys. The symbol list belongs in the signature: The cache outlives a
|
|
412
|
+
// single `minify()` call, so identical style sheets in differently marked-up
|
|
413
|
+
// documents must not share an entry; so do the element set and the safelist
|
|
414
|
+
// where the selector visitor reads them.
|
|
337
415
|
const inputCSS = wrapCSS(text, type);
|
|
338
416
|
const cssSig = stableStringify({
|
|
339
417
|
type,
|
|
340
418
|
opts: lightningCssOptions,
|
|
341
419
|
cont: !!options.continueOnMinifyError,
|
|
342
|
-
unused: unusedSymbols && unusedSymbols.length ? unusedSymbols.slice().sort() : undefined
|
|
420
|
+
unused: unusedSymbols && unusedSymbols.length ? unusedSymbols.slice().sort() : undefined,
|
|
421
|
+
elements: pruneSelectors ? context?.usedElementsKey : undefined,
|
|
422
|
+
safelist: pruneSelectors ? unusedCSSConfig?.safelist : undefined
|
|
343
423
|
});
|
|
344
424
|
const isCacheable = inputCSS.length <= MAX_CACHE_ENTRY_SIZE;
|
|
345
425
|
const cssKey = isCacheable
|
|
@@ -363,7 +443,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
363
443
|
// of the same CSS content (same pattern as JS minification)
|
|
364
444
|
const inFlight = (async () => {
|
|
365
445
|
const transformCSS = await cssLoader();
|
|
366
|
-
//
|
|
446
|
+
// `Buffer.from()` is required—the Lightning CSS API expects a `Uint8Array`
|
|
367
447
|
const result = transformCSS({
|
|
368
448
|
filename: 'input.css',
|
|
369
449
|
code: Buffer.from(inputCSS),
|
|
@@ -373,6 +453,9 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
373
453
|
// Union, so that a manually supplied `unusedSymbols` list survives
|
|
374
454
|
...(unusedSymbols && unusedSymbols.length
|
|
375
455
|
? { unusedSymbols: lightningCssOptions.unusedSymbols ? [...new Set([...lightningCssOptions.unusedSymbols, ...unusedSymbols])] : unusedSymbols }
|
|
456
|
+
: {}),
|
|
457
|
+
...(pruneSelectors && unusedCSSConfig && context?.usedElements
|
|
458
|
+
? { visitor: { Selector: createSelectorPruner(unusedSymbols ?? [], context.usedElements, unusedCSSConfig.safelist) } }
|
|
376
459
|
: {})
|
|
377
460
|
});
|
|
378
461
|
|
|
@@ -391,18 +474,14 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
391
474
|
|
|
392
475
|
const outputCSS = unwrapCSS(result.code.toString(), type);
|
|
393
476
|
|
|
394
|
-
//
|
|
395
|
-
//
|
|
396
|
-
//
|
|
397
|
-
// 2. UIDs representing custom fragments (only lowercase letters and digits, no spaces)
|
|
398
|
-
// CDATA sections, HTML entities, and other invalid CSS are allowed to be removed
|
|
477
|
+
// Where Lightning CSS emptied template syntax (`<?php ?>`, `{{ }}`: `<` or `>` without
|
|
478
|
+
// `CDATA`) or custom-fragment UIDs (long lowercase alphanumeric runs), keep the
|
|
479
|
+
// original; CDATA sections, entities, and other invalid CSS may go
|
|
399
480
|
const isCDATA = text.includes('<![CDATA[');
|
|
400
481
|
const uidPattern = /[a-z0-9]{10,}/; // UIDs are long alphanumeric strings
|
|
401
482
|
const hasUID = uidPattern.test(text) && !isCDATA; // Exclude CDATA from UID detection
|
|
402
483
|
const looksLikeTemplate = (text.includes('<') || text.includes('>')) && !isCDATA;
|
|
403
484
|
|
|
404
|
-
// Preserve if output is empty and input had template syntax or UIDs
|
|
405
|
-
// This catches cases where Lightning CSS removed content that should be preserved
|
|
406
485
|
const css = (text.trim() && !outputCSS.trim() && (looksLikeTemplate || hasUID)) ? text : outputCSS;
|
|
407
486
|
return { css, warnings };
|
|
408
487
|
})();
|
|
@@ -431,17 +510,14 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
431
510
|
const loadSwc = getSwc;
|
|
432
511
|
const jsCache = jsMinifyCache;
|
|
433
512
|
|
|
434
|
-
// Parse configuration
|
|
435
513
|
const config = typeof option === 'object' ? option : {};
|
|
436
|
-
const engine = (config.engine
|
|
514
|
+
const engine = normalizeEngine(config.engine, 'terser');
|
|
437
515
|
|
|
438
|
-
// Validate engine
|
|
439
516
|
const supportedEngines = ['terser', 'swc'];
|
|
440
|
-
if (!supportedEngines.includes(engine)) {
|
|
441
|
-
throw new Error(`Unsupported JS minifier engine:
|
|
517
|
+
if (!supportedEngines.includes(/** @type {string} */ (engine))) {
|
|
518
|
+
throw new Error(`Unsupported JS minifier engine: \`${describeEngine(engine)}\`. Supported engines: ${supportedEngines.join(', ')}`);
|
|
442
519
|
}
|
|
443
520
|
|
|
444
|
-
// Extract engine-specific options (excluding `engine` field itself)
|
|
445
521
|
const engineOptions = { ...config };
|
|
446
522
|
delete engineOptions.engine;
|
|
447
523
|
|
|
@@ -455,7 +531,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
455
531
|
// SWC options (when engine is `swc`)
|
|
456
532
|
const swcOptions = engine === 'swc' ? engineOptions : {};
|
|
457
533
|
|
|
458
|
-
//
|
|
534
|
+
// Option signatures, computed once rather than per call
|
|
459
535
|
const terserSig = stableStringify({
|
|
460
536
|
...terserOptions,
|
|
461
537
|
cont: !!options.continueOnMinifyError
|
|
@@ -474,14 +550,16 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
474
550
|
return '';
|
|
475
551
|
}
|
|
476
552
|
|
|
477
|
-
|
|
478
|
-
|
|
553
|
+
if (declinesMinify(options, 'canMinifyJS', text, inline)) {
|
|
554
|
+
return text;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// Inline JS always goes to Terser, for bare returns; script blocks go to the chosen engine
|
|
479
558
|
const useEngine = inline ? 'terser' : engine;
|
|
480
559
|
let jsKey;
|
|
481
560
|
const isCacheable = code.length <= MAX_CACHE_ENTRY_SIZE;
|
|
482
561
|
|
|
483
562
|
try {
|
|
484
|
-
// Select pre-computed signature based on engine
|
|
485
563
|
const optsSig = useEngine === 'terser' ? terserSig : swcSig;
|
|
486
564
|
|
|
487
565
|
if (isCacheable) {
|
|
@@ -496,7 +574,6 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
496
574
|
}
|
|
497
575
|
|
|
498
576
|
const inFlight = (async () => {
|
|
499
|
-
// Dispatch to appropriate minifier
|
|
500
577
|
if (useEngine === 'terser') {
|
|
501
578
|
// Create a copy to avoid mutating shared `terserOptions` (race condition)
|
|
502
579
|
const terserCallOptions = {
|
|
@@ -530,7 +607,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
530
607
|
return resolved;
|
|
531
608
|
} catch (err) {
|
|
532
609
|
if (jsKey !== undefined) jsCache.delete(jsKey);
|
|
533
|
-
if (!options.continueOnMinifyError) {
|
|
610
|
+
if (!options.continueOnMinifyError || isMissingDependency(err)) {
|
|
534
611
|
throw err;
|
|
535
612
|
}
|
|
536
613
|
options.log && options.log(err);
|
|
@@ -563,13 +640,11 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
563
640
|
const instanceCache = getUrlMinifyCache(urlOptions.site || '');
|
|
564
641
|
|
|
565
642
|
options.minifyURLs = function (/** @type {string} */ text) {
|
|
566
|
-
// Fast
|
|
567
|
-
// Only process if contains URL-like characters (`/`, `:`, `#`, `?`) or spaces that need encoding
|
|
643
|
+
// Fast path: Skip text without URL-like characters (`/`, `:`, `#`, `?`) or spaces
|
|
568
644
|
if (!/[/:?#\s]/.test(text)) {
|
|
569
645
|
return text;
|
|
570
646
|
}
|
|
571
647
|
|
|
572
|
-
// Check cache
|
|
573
648
|
const cached = /** @type {string | undefined} */ (instanceCache.get(text));
|
|
574
649
|
if (cached !== undefined) {
|
|
575
650
|
return cached;
|
|
@@ -597,9 +672,11 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
597
672
|
const loadSvgo = getSvgo;
|
|
598
673
|
const svgCache = svgMinifyCache;
|
|
599
674
|
|
|
600
|
-
|
|
675
|
+
// A config without `plugins` of its own keeps the plugins fit for inline SVG
|
|
676
|
+
const svgoConfig = typeof option === 'object' ? /** @type {{plugins?: unknown[]}} */ (option) : {};
|
|
677
|
+
const svgoOptions = { ...svgoConfig, plugins: svgoConfig.plugins ?? svgoPluginsInline };
|
|
601
678
|
|
|
602
|
-
//
|
|
679
|
+
// Option signature for cache keys
|
|
603
680
|
const svgSig = stableStringify({
|
|
604
681
|
...svgoOptions,
|
|
605
682
|
cont: !!options.continueOnMinifyError
|
|
@@ -610,11 +687,20 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
610
687
|
return svgContent;
|
|
611
688
|
}
|
|
612
689
|
|
|
690
|
+
if (declinesMinify(options, 'canMinifySVG', svgContent)) {
|
|
691
|
+
return svgContent;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// SVGO needs every attribute to carry a value, but its output is HTML again,
|
|
695
|
+
// where an empty value can go; it escapes quotes in values and text, so `=""`
|
|
696
|
+
// is always one. Read here, as options are processed in the order they are given.
|
|
697
|
+
const collapseEmpty = Boolean(options.collapseEmptyAttributes);
|
|
698
|
+
|
|
613
699
|
const isCacheable = svgContent.length <= MAX_CACHE_ENTRY_SIZE;
|
|
614
700
|
const svgKey = isCacheable
|
|
615
701
|
? (svgContent.length > 2048
|
|
616
702
|
? (hashContent(svgContent) + '|' + svgSig)
|
|
617
|
-
: (svgContent + '|' + svgSig))
|
|
703
|
+
: (svgContent + '|' + svgSig)) + (collapseEmpty ? '|c' : '')
|
|
618
704
|
: undefined;
|
|
619
705
|
|
|
620
706
|
try {
|
|
@@ -628,7 +714,7 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
628
714
|
const inFlight = (async () => {
|
|
629
715
|
const optimize = await loadSvgo();
|
|
630
716
|
const result = optimize(svgContent, svgoOptions);
|
|
631
|
-
return result.data;
|
|
717
|
+
return collapseEmpty ? result.data.replace(RE_SVGO_EMPTY_ATTRIBUTE, ' $1') : result.data;
|
|
632
718
|
})();
|
|
633
719
|
|
|
634
720
|
if (svgKey !== undefined) svgCache.set(svgKey, inFlight);
|
|
@@ -677,13 +763,10 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
677
763
|
}
|
|
678
764
|
}
|
|
679
765
|
|
|
680
|
-
//
|
|
681
|
-
//
|
|
682
|
-
//
|
|
683
|
-
//
|
|
684
|
-
// `[\s\S]*?` up to a literal terminator is linear and passes; HMN’s default
|
|
685
|
-
// fragments have exactly that shape, so the check flagging it would mean
|
|
686
|
-
// warning about the defaults themselves.
|
|
766
|
+
// Quantifiers or alternation compounded under unbounded repetition backtrack catastrophically,
|
|
767
|
+
// and a linear scan cannot stand in for them; patterns too long or too deeply nested to read are
|
|
768
|
+
// refused for that alone. A lone `[\s\S]*?` up to a literal terminator is linear and passes, as
|
|
769
|
+
// HMN’s default fragments have exactly that shape.
|
|
687
770
|
for (const re of options.ignoreCustomFragments || []) {
|
|
688
771
|
const risk = describeQuantifierRisk(re);
|
|
689
772
|
if (!risk) continue;
|
|
@@ -697,11 +780,9 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
697
780
|
}
|
|
698
781
|
}
|
|
699
782
|
|
|
700
|
-
// Options that
|
|
701
|
-
//
|
|
702
|
-
//
|
|
703
|
-
// for it; the option it needs is read from the settled options, so that a minifier
|
|
704
|
-
// asked for but not loadable counts as absent rather than as present.
|
|
783
|
+
// Options that do nothing without the option they build on say so. The option is read from
|
|
784
|
+
// `effectiveInput` (user options over preset), so a default filled in above doesn’t count as
|
|
785
|
+
// asked for; the one it needs comes from the settled options, so an unloadable minifier is absent
|
|
705
786
|
const requestedOptions = /** @type {Record<string, any>} */ (effectiveInput);
|
|
706
787
|
const settledOptions = /** @type {Record<string, any>} */ (options);
|
|
707
788
|
for (const { option, requires, unusable, clear } of optionDependencies) {
|
|
@@ -721,10 +802,9 @@ const processOptions = (inputOptions, { getLightningCSS, getTerser, getSwc, getS
|
|
|
721
802
|
return options;
|
|
722
803
|
};
|
|
723
804
|
|
|
724
|
-
// Exports
|
|
725
|
-
|
|
726
805
|
export {
|
|
727
806
|
optionDependencies,
|
|
807
|
+
optionKeysExtra,
|
|
728
808
|
shouldMinifyInnerHTML,
|
|
729
809
|
processOptions
|
|
730
810
|
};
|