Skip to content

Commit c7404a3

Browse files
author
Michael Vurchio
committed
Consider joint selector only (no parents/children) & media queries
1 parent ef7d3ac commit c7404a3

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

index.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const regex = {
1616
class: (className) => {
1717
return new RegExp(`\\.(${className})\\b(?![-_])`, 'gm');
1818
},
19+
classSelector: (className) => {
20+
return new RegExp(`\\S*\\.(${className})\\b(?![-_])\\S*`, 'gm');
21+
},
1922
};
2023

2124
let moduleClasses = {};
@@ -125,7 +128,6 @@ const markup = async ({ content, filename }) => {
125128
};
126129
};
127130

128-
const GLOBALIZE_PLACEHOLDER = '__to_globalize__';
129131
const style = async ({ content, filename }) => {
130132
let code = content;
131133

@@ -141,36 +143,21 @@ const style = async ({ content, filename }) => {
141143

142144
for (const className in classes) {
143145
code = code.replace(
144-
regex.class(className),
145-
() => `.${GLOBALIZE_PLACEHOLDER}${classes[className]}`
146-
);
147-
}
146+
regex.classSelector(className),
147+
(match) => {
148+
const generatedClass = match.replace(
149+
regex.class(className),
150+
() => `.${classes[className]}`
151+
);
148152

149-
let codeOutput = '';
150-
let cssRules = code.split('}');
151-
152-
// Remove last element of the split. It should be the empty string that comes after the last '}'.
153-
const lastChunk = cssRules.pop();
154-
155-
// We wrap all css selector containing a scoped CSS class in :global() svelte css statement
156-
for (const cssRule of cssRules) {
157-
let [selector, rule] = cssRule.split('{');
158-
if (selector.includes(GLOBALIZE_PLACEHOLDER)) {
159-
const selectorTrimmed = selector.trim();
160-
selector = selector.replace(
161-
selectorTrimmed,
162-
`:global(${selectorTrimmed.replace(
163-
new RegExp(GLOBALIZE_PLACEHOLDER, 'g'),
164-
''
165-
)})`
166-
);
167-
}
168-
codeOutput += `${selector}{${rule}}`;
153+
return generatedClass.indexOf(':global(') !== -1
154+
? generatedClass
155+
: `:global(${generatedClass})`;
156+
}
157+
);
169158
}
170159

171-
codeOutput += lastChunk;
172-
173-
return { code: codeOutput };
160+
return { code };
174161
};
175162

176163
module.exports = (options) => {

0 commit comments

Comments
 (0)