Skip to content

Commit c0a8c13

Browse files
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.
1 parent 78aaae3 commit c0a8c13

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.
@@ -16148,6 +16149,7 @@ module.exports.TokenStream = TokenStream;
1614816149
module.exports.askEmbeddedRenderer = askEmbeddedRenderer;
1614916150
module.exports.buildSkipSet = buildSkipSet;
1615016151
module.exports.collectEmbeddedDiagnostics = collectEmbeddedDiagnostics;
16152+
module.exports.cssMinify = cssMinify;
1615116153
module.exports.embeddedText = embeddedText;
1615216154
module.exports.equalsLowerCase = equalsLowerCase;
1615316155
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

@@ -13766,6 +13767,7 @@ module.exports.decodeEntities = decodeEntities;
1376613767
module.exports.embeddedText = embeddedText;
1376713768
module.exports.escapeAttribute = escapeAttribute;
1376813769
module.exports.escapeText = escapeText;
13770+
module.exports.htmlMinify = htmlMinify;
1376913771
module.exports.isAsciiWhitespace = isSpace;
1377013772
module.exports.metaTag = metaTag;
1377113773
// 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
@@ -9375,3 +9375,33 @@ describe("CssSyntax minify — a slot holding its own initial", () => {
93759375
);
93769376
});
93779377
});
9378+
9379+
describe("cssMinify export", () => {
9380+
it("is the minifier itself, reachable from the public entry", () => {
9381+
const webpack = require("../");
9382+
9383+
expect(webpack.css.syntax.cssMinify).toBe(require("../lib/css/cssMinify"));
9384+
});
9385+
9386+
it("carries the minimizer contract minimizer-webpack-plugin dispatches on", () => {
9387+
const webpack = require("../");
9388+
9389+
const { cssMinify } = webpack.css.syntax;
9390+
9391+
expect(cssMinify.getTypes()).toEqual(["css"]);
9392+
expect(cssMinify.getEmbeddedTypes()).toContain("css");
9393+
expect(cssMinify.supportsWorkerThreads()).toBe(true);
9394+
expect(cssMinify.filter("a.css")).toBe(true);
9395+
expect(cssMinify.filter("a.html")).toBe(false);
9396+
});
9397+
9398+
it("minifies through the public entry", async () => {
9399+
const webpack = require("../");
9400+
9401+
const { cssMinify } = webpack.css.syntax;
9402+
9403+
const { code } = await cssMinify({ "a.css": "a {\n\tcolor: red;\n}\n" });
9404+
9405+
expect(code).toBe("a{color:red}");
9406+
});
9407+
});

‎test/HtmlSyntax.unittest.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9062,3 +9062,38 @@ describe("htmlMinify — an answered `style` attribute that changes its quoting"
90629062
).toBe("<svg><rect style=color:red /></svg>");
90639063
});
90649064
});
9065+
9066+
describe("htmlMinify export", () => {
9067+
it("is the minifier itself, reachable from the public entry", () => {
9068+
const webpack = require("../");
9069+
9070+
expect(webpack.html.syntax.htmlMinify).toBe(
9071+
require("../lib/html/htmlMinify")
9072+
);
9073+
});
9074+
9075+
it("carries the minimizer contract minimizer-webpack-plugin dispatches on", () => {
9076+
const webpack = require("../");
9077+
9078+
const { htmlMinify } = webpack.html.syntax;
9079+
9080+
expect(htmlMinify.getTypes()).toEqual(["html"]);
9081+
expect(htmlMinify.getEmbeddedTypes()).toContain("css");
9082+
expect(htmlMinify.supportsWorkerThreads()).toBe(true);
9083+
expect(htmlMinify.filter("a.html")).toBe(true);
9084+
expect(htmlMinify.filter("a.css")).toBe(false);
9085+
});
9086+
9087+
it("minifies through the public entry", async () => {
9088+
const webpack = require("../");
9089+
9090+
const { htmlMinify } = webpack.html.syntax;
9091+
9092+
const { code } = await htmlMinify({
9093+
"a.html": "<p class='x'> a </p>"
9094+
});
9095+
9096+
// `</p>` is an optional end tag, so minifying drops it.
9097+
expect(code).toBe("<p class=x> a ");
9098+
});
9099+
});

‎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
@@ -30512,6 +30512,37 @@ declare namespace exports {
3051230512
errors?: (string | Error)[];
3051330513
}[]
3051430514
) => { warnings?: (string | Error)[]; errors?: (string | Error)[] };
30515+
export function cssMinify(
30516+
input: { [index: string]: string },
30517+
sourceMap?: object,
30518+
minimizerOptions?: {
30519+
as?: "stylesheet" | "block-contents";
30520+
environment?: CssEnvironment;
30521+
convertLengthUnits?: boolean;
30522+
rewriteCustomProperties?: boolean;
30523+
unusedSymbols?: string[];
30524+
pseudoClasses?: { [index: string]: string };
30525+
renderEmbeddedSource?: (
30526+
source: string,
30527+
info: { type: string; hostType: string }
30528+
) =>
30529+
| undefined
30530+
| string
30531+
| EmbeddedSourceResult
30532+
| Promise<undefined | string | EmbeddedSourceResult>;
30533+
} & CssTransformOptions
30534+
): Promise<{
30535+
code: string;
30536+
map?: SourceMap;
30537+
warnings?: (string | Error)[];
30538+
errors?: (string | Error)[];
30539+
}>;
30540+
export namespace cssMinify {
30541+
export let supportsWorkerThreads: () => boolean;
30542+
export let getTypes: () => string[];
30543+
export let getEmbeddedTypes: () => string[];
30544+
export let filter: (name: string) => boolean;
30545+
}
3051530546
export let embeddedText: (
3051630547
answer?: string | { code?: string }
3051730548
) => undefined | string;
@@ -30824,6 +30855,40 @@ declare namespace exports {
3082430855
minimal?: boolean
3082530856
) => string;
3082630857
export let escapeText: (s: string) => string;
30858+
export function htmlMinify(
30859+
input: { [index: string]: string },
30860+
sourceMap?: RawSourceMap,
30861+
minimizerOptions?: Omit<
30862+
HtmlPrintOptions,
30863+
"renderEmbeddedSource" | "deferEmbeddedSource"
30864+
> & {
30865+
css?: {
30866+
convertLengthUnits?: boolean;
30867+
rewriteCustomProperties?: boolean;
30868+
unusedSymbols?: string[];
30869+
pseudoClasses?: { [index: string]: string };
30870+
};
30871+
minifyConditionalComments?: boolean;
30872+
renderEmbeddedSource?: (
30873+
source: string,
30874+
info: { type: string; hostType: string; as?: string }
30875+
) =>
30876+
| undefined
30877+
| string
30878+
| EmbeddedSourceResult
30879+
| Promise<undefined | string | EmbeddedSourceResult>;
30880+
}
30881+
): Promise<{
30882+
code: string;
30883+
warnings?: (string | Error)[];
30884+
errors?: (string | Error)[];
30885+
}>;
30886+
export namespace htmlMinify {
30887+
export let supportsWorkerThreads: () => boolean;
30888+
export let getTypes: () => string[];
30889+
export let getEmbeddedTypes: () => string[];
30890+
export let filter: (name: string) => boolean;
30891+
}
3082730892
export let isAsciiWhitespace: (cc: number) => boolean;
3082830893
export let metaTag: (name: string, content: string) => string;
3082930894
export let parseCssUrls: (input: string) => [string, number, number][];

0 commit comments

Comments
 (0)