@docusaurus/bundler 0.0.0-6097 → 0.0.0-6098

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.
@@ -7,7 +7,10 @@
7
7
  import type { ConfigureWebpackUtils } from '@docusaurus/types';
8
8
  import type { MinimizerOptions as JsMinimizerOptions, CustomOptions } from 'terser-webpack-plugin';
9
9
  import type { MinimizerOptions as CssMinimizerOptions } from 'css-minimizer-webpack-plugin';
10
+ type FasterModule = Awaited<typeof import('@docusaurus/faster')>;
10
11
  export declare function importSwcJsLoaderFactory(): Promise<ConfigureWebpackUtils['getJSLoader']>;
11
12
  export declare function importSwcJsMinimizerOptions(): Promise<JsMinimizerOptions<CustomOptions>>;
13
+ export declare function importSwcHtmlMinifier(): Promise<ReturnType<FasterModule['getSwcHtmlMinifier']>>;
12
14
  export declare function importLightningCssMinimizerOptions(): Promise<CssMinimizerOptions<CustomOptions>>;
15
+ export {};
13
16
  //# sourceMappingURL=importFaster.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"importFaster.d.ts","sourceRoot":"","sources":["../src/importFaster.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAC,qBAAqB,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,KAAK,EACV,gBAAgB,IAAI,kBAAkB,EACtC,aAAa,EACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAC,gBAAgB,IAAI,mBAAmB,EAAC,MAAM,8BAA8B,CAAC;AAiB1F,wBAAsB,wBAAwB,IAAI,OAAO,CACvD,qBAAqB,CAAC,aAAa,CAAC,CACrC,CAGA;AAED,wBAAsB,2BAA2B,IAAI,OAAO,CAC1D,kBAAkB,CAAC,aAAa,CAAC,CAClC,CAGA;AAED,wBAAsB,kCAAkC,IAAI,OAAO,CACjE,mBAAmB,CAAC,aAAa,CAAC,CACnC,CAGA"}
