Skip to content

Commit 7cb6dd4

Browse files
feat: export cssMinify and htmlMinify on css.syntax and html.syntax (#21953)
* feat: export cssMinify and htmlMinify on css.syntax and html.syntax Both are written as `minify` functions for minimizer-webpack-plugin, and both already read their parser back out of `webpack.css.syntax` at call time, but neither was reachable from the public surface: they were absent from `lib/index.js` and from `types.d.ts`, so the only way to pass one to the plugin was a deep require into `lib/`. Re-export them from the syntax modules they already depend on. Neither minify file requires anything at module scope -- the `require("webpack")` that resolves the parser sits inside the function body, so it survives being shipped to the plugin's worker pool as source -- so this adds no cycle and no load-order constraint. * test: snapshot the minified output the export produces Both assertions check printed code, which the testing guide says to snapshot rather than pin with `toBe` -- a hand-written expectation over one string ignores every other byte the printer emits. The recorded values are unchanged.
1 parent f2581d3 commit 7cb6dd4

8 files changed

Lines changed: 145 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Export `cssMinify` and `htmlMinify` on `css.syntax` and `html.syntax`.

‎lib/css/syntax.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
parseDataURI
2323
} = require("../util/dataURL");
2424
const { makeCacheable } = require("../util/identifier");
25+
const cssMinify = require("./cssMinify");
2526

