Skip to content

Commit 1f42f0e

Browse files
fix: do not replace symbol @ in selectors
1 parent e5dfd23 commit 1f42f0e

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

src/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
8989
);
9090

9191
return hash
92-
.replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-')
92+
.replace(/\\@/g, '@')
93+
.replace(new RegExp('[^a-zA-Z0-9@\\-_\u00A0-\uFFFF]', 'g'), '-')
9394
.replace(/^((-?[0-9])|--)/, '_$1');
9495
}
9596

test/__snapshots__/localIdentName-option.test.js.snap

+92-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ Array [
3535
:local(.-a0-34a___f) {
3636
color: red;
3737
}
38-
",
38+
39+
:local(.m_x_\\\\@) {
40+
margin-left: auto !important;
41+
margin-right: auto !important;
42+
}",
3943
"",
4044
],
4145
]
@@ -78,7 +82,11 @@ Array [
7882
:local(.-a0-34a___f) {
7983
color: red;
8084
}
81-
",
85+
86+
:local(.m_x_\\\\@) {
87+
margin-left: auto !important;
88+
margin-right: auto !important;
89+
}",
8290
"",
8391
],
8492
]
@@ -121,7 +129,11 @@ Array [
121129
:local(.-a0-34a___f) {
122130
color: red;
123131
}
124-
",
132+
133+
:local(.m_x_\\\\@) {
134+
margin-left: auto !important;
135+
margin-right: auto !important;
136+
}",
125137
"",
126138
],
127139
]
@@ -164,7 +176,11 @@ Array [
164176
:local(.-a0-34a___f) {
165177
color: red;
166178
}
167-
",
179+
180+
:local(.m_x_\\\\@) {
181+
margin-left: auto !important;
182+
margin-right: auto !important;
183+
}",
168184
"",
169185
],
170186
]
@@ -207,7 +223,11 @@ Array [
207223
:local(.-a0-34a___f) {
208224
color: red;
209225
}
210-
",
226+
227+
:local(.m_x_\\\\@) {
228+
margin-left: auto !important;
229+
margin-right: auto !important;
230+
}",
211231
"",
212232
],
213233
]
@@ -250,7 +270,11 @@ Array [
250270
:local(.-a0-34a___f) {
251271
color: red;
252272
}
253-
",
273+
274+
:local(.m_x_\\\\@) {
275+
margin-left: auto !important;
276+
margin-right: auto !important;
277+
}",
254278
"",
255279
],
256280
]
@@ -293,10 +317,71 @@ Array [
293317
:local(.-a0-34a___f) {
294318
color: red;
295319
}
296-
",
320+
321+
:local(.m_x_\\\\@) {
322+
margin-left: auto !important;
323+
margin-right: auto !important;
324+
}",
297325
"",
298326
],
299327
]
300328
`;
301329

302330
exports[`localIdentName option should use hash prefix: warnings 1`] = `Array []`;
331+
332+
exports[`localIdentName option should сorrectly replace symbol @ in selector: errors 1`] = `Array []`;
333+
334+
exports[`localIdentName option should сorrectly replace symbol @ in selector: locals 1`] = `
335+
Object {
336+
"-a0-34a___f": "-a0-34a___f--2nJ5",
337+
"_test": "_test--23te",
338+
"className": "className--1E8H",
339+
"m_x_@": "m_x_@--2G3b",
340+
"someId": "someId--3w7J",
341+
"subClass": "subClass--3lo0",
342+
"test": "test--NW9Y",
343+
}
344+
`;
345+
346+
exports[`localIdentName option should сorrectly replace symbol @ in selector: module (evaluated) 1`] = `
347+
Array [
348+
Array [
349+
1,
350+
".test--NW9Y {
351+
background: red;
352+
}
353+
354+
._test--23te {
355+
background: blue;
356+
}
357+
358+
.className--1E8H {
359+
background: red;
360+
}
361+
362+
#someId--3w7J {
363+
background: green;
364+
}
365+
366+
.className--1E8H .subClass--3lo0 {
367+
color: green;
368+
}
369+
370+
#someId--3w7J .subClass--3lo0 {
371+
color: blue;
372+
}
373+
374+
.-a0-34a___f--2nJ5 {
375+
color: red;
376+
}
377+
378+
.m_x_@--2G3b {
379+
margin-left: auto !important;
380+
margin-right: auto !important;
381+
}",
382+
"",
383+
],
384+
]
385+
`;
386+
387+
exports[`localIdentName option should сorrectly replace symbol @ in selector: warnings 1`] = `Array []`;

test/fixtures/modules/localIdentName.css

+5
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@
2525
:local(.-a0-34a___f) {
2626
color: red;
2727
}
28+
29+
:local(.m_x_\@) {
30+
margin-left: auto !important;
31+
margin-right: auto !important;
32+
}

test/localIdentName-option.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,28 @@ describe('localIdentName option', () => {
117117
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
118118
expect(stats.compilation.errors).toMatchSnapshot('errors');
119119
});
120+
121+
it('should сorrectly replace symbol @ in selector', async () => {
122+
const config = {
123+
loader: {
124+
options: {
125+
importLoaders: 2,
126+
localIdentName: '[local]--[hash:base64:4]',
127+
modules: true,
128+
},
129+
},
130+
};
131+
const testId = './modules/localIdentName.css';
132+
const stats = await webpack(testId, config);
133+
const { modules } = stats.toJson();
134+
const module = modules.find((m) => m.id === testId);
135+
const evaluatedModule = evaluated(module.source, modules);
136+
137+
expect(evaluatedModule.locals['m_x_@']).toContain('m_x_@');
138+
139+
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
140+
expect(evaluatedModule.locals).toMatchSnapshot('locals');
141+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
142+
expect(stats.compilation.errors).toMatchSnapshot('errors');
143+
});
120144
});

0 commit comments

Comments
 (0)