1
+ {"version":3,"file":"importFaster.d.ts","sourceRoot":"","sources":["../src/importFaster.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAC,qBAAqB,EAAC,MAAM,mBAAmB,CAAC;AAC7D,OAAO,KAAK,EACV,gBAAgB,IAAI,kBAAkB,EACtC,aAAa,EACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAC,gBAAgB,IAAI,mBAAmB,EAAC,MAAM,8BAA8B,CAAC;AAE1F,KAAK,YAAY,GAAG,OAAO,CAAC,cAAc,oBAAoB,CAAC,CAAC,CAAC;AAiBjE,wBAAsB,wBAAwB,IAAI,OAAO,CACvD,qBAAqB,CAAC,aAAa,CAAC,CACrC,CAGA;AAED,wBAAsB,2BAA2B,IAAI,OAAO,CAC1D,kBAAkB,CAAC,aAAa,CAAC,CAClC,CAGA;AAED,wBAAsB,qBAAqB,IAAI,OAAO,CACpD,UAAU,CAAC,YAAY,CAAC,oBAAoB,CAAC,CAAC,CAC/C,CAGA;AAED,wBAAsB,kCAAkC,IAAI,OAAO,CACjE,mBAAmB,CAAC,aAAa,CAAC,CACnC,CAGA"}
@@ -8,6 +8,7 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.importSwcJsLoaderFactory = importSwcJsLoaderFactory;
10
10
  exports.importSwcJsMinimizerOptions = importSwcJsMinimizerOptions;
11
+ exports.importSwcHtmlMinifier = importSwcHtmlMinifier;
11
12
  exports.importLightningCssMinimizerOptions = importLightningCssMinimizerOptions;
12
13
  async function importFaster() {
13
14
  return import('@docusaurus/faster');
@@ -28,6 +29,10 @@ async function importSwcJsMinimizerOptions() {
28
29
  const faster = await ensureFaster();
29
30
  return faster.getSwcJsMinimizerOptions();
30
31
  }
32
+ async function importSwcHtmlMinifier() {
33
+ const faster = await ensureFaster();
34
+ return faster.getSwcHtmlMinifier();
35
+ }
31
36
  async function importLightningCssMinimizerOptions() {
32
37
  const faster = await ensureFaster();
33
38
  return faster.getLightningCssMinimizerOptions();
@@ -1 +1 @@
1
- {"version":3,"file":"importFaster.js","sourceRoot":"","sources":["../src/importFaster.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAwBH,4DAKC;AAED,kEAKC;AAED,gFAKC;AAlCD,KAAK,UAAU,YAAY;IACzB,OAAO,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACtC,CAAC;AAED,KAAK,UAAU,YAAY;IACzB,IAAI,CAAC;QACH,OAAO,MAAM,YAAY,EAAE,CAAC;IAC9B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,kFAAkF,EAClF,EAAC,KAAK,EAAE,KAAK,EAAC,CACf,CAAC;IACJ,CAAC;AACH,CAAC;AAEM,KAAK,UAAU,wBAAwB;IAG5C,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,qBAAqB,CAAC;AACtC,CAAC;AAEM,KAAK,UAAU,2BAA2B;IAG/C,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,wBAAwB,EAAuC,CAAC;AAChF,CAAC;AAEM,KAAK,UAAU,kCAAkC;IAGtD,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,+BAA+B,EAAwC,CAAC;AACxF,CAAC"}
1
+ {"version":3,"file":"importFaster.js","sourceRoot":"","sources":["../src/importFaster.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AA0BH,4DAKC;AAED,kEAKC;AAED,sDAKC;AAED,gFAKC;AAzCD,KAAK,UAAU,YAAY;IACzB,OAAO,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACtC,CAAC;AAED,KAAK,UAAU,YAAY;IACzB,IAAI,CAAC;QACH,OAAO,MAAM,YAAY,EAAE,CAAC;IAC9B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,kFAAkF,EAClF,EAAC,KAAK,EAAE,KAAK,EAAC,CACf,CAAC;IACJ,CAAC;AACH,CAAC;AAEM,KAAK,UAAU,wBAAwB;IAG5C,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,qBAAqB,CAAC;AACtC,CAAC;AAEM,KAAK,UAAU,2BAA2B;IAG/C,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,wBAAwB,EAAuC,CAAC;AAChF,CAAC;AAEM,KAAK,UAAU,qBAAqB;IAGzC,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,kBAAkB,EAAE,CAAC;AACrC,CAAC;AAEM,KAAK,UAAU,kCAAkC;IAGtD,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,MAAM,CAAC,+BAA+B,EAAwC,CAAC;AACxF,CAAC"}
package/lib/index.d.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  export { printStatsWarnings, formatStatsErrorMessage, compile } from './compiler';
8
8
  export { getCurrentBundler, getCSSExtractPlugin, getCopyPlugin, getProgressBarPlugin, } from './currentBundler';
9
9
  export { getMinimizers } from './minification';
10
+ export { getHtmlMinifier, type HtmlMinifier } from './minifyHtml';
10
11
  export { createJsLoaderFactory } from './loaders/jsLoader';
11
12
  export { createStyleLoadersFactory } from './loaders/styleLoader';
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAC,kBAAkB,EAAE,uBAAuB,EAAE,OAAO,EAAC,MAAM,YAAY,CAAC;AAEhF,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,oBAAoB,GACrB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAC,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAC,qBAAqB,EAAC,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAC,yBAAyB,EAAC,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAC,kBAAkB,EAAE,uBAAuB,EAAE,OAAO,EAAC,MAAM,YAAY,CAAC;AAEhF,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,oBAAoB,GACrB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAC,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAC,eAAe,EAAE,KAAK,YAAY,EAAC,MAAM,cAAc,CAAC;AAChE,OAAO,EAAC,qBAAqB,EAAC,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAC,yBAAyB,EAAC,MAAM,uBAAuB,CAAC"}
package/lib/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createStyleLoadersFactory = exports.createJsLoaderFactory = exports.getMinimizers = exports.getProgressBarPlugin = exports.getCopyPlugin = exports.getCSSExtractPlugin = exports.getCurrentBundler = exports.compile = exports.formatStatsErrorMessage = exports.printStatsWarnings = void 0;
9
+ exports.createStyleLoadersFactory = exports.createJsLoaderFactory = exports.getHtmlMinifier = exports.getMinimizers = exports.getProgressBarPlugin = exports.getCopyPlugin = exports.getCSSExtractPlugin = exports.getCurrentBundler = exports.compile = exports.formatStatsErrorMessage = exports.printStatsWarnings = void 0;
10
10
  var compiler_1 = require("./compiler");
11
11
  Object.defineProperty(exports, "printStatsWarnings", { enumerable: true, get: function () { return compiler_1.printStatsWarnings; } });
12
12
  Object.defineProperty(exports, "formatStatsErrorMessage", { enumerable: true, get: function () { return compiler_1.formatStatsErrorMessage; } });
@@ -18,6 +18,8 @@ Object.defineProperty(exports, "getCopyPlugin", { enumerable: true, get: functio
18
18
  Object.defineProperty(exports, "getProgressBarPlugin", { enumerable: true, get: function () { return currentBundler_1.getProgressBarPlugin; } });
19
19
  var minification_1 = require("./minification");
20
20
  Object.defineProperty(exports, "getMinimizers", { enumerable: true, get: function () { return minification_1.getMinimizers; } });
21
+ var minifyHtml_1 = require("./minifyHtml");
22
+ Object.defineProperty(exports, "getHtmlMinifier", { enumerable: true, get: function () { return minifyHtml_1.getHtmlMinifier; } });
21
23
  var jsLoader_1 = require("./loaders/jsLoader");
22
24
  Object.defineProperty(exports, "createJsLoaderFactory", { enumerable: true, get: function () { return jsLoader_1.createJsLoaderFactory; } });
23
25
  var styleLoader_1 = require("./loaders/styleLoader");
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,uCAAgF;AAAxE,8GAAA,kBAAkB,OAAA;AAAE,mHAAA,uBAAuB,OAAA;AAAE,mGAAA,OAAO,OAAA;AAE5D,mDAK0B;AAJxB,mHAAA,iBAAiB,OAAA;AACjB,qHAAA,mBAAmB,OAAA;AACnB,+GAAA,aAAa,OAAA;AACb,sHAAA,oBAAoB,OAAA;AAGtB,+CAA6C;AAArC,6GAAA,aAAa,OAAA;AACrB,+CAAyD;AAAjD,iHAAA,qBAAqB,OAAA;AAC7B,qDAAgE;AAAxD,wHAAA,yBAAyB,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,uCAAgF;AAAxE,8GAAA,kBAAkB,OAAA;AAAE,mHAAA,uBAAuB,OAAA;AAAE,mGAAA,OAAO,OAAA;AAE5D,mDAK0B;AAJxB,mHAAA,iBAAiB,OAAA;AACjB,qHAAA,mBAAmB,OAAA;AACnB,+GAAA,aAAa,OAAA;AACb,sHAAA,oBAAoB,OAAA;AAGtB,+CAA6C;AAArC,6GAAA,aAAa,OAAA;AACrB,2CAAgE;AAAxD,6GAAA,eAAe,OAAA;AACvB,+CAAyD;AAAjD,iHAAA,qBAAqB,OAAA;AAC7B,qDAAgE;AAAxD,wHAAA,yBAAyB,OAAA"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { DocusaurusConfig } from '@docusaurus/types';
8
+ export type HtmlMinifier = {
9
+ minify: (html: string) => Promise<string>;
10
+ };
11
+ type SiteConfigSlice = {
12
+ future: {
13
+ experimental_faster: Pick<DocusaurusConfig['future']['experimental_faster'], 'swcHtmlMinimizer'>;
14
+ };
15
+ };
16
+ export declare function getHtmlMinifier({ siteConfig, }: {
17
+ siteConfig: SiteConfigSlice;
18
+ }): Promise<HtmlMinifier>;
19
+ export {};
20
+ //# sourceMappingURL=minifyHtml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"minifyHtml.d.ts","sourceRoot":"","sources":["../src/minifyHtml.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AAKxD,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC3C,CAAC;AAMF,KAAK,eAAe,GAAG;IACrB,MAAM,EAAE;QACN,mBAAmB,EAAE,IAAI,CACvB,gBAAgB,CAAC,QAAQ,CAAC,CAAC,qBAAqB,CAAC,EACjD,kBAAkB,CACnB,CAAC;KACH,CAAC;CACH,CAAC;AAEF,wBAAsB,eAAe,CAAC,EACpC,UAAU,GACX,EAAE;IACD,UAAU,EAAE,eAAe,CAAC;CAC7B,GAAG,OAAO,CAAC,YAAY,CAAC,CASxB"}
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.getHtmlMinifier = getHtmlMinifier;
10
+ const tslib_1 = require("tslib");
11
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
12
+ const html_minifier_terser_1 = require("html-minifier-terser");
13
+ const importFaster_1 = require("./importFaster");
14
+ // Historical env variable
15
+ const SkipHtmlMinification = process.env.SKIP_HTML_MINIFICATION === 'true';
16
+ const NoopMinifier = {
17
+ minify: async (html) => html,
18
+ };
19
+ async function getHtmlMinifier({ siteConfig, }) {
20
+ if (SkipHtmlMinification) {
21
+ return NoopMinifier;
22
+ }
23
+ if (siteConfig.future.experimental_faster.swcHtmlMinimizer) {
24
+ return getSwcMinifier();
25
+ }
26
+ else {
27
+ return getTerserMinifier();
28
+ }
29
+ }
30
+ // Minify html with https://github.com/DanielRuf/html-minifier-terser
31
+ async function getTerserMinifier() {
32
+ return {
33
+ minify: async function minifyHtmlWithTerser(html) {
34
+ try {
35
+ return await (0, html_minifier_terser_1.minify)(html, {
36
+ removeComments: false,
37
+ removeRedundantAttributes: true,
38
+ removeEmptyAttributes: true,
39
+ removeScriptTypeAttributes: true,
40
+ removeStyleLinkTypeAttributes: true,
41
+ useShortDoctype: true,
42
+ minifyJS: true,
43
+ });
44
+ }
45
+ catch (err) {
46
+ throw new Error(`HTML minification failed (Terser)`, {
47
+ cause: err,
48
+ });
49
+ }
50
+ },
51
+ };
52
+ }
53
+ // Minify html with @swc/html
54
+ // Not well-documented but fast!
55
+ // See https://github.com/swc-project/swc/discussions/9616
56
+ async function getSwcMinifier() {
57
+ const swcHtmlMinifier = await (0, importFaster_1.importSwcHtmlMinifier)();
58
+ return {
59
+ minify: async function minifyHtmlWithSwc(html) {
60
+ try {
61
+ const result = await swcHtmlMinifier(Buffer.from(html), {
62
+ // Removing comments can lead to React hydration errors
63
+ // See https://x.com/sebastienlorber/status/1841966927440478577
64
+ removeComments: false,
65
+ // TODO maybe it's fine to only keep <!-- --> React comments?
66
+ preserveComments: [],
67
+ // Sorting these attributes (class) can lead to React hydration errors
68
+ sortSpaceSeparatedAttributeValues: false,
69
+ sortAttributes: false,
70
+ // @ts-expect-error: bad type https://github.com/swc-project/swc/pull/9615
71
+ removeRedundantAttributes: 'all',
72
+ removeEmptyAttributes: true,
73
+ minifyJs: true,
74
+ minifyJson: true,
75
+ minifyCss: true,
76
+ });
77
+ // Escape hatch because SWC is quite aggressive to report errors
78
+ // TODO figure out what to do with these errors: throw or swallow?
79
+ // See https://github.com/facebook/docusaurus/pull/10554
80
+ // See https://github.com/swc-project/swc/discussions/9616#discussioncomment-10846201
81
+ const ignoreSwcMinifierErrors = process.env.DOCUSAURUS_IGNORE_SWC_HTML_MINIFIER_ERRORS === 'true';
82
+ if (!ignoreSwcMinifierErrors && result.errors) {
83
+ const ignoredErrors = [
84
+ // TODO Docusaurus seems to emit NULL chars, and minifier detects it
85
+ // see https://github.com/facebook/docusaurus/issues/9985
86
+ 'Unexpected null character',
87
+ ];
88
+ result.errors = result.errors.filter((diagnostic) => !ignoredErrors.includes(diagnostic.message));
89
+ if (result.errors.length) {
90
+ throw new Error(`HTML minification diagnostic errors:
91
+ - ${result.errors
92
+ .map((diagnostic) => `[${diagnostic.level}] ${diagnostic.message} - ${JSON.stringify(diagnostic.span)}`)
93
+ .join('\n- ')}
94
+ Note: please report the problem to the Docusaurus team
95
+ In the meantime, you can skip this error with ${logger_1.default.code('DOCUSAURUS_IGNORE_SWC_HTML_MINIFIER_ERRORS=true')}`);
96
+ }
97
+ /*
98
+ if (result.errors.length) {
99
+ throw new AggregateError(
100
+ result.errors.map(
101
+ (diagnostic) => new Error(JSON.stringify(diagnostic, null, 2)),
102
+ ),
103
+ );
104
+ }
105
+ */
106
+ }
107
+ return result.code;
108
+ }
109
+ catch (err) {
110
+ throw new Error(`HTML minification failed (SWC)`, {
111
+ cause: err,
112
+ });
113
+ }
114
+ },
115
+ };
116
+ }
117
+ //# sourceMappingURL=minifyHtml.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"minifyHtml.js","sourceRoot":"","sources":["../src/minifyHtml.ts"],"names":[],"mappings":"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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/bundler",
3
- "version": "0.0.0-6097",
3
+ "version": "0.0.0-6098",
4
4
  "description": "Docusaurus util package to abstract the current bundler.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -19,12 +19,12 @@
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
21
  "@babel/core": "^7.23.3",
22
- "@docusaurus/babel": "0.0.0-6097",
23
- "@docusaurus/cssnano-preset": "0.0.0-6097",
24
- "@docusaurus/faster": "0.0.0-6097",
25
- "@docusaurus/logger": "0.0.0-6097",
26
- "@docusaurus/types": "0.0.0-6097",
27
- "@docusaurus/utils": "0.0.0-6097",
22
+ "@docusaurus/babel": "0.0.0-6098",
23
+ "@docusaurus/cssnano-preset": "0.0.0-6098",
24
+ "@docusaurus/faster": "0.0.0-6098",
25
+ "@docusaurus/logger": "0.0.0-6098",
26
+ "@docusaurus/types": "0.0.0-6098",
27
+ "@docusaurus/utils": "0.0.0-6098",
28
28
  "autoprefixer": "^10.4.14",
29
29
  "babel-loader": "^9.1.3",
30
30
  "clean-css": "^5.3.2",
@@ -33,6 +33,7 @@
33
33
  "css-minimizer-webpack-plugin": "^5.0.1",
34
34
  "cssnano": "^6.1.2",
35
35
  "file-loader": "^6.2.0",
36
+ "html-minifier-terser": "^7.2.0",
36
37
  "mini-css-extract-plugin": "^2.9.1",
37
38
  "null-loader": "^4.0.1",
38
39
  "postcss": "^8.4.26",
@@ -50,5 +51,5 @@
50
51
  "engines": {
51
52
  "node": ">=18.0"
52
53
  },
53
- "gitHead": "100530cd36591548d4c29cbac58ddd10a91a6686"
54
+ "gitHead": "d0a89b89c2ace47f7e76829fbf41681cde932bb9"
54
55
  }
@@ -12,11 +12,13 @@ import type {
12
12
  } from 'terser-webpack-plugin';
13
13
  import type {MinimizerOptions as CssMinimizerOptions} from 'css-minimizer-webpack-plugin';
14
14
 
15
- async function importFaster() {
15
+ type FasterModule = Awaited<typeof import('@docusaurus/faster')>;
16
+
17
+ async function importFaster(): Promise<FasterModule> {
16
18
  return import('@docusaurus/faster');
17
19
  }
18
20
 
19
- async function ensureFaster() {
21
+ async function ensureFaster(): Promise<FasterModule> {
20
22
  try {
21
23
  return await importFaster();
22
24
  } catch (error) {
@@ -41,6 +43,13 @@ export async function importSwcJsMinimizerOptions(): Promise<
41
43
  return faster.getSwcJsMinimizerOptions() as JsMinimizerOptions<CustomOptions>;
42
44
  }
43
45
 
46
+ export async function importSwcHtmlMinifier(): Promise<
47
+ ReturnType<FasterModule['getSwcHtmlMinifier']>
48
+ > {
49
+ const faster = await ensureFaster();
50
+ return faster.getSwcHtmlMinifier();
51
+ }
52
+
44
53
  export async function importLightningCssMinimizerOptions(): Promise<
45
54
  CssMinimizerOptions<CustomOptions>
46
55
  > {
package/src/index.ts CHANGED
@@ -15,5 +15,6 @@ export {
15
15
  } from './currentBundler';
16
16
 
17
17
  export {getMinimizers} from './minification';
18
+ export {getHtmlMinifier, type HtmlMinifier} from './minifyHtml';
18
19
  export {createJsLoaderFactory} from './loaders/jsLoader';
19
20
  export {createStyleLoadersFactory} from './loaders/styleLoader';
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import logger from '@docusaurus/logger';
9
+ import {minify as terserHtmlMinifier} from 'html-minifier-terser';
10
+ import {importSwcHtmlMinifier} from './importFaster';
11
+ import type {DocusaurusConfig} from '@docusaurus/types';
12
+
13
+ // Historical env variable
14
+ const SkipHtmlMinification = process.env.SKIP_HTML_MINIFICATION === 'true';
15
+
16
+ export type HtmlMinifier = {
17
+ minify: (html: string) => Promise<string>;
18
+ };
19
+
20
+ const NoopMinifier: HtmlMinifier = {
21
+ minify: async (html: string) => html,
22
+ };
23
+
24
+ type SiteConfigSlice = {
25
+ future: {
26
+ experimental_faster: Pick<
27
+ DocusaurusConfig['future']['experimental_faster'],
28
+ 'swcHtmlMinimizer'
29
+ >;
30
+ };
31
+ };
32
+
33
+ export async function getHtmlMinifier({
34
+ siteConfig,
35
+ }: {
36
+ siteConfig: SiteConfigSlice;
37
+ }): Promise<HtmlMinifier> {
38
+ if (SkipHtmlMinification) {
39
+ return NoopMinifier;
40
+ }
41
+ if (siteConfig.future.experimental_faster.swcHtmlMinimizer) {
42
+ return getSwcMinifier();
43
+ } else {
44
+ return getTerserMinifier();
45
+ }
46
+ }
47
+
48
+ // Minify html with https://github.com/DanielRuf/html-minifier-terser
49
+ async function getTerserMinifier(): Promise<HtmlMinifier> {
50
+ return {
51
+ minify: async function minifyHtmlWithTerser(html) {
52
+ try {
53
+ return await terserHtmlMinifier(html, {
54
+ removeComments: false,
55
+ removeRedundantAttributes: true,
56
+ removeEmptyAttributes: true,
57
+ removeScriptTypeAttributes: true,
58
+ removeStyleLinkTypeAttributes: true,
59
+ useShortDoctype: true,
60
+ minifyJS: true,
61
+ });
62
+ } catch (err) {
63
+ throw new Error(`HTML minification failed (Terser)`, {
64
+ cause: err as Error,
65
+ });
66
+ }
67
+ },
68
+ };
69
+ }
70
+
71
+ // Minify html with @swc/html
72
+ // Not well-documented but fast!
73
+ // See https://github.com/swc-project/swc/discussions/9616
74
+ async function getSwcMinifier(): Promise<HtmlMinifier> {
75
+ const swcHtmlMinifier = await importSwcHtmlMinifier();
76
+ return {
77
+ minify: async function minifyHtmlWithSwc(html) {
78
+ try {
79
+ const result = await swcHtmlMinifier(Buffer.from(html), {
80
+ // Removing comments can lead to React hydration errors
81
+ // See https://x.com/sebastienlorber/status/1841966927440478577
82
+ removeComments: false,
83
+ // TODO maybe it's fine to only keep <!-- --> React comments?
84
+ preserveComments: [],
85
+
86
+ // Sorting these attributes (class) can lead to React hydration errors
87
+ sortSpaceSeparatedAttributeValues: false,
88
+ sortAttributes: false,
89
+
90
+ // @ts-expect-error: bad type https://github.com/swc-project/swc/pull/9615
91
+ removeRedundantAttributes: 'all',
92
+ removeEmptyAttributes: true,
93
+ minifyJs: true,
94
+ minifyJson: true,
95
+ minifyCss: true,
96
+ });
97
+
98
+ // Escape hatch because SWC is quite aggressive to report errors
99
+ // TODO figure out what to do with these errors: throw or swallow?
100
+ // See https://github.com/facebook/docusaurus/pull/10554
101
+ // See https://github.com/swc-project/swc/discussions/9616#discussioncomment-10846201
102
+ const ignoreSwcMinifierErrors =
103
+ process.env.DOCUSAURUS_IGNORE_SWC_HTML_MINIFIER_ERRORS === 'true';
104
+ if (!ignoreSwcMinifierErrors && result.errors) {
105
+ const ignoredErrors: string[] = [
106
+ // TODO Docusaurus seems to emit NULL chars, and minifier detects it
107
+ // see https://github.com/facebook/docusaurus/issues/9985
108
+ 'Unexpected null character',
109
+ ];
110
+ result.errors = result.errors.filter(
111
+ (diagnostic) => !ignoredErrors.includes(diagnostic.message),
112
+ );
113
+ if (result.errors.length) {
114
+ throw new Error(
115
+ `HTML minification diagnostic errors:
116
+ - ${result.errors
117
+ .map(
118
+ (diagnostic) =>
119
+ `[${diagnostic.level}] ${
120
+ diagnostic.message
121
+ } - ${JSON.stringify(diagnostic.span)}`,
122
+ )
123
+ .join('\n- ')}
124
+ Note: please report the problem to the Docusaurus team
125
+ In the meantime, you can skip this error with ${logger.code(
126
+ 'DOCUSAURUS_IGNORE_SWC_HTML_MINIFIER_ERRORS=true',
127
+ )}`,
128
+ );
129
+ }
130
+ /*
131
+ if (result.errors.length) {
132
+ throw new AggregateError(
133
+ result.errors.map(
134
+ (diagnostic) => new Error(JSON.stringify(diagnostic, null, 2)),
135
+ ),
136
+ );
137
+ }
138
+ */
139
+ }
140
+ return result.code;
141
+ } catch (err) {
142
+ throw new Error(`HTML minification failed (SWC)`, {
143
+ cause: err as Error,
144
+ });
145
+ }
146
+ },
147
+ };
148
+ }