htmlnano 2.1.1 → 2.1.3

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.
Files changed (186) hide show
  1. package/README.md +53 -12
  2. package/dist/_modules/collapseAttributeWhitespace.d.mts +57 -0
  3. package/dist/_modules/collapseAttributeWhitespace.d.ts +57 -0
  4. package/dist/_modules/collapseAttributeWhitespace.js +296 -0
  5. package/dist/_modules/collapseAttributeWhitespace.mjs +293 -0
  6. package/dist/_modules/collapseBooleanAttributes.d.mts +60 -0
  7. package/dist/_modules/collapseBooleanAttributes.d.ts +60 -0
  8. package/dist/_modules/collapseBooleanAttributes.js +159 -0
  9. package/{lib/modules → dist/_modules}/collapseBooleanAttributes.mjs +39 -57
  10. package/dist/_modules/collapseWhitespace.d.mts +57 -0
  11. package/dist/_modules/collapseWhitespace.d.ts +57 -0
  12. package/dist/_modules/collapseWhitespace.js +172 -0
  13. package/dist/_modules/collapseWhitespace.mjs +170 -0
  14. package/dist/_modules/custom.d.mts +57 -0
  15. package/dist/_modules/custom.d.ts +57 -0
  16. package/dist/_modules/custom.js +22 -0
  17. package/dist/_modules/custom.mjs +20 -0
  18. package/dist/_modules/deduplicateAttributeValues.d.mts +56 -0
  19. package/dist/_modules/deduplicateAttributeValues.d.ts +56 -0
  20. package/dist/_modules/deduplicateAttributeValues.js +38 -0
  21. package/dist/_modules/deduplicateAttributeValues.mjs +36 -0
  22. package/dist/_modules/example.d.mts +59 -0
  23. package/dist/_modules/example.d.ts +59 -0
  24. package/dist/_modules/example.js +67 -0
  25. package/dist/_modules/example.mjs +65 -0
  26. package/dist/_modules/mergeScripts.d.mts +56 -0
  27. package/dist/_modules/mergeScripts.d.ts +56 -0
  28. package/dist/_modules/mergeScripts.js +53 -0
  29. package/dist/_modules/mergeScripts.mjs +51 -0
  30. package/dist/_modules/mergeStyles.d.mts +56 -0
  31. package/dist/_modules/mergeStyles.d.ts +56 -0
  32. package/dist/_modules/mergeStyles.js +42 -0
  33. package/dist/_modules/mergeStyles.mjs +40 -0
  34. package/dist/_modules/minifyConditionalComments.d.mts +56 -0
  35. package/dist/_modules/minifyConditionalComments.d.ts +56 -0
  36. package/dist/_modules/minifyConditionalComments.js +54 -0
  37. package/{lib/modules → dist/_modules}/minifyConditionalComments.mjs +21 -22
  38. package/dist/_modules/minifyCss.d.mts +56 -0
  39. package/dist/_modules/minifyCss.d.ts +56 -0
  40. package/dist/_modules/minifyCss.js +84 -0
  41. package/dist/_modules/minifyCss.mjs +82 -0
  42. package/dist/_modules/minifyJs.d.mts +56 -0
  43. package/dist/_modules/minifyJs.d.ts +56 -0
  44. package/dist/_modules/minifyJs.js +108 -0
  45. package/dist/_modules/minifyJs.mjs +106 -0
  46. package/dist/_modules/minifyJson.d.mts +56 -0
  47. package/dist/_modules/minifyJson.d.ts +56 -0
  48. package/dist/_modules/minifyJson.js +35 -0
  49. package/dist/_modules/minifyJson.mjs +33 -0
  50. package/dist/_modules/minifySvg.d.mts +56 -0
  51. package/dist/_modules/minifySvg.d.ts +56 -0
  52. package/dist/_modules/minifySvg.js +40 -0
  53. package/dist/_modules/minifySvg.mjs +38 -0
  54. package/dist/_modules/minifyUrls.d.mts +56 -0
  55. package/dist/_modules/minifyUrls.d.ts +56 -0
  56. package/dist/_modules/minifyUrls.js +180 -0
  57. package/dist/_modules/minifyUrls.mjs +178 -0
  58. package/dist/_modules/normalizeAttributeValues.d.mts +56 -0
  59. package/dist/_modules/normalizeAttributeValues.d.ts +56 -0
  60. package/dist/_modules/normalizeAttributeValues.js +234 -0
  61. package/dist/_modules/normalizeAttributeValues.mjs +232 -0
  62. package/dist/_modules/removeAttributeQuotes.d.mts +56 -0
  63. package/dist/_modules/removeAttributeQuotes.d.ts +56 -0
  64. package/dist/_modules/removeAttributeQuotes.js +15 -0
  65. package/dist/_modules/removeAttributeQuotes.mjs +13 -0
  66. package/dist/_modules/removeComments.d.mts +58 -0
  67. package/dist/_modules/removeComments.d.ts +58 -0
  68. package/dist/_modules/removeComments.js +83 -0
  69. package/{lib/modules → dist/_modules}/removeComments.mjs +24 -35
  70. package/dist/_modules/removeEmptyAttributes.d.mts +56 -0
  71. package/dist/_modules/removeEmptyAttributes.d.ts +56 -0
  72. package/dist/_modules/removeEmptyAttributes.js +197 -0
  73. package/dist/_modules/removeEmptyAttributes.mjs +195 -0
  74. package/dist/_modules/removeOptionalTags.d.mts +56 -0
  75. package/dist/_modules/removeOptionalTags.d.ts +56 -0
  76. package/dist/_modules/removeOptionalTags.js +190 -0
  77. package/{lib/modules → dist/_modules}/removeOptionalTags.mjs +54 -91
  78. package/dist/_modules/removeRedundantAttributes.d.mts +57 -0
  79. package/dist/_modules/removeRedundantAttributes.d.ts +57 -0
  80. package/dist/_modules/removeRedundantAttributes.js +128 -0
  81. package/{lib/modules → dist/_modules}/removeRedundantAttributes.mjs +43 -59
  82. package/dist/_modules/removeUnusedCss.d.mts +60 -0
  83. package/dist/_modules/removeUnusedCss.d.ts +60 -0
  84. package/dist/_modules/removeUnusedCss.js +134 -0
  85. package/dist/_modules/removeUnusedCss.mjs +132 -0
  86. package/dist/_modules/sortAttributes.d.mts +57 -0
  87. package/dist/_modules/sortAttributes.d.ts +57 -0
  88. package/dist/_modules/sortAttributes.js +102 -0
  89. package/{lib/modules → dist/_modules}/sortAttributes.mjs +39 -60
  90. package/dist/_modules/sortAttributesWithLists.d.mts +56 -0
  91. package/dist/_modules/sortAttributesWithLists.d.ts +56 -0
  92. package/dist/_modules/sortAttributesWithLists.js +118 -0
  93. package/{lib/modules → dist/_modules}/sortAttributesWithLists.mjs +41 -60
  94. package/dist/helpers.js +72 -0
  95. package/dist/helpers.mjs +63 -0
  96. package/dist/index.js +223 -0
  97. package/dist/index.mjs +209 -0
  98. package/dist/presets/ampSafe.js +19 -0
  99. package/{lib → dist}/presets/ampSafe.mjs +6 -4
  100. package/dist/presets/max.js +28 -0
  101. package/{lib → dist}/presets/max.mjs +6 -4
  102. package/dist/presets/safe.js +60 -0
  103. package/{lib → dist}/presets/safe.mjs +13 -20
  104. package/package.json +53 -59
  105. package/.eslintignore +0 -3
  106. package/CHANGELOG.md +0 -398
  107. package/docs/README.md +0 -33
  108. package/docs/babel.config.js +0 -3
  109. package/docs/docs/010-introduction.md +0 -22
  110. package/docs/docs/020-usage.md +0 -117
  111. package/docs/docs/030-config.md +0 -21
  112. package/docs/docs/040-presets.md +0 -75
  113. package/docs/docs/050-modules.md +0 -855
  114. package/docs/docs/060-contribute.md +0 -16
  115. package/docs/docusaurus.config.js +0 -65
  116. package/docs/netlify.toml +0 -4
  117. package/docs/package-lock.json +0 -21796
  118. package/docs/package.json +0 -40
  119. package/docs/sidebars.js +0 -26
  120. package/docs/versioned_docs/version-1.1.1/010-introduction.md +0 -22
  121. package/docs/versioned_docs/version-1.1.1/020-usage.md +0 -77
  122. package/docs/versioned_docs/version-1.1.1/030-config.md +0 -21
  123. package/docs/versioned_docs/version-1.1.1/040-presets.md +0 -75
  124. package/docs/versioned_docs/version-1.1.1/050-modules.md +0 -785
  125. package/docs/versioned_docs/version-1.1.1/060-contribute.md +0 -16
  126. package/docs/versioned_docs/version-2.0.0/010-introduction.md +0 -22
  127. package/docs/versioned_docs/version-2.0.0/020-usage.md +0 -77
  128. package/docs/versioned_docs/version-2.0.0/030-config.md +0 -21
  129. package/docs/versioned_docs/version-2.0.0/040-presets.md +0 -75
  130. package/docs/versioned_docs/version-2.0.0/050-modules.md +0 -838
  131. package/docs/versioned_docs/version-2.0.0/060-contribute.md +0 -16
  132. package/docs/versioned_sidebars/version-1.1.1-sidebars.json +0 -8
  133. package/docs/versioned_sidebars/version-2.0.0-sidebars.json +0 -8
  134. package/docs/versions.json +0 -4
  135. package/index.cjs +0 -11
  136. package/index.d.cts +0 -3
  137. package/index.d.mts +0 -3
  138. package/index.d.ts +0 -93
  139. package/index.mjs +0 -2
  140. package/lib/helpers.cjs +0 -79
  141. package/lib/helpers.mjs +0 -53
  142. package/lib/htmlnano.cjs +0 -200
  143. package/lib/htmlnano.mjs +0 -196
  144. package/lib/modules/collapseAttributeWhitespace.cjs +0 -86
  145. package/lib/modules/collapseAttributeWhitespace.mjs +0 -104
  146. package/lib/modules/collapseBooleanAttributes.cjs +0 -62
  147. package/lib/modules/collapseWhitespace.cjs +0 -100
  148. package/lib/modules/collapseWhitespace.mjs +0 -132
  149. package/lib/modules/custom.cjs +0 -19
  150. package/lib/modules/custom.mjs +0 -16
  151. package/lib/modules/deduplicateAttributeValues.cjs +0 -38
  152. package/lib/modules/deduplicateAttributeValues.mjs +0 -40
  153. package/lib/modules/example.cjs +0 -85
  154. package/lib/modules/example.mjs +0 -75
  155. package/lib/modules/mergeScripts.cjs +0 -54
  156. package/lib/modules/mergeScripts.mjs +0 -56
  157. package/lib/modules/mergeStyles.cjs +0 -38
  158. package/lib/modules/mergeStyles.mjs +0 -36
  159. package/lib/modules/minifyConditionalComments.cjs +0 -47
  160. package/lib/modules/minifyCss.cjs +0 -73
  161. package/lib/modules/minifyCss.mjs +0 -88
  162. package/lib/modules/minifyJs.cjs +0 -103
  163. package/lib/modules/minifyJs.mjs +0 -121
  164. package/lib/modules/minifyJson.cjs +0 -24
  165. package/lib/modules/minifyJson.mjs +0 -21
  166. package/lib/modules/minifySvg.cjs +0 -37
  167. package/lib/modules/minifySvg.mjs +0 -30
  168. package/lib/modules/minifyUrls.cjs +0 -141
  169. package/lib/modules/minifyUrls.mjs +0 -229
  170. package/lib/modules/normalizeAttributeValues.cjs +0 -120
  171. package/lib/modules/normalizeAttributeValues.mjs +0 -140
  172. package/lib/modules/removeAttributeQuotes.cjs +0 -17
  173. package/lib/modules/removeAttributeQuotes.mjs +0 -12
  174. package/lib/modules/removeComments.cjs +0 -86
  175. package/lib/modules/removeEmptyAttributes.cjs +0 -72
  176. package/lib/modules/removeEmptyAttributes.mjs +0 -121
  177. package/lib/modules/removeOptionalTags.cjs +0 -183
  178. package/lib/modules/removeRedundantAttributes.cjs +0 -112
  179. package/lib/modules/removeUnusedCss.cjs +0 -113
  180. package/lib/modules/removeUnusedCss.mjs +0 -122
  181. package/lib/modules/sortAttributes.cjs +0 -99
  182. package/lib/modules/sortAttributesWithLists.cjs +0 -115
  183. package/lib/presets/ampSafe.cjs +0 -18
  184. package/lib/presets/max.cjs +0 -27
  185. package/lib/presets/safe.cjs +0 -65
  186. package/test.js +0 -48
