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.
Files changed (191) hide show
  1. package/README.md +54 -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 -58
  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 -59
  94. package/dist/helpers.d.ts +16 -0
  95. package/dist/helpers.js +72 -0
  96. package/dist/helpers.mjs +63 -0
  97. package/dist/index.d.ts +79 -0
  98. package/dist/index.js +223 -0
  99. package/dist/index.mjs +209 -0
  100. package/dist/presets/ampSafe.d.ts +47 -0
  101. package/dist/presets/ampSafe.js +19 -0
  102. package/{lib → dist}/presets/ampSafe.mjs +6 -4
  103. package/dist/presets/max.d.ts +47 -0
  104. package/dist/presets/max.js +28 -0
  105. package/{lib → dist}/presets/max.mjs +6 -4
  106. package/dist/presets/safe.d.ts +47 -0
  107. package/dist/presets/safe.js +60 -0
  108. package/{lib → dist}/presets/safe.mjs +13 -20
  109. package/package.json +53 -56
  110. package/.eslintignore +0 -3
  111. package/CHANGELOG.md +0 -409
  112. package/docs/README.md +0 -33
  113. package/docs/babel.config.js +0 -3
  114. package/docs/docs/010-introduction.md +0 -22
  115. package/docs/docs/020-usage.md +0 -117
  116. package/docs/docs/030-config.md +0 -21
  117. package/docs/docs/040-presets.md +0 -75
  118. package/docs/docs/050-modules.md +0 -855
  119. package/docs/docs/060-contribute.md +0 -16
  120. package/docs/docusaurus.config.js +0 -65
  121. package/docs/netlify.toml +0 -4
  122. package/docs/package-lock.json +0 -21630
  123. package/docs/package.json +0 -40
  124. package/docs/sidebars.js +0 -26
  125. package/docs/versioned_docs/version-1.1.1/010-introduction.md +0 -22
  126. package/docs/versioned_docs/version-1.1.1/020-usage.md +0 -77
  127. package/docs/versioned_docs/version-1.1.1/030-config.md +0 -21
  128. package/docs/versioned_docs/version-1.1.1/040-presets.md +0 -75
  129. package/docs/versioned_docs/version-1.1.1/050-modules.md +0 -785
  130. package/docs/versioned_docs/version-1.1.1/060-contribute.md +0 -16
  131. package/docs/versioned_docs/version-2.0.0/010-introduction.md +0 -22
  132. package/docs/versioned_docs/version-2.0.0/020-usage.md +0 -77
  133. package/docs/versioned_docs/version-2.0.0/030-config.md +0 -21
  134. package/docs/versioned_docs/version-2.0.0/040-presets.md +0 -75
  135. package/docs/versioned_docs/version-2.0.0/050-modules.md +0 -838
  136. package/docs/versioned_docs/version-2.0.0/060-contribute.md +0 -16
  137. package/docs/versioned_sidebars/version-1.1.1-sidebars.json +0 -8
  138. package/docs/versioned_sidebars/version-2.0.0-sidebars.json +0 -8
  139. package/docs/versions.json +0 -4
  140. package/index.cjs +0 -11
  141. package/index.d.cts +0 -3
  142. package/index.d.mts +0 -3
  143. package/index.d.ts +0 -94
  144. package/index.mjs +0 -2
  145. package/lib/helpers.cjs +0 -79
  146. package/lib/helpers.mjs +0 -53
  147. package/lib/htmlnano.cjs +0 -202
  148. package/lib/htmlnano.mjs +0 -198
  149. package/lib/modules/collapseAttributeWhitespace.cjs +0 -86
  150. package/lib/modules/collapseAttributeWhitespace.mjs +0 -104
  151. package/lib/modules/collapseBooleanAttributes.cjs +0 -62
  152. package/lib/modules/collapseWhitespace.cjs +0 -100
  153. package/lib/modules/collapseWhitespace.mjs +0 -132
  154. package/lib/modules/custom.cjs +0 -19
  155. package/lib/modules/custom.mjs +0 -16
  156. package/lib/modules/deduplicateAttributeValues.cjs +0 -38
  157. package/lib/modules/deduplicateAttributeValues.mjs +0 -40
  158. package/lib/modules/example.cjs +0 -85
  159. package/lib/modules/example.mjs +0 -75
  160. package/lib/modules/mergeScripts.cjs +0 -54
  161. package/lib/modules/mergeScripts.mjs +0 -56
  162. package/lib/modules/mergeStyles.cjs +0 -38
  163. package/lib/modules/mergeStyles.mjs +0 -36
  164. package/lib/modules/minifyConditionalComments.cjs +0 -47
  165. package/lib/modules/minifyCss.cjs +0 -73
  166. package/lib/modules/minifyCss.mjs +0 -88
  167. package/lib/modules/minifyJs.cjs +0 -103
  168. package/lib/modules/minifyJs.mjs +0 -121
  169. package/lib/modules/minifyJson.cjs +0 -24
  170. package/lib/modules/minifyJson.mjs +0 -21
  171. package/lib/modules/minifySvg.cjs +0 -37
  172. package/lib/modules/minifySvg.mjs +0 -30
  173. package/lib/modules/minifyUrls.cjs +0 -141
  174. package/lib/modules/minifyUrls.mjs +0 -229
  175. package/lib/modules/normalizeAttributeValues.cjs +0 -120
  176. package/lib/modules/normalizeAttributeValues.mjs +0 -140
  177. package/lib/modules/removeAttributeQuotes.cjs +0 -17
  178. package/lib/modules/removeAttributeQuotes.mjs +0 -12
  179. package/lib/modules/removeComments.cjs +0 -86
  180. package/lib/modules/removeEmptyAttributes.cjs +0 -72
  181. package/lib/modules/removeEmptyAttributes.mjs +0 -121
  182. package/lib/modules/removeOptionalTags.cjs +0 -183
  183. package/lib/modules/removeRedundantAttributes.cjs +0 -112
  184. package/lib/modules/removeUnusedCss.cjs +0 -113
  185. package/lib/modules/removeUnusedCss.mjs +0 -122
  186. package/lib/modules/sortAttributes.cjs +0 -98
  187. package/lib/modules/sortAttributesWithLists.cjs +0 -114
  188. package/lib/presets/ampSafe.cjs +0 -18
  189. package/lib/presets/max.cjs +0 -27
  190. package/lib/presets/safe.cjs +0 -65
  191. 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).
@@ -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,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
@@ -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,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;