Skip to content

Commit 950c715

Browse files
committed
Fix a query params parsing using this.getOptions() from webpack 5
`loader-utils` has specific behavior for parsing query strings (true, false and null won't be parsed as string but as a primitive value) https://webpack.js.org/migrate/5/#getoptions-method-for-loaders
1 parent cea265b commit 950c715

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/index.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,43 @@ function parsePrevSourceMap(
4343
return undefined;
4444
}
4545

46+
function getLoaderOptions(_that: LoaderContext<LoaderOptions>): LoaderOptions {
47+
// 3.x.x removed `getOptions` need to use `this.getOptions`
48+
if(typeof getOptions === 'function') {
49+
// 1.x.x return null if empty query
50+
// 2.x.x return empty object if empty query
51+
return getOptions(_that) || {};
52+
}
53+
54+
if(typeof _that.getOptions === 'function') {
55+
const rawOptions:LoaderOptions = _that.getOptions() || {};
56+
57+
// `loader-utils` has specific behavior for parsing query strings
58+
// (true, false and null won't be parsed as string but as a primitive value)
59+
// https://webpack.js.org/migrate/5/#getoptions-method-for-loaders
60+
Object.keys(rawOptions).forEach((key) => {
61+
const value = rawOptions[key as keyof LoaderOptions];
62+
63+
if(['false','true','null'].includes(value as string)) {
64+
rawOptions[key as keyof LoaderOptions] = JSON.parse(value as string);
65+
}
66+
});
67+
68+
return rawOptions;
69+
}
70+
71+
return {};
72+
}
73+
4674
function cleanCssLoader(
4775
this: LoaderContext<LoaderOptions>,
4876
content: string | Buffer,
4977
prevSourceMap?: string | SourceMap,
5078
additionalData?: AdditionalData
5179
): void {
5280
const callback = this.async();
53-
// 1.x.x return null if empty query
54-
// 2.x.x return empty object if empty query
55-
// 3.x.x removed `getOptions` need to use `this.getOptions`
56-
const loaderOptions = (typeof this.getOptions === 'function' ? this.getOptions() : getOptions?.(this)) || {};
81+
82+
const loaderOptions = getLoaderOptions(this);
5783

5884
validate(schema as JSONSchema7, loaderOptions, {
5985
name: "clean-css-loader",

test/unit/test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe("clean-css-loader", () => {
7777
expect(map).toBeUndefined();
7878
});
7979

80-
it("should skiped Warnings", async () => {
80+
it("should skipped Warnings", async () => {
8181
const [css, map, warn] = await runW5("a{display:block", {
8282
skipWarn: true,
8383
});
@@ -86,6 +86,16 @@ describe("clean-css-loader", () => {
8686
expect(map).toBeUndefined();
8787
});
8888

89+
it("should skipped Warnings and parse bool params", async () => {
90+
const [css, map, warn] = await runW5("a{display:block", {
91+
skipWarn: 'true',
92+
});
93+
expectOutput(css, "a{display:block}");
94+
expect(warn).toEqual([]);
95+
expect(map).toBeUndefined();
96+
});
97+
98+
8999
it("should return sourceMap", async () => {
90100
const [css, map, warn] = await runW5("a { display : block; }", {
91101
sourceMap: true,

0 commit comments

Comments
 (0)