Skip to content

Commit 53fc5af

Browse files
committed
Auto define source map options (#15)
- [`.devtool`](https://webpack.js.org/configuration/devtool/#devtool)
1 parent 7a302df commit 53fc5af

File tree

8 files changed

+229
-1307
lines changed

8 files changed

+229
-1307
lines changed

CHANGELOG.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Change Log
22

3+
## 4.2.0
4+
5+
- Auto-configure `sourceMap` option, based on [`.devtool`](https://webpack.js.org/configuration/devtool/#devtool)
6+
37
## 4.1.0
48

5-
- Add typescript
9+
- Fix [invalid](https://github.com/retyui/group-css-media-queries-loader/issues/11#issuecomment-925331021) `postcss` import
10+
- Add loader options Schema validation
611
- Add E2E tests
7-
- Fix [invalid](https://github.com/retyui/group-css-media-queries-loader/issues/11#issuecomment-925331021) `postcss` import
12+
- Add typescript
813

914
## 4.0.0
1015

@@ -23,6 +28,6 @@
2328

2429
## 2.0.0
2530

26-
- deprecated node@4.x.x
27-
- Deprecated webpack@1.x.x
28-
- Added `sourceMap: true|false` options (based on postcss)
31+
- deprecated `node@4.x.x`
32+
- Deprecated `webpack@1.x.x`
33+
- Added `sourceMap: boolean` options (based on postcss)

README.md

+10-23
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,28 @@ A [group-css-media-queries](https://github.com/Se7enSky/group-css-media-queries)
77
[![CI](https://github.com/retyui/group-css-media-queries-loader/actions/workflows/nodejs.yml/badge.svg)](https://github.com/retyui/group-css-media-queries-loader/actions/workflows/nodejs.yml)
88
[![count downloads group-css-media-queries-loader](https://img.shields.io/npm/dm/group-css-media-queries-loader.svg)](https://www.npmjs.com/package/group-css-media-queries-loader)
99

10-
## Install
10+
## Getting Started
11+
12+
To begin, you'll need to install loader:
1113

1214
```bash
1315
yarn add -D group-css-media-queries-loader
1416
```
1517

16-
## Usage
17-
18-
Use the loader either via your webpack config, CLI or inline.
19-
20-
### Via webpack config (recommended)
21-
22-
**webpack.config.js**
18+
Then add the plugin to your webpack config. For example:
2319

24-
```js
25-
const sourceMap = true;
20+
```tsx
21+
// webpack.config.js
2622

2723
module.exports = {
28-
devtool: sourceMap && "source-map",
2924
module: {
3025
rules: [
3126
{
3227
test: /\.css$/,
3328
use: [
3429
"style-loader",
35-
{
36-
loader: "css-loader",
37-
options: { sourceMap },
38-
},
39-
// 'group-css-media-queries-loader',
40-
// or with config
41-
{
42-
loader: "group-css-media-queries-loader",
43-
options: { sourceMap },
44-
},
30+
"css-loader",
31+
"group-css-media-queries-loader",
4532
],
4633
},
4734
],
@@ -51,6 +38,6 @@ module.exports = {
5138

5239
## Options
5340

54-
#### **`sourceMap`** boolean `true` or `false`
41+
#### `sourceMap: boolean`
5542

56-
Enable CSS source maps.
43+
Enables/Disables generation of source maps. (default: `compiler.devtool`)

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "group-css-media-queries-loader",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"main": "lib/index.js",
55
"types": "lib/index.d.ts",
66
"files": ["lib"],
@@ -28,6 +28,9 @@
2828
"test": "node ./test/test.js && cd example/webpack5 && yarn --ignore-engines && yarn build && cd ../..",
2929
"prepublishOnly": "npm run test"
3030
},
31+
"optionalDependencies": {
32+
"postcss-loader": "^6.0.0"
33+
},
3134
"devDependencies": {
3235
"@tsconfig/node10": "^1.0.1",
3336
"@types/loader-utils": "^2.0.3",
@@ -43,6 +46,6 @@
4346
"node": ">=10.0.0"
4447
},
4548
"repository": "https://github.com/retyui/group-css-media-queries-loader",
46-
"author": "David Narbutovich <vamnemne@gmail.com>",
49+
"author": "David <4661784+retyui@users.noreply.github.com>",
4750
"license": "MIT"
4851
}

src/index.ts

+10-39
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,41 @@ import {
55
getCurrentRequest,
66
} from "loader-utils";
77
import { validate } from "schema-utils";
8+
import type { JSONSchema7 } from "schema-utils/declarations/validate";
89
import type { LoaderContext } from "webpack";
10+
import type { LoaderOptions, SourceMap, AdditionalData } from "./types";
11+
import initSourceMaps from "./source-map";
912
import schema from "./schema.json";
10-
const GroupCssMediaQueriesPostCssPlugin = require("./group-css-media-queries");
11-
12-
interface LoaderOptions {
13-
sourceMap?: boolean;
14-
}
1513

16-
interface SourceMap {
17-
version: number;
18-
sources: string[];
19-
mappings: string;
20-
file?: string;
21-
sourceRoot?: string;
22-
sourcesContent?: string[];
23-
names?: string[];
24-
}
25-
26-
interface AdditionalData {
27-
[index: string]: any;
28-
webpackAST: object;
29-
}
14+
const GroupCssMediaQueriesPostCssPlugin = require("./group-css-media-queries");
3015

3116
function GroupCssMediaQueriesLoader(
3217
this: LoaderContext<LoaderOptions>,
3318
content: string | Buffer,
3419
prevSourceMap?: string | SourceMap,
3520
additionalData?: AdditionalData
3621
) {
37-
// 1.x.x return null if empty query
38-
// 2.x.x return empty object if empty query
22+
// 1.x.x return `null` if empty query
23+
// 2.x.x return empty `object` if empty query
3924
const loaderOptions = getOptions(this) || {};
4025

41-
// @ts-ignore
42-
validate(schema, loaderOptions, {
26+
validate(schema as JSONSchema7, loaderOptions, {
4327
name: "group-css-media-queries-loader",
4428
});
4529

4630
const callback = this.async();
31+
const sourceMap = initSourceMaps(loaderOptions, () => this);
4732

48-
// @ts-ignore
4933
const pipeline = postcss(GroupCssMediaQueriesPostCssPlugin);
5034

5135
pipeline
5236
.process(content, {
5337
from: getRemainingRequest(this).split("!").pop(),
5438
to: getCurrentRequest(this).split("!").pop(),
55-
map: loaderOptions.sourceMap
56-
? {
57-
prev: prevSourceMap,
58-
inline: false,
59-
annotation: false,
60-
sourcesContent: true,
61-
}
62-
: undefined,
39+
map: sourceMap.getOptions(prevSourceMap),
6340
})
6441
.then((result) =>
65-
callback(
66-
null,
67-
result.css,
68-
// @ts-ignore
69-
result.map?.toJSON(),
70-
additionalData
71-
)
42+
callback(null, result.css, sourceMap.onResult(result), additionalData)
7243
)
7344
.catch(callback);
7445
}

src/schema.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "group-css-media-queries-loader options",
24
"type": "object",
35
"properties": {
46
"sourceMap": {

src/source-map.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { LoaderContext } from "webpack";
2+
import type { Result } from "postcss";
3+
import type { LoaderOptions, SourceMap } from "./types";
4+
5+
const initSourceMaps = (
6+
options: LoaderOptions,
7+
getLoaderContext: () => LoaderContext<LoaderOptions>
8+
) => {
9+
const enabled = options.sourceMap ?? getLoaderContext().sourceMap;
10+
11+
const normalizeSourceMapBefore = (
12+
prevSourceMap?: string | SourceMap
13+
): string | SourceMap | undefined => {
14+
try {
15+
const { normalizeSourceMap } = require("postcss-loader/dist/utils.js");
16+
17+
return (
18+
prevSourceMap &&
19+
normalizeSourceMap(prevSourceMap, getLoaderContext().context)
20+
);
21+
} catch {
22+
return prevSourceMap;
23+
}
24+
};
25+
26+
return {
27+
getOptions: (prevSourceMap?: string | SourceMap) => {
28+
if (!enabled) {
29+
return undefined;
30+
}
31+
32+
return {
33+
prev: normalizeSourceMapBefore(prevSourceMap),
34+
inline: false,
35+
annotation: false,
36+
sourcesContent: true,
37+
};
38+
},
39+
onResult: (result: Result): SourceMap | undefined => {
40+
// @ts-ignore
41+
const map = result?.map?.toJSON?.() as SourceMap | undefined;
42+
43+
try {
44+
const {
45+
normalizeSourceMapAfterPostcss,
46+
} = require("postcss-loader/dist/utils.js");
47+
48+
if (enabled && map) {
49+
return normalizeSourceMapAfterPostcss(
50+
map,
51+
getLoaderContext().context
52+
);
53+
}
54+
55+
return map;
56+
} catch {
57+
return map;
58+
}
59+
},
60+
};
61+
};
62+
63+
export = initSourceMaps;

src/types.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface LoaderOptions {
2+
sourceMap?: boolean;
3+
}
4+
5+
export interface SourceMap {
6+
version: number;
7+
sources: string[];
8+
mappings: string;
9+
file?: string;
10+
sourceRoot?: string;
11+
sourcesContent?: string[];
12+
names?: string[];
13+
}
14+
15+
export interface AdditionalData {
16+
[index: string]: any;
17+
webpackAST: object;
18+
}

0 commit comments

Comments
 (0)