htmlnano 2.1.2 → 2.1.4
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 +54 -12
- package/dist/_modules/collapseAttributeWhitespace.d.mts +57 -0
- package/dist/_modules/collapseAttributeWhitespace.d.ts +57 -0
- package/dist/_modules/collapseAttributeWhitespace.js +296 -0
- package/dist/_modules/collapseAttributeWhitespace.mjs +293 -0
- package/dist/_modules/collapseBooleanAttributes.d.mts +60 -0
- package/dist/_modules/collapseBooleanAttributes.d.ts +60 -0
- package/dist/_modules/collapseBooleanAttributes.js +159 -0
- package/{lib/modules → dist/_modules}/collapseBooleanAttributes.mjs +39 -57
- package/dist/_modules/collapseWhitespace.d.mts +57 -0
- package/dist/_modules/collapseWhitespace.d.ts +57 -0
- package/dist/_modules/collapseWhitespace.js +172 -0
- package/dist/_modules/collapseWhitespace.mjs +170 -0
- package/dist/_modules/custom.d.mts +57 -0
- package/dist/_modules/custom.d.ts +57 -0
- package/dist/_modules/custom.js +22 -0
- package/dist/_modules/custom.mjs +20 -0
- package/dist/_modules/deduplicateAttributeValues.d.mts +56 -0
- package/dist/_modules/deduplicateAttributeValues.d.ts +56 -0
- package/dist/_modules/deduplicateAttributeValues.js +38 -0
- package/dist/_modules/deduplicateAttributeValues.mjs +36 -0
- package/dist/_modules/example.d.mts +59 -0
- package/dist/_modules/example.d.ts +59 -0
- package/dist/_modules/example.js +67 -0
- package/dist/_modules/example.mjs +65 -0
- package/dist/_modules/mergeScripts.d.mts +56 -0
- package/dist/_modules/mergeScripts.d.ts +56 -0
- package/dist/_modules/mergeScripts.js +53 -0
- package/dist/_modules/mergeScripts.mjs +51 -0
- package/dist/_modules/mergeStyles.d.mts +56 -0
- package/dist/_modules/mergeStyles.d.ts +56 -0
- package/dist/_modules/mergeStyles.js +42 -0
- package/dist/_modules/mergeStyles.mjs +40 -0
- package/dist/_modules/minifyConditionalComments.d.mts +56 -0
- package/dist/_modules/minifyConditionalComments.d.ts +56 -0
- package/dist/_modules/minifyConditionalComments.js +54 -0
- package/{lib/modules → dist/_modules}/minifyConditionalComments.mjs +21 -22
- package/dist/_modules/minifyCss.d.mts +56 -0
- package/dist/_modules/minifyCss.d.ts +56 -0
- package/dist/_modules/minifyCss.js +84 -0
- package/dist/_modules/minifyCss.mjs +82 -0
- package/dist/_modules/minifyJs.d.mts +56 -0
- package/dist/_modules/minifyJs.d.ts +56 -0
- package/dist/_modules/minifyJs.js +108 -0
- package/dist/_modules/minifyJs.mjs +106 -0
- package/dist/_modules/minifyJson.d.mts +56 -0
- package/dist/_modules/minifyJson.d.ts +56 -0
- package/dist/_modules/minifyJson.js +35 -0
- package/dist/_modules/minifyJson.mjs +33 -0
- package/dist/_modules/minifySvg.d.mts +56 -0
- package/dist/_modules/minifySvg.d.ts +56 -0
- package/dist/_modules/minifySvg.js +40 -0
- package/dist/_modules/minifySvg.mjs +38 -0
- package/dist/_modules/minifyUrls.d.mts +56 -0
- package/dist/_modules/minifyUrls.d.ts +56 -0
- package/dist/_modules/minifyUrls.js +180 -0
- package/dist/_modules/minifyUrls.mjs +178 -0
- package/dist/_modules/normalizeAttributeValues.d.mts +56 -0
- package/dist/_modules/normalizeAttributeValues.d.ts +56 -0
- package/dist/_modules/normalizeAttributeValues.js +234 -0
- package/dist/_modules/normalizeAttributeValues.mjs +232 -0
- package/dist/_modules/removeAttributeQuotes.d.mts +56 -0
- package/dist/_modules/removeAttributeQuotes.d.ts +56 -0
- package/dist/_modules/removeAttributeQuotes.js +15 -0
- package/dist/_modules/removeAttributeQuotes.mjs +13 -0
- package/dist/_modules/removeComments.d.mts +58 -0
- package/dist/_modules/removeComments.d.ts +58 -0
- package/dist/_modules/removeComments.js +83 -0
- package/{lib/modules → dist/_modules}/removeComments.mjs +24 -35
- package/dist/_modules/removeEmptyAttributes.d.mts +56 -0
- package/dist/_modules/removeEmptyAttributes.d.ts +56 -0
- package/dist/_modules/removeEmptyAttributes.js +197 -0
- package/dist/_modules/removeEmptyAttributes.mjs +195 -0
- package/dist/_modules/removeOptionalTags.d.mts +56 -0
- package/dist/_modules/removeOptionalTags.d.ts +56 -0
- package/dist/_modules/removeOptionalTags.js +190 -0
- package/{lib/modules → dist/_modules}/removeOptionalTags.mjs +54 -91
- package/dist/_modules/removeRedundantAttributes.d.mts +57 -0
- package/dist/_modules/removeRedundantAttributes.d.ts +57 -0
- package/dist/_modules/removeRedundantAttributes.js +128 -0
- package/{lib/modules → dist/_modules}/removeRedundantAttributes.mjs +43 -59
- package/dist/_modules/removeUnusedCss.d.mts +60 -0
- package/dist/_modules/removeUnusedCss.d.ts +60 -0
- package/dist/_modules/removeUnusedCss.js +134 -0
- package/dist/_modules/removeUnusedCss.mjs +132 -0
- package/dist/_modules/sortAttributes.d.mts +57 -0
- package/dist/_modules/sortAttributes.d.ts +57 -0
- package/dist/_modules/sortAttributes.js +102 -0
- package/{lib/modules → dist/_modules}/sortAttributes.mjs +39 -58
- package/dist/_modules/sortAttributesWithLists.d.mts +56 -0
- package/dist/_modules/sortAttributesWithLists.d.ts +56 -0
- package/dist/_modules/sortAttributesWithLists.js +118 -0
- package/{lib/modules → dist/_modules}/sortAttributesWithLists.mjs +41 -59
- package/dist/helpers.d.ts +16 -0
- package/dist/helpers.js +72 -0
- package/dist/helpers.mjs +63 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +223 -0
- package/dist/index.mjs +209 -0
- package/dist/presets/ampSafe.d.ts +47 -0
- package/dist/presets/ampSafe.js +19 -0
- package/{lib → dist}/presets/ampSafe.mjs +6 -4
- package/dist/presets/max.d.ts +47 -0
- package/dist/presets/max.js +28 -0
- package/{lib → dist}/presets/max.mjs +6 -4
- package/dist/presets/safe.d.ts +47 -0
- package/dist/presets/safe.js +60 -0
- package/{lib → dist}/presets/safe.mjs +13 -20
- package/package.json +53 -56
- package/.eslintignore +0 -3
- package/CHANGELOG.md +0 -409
- package/docs/README.md +0 -33
- package/docs/babel.config.js +0 -3
- package/docs/docs/010-introduction.md +0 -22
- package/docs/docs/020-usage.md +0 -117
- package/docs/docs/030-config.md +0 -21
- package/docs/docs/040-presets.md +0 -75
- package/docs/docs/050-modules.md +0 -855
- package/docs/docs/060-contribute.md +0 -16
- package/docs/docusaurus.config.js +0 -65
- package/docs/netlify.toml +0 -4
- package/docs/package-lock.json +0 -21630
- package/docs/package.json +0 -40
- package/docs/sidebars.js +0 -26
- package/docs/versioned_docs/version-1.1.1/010-introduction.md +0 -22
- package/docs/versioned_docs/version-1.1.1/020-usage.md +0 -77
- package/docs/versioned_docs/version-1.1.1/030-config.md +0 -21
- package/docs/versioned_docs/version-1.1.1/040-presets.md +0 -75
- package/docs/versioned_docs/version-1.1.1/050-modules.md +0 -785
- package/docs/versioned_docs/version-1.1.1/060-contribute.md +0 -16
- package/docs/versioned_docs/version-2.0.0/010-introduction.md +0 -22
- package/docs/versioned_docs/version-2.0.0/020-usage.md +0 -77
- package/docs/versioned_docs/version-2.0.0/030-config.md +0 -21
- package/docs/versioned_docs/version-2.0.0/040-presets.md +0 -75
- package/docs/versioned_docs/version-2.0.0/050-modules.md +0 -838
- package/docs/versioned_docs/version-2.0.0/060-contribute.md +0 -16
- package/docs/versioned_sidebars/version-1.1.1-sidebars.json +0 -8
- package/docs/versioned_sidebars/version-2.0.0-sidebars.json +0 -8
- package/docs/versions.json +0 -4
- package/index.cjs +0 -11
- package/index.d.cts +0 -3
- package/index.d.mts +0 -3
- package/index.d.ts +0 -94
- package/index.mjs +0 -2
- package/lib/helpers.cjs +0 -79
- package/lib/helpers.mjs +0 -53
- package/lib/htmlnano.cjs +0 -202
- package/lib/htmlnano.mjs +0 -198
- package/lib/modules/collapseAttributeWhitespace.cjs +0 -86
- package/lib/modules/collapseAttributeWhitespace.mjs +0 -104
- package/lib/modules/collapseBooleanAttributes.cjs +0 -62
- package/lib/modules/collapseWhitespace.cjs +0 -100
- package/lib/modules/collapseWhitespace.mjs +0 -132
- package/lib/modules/custom.cjs +0 -19
- package/lib/modules/custom.mjs +0 -16
- package/lib/modules/deduplicateAttributeValues.cjs +0 -38
- package/lib/modules/deduplicateAttributeValues.mjs +0 -40
- package/lib/modules/example.cjs +0 -85
- package/lib/modules/example.mjs +0 -75
- package/lib/modules/mergeScripts.cjs +0 -54
- package/lib/modules/mergeScripts.mjs +0 -56
- package/lib/modules/mergeStyles.cjs +0 -38
- package/lib/modules/mergeStyles.mjs +0 -36
- package/lib/modules/minifyConditionalComments.cjs +0 -47
- package/lib/modules/minifyCss.cjs +0 -73
- package/lib/modules/minifyCss.mjs +0 -88
- package/lib/modules/minifyJs.cjs +0 -103
- package/lib/modules/minifyJs.mjs +0 -121
- package/lib/modules/minifyJson.cjs +0 -24
- package/lib/modules/minifyJson.mjs +0 -21
- package/lib/modules/minifySvg.cjs +0 -37
- package/lib/modules/minifySvg.mjs +0 -30
- package/lib/modules/minifyUrls.cjs +0 -141
- package/lib/modules/minifyUrls.mjs +0 -229
- package/lib/modules/normalizeAttributeValues.cjs +0 -120
- package/lib/modules/normalizeAttributeValues.mjs +0 -140
- package/lib/modules/removeAttributeQuotes.cjs +0 -17
- package/lib/modules/removeAttributeQuotes.mjs +0 -12
- package/lib/modules/removeComments.cjs +0 -86
- package/lib/modules/removeEmptyAttributes.cjs +0 -72
- package/lib/modules/removeEmptyAttributes.mjs +0 -121
- package/lib/modules/removeOptionalTags.cjs +0 -183
- package/lib/modules/removeRedundantAttributes.cjs +0 -112
- package/lib/modules/removeUnusedCss.cjs +0 -113
- package/lib/modules/removeUnusedCss.mjs +0 -122
- package/lib/modules/sortAttributes.cjs +0 -98
- package/lib/modules/sortAttributesWithLists.cjs +0 -114
- package/lib/presets/ampSafe.cjs +0 -18
- package/lib/presets/max.cjs +0 -27
- package/lib/presets/safe.cjs +0 -65
- package/test.js +0 -23
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Contribute
|
|
2
|
-
|
|
3
|
-
Since the minifier is modular, it's very easy to add new modules:
|
|
4
|
-
|
|
5
|
-
1. Create a ES6-file inside `lib/modules/` with a function that does some minification. For example you can check [`lib/modules/example.mjs`](https://github.com/posthtml/htmlnano/blob/master/lib/modules/example.mjs).
|
|
6
|
-
|
|
7
|
-
2. Add the module's name into one of those [presets](https://github.com/posthtml/htmlnano/tree/master/lib/presets). You can choose either `ampSafe`, `max`, or `safe`.
|
|
8
|
-
|
|
9
|
-
3. Create a JS-file inside `test/modules/` with some unit-tests.
|
|
10
|
-
|
|
11
|
-
4. Describe your module in the section "[Modules](https://github.com/posthtml/htmlnano/blob/master/README.md#modules)".
|
|
12
|
-
|
|
13
|
-
5. Send me a pull request.
|
|
14
|
-
|
|
15
|
-
Other types of contribution (bug fixes, documentation improves, etc) are also welcome!
|
|
16
|
-
Would like to contribute, but don't have any ideas what to do? Check out [our issues](https://github.com/posthtml/htmlnano/labels/help%20wanted).
|
package/docs/versions.json
DELETED
package/index.cjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
const htmlnano = require("./lib/htmlnano.cjs").default;
|
|
8
|
-
exports.default = htmlnano;
|
|
9
|
-
|
|
10
|
-
// for backward compatibility with require('htmlnano')
|
|
11
|
-
module.exports = htmlnano;
|
package/index.d.cts
DELETED
package/index.d.mts
DELETED
package/index.d.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import type PostHTML from "posthtml";
|
|
2
|
-
import type { MinifyOptions } from "terser";
|
|
3
|
-
import type { Options as CssNanoOptions } from "cssnano";
|
|
4
|
-
import type { Config as SvgoOptimizeOptions } from "svgo";
|
|
5
|
-
|
|
6
|
-
export interface HtmlnanoOptions {
|
|
7
|
-
skipConfigLoading?: boolean;
|
|
8
|
-
skipInternalWarnings?: boolean;
|
|
9
|
-
collapseAttributeWhitespace?: boolean;
|
|
10
|
-
collapseBooleanAttributes?: {
|
|
11
|
-
amphtml?: boolean;
|
|
12
|
-
};
|
|
13
|
-
collapseWhitespace?: "conservative" | "all" | "aggressive";
|
|
14
|
-
custom?: (tree: PostHTML.Node, options?: any) => PostHTML.Node;
|
|
15
|
-
deduplicateAttributeValues?: boolean;
|
|
16
|
-
minifyUrls?: URL | string | false;
|
|
17
|
-
mergeStyles?: boolean;
|
|
18
|
-
mergeScripts?: boolean;
|
|
19
|
-
minifyCss?: CssNanoOptions | boolean;
|
|
20
|
-
minifyConditionalComments?: boolean;
|
|
21
|
-
minifyJs?: MinifyOptions | boolean;
|
|
22
|
-
minifyJson?: boolean;
|
|
23
|
-
minifySvg?: SvgoOptimizeOptions | boolean;
|
|
24
|
-
normalizeAttributeValues?: boolean;
|
|
25
|
-
removeAttributeQuotes?: boolean;
|
|
26
|
-
removeComments?: boolean | "safe" | "all" | RegExp | ((comment: string) => boolean);
|
|
27
|
-
removeEmptyAttributes?: boolean;
|
|
28
|
-
removeRedundantAttributes?: boolean;
|
|
29
|
-
removeOptionalTags?: boolean;
|
|
30
|
-
removeUnusedCss?: boolean;
|
|
31
|
-
sortAttributes?: boolean | "alphabetical" | "frequency";
|
|
32
|
-
sortAttributesWithLists?: boolean | "alphabetical" | "frequency";
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface HtmlnanoPreset extends Omit<HtmlnanoOptions, "skipConfigLoading"> {}
|
|
36
|
-
|
|
37
|
-
export interface Presets {
|
|
38
|
-
safe: HtmlnanoPreset;
|
|
39
|
-
ampSafe: HtmlnanoPreset;
|
|
40
|
-
max: HtmlnanoPreset;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
type Preset = Presets[keyof Presets];
|
|
44
|
-
|
|
45
|
-
export function loadConfig(
|
|
46
|
-
options?: HtmlnanoOptions,
|
|
47
|
-
preset?: Preset,
|
|
48
|
-
configPath?: string
|
|
49
|
-
): [HtmlnanoOptions | {}, Preset];
|
|
50
|
-
|
|
51
|
-
declare function htmlnano<TMessage>(
|
|
52
|
-
options?: HtmlnanoOptions,
|
|
53
|
-
preset?: Preset
|
|
54
|
-
): (tree: PostHTML.Node) => Promise<PostHTML.Result<TMessage>>;
|
|
55
|
-
|
|
56
|
-
interface PostHtmlOptions {
|
|
57
|
-
directives?: Array<{
|
|
58
|
-
name: string | RegExp;
|
|
59
|
-
start: string;
|
|
60
|
-
end: string;
|
|
61
|
-
}>;
|
|
62
|
-
sourceLocations?: boolean;
|
|
63
|
-
recognizeNoValueAttribute?: boolean;
|
|
64
|
-
xmlMode?: boolean;
|
|
65
|
-
decodeEntities?: boolean;
|
|
66
|
-
lowerCaseTags?: boolean;
|
|
67
|
-
lowerCaseAttributeNames?: boolean;
|
|
68
|
-
recognizeCDATA?: boolean;
|
|
69
|
-
recognizeSelfClosing?: boolean;
|
|
70
|
-
Tokenizer?: any;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
declare namespace htmlnano {
|
|
74
|
-
export function process<TMessage>(
|
|
75
|
-
html: string,
|
|
76
|
-
options?: HtmlnanoOptions,
|
|
77
|
-
preset?: Preset,
|
|
78
|
-
postHtmlOptions?: PostHtmlOptions
|
|
79
|
-
): Promise<PostHTML.Result<TMessage>>;
|
|
80
|
-
|
|
81
|
-
export function getRequiredOptionalDependencies(
|
|
82
|
-
optionsRun?: HtmlnanoOptions,
|
|
83
|
-
presetRun?: Preset
|
|
84
|
-
): string[];
|
|
85
|
-
|
|
86
|
-
export function htmlMinimizerWebpackPluginMinify(
|
|
87
|
-
input: { [file: string]: string },
|
|
88
|
-
minimizerOptions?: HtmlnanoOptions
|
|
89
|
-
): any;
|
|
90
|
-
|
|
91
|
-
export const presets: Presets;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export default htmlnano;
|
package/index.mjs
DELETED
package/lib/helpers.cjs
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.extractCssFromStyleNode = extractCssFromStyleNode;
|
|
7
|
-
exports.isAmpBoilerplate = isAmpBoilerplate;
|
|
8
|
-
exports.isComment = isComment;
|
|
9
|
-
exports.isConditionalComment = isConditionalComment;
|
|
10
|
-
exports.isEventHandler = isEventHandler;
|
|
11
|
-
exports.isStyleNode = isStyleNode;
|
|
12
|
-
exports.optionalImport = optionalImport;
|
|
13
|
-
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
14
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
15
|
-
function __transformExtension(filepath, extMapping) {
|
|
16
|
-
if (!filepath.startsWith('./') && !filepath.startsWith('../')) {
|
|
17
|
-
// Package import
|
|
18
|
-
return filepath;
|
|
19
|
-
}
|
|
20
|
-
const idx = filepath.lastIndexOf('.');
|
|
21
|
-
if (idx === -1 || filepath.includes('/', idx)) {
|
|
22
|
-
// No extension
|
|
23
|
-
const newExt = extMapping[''];
|
|
24
|
-
if (newExt) {
|
|
25
|
-
return filepath + newExt;
|
|
26
|
-
}
|
|
27
|
-
return filepath;
|
|
28
|
-
}
|
|
29
|
-
for (let [origExt, newExt] of Object.entries(extMapping).sort((a, b) => b[0].length - a[0].length)) {
|
|
30
|
-
if (filepath.endsWith(origExt)) {
|
|
31
|
-
return filepath.slice(0, -origExt.length) + newExt;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return filepath;
|
|
35
|
-
}
|
|
36
|
-
const ampBoilerplateAttributes = ['amp-boilerplate', 'amp4ads-boilerplate', 'amp4email-boilerplate'];
|
|
37
|
-
function isAmpBoilerplate(node) {
|
|
38
|
-
if (!node.attrs) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
for (const attr of ampBoilerplateAttributes) {
|
|
42
|
-
if (attr in node.attrs) {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
function isComment(content) {
|
|
49
|
-
if (typeof content === 'string') {
|
|
50
|
-
return content.trim().startsWith('<!--');
|
|
51
|
-
}
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
function isConditionalComment(content) {
|
|
55
|
-
const clean = (content || '').trim();
|
|
56
|
-
return clean.startsWith('<!--[if') || clean === '<!--<![endif]-->';
|
|
57
|
-
}
|
|
58
|
-
function isStyleNode(node) {
|
|
59
|
-
return node.tag === 'style' && !isAmpBoilerplate(node) && 'content' in node && node.content.length > 0;
|
|
60
|
-
}
|
|
61
|
-
function extractCssFromStyleNode(node) {
|
|
62
|
-
return Array.isArray(node.content) ? node.content.join(' ') : node.content;
|
|
63
|
-
}
|
|
64
|
-
function isEventHandler(attributeName) {
|
|
65
|
-
return attributeName && attributeName.slice && attributeName.slice(0, 2).toLowerCase() === 'on' && attributeName.length >= 5;
|
|
66
|
-
}
|
|
67
|
-
async function optionalImport(moduleName) {
|
|
68
|
-
try {
|
|
69
|
-
const module = await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(__transformExtension(moduleName, {
|
|
70
|
-
".mjs": ".cjs"
|
|
71
|
-
}));
|
|
72
|
-
return module.default || module;
|
|
73
|
-
} catch (e) {
|
|
74
|
-
if (e.code === 'MODULE_NOT_FOUND' || e.code === 'ERR_MODULE_NOT_FOUND') {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
throw e;
|
|
78
|
-
}
|
|
79
|
-
}
|
package/lib/helpers.mjs
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
const ampBoilerplateAttributes = [
|
|
2
|
-
'amp-boilerplate',
|
|
3
|
-
'amp4ads-boilerplate',
|
|
4
|
-
'amp4email-boilerplate'
|
|
5
|
-
];
|
|
6
|
-
|
|
7
|
-
export function isAmpBoilerplate(node) {
|
|
8
|
-
if (!node.attrs) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
for (const attr of ampBoilerplateAttributes) {
|
|
12
|
-
if (attr in node.attrs) {
|
|
13
|
-
return true;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isComment(content) {
|
|
20
|
-
if (typeof content === 'string') {
|
|
21
|
-
return content.trim().startsWith('<!--');
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function isConditionalComment(content) {
|
|
27
|
-
const clean = (content || '').trim();
|
|
28
|
-
return clean.startsWith('<!--[if') || clean === '<!--<![endif]-->';
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function isStyleNode(node) {
|
|
32
|
-
return node.tag === 'style' && !isAmpBoilerplate(node) && 'content' in node && node.content.length > 0;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function extractCssFromStyleNode(node) {
|
|
36
|
-
return Array.isArray(node.content) ? node.content.join(' ') : node.content;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function isEventHandler(attributeName) {
|
|
40
|
-
return attributeName && attributeName.slice && attributeName.slice(0, 2).toLowerCase() === 'on' && attributeName.length >= 5;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export async function optionalImport(moduleName) {
|
|
44
|
-
try {
|
|
45
|
-
const module = await import(moduleName);
|
|
46
|
-
return module.default || module;
|
|
47
|
-
} catch (e) {
|
|
48
|
-
if (e.code === 'MODULE_NOT_FOUND' || e.code === 'ERR_MODULE_NOT_FOUND') {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
throw e;
|
|
52
|
-
}
|
|
53
|
-
}
|
package/lib/htmlnano.cjs
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
exports.loadConfig = loadConfig;
|
|
8
|
-
var _posthtml = _interopRequireDefault(require("posthtml"));
|
|
9
|
-
var _cosmiconfig = require("cosmiconfig");
|
|
10
|
-
var _safe = _interopRequireDefault(require("./presets/safe.cjs"));
|
|
11
|
-
var _ampSafe = _interopRequireDefault(require("./presets/ampSafe.cjs"));
|
|
12
|
-
var _max = _interopRequireDefault(require("./presets/max.cjs"));
|
|
13
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
-
function __transformExtension(filepath, extMapping) {
|
|
15
|
-
if (!filepath.startsWith('./') && !filepath.startsWith('../')) {
|
|
16
|
-
// Package import
|
|
17
|
-
return filepath;
|
|
18
|
-
}
|
|
19
|
-
const idx = filepath.lastIndexOf('.');
|
|
20
|
-
if (idx === -1 || filepath.includes('/', idx)) {
|
|
21
|
-
// No extension
|
|
22
|
-
const newExt = extMapping[''];
|
|
23
|
-
if (newExt) {
|
|
24
|
-
return filepath + newExt;
|
|
25
|
-
}
|
|
26
|
-
return filepath;
|
|
27
|
-
}
|
|
28
|
-
for (let [origExt, newExt] of Object.entries(extMapping).sort((a, b) => b[0].length - a[0].length)) {
|
|
29
|
-
if (filepath.endsWith(origExt)) {
|
|
30
|
-
return filepath.slice(0, -origExt.length) + newExt;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return filepath;
|
|
34
|
-
}
|
|
35
|
-
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
36
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
37
|
-
const presets = {
|
|
38
|
-
safe: _safe.default,
|
|
39
|
-
ampSafe: _ampSafe.default,
|
|
40
|
-
max: _max.default
|
|
41
|
-
};
|
|
42
|
-
function loadConfig(options, preset, configPath) {
|
|
43
|
-
let {
|
|
44
|
-
skipConfigLoading = false,
|
|
45
|
-
...rest
|
|
46
|
-
} = options || {};
|
|
47
|
-
if (!skipConfigLoading) {
|
|
48
|
-
const explorer = (0, _cosmiconfig.cosmiconfigSync)('htmlnano');
|
|
49
|
-
const rc = configPath ? explorer.load(configPath) : explorer.search();
|
|
50
|
-
if (rc) {
|
|
51
|
-
const {
|
|
52
|
-
preset: presetName
|
|
53
|
-
} = rc.config;
|
|
54
|
-
if (presetName) {
|
|
55
|
-
if (!preset && presets[presetName]) {
|
|
56
|
-
preset = presets[presetName];
|
|
57
|
-
}
|
|
58
|
-
delete rc.config.preset;
|
|
59
|
-
}
|
|
60
|
-
if (!options) {
|
|
61
|
-
rest = rc.config;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return [rest || {}, preset || _safe.default];
|
|
66
|
-
}
|
|
67
|
-
const optionalDependencies = {
|
|
68
|
-
minifyCss: ['cssnano', 'postcss'],
|
|
69
|
-
minifyJs: ['terser'],
|
|
70
|
-
minifyUrl: ['relateurl', 'srcset', 'terser'],
|
|
71
|
-
minifySvg: ['svgo']
|
|
72
|
-
};
|
|
73
|
-
const modules = {
|
|
74
|
-
collapseAttributeWhitespace: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/collapseAttributeWhitespace.cjs"))),
|
|
75
|
-
collapseBooleanAttributes: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/collapseBooleanAttributes.cjs"))),
|
|
76
|
-
collapseWhitespace: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/collapseWhitespace.cjs"))),
|
|
77
|
-
custom: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/custom.cjs"))),
|
|
78
|
-
deduplicateAttributeValues: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/deduplicateAttributeValues.cjs"))),
|
|
79
|
-
example: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/example.cjs"))),
|
|
80
|
-
mergeScripts: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/mergeScripts.cjs"))),
|
|
81
|
-
mergeStyles: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/mergeStyles.cjs"))),
|
|
82
|
-
minifyConditionalComments: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/minifyConditionalComments.cjs"))),
|
|
83
|
-
minifyCss: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/minifyCss.cjs"))),
|
|
84
|
-
minifyJs: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/minifyJs.cjs"))),
|
|
85
|
-
minifyJson: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/minifyJson.cjs"))),
|
|
86
|
-
minifySvg: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/minifySvg.cjs"))),
|
|
87
|
-
minifyUrls: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/minifyUrls.cjs"))),
|
|
88
|
-
normalizeAttributeValues: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/normalizeAttributeValues.cjs"))),
|
|
89
|
-
removeAttributeQuotes: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/removeAttributeQuotes.cjs"))),
|
|
90
|
-
removeComments: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/removeComments.cjs"))),
|
|
91
|
-
removeEmptyAttributes: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/removeEmptyAttributes.cjs"))),
|
|
92
|
-
removeOptionalTags: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/removeOptionalTags.cjs"))),
|
|
93
|
-
removeRedundantAttributes: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/removeRedundantAttributes.cjs"))),
|
|
94
|
-
removeUnusedCss: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/removeUnusedCss.cjs"))),
|
|
95
|
-
sortAttributes: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/sortAttributes.cjs"))),
|
|
96
|
-
sortAttributesWithLists: () => Promise.resolve().then(() => _interopRequireWildcard(require("./modules/sortAttributesWithLists.cjs")))
|
|
97
|
-
};
|
|
98
|
-
function htmlnano(optionsRun, presetRun) {
|
|
99
|
-
let [options, preset] = loadConfig(optionsRun, presetRun);
|
|
100
|
-
return async function minifier(tree) {
|
|
101
|
-
const nodeHandlers = [];
|
|
102
|
-
const attrsHandlers = [];
|
|
103
|
-
const contentsHandlers = [];
|
|
104
|
-
options = {
|
|
105
|
-
...preset,
|
|
106
|
-
...options
|
|
107
|
-
};
|
|
108
|
-
let promise = Promise.resolve(tree);
|
|
109
|
-
for (const [moduleName, moduleOptions] of Object.entries(options)) {
|
|
110
|
-
if (!moduleOptions) {
|
|
111
|
-
// The module is disabled
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
if (_safe.default[moduleName] === undefined) {
|
|
115
|
-
throw new Error('Module "' + moduleName + '" is not defined');
|
|
116
|
-
}
|
|
117
|
-
(optionalDependencies[moduleName] || []).forEach(async dependency => {
|
|
118
|
-
try {
|
|
119
|
-
await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(__transformExtension(dependency, {
|
|
120
|
-
".mjs": ".cjs"
|
|
121
|
-
}));
|
|
122
|
-
} catch (e) {
|
|
123
|
-
if (e.code === 'MODULE_NOT_FOUND' || e.code === 'ERR_MODULE_NOT_FOUND') {
|
|
124
|
-
if (!options.skipInternalWarnings) {
|
|
125
|
-
console.warn(`You have to install "${dependency}" in order to use htmlnano's "${moduleName}" module`);
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
throw e;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
const module = moduleName in modules ? await modules[moduleName]() : await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(__transformExtension(`./modules/${moduleName}.mjs`, {
|
|
133
|
-
".mjs": ".cjs"
|
|
134
|
-
}));
|
|
135
|
-
if (typeof module.onAttrs === 'function') {
|
|
136
|
-
attrsHandlers.push(module.onAttrs(options, moduleOptions));
|
|
137
|
-
}
|
|
138
|
-
if (typeof module.onContent === 'function') {
|
|
139
|
-
contentsHandlers.push(module.onContent(options, moduleOptions));
|
|
140
|
-
}
|
|
141
|
-
if (typeof module.onNode === 'function') {
|
|
142
|
-
nodeHandlers.push(module.onNode(options, moduleOptions));
|
|
143
|
-
}
|
|
144
|
-
if (typeof module.default === 'function') {
|
|
145
|
-
promise = promise.then(async tree => await module.default(tree, options, moduleOptions));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (attrsHandlers.length + contentsHandlers.length + nodeHandlers.length === 0) {
|
|
149
|
-
return promise;
|
|
150
|
-
}
|
|
151
|
-
return promise.then(tree => {
|
|
152
|
-
tree.walk(node => {
|
|
153
|
-
if (node) {
|
|
154
|
-
if (node.attrs && typeof node.attrs === 'object') {
|
|
155
|
-
// Convert all attrs' key to lower case
|
|
156
|
-
let newAttrsObj = {};
|
|
157
|
-
Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
|
|
158
|
-
newAttrsObj[attrName.toLowerCase()] = attrValue;
|
|
159
|
-
});
|
|
160
|
-
for (const handler of attrsHandlers) {
|
|
161
|
-
newAttrsObj = handler(newAttrsObj, node);
|
|
162
|
-
}
|
|
163
|
-
node.attrs = newAttrsObj;
|
|
164
|
-
}
|
|
165
|
-
if (node.content) {
|
|
166
|
-
node.content = typeof node.content === 'string' ? [node.content] : node.content;
|
|
167
|
-
if (Array.isArray(node.content) && node.content.length > 0) {
|
|
168
|
-
for (const handler of contentsHandlers) {
|
|
169
|
-
const result = handler(node.content, node);
|
|
170
|
-
node.content = typeof result === 'string' ? [result] : result;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
for (const handler of nodeHandlers) {
|
|
175
|
-
node = handler(node);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return node;
|
|
179
|
-
});
|
|
180
|
-
return tree;
|
|
181
|
-
});
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
htmlnano.getRequiredOptionalDependencies = function (optionsRun, presetRun) {
|
|
185
|
-
const [options] = loadConfig(optionsRun, presetRun);
|
|
186
|
-
return [...new Set(Object.keys(options).filter(moduleName => options[moduleName]).map(moduleName => optionalDependencies[moduleName]).flat())];
|
|
187
|
-
};
|
|
188
|
-
htmlnano.process = function (html, options, preset, postHtmlOptions) {
|
|
189
|
-
return (0, _posthtml.default)([htmlnano(options, preset)]).process(html, postHtmlOptions);
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
// https://github.com/webpack-contrib/html-minimizer-webpack-plugin/blob/faca00f2219514bc671c5942685721f0b5dbaa70/src/utils.js#L74
|
|
193
|
-
htmlnano.htmlMinimizerWebpackPluginMinify = function htmlNano(input, minimizerOptions = {}) {
|
|
194
|
-
const [[, code]] = Object.entries(input);
|
|
195
|
-
return htmlnano.process(code, minimizerOptions, presets.safe).then(result => {
|
|
196
|
-
return {
|
|
197
|
-
code: result.html
|
|
198
|
-
};
|
|
199
|
-
});
|
|
200
|
-
};
|
|
201
|
-
htmlnano.presets = presets;
|
|
202
|
-
var _default = exports.default = htmlnano;
|