Skip to content

Commit 56a6465

Browse files
authored
Auto-configure sourceMap option, based on compiler.devtool (#19)
1 parent 831eeb4 commit 56a6465

File tree

7 files changed

+65
-32
lines changed

7 files changed

+65
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 4.1.0
6+
7+
- Auto-configure `sourceMap` option, based on [`.devtool`](https://webpack.js.org/configuration/devtool/#devtool)
8+
59
## 4.0.0
610

711
- Migrate to the latest [`clean-css`](https://clean-css.github.io/) version `5.x.x`

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Then add the plugin to your webpack config. For example:
2929
const isProduction = process.env.NODE_ENV === "production";
3030

3131
module.exports = {
32-
mode: isProduction ? "production" : "development",
3332
module: {
3433
rules: [
3534
{
@@ -77,6 +76,10 @@ This option enables/disables minify, useful to easily disable on development mod
7776

7877
This option enables/disables output warnings (default: `false`)
7978

79+
#### `sourceMap: boolean`
80+
81+
Enables/Disables generation of source maps. (default: `compiler.devtool`)
82+
8083
## `CleanCSS` module options
8184

8285
- [clean-css/clean-css#constructor-options](https://github.com/jakubpawlowicz/clean-css#constructor-options)

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "clean-css-loader",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"main": "lib/index.js",
5-
"types": "lib/index.d.ts",
6-
"dependencies": {
5+
"types": "lib/index.d.ts",
6+
"dependencies": {
77
"clean-css": "^5.0.0"
88
},
99
"peerDependencies": {
@@ -19,7 +19,7 @@
1919
}
2020
},
2121
"scripts": {
22-
"pree2e": "yarn build",
22+
"pree2e": "yarn build",
2323
"e2e": "yarn update-e2e && yarn test",
2424
"force-clean": "rimraf ./test/e2e/webpack*/node_modules/ ./test/e2e/webpack*/tests/**/*.test.js",
2525
"install-sub": "yarn link && cd test/e2e/webpack5 && yarn && yarn build && cd ../../..",
@@ -36,7 +36,7 @@
3636
"loader"
3737
],
3838
"repository": "https://github.com/retyui/clean-css-loader",
39-
"author": "David <4661784+retyui@users.noreply.github.com>",
39+
"author": "David <4661784+retyui@users.noreply.github.com>",
4040
"license": "MIT",
4141
"files": ["lib"],
4242
"engines": {
@@ -53,6 +53,7 @@
5353
"prettier": "^2.4.1",
5454
"rimraf": "^3.0.2",
5555
"schema-utils": "^3.1.1",
56+
"source-maps": "^1.0.12",
5657
"typescript": "^4.4.3",
5758
"webpack": "^5.53.0"
5859
}

prettier.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/index.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CleanCSS from "clean-css";
22
import { getOptions } from "loader-utils";
33
import { validate } from "schema-utils";
4-
4+
import type { RawSourceMap } from "source-map";
55
import type { LoaderContext } from "webpack";
66
import type { JSONSchema7 } from "schema-utils/declarations/validate";
77

@@ -15,6 +15,7 @@ type CleanCSSOptions = Omit<
1515
interface LoaderOptions extends CleanCSSOptions {
1616
skipWarn?: boolean;
1717
disable?: boolean;
18+
sourceMap?: boolean;
1819
}
1920

2021
interface SourceMap {
@@ -32,6 +33,16 @@ interface AdditionalData {
3233
webpackAST: object;
3334
}
3435

36+
function parsePrevSourceMap(
37+
prevSourceMap?: string | SourceMap
38+
): RawSourceMap | string | undefined {
39+
if (prevSourceMap != null && typeof prevSourceMap === "object") {
40+
return JSON.stringify(prevSourceMap);
41+
}
42+
43+
return undefined;
44+
}
45+
3546
function cleanCssLoader(
3647
this: LoaderContext<LoaderOptions>,
3748
content: string | Buffer,
@@ -44,38 +55,42 @@ function cleanCssLoader(
4455
const loaderOptions = getOptions(this) || {};
4556

4657
validate(schema as JSONSchema7, loaderOptions, {
47-
name: "group-css-media-queries-loader",
58+
name: "clean-css-loader",
4859
});
4960

50-
const { disable, skipWarn, ...options } = loaderOptions;
61+
const { sourceMap, disable, skipWarn, ...options } = loaderOptions;
62+
const useSourceMap = Boolean(sourceMap ?? this.sourceMap);
5163

5264
if (disable) {
5365
return callback(null, content, prevSourceMap, additionalData);
5466
}
5567

5668
new CleanCSS({
57-
returnPromise: true,
5869
...options,
70+
returnPromise: true,
71+
sourceMap: useSourceMap,
5972
})
60-
.minify(
61-
content,
62-
// @ts-ignore
63-
prevSourceMap
64-
)
73+
.minify(content, parsePrevSourceMap(prevSourceMap))
6574
.then((output) => {
6675
if (!skipWarn && Array.isArray(output.warnings)) {
6776
output.warnings.forEach((warning) => {
6877
this.emitWarning(new Error(warning));
6978
});
7079
}
7180

72-
return callback(
73-
null,
74-
output.styles,
75-
// @ts-ignore
76-
output.sourceMap,
77-
additionalData
78-
);
81+
let resultSourceMap;
82+
83+
if (useSourceMap && output.sourceMap) {
84+
resultSourceMap = {
85+
...JSON.parse(output.sourceMap.toString()),
86+
// @ts-ignore
87+
sources: prevSourceMap?.sources || [this.resourcePath],
88+
// @ts-ignore
89+
sourcesContent: prevSourceMap?.sourcesContent || [content.toString()],
90+
};
91+
}
92+
93+
return callback(null, output.styles, resultSourceMap, additionalData);
7994
})
8095
.catch(callback);
8196
}

test/unit/test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ const runW5 = (input, options = {}) =>
1919
);
2020
});
2121

22-
const expectOutput = (input, output) =>
23-
expect(input.replace(/\r\n/g, "\n")).toEqual(output);
22+
const expectOutput = (input, output) => expect(input.replace(/\r\n/g, "\n")).toEqual(output);
2423

2524
describe("clean-css-loader", () => {
2625
describe("webpack 5", () => {
@@ -92,9 +91,7 @@ describe("clean-css-loader", () => {
9291
});
9392
expectOutput(css, "a{display:block}");
9493
expect(warn).toEqual([]);
95-
expect(map.toString()).toEqual(
96-
`{"version":3,"sources":["$stdin"],"names":[],"mappings":"AAAA,EAAI,QAAU"}`
97-
);
94+
expect(map).toEqual({ version: 3, sources: [undefined], names: [], mappings: "AAAA,EAAI,QAAU", sourcesContent: ["a { display : block; }"] });
9895
});
9996
});
10097
});

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,16 @@ neo-async@^2.6.2:
22702270
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
22712271
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==
22722272

2273+
node-addon-api@^2.0.0:
2274+
version "2.0.2"
2275+
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-2.0.2.tgz#432cfa82962ce494b132e9d72a15b29f71ff5d32"
2276+
integrity sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==
2277+
2278+
node-gyp-build@^4.2.1:
2279+
version "4.3.0"
2280+
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.3.0.tgz#9f256b03e5826150be39c764bf51e993946d71a3"
2281+
integrity sha512-iWjXZvmboq0ja1pUGULQBexmxq8CV4xBhX7VDOTbL7ZR4FOowwY/VOtRxBN/yKxmdGoIp4j5ysNT4u3S2pDQ3Q==
2282+
22732283
node-int64@^0.4.0:
22742284
version "0.4.0"
22752285
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
@@ -2585,6 +2595,14 @@ source-map@^0.7.3, source-map@~0.7.2:
25852595
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
25862596
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
25872597

2598+
source-maps@^1.0.12:
2599+
version "1.0.12"
2600+
resolved "https://registry.yarnpkg.com/source-maps/-/source-maps-1.0.12.tgz#0e0eee71102c0ace62a9fba8203a79989ca1f482"
2601+
integrity sha512-G7fZoB+oFf9lk+n59IxnRUnpDmz8vvTuGKZQQHXDhICLW9J0vvKhmQKFiSffEZ+DgLOAOSbRRA1NiJzf+BH0+Q==
2602+
dependencies:
2603+
node-addon-api "^2.0.0"
2604+
node-gyp-build "^4.2.1"
2605+
25882606
sprintf-js@~1.0.2:
25892607
version "1.0.3"
25902608
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"

0 commit comments

Comments
 (0)