@@ -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).
@@ -1,8 +0,0 @@
1
- {
2
- "version-1.1.1/tutorialSidebar": [
3
- {
4
- "type": "autogenerated",
5
- "dirName": "."
6
- }
7
- ]
8
- }
@@ -1,8 +0,0 @@
1
- {
2
- "version-2.0.0/tutorialSidebar": [
3
- {
4
- "type": "autogenerated",
5
- "dirName": "."
6
- }
7
- ]
8
- }
@@ -1,4 +0,0 @@
1
- [
2
- "2.0.0",
3
- "1.1.1"
4
- ]
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
@@ -1,3 +0,0 @@
1
- export type * from './index.d';
2
- import htmlnano from './index.d';
3
- export default htmlnano;
package/index.d.mts DELETED
@@ -1,3 +0,0 @@
1
- export type * from './index.d';
2
- import htmlnano from './index.d';
3
- export default htmlnano;
package/index.d.ts DELETED
@@ -1,93 +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
- collapseAttributeWhitespace?: boolean;
9
- collapseBooleanAttributes?: {
10
- amphtml?: boolean;
11
- };
12
- collapseWhitespace?: "conservative" | "all" | "aggressive";
13
- custom?: (tree: PostHTML.Node, options?: any) => PostHTML.Node;
14
- deduplicateAttributeValues?: boolean;
15
- minifyUrls?: URL | string | false;
16
- mergeStyles?: boolean;
17
- mergeScripts?: boolean;
18
- minifyCss?: CssNanoOptions | boolean;
19
- minifyConditionalComments?: boolean;
20
- minifyJs?: MinifyOptions | boolean;
21
- minifyJson?: boolean;
22
- minifySvg?: SvgoOptimizeOptions | boolean;
23
- normalizeAttributeValues?: boolean;
24
- removeAttributeQuotes?: boolean;
25
- removeComments?: boolean | "safe" | "all" | RegExp | (() => boolean);
26
- removeEmptyAttributes?: boolean;
27
- removeRedundantAttributes?: boolean;
28
- removeOptionalTags?: boolean;
29
- removeUnusedCss?: boolean;
30
- sortAttributes?: boolean | "alphabetical" | "frequency";
31
- sortAttributesWithLists?: boolean | "alphabetical" | "frequency";
32
- }
33
-
34
- export interface HtmlnanoPreset extends Omit<HtmlnanoOptions, "skipConfigLoading"> {}
35
-
36
- export interface Presets {
37
- safe: HtmlnanoPreset;
38
- ampSafe: HtmlnanoPreset;
39
- max: HtmlnanoPreset;
40
- }
41
-
42
- type Preset = Presets[keyof Presets];
43
-
44
- export function loadConfig(
45
- options?: HtmlnanoOptions,
46
- preset?: Preset,
47
- configPath?: string
48
- ): [HtmlnanoOptions | {}, Preset];
49
-
50
- declare function htmlnano<TMessage>(
51
- options?: HtmlnanoOptions,
52
- preset?: Preset
53
- ): (tree: PostHTML.Node) => Promise<PostHTML.Result<TMessage>>;
54
-
55
- interface PostHtmlOptions {
56
- directives?: Array<{
57
- name: string | RegExp;
58
- start: string;
59
- end: string;
60
- }>;
61
- sourceLocations?: boolean;
62
- recognizeNoValueAttribute?: boolean;
63
- xmlMode?: boolean;
64
- decodeEntities?: boolean;
65
- lowerCaseTags?: boolean;
66
- lowerCaseAttributeNames?: boolean;
67
- recognizeCDATA?: boolean;
68
- recognizeSelfClosing?: boolean;
69
- Tokenizer?: any;
70
- }
71
-
72
- declare namespace htmlnano {
73
- export function process<TMessage>(
74
- html: string,
75
- options?: HtmlnanoOptions,
76
- preset?: Preset,
77
- postHtmlOptions?: PostHtmlOptions
78
- ): Promise<PostHTML.Result<TMessage>>;
79
-
80
- export function getRequiredOptionalDependencies(
81
- optionsRun?: HtmlnanoOptions,
82
- presetRun?: Preset
83
- ): string[];
84
-
85
- export function htmlMinimizerWebpackPluginMinify(
86
- input: { [file: string]: string },
87
- minimizerOptions?: HtmlnanoOptions
88
- ): any;
89
-
90
- export const presets: Presets;
91
- }
92
-
93
- export default htmlnano;
package/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- import htmlnano from './lib/htmlnano.mjs';
2
- export default htmlnano;
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,200 +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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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
- console.warn(`You have to install "${dependency}" in order to use htmlnano's "${moduleName}" module`);
125
- } else {
126
- throw e;
127
- }
128
- }
129
- });
130
- const module = moduleName in modules ? await modules[moduleName]() : await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(__transformExtension(`./modules/${moduleName}.mjs`, {
131
- ".mjs": ".cjs"
132
- }));
133
- if (typeof module.onAttrs === 'function') {
134
- attrsHandlers.push(module.onAttrs(options, moduleOptions));
135
- }
136
- if (typeof module.onContent === 'function') {
137
- contentsHandlers.push(module.onContent(options, moduleOptions));
138
- }
139
- if (typeof module.onNode === 'function') {
140
- nodeHandlers.push(module.onNode(options, moduleOptions));
141
- }
142
- if (typeof module.default === 'function') {
143
- promise = promise.then(async tree => await module.default(tree, options, moduleOptions));
144
- }
145
- }
146
- if (attrsHandlers.length + contentsHandlers.length + nodeHandlers.length === 0) {
147
- return promise;
148
- }
149
- return promise.then(tree => {
150
- tree.walk(node => {
151
- if (node) {
152
- if (node.attrs && typeof node.attrs === 'object') {
153
- // Convert all attrs' key to lower case
154
- let newAttrsObj = {};
155
- Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
156
- newAttrsObj[attrName.toLowerCase()] = attrValue;
157
- });
158
- for (const handler of attrsHandlers) {
159
- newAttrsObj = handler(newAttrsObj, node);
160
- }
161
- node.attrs = newAttrsObj;
162
- }
163
- if (node.content) {
164
- node.content = typeof node.content === 'string' ? [node.content] : node.content;
165
- if (Array.isArray(node.content) && node.content.length > 0) {
166
- for (const handler of contentsHandlers) {
167
- const result = handler(node.content, node);
168
- node.content = typeof result === 'string' ? [result] : result;
169
- }
170
- }
171
- }
172
- for (const handler of nodeHandlers) {
173
- node = handler(node);
174
- }
175
- }
176
- return node;
177
- });
178
- return tree;
179
- });
180
- };
181
- }
182
- htmlnano.getRequiredOptionalDependencies = function (optionsRun, presetRun) {
183
- const [options] = loadConfig(optionsRun, presetRun);
184
- return [...new Set(Object.keys(options).filter(moduleName => options[moduleName]).map(moduleName => optionalDependencies[moduleName]).flat())];
185
- };
186
- htmlnano.process = function (html, options, preset, postHtmlOptions) {
187
- return (0, _posthtml.default)([htmlnano(options, preset)]).process(html, postHtmlOptions);
188
- };
189
-
190
- // https://github.com/webpack-contrib/html-minimizer-webpack-plugin/blob/faca00f2219514bc671c5942685721f0b5dbaa70/src/utils.js#L74
191
- htmlnano.htmlMinimizerWebpackPluginMinify = function htmlNano(input, minimizerOptions = {}) {
192
- const [[, code]] = Object.entries(input);
193
- return htmlnano.process(code, minimizerOptions, presets.safe).then(result => {
194
- return {
195
- code: result.html
196
- };
197
- });
198
- };
199
- htmlnano.presets = presets;
200
- var _default = exports.default = htmlnano;