2627
/**
2728
* Renders source this stylesheet embeds — a `data:` URL's payload today.
@@ -16642,6 +16643,7 @@ module.exports.TokenStream = TokenStream;
1664216643
module.exports.askEmbeddedRenderer = askEmbeddedRenderer;
1664316644
module.exports.buildSkipSet = buildSkipSet;
1664416645
module.exports.collectEmbeddedDiagnostics = collectEmbeddedDiagnostics;
16646+
module.exports.cssMinify = cssMinify;
1664516647
module.exports.embeddedText = embeddedText;
1664616648
module.exports.equalsLowerCase = equalsLowerCase;
1664716649
module.exports.escapeIdentifier = escapeIdentifier;

‎lib/html/syntax.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const {
149149
VOID,
150150
VOID_FORMATTING
151151
} = require("./data");
152+
const htmlMinify = require("./htmlMinify");
152153

153154
// cspell:ignore apos notpre noncharacter noncharacters DFFF FFFE CCLS ALNUM
154155

@@ -13769,6 +13770,7 @@ module.exports.decodeEntities = decodeEntities;
1376913770
module.exports.embeddedText = embeddedText;
1377013771
module.exports.escapeAttribute = escapeAttribute;
1377113772
module.exports.escapeText = escapeText;
13773+
module.exports.htmlMinify = htmlMinify;
1377213774
module.exports.isAsciiWhitespace = isSpace;
1377313775
module.exports.metaTag = metaTag;
1377413776
// WHATWG "ASCII whitespace" (tab / LF / FF / CR / space) — the tokenizer's

‎test/CssSyntax.unittest.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9880,3 +9880,33 @@ describe("CssSyntax minify — a `color-mix()` the target cannot read", () => {
98809880
);
98819881
});
98829882
});
9883+
9884+
describe("cssMinify export", () => {
9885+
it("is the minifier itself, reachable from the public entry", () => {
9886+
const webpack = require("../");
9887+
9888+
expect(webpack.css.syntax.cssMinify).toBe(require("../lib/css/cssMinify"));
9889+
});
9890+
9891+
it("carries the minimizer contract minimizer-webpack-plugin dispatches on", () => {
9892+
const webpack = require("../");
9893+
9894+
const { cssMinify } = webpack.css.syntax;
9895+
9896+
expect(cssMinify.getTypes()).toEqual(["css"]);
9897+
expect(cssMinify.getEmbeddedTypes()).toContain("css");
9898+
expect(cssMinify.supportsWorkerThreads()).toBe(true);
9899+
expect(cssMinify.filter("a.css")).toBe(true);
9900+
expect(cssMinify.filter("a.html")).toBe(false);
9901+
});
9902+
9903+
it("minifies through the public entry", async () => {
9904+
const webpack = require("../");
9905+
9906+
const { cssMinify } = webpack.css.syntax;
9907+
9908+
const { code } = await cssMinify({ "a.css": "a {\n\tcolor: red;\n}\n" });
9909+
9910+
expect(code).toMatchInlineSnapshot('"a{color:red}"');
9911+
});
9912+
});

‎test/HtmlSyntax.unittest.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9164,3 +9164,38 @@ describe("htmlMinify — an answered `style` attribute that changes its quoting"
91649164
).toBe("<svg><rect style=color:red /></svg>");
91659165
});
91669166
});
9167+
9168+
describe("htmlMinify export", () => {
9169+
it("is the minifier itself, reachable from the public entry", () => {
9170+
const webpack = require("../");
9171+
9172+
expect(webpack.html.syntax.htmlMinify).toBe(
9173+
require("../lib/html/htmlMinify")
9174+
);
9175+
});
9176+
9177+
it("carries the minimizer contract minimizer-webpack-plugin dispatches on", () => {
9178+
const webpack = require("../");
9179+
9180+
const { htmlMinify } = webpack.html.syntax;
9181+
9182+
expect(htmlMinify.getTypes()).toEqual(["html"]);
9183+
expect(htmlMinify.getEmbeddedTypes()).toContain("css");
9184+
expect(htmlMinify.supportsWorkerThreads()).toBe(true);
9185+
expect(htmlMinify.filter("a.html")).toBe(true);
9186+
expect(htmlMinify.filter("a.css")).toBe(false);
9187+
});
9188+
9189+
it("minifies through the public entry", async () => {
9190+
const webpack = require("../");
9191+
9192+
const { htmlMinify } = webpack.html.syntax;
9193+
9194+
const { code } = await htmlMinify({
9195+
"a.html": "<p class='x'> a </p>"
9196+
});
9197+
9198+
// `</p>` is an optional end tag, so minifying drops it.
9199+
expect(code).toMatchInlineSnapshot('"<p class=x> a "');
9200+
});
9201+
});

‎test/configCases/css/minimize-bad-string/webpack.config.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use strict";
22

33
const MinimizerPlugin = require("minimizer-webpack-plugin");
4-
const cssMinify = require("../../../../lib/css/cssMinify");
4+
const webpack = require("../../../../");
5+
6+
const { cssMinify } = webpack.css.syntax;
57

68
/** @type {import("../../../../").Configuration} */
79
module.exports = {

‎test/configCases/html/minimize/webpack.config.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use strict";
22

33
const MinimizerPlugin = require("minimizer-webpack-plugin");
4-
const htmlMinify = require("../../../../lib/html/htmlMinify");
4+
const webpack = require("../../../../");
5+
6+
const { htmlMinify } = webpack.html.syntax;
57

68
/** @type {import("../../../../").Configuration} */
79
module.exports = {

‎types.d.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30556,6 +30556,37 @@ declare namespace exports {
3055630556
errors?: (string | Error)[];
3055730557
}[]
3055830558
) => { warnings?: (string | Error)[]; errors?: (string | Error)[] };
30559+
export function cssMinify(
30560+
input: { [index: string]: string },
30561+
sourceMap?: object,
30562+
minimizerOptions?: {
30563+
as?: "stylesheet" | "block-contents";
30564+
environment?: CssEnvironment;
30565+
convertLengthUnits?: boolean;
30566+
rewriteCustomProperties?: boolean;
30567+
unusedSymbols?: string[];
30568+
pseudoClasses?: { [index: string]: string };
30569+
renderEmbeddedSource?: (
30570+
source: string,
30571+
info: { type: string; hostType: string }
30572+
) =>
30573+
| undefined
30574+
| string
30575+
| EmbeddedSourceResult
30576+
| Promise<undefined | string | EmbeddedSourceResult>;
30577+
} & CssTransformOptions
30578+
): Promise<{
30579+
code: string;
30580+
map?: SourceMap;
30581+
warnings?: (string | Error)[];
30582+
errors?: (string | Error)[];
30583+
}>;
30584+
export namespace cssMinify {
30585+
export let supportsWorkerThreads: () => boolean;
30586+
export let getTypes: () => string[];
30587+
export let getEmbeddedTypes: () => string[];
30588+
export let filter: (name: string) => boolean;
30589+
}
3055930590
export let embeddedText: (
3056030591
answer?: string | { code?: string }
3056130592
) => undefined | string;
@@ -30868,6 +30899,40 @@ declare namespace exports {
3086830899
minimal?: boolean
3086930900
) => string;
3087030901
export let escapeText: (s: string) => string;
30902+
export function htmlMinify(
30903+
input: { [index: string]: string },
30904+
sourceMap?: RawSourceMap,
30905+
minimizerOptions?: Omit<
30906+
HtmlPrintOptions,
30907+
"renderEmbeddedSource" | "deferEmbeddedSource"
30908+
> & {
30909+
css?: {
30910+
convertLengthUnits?: boolean;
30911+
rewriteCustomProperties?: boolean;
30912+
unusedSymbols?: string[];
30913+
pseudoClasses?: { [index: string]: string };
30914+
};
30915+
minifyConditionalComments?: boolean;
30916+
renderEmbeddedSource?: (
30917+
source: string,
30918+
info: { type: string; hostType: string; as?: string }
30919+
) =>
30920+
| undefined
30921+
| string
30922+
| EmbeddedSourceResult
30923+
| Promise<undefined | string | EmbeddedSourceResult>;
30924+
}
30925+
): Promise<{
30926+
code: string;
30927+
warnings?: (string | Error)[];
30928+
errors?: (string | Error)[];
30929+
}>;
30930+
export namespace htmlMinify {
30931+
export let supportsWorkerThreads: () => boolean;
30932+
export let getTypes: () => string[];
30933+
export let getEmbeddedTypes: () => string[];
30934+
export let filter: (name: string) => boolean;
30935+
}
3087130936
export let isAsciiWhitespace: (cc: number) => boolean;
3087230937
export let metaTag: (name: string, content: string) => string;
3087330938
export let parseCssUrls: (input: string) => [string, number, number][];

0 commit comments

Comments
 (0)