This repository was archived by the owner on Sep 22, 2025. It is now read-only.
forked from Megaputer/dts-css-modules-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (125 loc) · 3.56 KB
/
index.js
File metadata and controls
143 lines (125 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// @ts-check
const fs = require('fs');
const fp = require('path');
const schema = require("./options.json");
/**
* @template T
* @typedef {Object} LoaderOptions<T>
* @type {{
* banner?: string,
* namedExport?: boolean,
* customTypings?: (classes: string[]) => string,
* dropEmptyFile?: boolean
* }}
* @property {string} [severityError] Allows to choose how errors are displayed.
*/
/**
* @template T
* @this {import("webpack").LoaderContext<LoaderOptions<T>>}
* @param {Buffer} content
*/
module.exports = function (content) {
this.cacheable && this.cacheable();
/**
* @type {{
* banner?: string,
* namedExport?: boolean,
* customTypings?: (classes: string[]) => string,
* dropEmptyFile?: boolean
* }}
*/
const options = this.getOptions(schema) || {};
const callback = this.async();
const classes = getClasses(content);
const dtsPath = getDtsPath(this.resourcePath);
if (options.dropEmptyFile && classes.length === 0) {
if (fs.existsSync(dtsPath)) {
fs.rmSync(dtsPath);
}
} else {
let typings = options.banner ? `${options.banner}\n` : '';
if (options.customTypings) {
typings = options.customTypings(classes);
} else if (options.namedExport) {
for (let c of classes) {
typings += `export const ${c}: string;\n`;
}
} else {
const i = getInterfaceName(this.resourcePath);
typings += `export interface ${i} {\n`;
for (let c of classes) {
typings += ` '${c}': string;\n`;
}
typings += `}\ndeclare const styles: ${i};\nexport = styles;\n`;
}
if (!fs.existsSync(dtsPath) || fs.readFileSync(dtsPath, "utf-8") != typings) {
fs.writeFileSync(dtsPath, typings, "utf8");
}
}
callback(null, content);
};
/**
* @param {string | Buffer} [content]
*/
function getClasses(content) {
if (Buffer.isBuffer(content)) {
content = content.toString('utf8');
}
/** @type {string[]} */
let classes = [];
let isCssLoaderNamedExport = false;
// check v4 / v5
let from = content.indexOf('___CSS_LOADER_EXPORT___.locals = {');
if (from === -1) {
// >= v5.2.5
from = content.indexOf('export var ');
if (from === -1) {
// < v5.2.5
from = content.indexOf('export const ');
}
isCssLoaderNamedExport = from !== -1;
}
// check v3
if (from === -1) {
// when `onlyLocals` is on
from = content.indexOf('module.exports = {');
}
if (from === -1) {
// when `onlyLocals` is off
from = content.indexOf('exports.locals = {');
}
// FIX START: fix for empty classes for gatsby
if (from === -1) {
from = content.indexOf('export default {')
}
// FIX END
if (~from) {
content = content.slice(from);
/** @type {RegExpExecArray} */
let match;
const regex = isCssLoaderNamedExport ? classesOfNamedExportRegex : classesRegex;
while ((match = regex.exec(content))) {
if (classes.indexOf(match[1]) === -1) {
classes.push(match[1]);
}
}
}
return classes;
}
const classesRegex = /"([^"\\/;()\n]+)":/g;
const classesOfNamedExportRegex = /export (?:var|const) (\w+) =/g;
/**
* @param {string} [path]
*/
function getDtsPath(path) {
return fp.join(fp.dirname(path), `${fp.basename(path)}.d.ts`);
}
/**
* @param {string} [path]
*/
function getInterfaceName(path) {
return fp
.basename(path)
.replace(/^(\w)/, (_, c) => 'I' + c.toUpperCase())
.replace(/\W+(\w)/g, (_, c) => c.toUpperCase());
}