@lwc/style-compiler 2.45.2 → 2.45.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +911 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.js +907 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -20
- package/dist/commonjs/css-import/transform.js +0 -41
- package/dist/commonjs/css-import/transform.js.map +0 -1
- package/dist/commonjs/custom-properties/transform.js +0 -23
- package/dist/commonjs/custom-properties/transform.js.map +0 -1
- package/dist/commonjs/dir-pseudo-class/transform.js +0 -74
- package/dist/commonjs/dir-pseudo-class/transform.js.map +0 -1
- package/dist/commonjs/index.js +0 -24
- package/dist/commonjs/index.js.map +0 -1
- package/dist/commonjs/no-id-selectors/validate.js +0 -13
- package/dist/commonjs/no-id-selectors/validate.js.map +0 -1
- package/dist/commonjs/postcss-lwc-plugin.js +0 -64
- package/dist/commonjs/postcss-lwc-plugin.js.map +0 -1
- package/dist/commonjs/scope-at-rules/transform.js +0 -53
- package/dist/commonjs/scope-at-rules/transform.js.map +0 -1
- package/dist/commonjs/selector-scoping/transform.js +0 -123
- package/dist/commonjs/selector-scoping/transform.js.map +0 -1
- package/dist/commonjs/selector-scoping/validate.js +0 -50
- package/dist/commonjs/selector-scoping/validate.js.map +0 -1
- package/dist/commonjs/serialize.js +0 -430
- package/dist/commonjs/serialize.js.map +0 -1
- package/dist/commonjs/transform.js +0 -26
- package/dist/commonjs/transform.js.map +0 -1
- package/dist/commonjs/utils/dir-pseudoclass.js +0 -14
- package/dist/commonjs/utils/dir-pseudoclass.js.map +0 -1
- package/dist/commonjs/utils/message.js +0 -31
- package/dist/commonjs/utils/message.js.map +0 -1
- package/dist/commonjs/utils/rtl.js +0 -15
- package/dist/commonjs/utils/rtl.js.map +0 -1
- package/dist/commonjs/utils/selector-parser.js +0 -28
- package/dist/commonjs/utils/selector-parser.js.map +0 -1
- package/dist/commonjs/utils/selectors-scoping.js +0 -12
- package/dist/commonjs/utils/selectors-scoping.js.map +0 -1
- /package/dist/{types/css-import → css-import}/transform.d.ts +0 -0
- /package/dist/{types/custom-properties → custom-properties}/transform.d.ts +0 -0
- /package/dist/{types/dir-pseudo-class → dir-pseudo-class}/transform.d.ts +0 -0
- /package/dist/{types/index.d.ts → index.d.ts} +0 -0
- /package/dist/{types/no-id-selectors → no-id-selectors}/validate.d.ts +0 -0
- /package/dist/{types/postcss-lwc-plugin.d.ts → postcss-lwc-plugin.d.ts} +0 -0
- /package/dist/{types/scope-at-rules → scope-at-rules}/transform.d.ts +0 -0
- /package/dist/{types/selector-scoping → selector-scoping}/transform.d.ts +0 -0
- /package/dist/{types/selector-scoping → selector-scoping}/validate.d.ts +0 -0
- /package/dist/{types/serialize.d.ts → serialize.d.ts} +0 -0
- /package/dist/{types/transform.d.ts → transform.d.ts} +0 -0
- /package/dist/{types/utils → utils}/dir-pseudoclass.d.ts +0 -0
- /package/dist/{types/utils → utils}/message.d.ts +0 -0
- /package/dist/{types/utils → utils}/rtl.d.ts +0 -0
- /package/dist/{types/utils → utils}/selector-parser.d.ts +0 -0
- /package/dist/{types/utils → utils}/selectors-scoping.d.ts +0 -0
package/dist/index.js
ADDED
@@ -0,0 +1,907 @@
|
|
1
|
+
/* proxy-compat-disable */
|
2
|
+
/**
|
3
|
+
* Copyright (C) 2023 salesforce.com, inc.
|
4
|
+
*/
|
5
|
+
import postcss from 'postcss';
|
6
|
+
import postcssValueParser from 'postcss-value-parser';
|
7
|
+
import matchAll from 'string.prototype.matchall';
|
8
|
+
import { LWC_VERSION_COMMENT, KEY__SCOPED_CSS } from '@lwc/shared';
|
9
|
+
import postCssSelector, { isPseudoClass, isCombinator, isPseudoElement, attribute, combinator } from 'postcss-selector-parser';
|
10
|
+
|
11
|
+
const PLUGIN_NAME = '@lwc/style-compiler';
|
12
|
+
const IMPORT_TYPE = 'import';
|
13
|
+
const VAR_FUNCTION_TYPE = 'var-function';
|
14
|
+
function importMessage(id) {
|
15
|
+
return {
|
16
|
+
plugin: PLUGIN_NAME,
|
17
|
+
type: IMPORT_TYPE,
|
18
|
+
id,
|
19
|
+
};
|
20
|
+
}
|
21
|
+
function isImportMessage(message) {
|
22
|
+
return message.type === IMPORT_TYPE && message.id;
|
23
|
+
}
|
24
|
+
function varFunctionMessage(original) {
|
25
|
+
return {
|
26
|
+
plugin: PLUGIN_NAME,
|
27
|
+
type: VAR_FUNCTION_TYPE,
|
28
|
+
original,
|
29
|
+
};
|
30
|
+
}
|
31
|
+
function isVarFunctionMessage(message) {
|
32
|
+
return message.type === VAR_FUNCTION_TYPE && message.original;
|
33
|
+
}
|
34
|
+
|
35
|
+
/*
|
36
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
37
|
+
* All rights reserved.
|
38
|
+
* SPDX-License-Identifier: MIT
|
39
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
40
|
+
*/
|
41
|
+
const HOST_ATTRIBUTE = '__hostAttribute__';
|
42
|
+
const SHADOW_ATTRIBUTE = '__shadowAttribute__';
|
43
|
+
|
44
|
+
/*
|
45
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
46
|
+
* All rights reserved.
|
47
|
+
* SPDX-License-Identifier: MIT
|
48
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
49
|
+
*/
|
50
|
+
const DIR_ATTRIBUTE_NATIVE_LTR = `__dirAttributeNativeLtr__`;
|
51
|
+
const DIR_ATTRIBUTE_NATIVE_RTL = `__dirAttributeNativeRtl__`;
|
52
|
+
const DIR_ATTRIBUTE_SYNTHETIC_LTR = `__dirAttributeSyntheticLtr__`;
|
53
|
+
const DIR_ATTRIBUTE_SYNTHETIC_RTL = `__dirAttributeSyntheticRtl__`;
|
54
|
+
|
55
|
+
/*
|
56
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
57
|
+
* All rights reserved.
|
58
|
+
* SPDX-License-Identifier: MIT
|
59
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
60
|
+
*/
|
61
|
+
var TokenType;
|
62
|
+
(function (TokenType) {
|
63
|
+
TokenType["text"] = "text";
|
64
|
+
TokenType["expression"] = "expression";
|
65
|
+
TokenType["identifier"] = "identifier";
|
66
|
+
TokenType["divider"] = "divider";
|
67
|
+
})(TokenType || (TokenType = {}));
|
68
|
+
// "1400 binary expressions are enough to reach Node.js maximum call stack size"
|
69
|
+
// https://github.com/salesforce/lwc/issues/1726
|
70
|
+
// The vast majority of stylesheet functions are much less than this, so we can set the limit lower
|
71
|
+
// to play it safe.
|
72
|
+
const BINARY_EXPRESSION_LIMIT = 100;
|
73
|
+
// Javascript identifiers used for the generation of the style module
|
74
|
+
const HOST_SELECTOR_IDENTIFIER = 'hostSelector';
|
75
|
+
const SHADOW_SELECTOR_IDENTIFIER = 'shadowSelector';
|
76
|
+
const SUFFIX_TOKEN_IDENTIFIER = 'suffixToken';
|
77
|
+
const USE_ACTUAL_HOST_SELECTOR = 'useActualHostSelector';
|
78
|
+
const USE_NATIVE_DIR_PSEUDOCLASS = 'useNativeDirPseudoclass';
|
79
|
+
const TOKEN = 'token';
|
80
|
+
const STYLESHEET_IDENTIFIER = 'stylesheet';
|
81
|
+
const VAR_RESOLVER_IDENTIFIER = 'varResolver';
|
82
|
+
function serialize(result, config) {
|
83
|
+
const { messages } = result;
|
84
|
+
const collectVarFunctions = Boolean(config.customProperties && config.customProperties.resolverModule);
|
85
|
+
const useVarResolver = messages.some(isVarFunctionMessage);
|
86
|
+
const importedStylesheets = messages.filter(isImportMessage).map((message) => message.id);
|
87
|
+
const disableSyntheticShadow = Boolean(config.disableSyntheticShadowSupport);
|
88
|
+
const scoped = Boolean(config.scoped);
|
89
|
+
let buffer = '';
|
90
|
+
if (collectVarFunctions && useVarResolver) {
|
91
|
+
buffer += `import ${VAR_RESOLVER_IDENTIFIER} from "${config.customProperties
|
92
|
+
.resolverModule}";\n`;
|
93
|
+
}
|
94
|
+
for (let i = 0; i < importedStylesheets.length; i++) {
|
95
|
+
buffer += `import ${STYLESHEET_IDENTIFIER + i} from "${importedStylesheets[i]}";\n`;
|
96
|
+
}
|
97
|
+
if (importedStylesheets.length) {
|
98
|
+
buffer += '\n';
|
99
|
+
}
|
100
|
+
const stylesheetList = importedStylesheets.map((_str, i) => `${STYLESHEET_IDENTIFIER + i}`);
|
101
|
+
const serializedStyle = serializeCss(result, collectVarFunctions).trim();
|
102
|
+
if (serializedStyle) {
|
103
|
+
// inline function
|
104
|
+
if (disableSyntheticShadow && !scoped) {
|
105
|
+
// If synthetic shadow DOM support is disabled and this is not a scoped stylesheet, then the
|
106
|
+
// function signature will always be:
|
107
|
+
// stylesheet(token = undefined, useActualHostSelector = true, useNativeDirPseudoclass = true)
|
108
|
+
// This means that we can just have a function that takes no arguments and returns a string,
|
109
|
+
// reducing the bundle size when minified.
|
110
|
+
buffer += `function ${STYLESHEET_IDENTIFIER}() {\n`;
|
111
|
+
buffer += ` var ${TOKEN};\n`; // undefined
|
112
|
+
buffer += ` var ${USE_ACTUAL_HOST_SELECTOR} = true;\n`;
|
113
|
+
buffer += ` var ${USE_NATIVE_DIR_PSEUDOCLASS} = true;\n`;
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
buffer += `function ${STYLESHEET_IDENTIFIER}(${TOKEN}, ${USE_ACTUAL_HOST_SELECTOR}, ${USE_NATIVE_DIR_PSEUDOCLASS}) {\n`;
|
117
|
+
}
|
118
|
+
// For scoped stylesheets, we use classes, but for synthetic shadow DOM, we use attributes
|
119
|
+
if (scoped) {
|
120
|
+
buffer += ` var ${SHADOW_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("." + ${TOKEN}) : "";\n`;
|
121
|
+
buffer += ` var ${HOST_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("." + ${TOKEN} + "-host") : "";\n`;
|
122
|
+
}
|
123
|
+
else {
|
124
|
+
buffer += ` var ${SHADOW_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("[" + ${TOKEN} + "]") : "";\n`;
|
125
|
+
buffer += ` var ${HOST_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("[" + ${TOKEN} + "-host]") : "";\n`;
|
126
|
+
}
|
127
|
+
// Used for keyframes
|
128
|
+
buffer += ` var ${SUFFIX_TOKEN_IDENTIFIER} = ${TOKEN} ? ("-" + ${TOKEN}) : "";\n`;
|
129
|
+
buffer += ` return ${serializedStyle};\n`;
|
130
|
+
buffer += ` /*${LWC_VERSION_COMMENT}*/\n`;
|
131
|
+
buffer += `}\n`;
|
132
|
+
if (scoped) {
|
133
|
+
// Mark the stylesheet as scoped so that we can distinguish it later at runtime
|
134
|
+
buffer += `${STYLESHEET_IDENTIFIER}.${KEY__SCOPED_CSS} = true;\n`;
|
135
|
+
}
|
136
|
+
// add import at the end
|
137
|
+
stylesheetList.push(STYLESHEET_IDENTIFIER);
|
138
|
+
}
|
139
|
+
// exports
|
140
|
+
if (stylesheetList.length) {
|
141
|
+
buffer += `export default [${stylesheetList.join(', ')}];`;
|
142
|
+
}
|
143
|
+
else {
|
144
|
+
buffer += `export default undefined;`;
|
145
|
+
}
|
146
|
+
return buffer;
|
147
|
+
}
|
148
|
+
function reduceTokens(tokens) {
|
149
|
+
return [{ type: TokenType.text, value: '' }, ...tokens, { type: TokenType.text, value: '' }]
|
150
|
+
.reduce((acc, token) => {
|
151
|
+
const prev = acc[acc.length - 1];
|
152
|
+
if (token.type === TokenType.text && prev && prev.type === TokenType.text) {
|
153
|
+
// clone the previous token to avoid mutating it in-place
|
154
|
+
acc[acc.length - 1] = {
|
155
|
+
type: prev.type,
|
156
|
+
value: prev.value + token.value,
|
157
|
+
};
|
158
|
+
return acc;
|
159
|
+
}
|
160
|
+
else {
|
161
|
+
return [...acc, token];
|
162
|
+
}
|
163
|
+
}, [])
|
164
|
+
.filter((t) => t.value !== '');
|
165
|
+
}
|
166
|
+
function normalizeString(str) {
|
167
|
+
return str.replace(/(\r\n\t|\n|\r\t)/gm, '').trim();
|
168
|
+
}
|
169
|
+
function generateExpressionFromTokens(tokens) {
|
170
|
+
const serializedTokens = reduceTokens(tokens).map(({ type, value }) => {
|
171
|
+
switch (type) {
|
172
|
+
// Note that we don't expect to get a TokenType.divider here. It should be converted into an
|
173
|
+
// expression elsewhere.
|
174
|
+
case TokenType.text:
|
175
|
+
return JSON.stringify(value);
|
176
|
+
// Expressions may be concatenated with " + ", in which case we must remove ambiguity
|
177
|
+
case TokenType.expression:
|
178
|
+
return `(${value})`;
|
179
|
+
default:
|
180
|
+
return value;
|
181
|
+
}
|
182
|
+
});
|
183
|
+
if (serializedTokens.length === 0) {
|
184
|
+
return '';
|
185
|
+
}
|
186
|
+
else if (serializedTokens.length === 1) {
|
187
|
+
return serializedTokens[0];
|
188
|
+
}
|
189
|
+
else if (serializedTokens.length < BINARY_EXPRESSION_LIMIT) {
|
190
|
+
return serializedTokens.join(' + ');
|
191
|
+
}
|
192
|
+
else {
|
193
|
+
// #1726 Using Array.prototype.join() instead of a standard "+" operator to concatenate the
|
194
|
+
// string to avoid running into a maximum call stack error when the stylesheet is parsed
|
195
|
+
// again by the bundler.
|
196
|
+
return `[${serializedTokens.join(', ')}].join('')`;
|
197
|
+
}
|
198
|
+
}
|
199
|
+
function areTokensEqual(left, right) {
|
200
|
+
return left.type === right.type && left.value === right.value;
|
201
|
+
}
|
202
|
+
function calculateNumDuplicatedTokens(left, right) {
|
203
|
+
// Walk backwards until we find a token that is different between left and right
|
204
|
+
let i = 0;
|
205
|
+
for (; i < left.length && i < right.length; i++) {
|
206
|
+
const currentLeft = left[left.length - 1 - i];
|
207
|
+
const currentRight = right[right.length - 1 - i];
|
208
|
+
if (!areTokensEqual(currentLeft, currentRight)) {
|
209
|
+
break;
|
210
|
+
}
|
211
|
+
}
|
212
|
+
return i;
|
213
|
+
}
|
214
|
+
// For `:host` selectors, the token lists for native vs synthetic will be identical at the end of
|
215
|
+
// each list. So as an optimization, we can de-dup these tokens.
|
216
|
+
// See: https://github.com/salesforce/lwc/issues/3224#issuecomment-1353520052
|
217
|
+
function deduplicateHostTokens(nativeHostTokens, syntheticHostTokens) {
|
218
|
+
const numDuplicatedTokens = calculateNumDuplicatedTokens(nativeHostTokens, syntheticHostTokens);
|
219
|
+
const numUniqueNativeTokens = nativeHostTokens.length - numDuplicatedTokens;
|
220
|
+
const numUniqueSyntheticTokens = syntheticHostTokens.length - numDuplicatedTokens;
|
221
|
+
const uniqueNativeTokens = nativeHostTokens.slice(0, numUniqueNativeTokens);
|
222
|
+
const uniqueSyntheticTokens = syntheticHostTokens.slice(0, numUniqueSyntheticTokens);
|
223
|
+
const nativeExpression = generateExpressionFromTokens(uniqueNativeTokens);
|
224
|
+
const syntheticExpression = generateExpressionFromTokens(uniqueSyntheticTokens);
|
225
|
+
// Generate a conditional ternary to switch between native vs synthetic for the unique tokens
|
226
|
+
const conditionalToken = {
|
227
|
+
type: TokenType.expression,
|
228
|
+
value: `(${USE_ACTUAL_HOST_SELECTOR} ? ${nativeExpression} : ${syntheticExpression})`,
|
229
|
+
};
|
230
|
+
return [
|
231
|
+
conditionalToken,
|
232
|
+
// The remaining tokens are the same between native and synthetic
|
233
|
+
...syntheticHostTokens.slice(numUniqueSyntheticTokens),
|
234
|
+
];
|
235
|
+
}
|
236
|
+
function serializeCss(result, collectVarFunctions) {
|
237
|
+
const tokens = [];
|
238
|
+
let currentRuleTokens = [];
|
239
|
+
let nativeHostTokens;
|
240
|
+
// Walk though all nodes in the CSS...
|
241
|
+
postcss.stringify(result.root, (part, node, nodePosition) => {
|
242
|
+
// When consuming the beginning of a rule, first we tokenize the selector
|
243
|
+
if (node && node.type === 'rule' && nodePosition === 'start') {
|
244
|
+
currentRuleTokens.push(...tokenizeCss(normalizeString(part)));
|
245
|
+
// When consuming the end of a rule we normalize it and produce a new one
|
246
|
+
}
|
247
|
+
else if (node && node.type === 'rule' && nodePosition === 'end') {
|
248
|
+
currentRuleTokens.push({ type: TokenType.text, value: part });
|
249
|
+
// If we are in synthetic shadow or scoped light DOM, we don't want to have native :host selectors
|
250
|
+
// Note that postcss-lwc-plugin should ensure that _isNativeHost appears before _isSyntheticHost
|
251
|
+
if (node._isNativeHost) {
|
252
|
+
// Save native tokens so in the next rule we can apply a conditional ternary
|
253
|
+
nativeHostTokens = [...currentRuleTokens];
|
254
|
+
}
|
255
|
+
else if (node._isSyntheticHost) {
|
256
|
+
/* istanbul ignore if */
|
257
|
+
if (!nativeHostTokens) {
|
258
|
+
throw new Error('Unexpected host rules ordering');
|
259
|
+
}
|
260
|
+
const hostTokens = deduplicateHostTokens(nativeHostTokens, currentRuleTokens);
|
261
|
+
tokens.push(...hostTokens);
|
262
|
+
nativeHostTokens = undefined;
|
263
|
+
}
|
264
|
+
else {
|
265
|
+
/* istanbul ignore if */
|
266
|
+
if (nativeHostTokens) {
|
267
|
+
throw new Error('Unexpected host rules ordering');
|
268
|
+
}
|
269
|
+
tokens.push(...currentRuleTokens);
|
270
|
+
}
|
271
|
+
// Reset rule
|
272
|
+
currentRuleTokens = [];
|
273
|
+
// When inside a declaration, tokenize it and push it to the current token list
|
274
|
+
}
|
275
|
+
else if (node && node.type === 'decl') {
|
276
|
+
if (collectVarFunctions) {
|
277
|
+
const declTokens = tokenizeCssDeclaration(node);
|
278
|
+
currentRuleTokens.push(...declTokens);
|
279
|
+
currentRuleTokens.push({ type: TokenType.text, value: ';' });
|
280
|
+
}
|
281
|
+
else {
|
282
|
+
currentRuleTokens.push(...tokenizeCss(part));
|
283
|
+
}
|
284
|
+
}
|
285
|
+
else if (node && node.type === 'atrule') {
|
286
|
+
// Certain atrules have declaration associated with for example @font-face. We need to add the rules tokens
|
287
|
+
// when it's the case.
|
288
|
+
if (currentRuleTokens.length) {
|
289
|
+
tokens.push(...currentRuleTokens);
|
290
|
+
currentRuleTokens = [];
|
291
|
+
}
|
292
|
+
tokens.push(...tokenizeCss(normalizeString(part)));
|
293
|
+
}
|
294
|
+
else {
|
295
|
+
// When inside anything else but a comment just push it
|
296
|
+
if (!node || node.type !== 'comment') {
|
297
|
+
currentRuleTokens.push({ type: TokenType.text, value: normalizeString(part) });
|
298
|
+
}
|
299
|
+
}
|
300
|
+
});
|
301
|
+
return generateExpressionFromTokens(tokens);
|
302
|
+
}
|
303
|
+
// Given any CSS string, replace the scope tokens from the CSS with code to properly
|
304
|
+
// replace it in the stylesheet function.
|
305
|
+
function tokenizeCss(data) {
|
306
|
+
data = data.replace(/( {2,})/gm, ' '); // remove when there are more than two spaces
|
307
|
+
const tokens = [];
|
308
|
+
const attributes = [
|
309
|
+
SHADOW_ATTRIBUTE,
|
310
|
+
HOST_ATTRIBUTE,
|
311
|
+
DIR_ATTRIBUTE_NATIVE_LTR,
|
312
|
+
DIR_ATTRIBUTE_NATIVE_RTL,
|
313
|
+
DIR_ATTRIBUTE_SYNTHETIC_LTR,
|
314
|
+
DIR_ATTRIBUTE_SYNTHETIC_RTL,
|
315
|
+
];
|
316
|
+
const regex = new RegExp(`[[-](${attributes.join('|')})]?`, 'g');
|
317
|
+
let lastIndex = 0;
|
318
|
+
for (const match of matchAll(data, regex)) {
|
319
|
+
const index = match.index;
|
320
|
+
const [matchString, substring] = match;
|
321
|
+
if (index > lastIndex) {
|
322
|
+
tokens.push({ type: TokenType.text, value: data.substring(lastIndex, index) });
|
323
|
+
}
|
324
|
+
const identifier = substring === SHADOW_ATTRIBUTE ? SHADOW_SELECTOR_IDENTIFIER : HOST_SELECTOR_IDENTIFIER;
|
325
|
+
if (matchString.startsWith('[')) {
|
326
|
+
if (substring === SHADOW_ATTRIBUTE || substring === HOST_ATTRIBUTE) {
|
327
|
+
// attribute in a selector, e.g. `[__shadowAttribute__]` or `[__hostAttribute__]`
|
328
|
+
tokens.push({
|
329
|
+
type: TokenType.identifier,
|
330
|
+
value: identifier,
|
331
|
+
});
|
332
|
+
}
|
333
|
+
else {
|
334
|
+
// :dir pseudoclass placeholder, e.g. `[__dirAttributeNativeLtr__]` or `[__dirAttributeSyntheticRtl__]`
|
335
|
+
const native = substring === DIR_ATTRIBUTE_NATIVE_LTR ||
|
336
|
+
substring === DIR_ATTRIBUTE_NATIVE_RTL;
|
337
|
+
const dirValue = substring === DIR_ATTRIBUTE_NATIVE_LTR ||
|
338
|
+
substring === DIR_ATTRIBUTE_SYNTHETIC_LTR
|
339
|
+
? 'ltr'
|
340
|
+
: 'rtl';
|
341
|
+
tokens.push({
|
342
|
+
type: TokenType.expression,
|
343
|
+
// use the native :dir() pseudoclass for native shadow, the [dir] attribute otherwise
|
344
|
+
value: native
|
345
|
+
? `${USE_NATIVE_DIR_PSEUDOCLASS} ? ':dir(${dirValue})' : ''`
|
346
|
+
: `${USE_NATIVE_DIR_PSEUDOCLASS} ? '' : '[dir="${dirValue}"]'`,
|
347
|
+
});
|
348
|
+
}
|
349
|
+
}
|
350
|
+
else {
|
351
|
+
// suffix for an at-rule, e.g. `@keyframes spin-__shadowAttribute__`
|
352
|
+
tokens.push({
|
353
|
+
type: TokenType.identifier,
|
354
|
+
// Suffix the keyframe (i.e. "-" plus the token)
|
355
|
+
value: SUFFIX_TOKEN_IDENTIFIER,
|
356
|
+
});
|
357
|
+
}
|
358
|
+
lastIndex = index + matchString.length;
|
359
|
+
}
|
360
|
+
if (lastIndex < data.length) {
|
361
|
+
tokens.push({ type: TokenType.text, value: data.substring(lastIndex, data.length) });
|
362
|
+
}
|
363
|
+
return tokens;
|
364
|
+
}
|
365
|
+
function isTextOrExpression(token) {
|
366
|
+
return token.type === TokenType.text || token.type == TokenType.expression;
|
367
|
+
}
|
368
|
+
/*
|
369
|
+
* This method takes a tokenized CSS property value `1px solid var(--foo , bar)`
|
370
|
+
* and transforms its custom variables in function calls
|
371
|
+
*/
|
372
|
+
function recursiveValueParse(node, inVarExpression = false) {
|
373
|
+
const { type, nodes, value } = node;
|
374
|
+
// If it has not type it means is the root, process its children
|
375
|
+
if (!type && nodes) {
|
376
|
+
return nodes.reduce((acc, n) => {
|
377
|
+
acc.push(...recursiveValueParse(n, inVarExpression));
|
378
|
+
return acc;
|
379
|
+
}, []);
|
380
|
+
}
|
381
|
+
if (type === 'comment') {
|
382
|
+
return [];
|
383
|
+
}
|
384
|
+
if (type === 'div') {
|
385
|
+
return [
|
386
|
+
{
|
387
|
+
type: inVarExpression ? TokenType.divider : TokenType.text,
|
388
|
+
value,
|
389
|
+
},
|
390
|
+
];
|
391
|
+
}
|
392
|
+
if (type === 'string') {
|
393
|
+
const { quote } = node;
|
394
|
+
return [
|
395
|
+
{
|
396
|
+
type: TokenType.text,
|
397
|
+
value: quote ? quote + value + quote : value,
|
398
|
+
},
|
399
|
+
];
|
400
|
+
}
|
401
|
+
// If we are inside a var() expression use need to stringify since we are converting it into a function
|
402
|
+
if (type === 'word') {
|
403
|
+
const convertIdentifier = value.startsWith('--');
|
404
|
+
if (convertIdentifier) {
|
405
|
+
return [{ type: TokenType.identifier, value: `"${value}"` }];
|
406
|
+
}
|
407
|
+
// For animation properties, the shadow/host attributes may be in this text
|
408
|
+
return tokenizeCss(value);
|
409
|
+
}
|
410
|
+
// If we inside a var() function we need to prepend and append to generate an expression
|
411
|
+
if (type === 'function') {
|
412
|
+
if (value === 'var') {
|
413
|
+
// `tokens` may include tokens of type `divider`, `expression`, `identifier`,
|
414
|
+
// and `text`. However, an identifier will only ever be adjacent to a divider,
|
415
|
+
// whereas text and expression tokens may be adjacent to one another. This is
|
416
|
+
// important below when inserting concatenetors.
|
417
|
+
//
|
418
|
+
// For fuller context, see the following conversation:
|
419
|
+
// https://github.com/salesforce/lwc/pull/2902#discussion_r904626421
|
420
|
+
let tokens = recursiveValueParse({ nodes }, true);
|
421
|
+
tokens = reduceTokens(tokens);
|
422
|
+
const exprToken = tokens.reduce((buffer, token, index) => {
|
423
|
+
const isTextToken = token.type === TokenType.text;
|
424
|
+
const normalizedToken = isTextToken ? JSON.stringify(token.value) : token.value;
|
425
|
+
const nextToken = tokens[index + 1];
|
426
|
+
// If we have a token sequence of text + expression or expression + text,
|
427
|
+
// we need to add the concatenation operator. Examples:
|
428
|
+
//
|
429
|
+
// Input: var(--x, 0 0 2px var(--y, #fff))
|
430
|
+
// Output: varResolver("--x", "0 0 2px " + varResolver("--y", "#fff"))
|
431
|
+
//
|
432
|
+
// Input: var(--x, var(--y, #fff) 0 0 2px)
|
433
|
+
// Output: varResolver("--x", varResolver("--y", "#fff") + " 0 0 2px"))
|
434
|
+
//
|
435
|
+
// Since identifier tokens will never be adjacent to text or expression
|
436
|
+
// tokens (see above comment), a concatenator will never be required if
|
437
|
+
// `token` or `nextToken` is an identifier.
|
438
|
+
const shouldAddConcatenator = isTextOrExpression(token) && nextToken && isTextOrExpression(nextToken);
|
439
|
+
const concatOperator = shouldAddConcatenator ? ' + ' : '';
|
440
|
+
return buffer + normalizedToken + concatOperator;
|
441
|
+
}, '');
|
442
|
+
// Generate the function call for runtime evaluation
|
443
|
+
return [
|
444
|
+
{
|
445
|
+
type: TokenType.expression,
|
446
|
+
value: `${VAR_RESOLVER_IDENTIFIER}(${exprToken})`,
|
447
|
+
},
|
448
|
+
];
|
449
|
+
// for any other function just do the equivalent string concatenation (no need for expressions)
|
450
|
+
}
|
451
|
+
else {
|
452
|
+
const tokens = nodes.reduce((acc, n) => {
|
453
|
+
acc.push(...recursiveValueParse(n, false));
|
454
|
+
return acc;
|
455
|
+
}, []);
|
456
|
+
return [
|
457
|
+
{ type: TokenType.text, value: `${value}(` },
|
458
|
+
...reduceTokens(tokens),
|
459
|
+
{ type: TokenType.text, value: ')' },
|
460
|
+
];
|
461
|
+
}
|
462
|
+
}
|
463
|
+
// For any other token types we just need to return text
|
464
|
+
return [{ type: TokenType.text, value }];
|
465
|
+
}
|
466
|
+
function tokenizeCssDeclaration(node) {
|
467
|
+
const valueRoot = postcssValueParser(node.value);
|
468
|
+
const parsed = recursiveValueParse(valueRoot);
|
469
|
+
return [{ type: TokenType.text, value: `${node.prop.trim()}: ` }, ...parsed];
|
470
|
+
}
|
471
|
+
|
472
|
+
function validateIdSelectors (root) {
|
473
|
+
root.walkIds((node) => {
|
474
|
+
const message = `Invalid usage of id selector '#${node.value}'. Try using a class selector or some other selector.`;
|
475
|
+
throw root.error(message, {
|
476
|
+
index: node.sourceIndex,
|
477
|
+
word: node.value,
|
478
|
+
});
|
479
|
+
});
|
480
|
+
}
|
481
|
+
|
482
|
+
function process$2(root, result, isScoped) {
|
483
|
+
root.walkAtRules('import', (node) => {
|
484
|
+
if (isScoped) {
|
485
|
+
throw node.error(`Invalid import statement, imports are not allowed in *.scoped.css files.`);
|
486
|
+
}
|
487
|
+
// Ensure @import are at the top of the file
|
488
|
+
let prev = node.prev();
|
489
|
+
while (prev) {
|
490
|
+
if (prev.type === 'comment' || (prev.type === 'atrule' && prev.name === 'import')) {
|
491
|
+
prev = prev.prev();
|
492
|
+
}
|
493
|
+
else {
|
494
|
+
throw prev.error('@import must precede all other statements');
|
495
|
+
}
|
496
|
+
}
|
497
|
+
const { nodes: params } = postcssValueParser(node.params);
|
498
|
+
// Ensure import match the following syntax:
|
499
|
+
// @import "foo";
|
500
|
+
// @import "./foo.css";
|
501
|
+
if (!params.length || params[0].type !== 'string' || !params[0].value) {
|
502
|
+
throw node.error(`Invalid import statement, unable to find imported module.`);
|
503
|
+
}
|
504
|
+
if (params.length > 1) {
|
505
|
+
throw node.error(`Invalid import statement, import statement only support a single parameter.`);
|
506
|
+
}
|
507
|
+
// Add the imported to results messages
|
508
|
+
const message = importMessage(params[0].value);
|
509
|
+
result.messages.push(message);
|
510
|
+
// Remove the import from the generated css
|
511
|
+
node.remove();
|
512
|
+
});
|
513
|
+
}
|
514
|
+
|
515
|
+
/*
|
516
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
517
|
+
* All rights reserved.
|
518
|
+
* SPDX-License-Identifier: MIT
|
519
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
520
|
+
*/
|
521
|
+
function isDirPseudoClass(node) {
|
522
|
+
return isPseudoClass(node) && node.value === ':dir';
|
523
|
+
}
|
524
|
+
|
525
|
+
function findNode(container, predicate) {
|
526
|
+
return container && container.nodes && container.nodes.find(predicate);
|
527
|
+
}
|
528
|
+
function replaceNodeWith(oldNode, ...newNodes) {
|
529
|
+
if (newNodes.length) {
|
530
|
+
const { parent } = oldNode;
|
531
|
+
if (!parent) {
|
532
|
+
throw new Error(`Impossible to replace root node.`);
|
533
|
+
}
|
534
|
+
newNodes.forEach((node) => {
|
535
|
+
parent.insertBefore(oldNode, node);
|
536
|
+
});
|
537
|
+
oldNode.remove();
|
538
|
+
}
|
539
|
+
}
|
540
|
+
function trimNodeWhitespaces(node) {
|
541
|
+
if (node && node.spaces) {
|
542
|
+
node.spaces.before = '';
|
543
|
+
node.spaces.after = '';
|
544
|
+
}
|
545
|
+
}
|
546
|
+
|
547
|
+
const DEPRECATED_SELECTORS = new Set(['/deep/', '::shadow', '>>>']);
|
548
|
+
const UNSUPPORTED_SELECTORS = new Set([':root', ':host-context']);
|
549
|
+
const TEMPLATE_DIRECTIVES = [/^key$/, /^lwc:*/, /^if:*/, /^for:*/, /^iterator:*/];
|
550
|
+
function validateSelectors(root) {
|
551
|
+
root.walk((node) => {
|
552
|
+
const { value, sourceIndex } = node;
|
553
|
+
if (value) {
|
554
|
+
// Ensure the selector doesn't use a deprecated CSS selector.
|
555
|
+
if (DEPRECATED_SELECTORS.has(value)) {
|
556
|
+
throw root.error(`Invalid usage of deprecated selector "${value}".`, {
|
557
|
+
index: sourceIndex,
|
558
|
+
word: value,
|
559
|
+
});
|
560
|
+
}
|
561
|
+
// Ensure the selector doesn't use an unsupported selector.
|
562
|
+
if (UNSUPPORTED_SELECTORS.has(value)) {
|
563
|
+
throw root.error(`Invalid usage of unsupported selector "${value}".`, {
|
564
|
+
index: sourceIndex,
|
565
|
+
word: value,
|
566
|
+
});
|
567
|
+
}
|
568
|
+
}
|
569
|
+
});
|
570
|
+
}
|
571
|
+
function validateAttribute(root) {
|
572
|
+
root.walkAttributes((node) => {
|
573
|
+
const { attribute: attributeName, sourceIndex } = node;
|
574
|
+
const isTemplateDirective = TEMPLATE_DIRECTIVES.some((directive) => {
|
575
|
+
return directive.test(attributeName);
|
576
|
+
});
|
577
|
+
if (isTemplateDirective) {
|
578
|
+
const message = [
|
579
|
+
`Invalid usage of attribute selector "${attributeName}". `,
|
580
|
+
`"${attributeName}" is a template directive and therefore not supported in css rules.`,
|
581
|
+
];
|
582
|
+
throw root.error(message.join(''), {
|
583
|
+
index: sourceIndex,
|
584
|
+
word: attributeName,
|
585
|
+
});
|
586
|
+
}
|
587
|
+
});
|
588
|
+
}
|
589
|
+
function validate(root) {
|
590
|
+
validateSelectors(root);
|
591
|
+
validateAttribute(root);
|
592
|
+
}
|
593
|
+
|
594
|
+
/*
|
595
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
596
|
+
* All rights reserved.
|
597
|
+
* SPDX-License-Identifier: MIT
|
598
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
599
|
+
*/
|
600
|
+
function isHostPseudoClass(node) {
|
601
|
+
return isPseudoClass(node) && node.value === ':host';
|
602
|
+
}
|
603
|
+
/**
|
604
|
+
* Add scoping attributes to all the matching selectors:
|
605
|
+
* h1 -> h1[x-foo_tmpl]
|
606
|
+
* p a -> p[x-foo_tmpl] a[x-foo_tmpl]
|
607
|
+
*/
|
608
|
+
function scopeSelector(selector) {
|
609
|
+
const compoundSelectors = [[]];
|
610
|
+
// Split the selector per compound selector. Compound selectors are interleaved with combinator nodes.
|
611
|
+
// https://drafts.csswg.org/selectors-4/#typedef-complex-selector
|
612
|
+
selector.each((node) => {
|
613
|
+
if (isCombinator(node)) {
|
614
|
+
compoundSelectors.push([]);
|
615
|
+
}
|
616
|
+
else {
|
617
|
+
const current = compoundSelectors[compoundSelectors.length - 1];
|
618
|
+
current.push(node);
|
619
|
+
}
|
620
|
+
});
|
621
|
+
for (const compoundSelector of compoundSelectors) {
|
622
|
+
// Compound selectors with only a single :dir pseudo class should be scoped, the dir pseudo
|
623
|
+
// class transform will take care of transforming it properly.
|
624
|
+
const containsSingleDirSelector = compoundSelector.length === 1 && isDirPseudoClass(compoundSelector[0]);
|
625
|
+
// Compound selectors containing :host have a special treatment and should not be scoped
|
626
|
+
// like the rest of the complex selectors.
|
627
|
+
const containsHost = compoundSelector.some(isHostPseudoClass);
|
628
|
+
if (!containsSingleDirSelector && !containsHost) {
|
629
|
+
let nodeToScope;
|
630
|
+
// In each compound selector we need to locate the last selector to scope.
|
631
|
+
for (const node of compoundSelector) {
|
632
|
+
if (!isPseudoElement(node)) {
|
633
|
+
nodeToScope = node;
|
634
|
+
}
|
635
|
+
}
|
636
|
+
const shadowAttribute = attribute({
|
637
|
+
attribute: SHADOW_ATTRIBUTE,
|
638
|
+
value: undefined,
|
639
|
+
raws: {},
|
640
|
+
});
|
641
|
+
if (nodeToScope) {
|
642
|
+
// Add the scoping attribute right after the node scope
|
643
|
+
selector.insertAfter(nodeToScope, shadowAttribute);
|
644
|
+
}
|
645
|
+
else {
|
646
|
+
// Add the scoping token in the first position of the compound selector as a fallback
|
647
|
+
// when there is no node to scope. For example: ::after {}
|
648
|
+
const [firstSelector] = compoundSelector;
|
649
|
+
selector.insertBefore(firstSelector, shadowAttribute);
|
650
|
+
// Move any whitespace before the selector (e.g. " ::after") to before the shadow attribute,
|
651
|
+
// so that the resulting selector is correct (e.g. " [attr]::after", not "[attr] ::after")
|
652
|
+
if (firstSelector && firstSelector.spaces.before) {
|
653
|
+
shadowAttribute.spaces.before = firstSelector.spaces.before;
|
654
|
+
const clonedFirstSelector = firstSelector.clone({});
|
655
|
+
clonedFirstSelector.spaces.before = '';
|
656
|
+
firstSelector.replaceWith(clonedFirstSelector);
|
657
|
+
}
|
658
|
+
}
|
659
|
+
}
|
660
|
+
}
|
661
|
+
}
|
662
|
+
/**
|
663
|
+
* Mark the :host selector with a placeholder. If the selector has a list of
|
664
|
+
* contextual selector it will generate a rule for each of them.
|
665
|
+
* :host -> [x-foo_tmpl-host]
|
666
|
+
* :host(.foo, .bar) -> [x-foo_tmpl-host].foo, [x-foo_tmpl-host].bar
|
667
|
+
*/
|
668
|
+
function transformHost(selector) {
|
669
|
+
// Locate the first :host pseudo-class
|
670
|
+
const hostNode = findNode(selector, isHostPseudoClass);
|
671
|
+
if (hostNode) {
|
672
|
+
// Store the original location of the :host in the selector
|
673
|
+
const hostIndex = selector.index(hostNode);
|
674
|
+
// Swap the :host pseudo-class with the host scoping token
|
675
|
+
const hostAttribute = attribute({
|
676
|
+
attribute: HOST_ATTRIBUTE,
|
677
|
+
value: undefined,
|
678
|
+
raws: {},
|
679
|
+
});
|
680
|
+
hostNode.replaceWith(hostAttribute);
|
681
|
+
// Generate a unique contextualized version of the selector for each selector pass as argument
|
682
|
+
// to the :host
|
683
|
+
const contextualSelectors = hostNode.nodes.map((contextSelectors) => {
|
684
|
+
const clonedSelector = selector.clone({});
|
685
|
+
const clonedHostNode = clonedSelector.at(hostIndex);
|
686
|
+
// Add to the compound selector previously containing the :host pseudo class
|
687
|
+
// the contextual selectors.
|
688
|
+
contextSelectors.each((node) => {
|
689
|
+
trimNodeWhitespaces(node);
|
690
|
+
clonedSelector.insertAfter(clonedHostNode, node);
|
691
|
+
});
|
692
|
+
return clonedSelector;
|
693
|
+
});
|
694
|
+
// Replace the current selector with the different variants
|
695
|
+
replaceNodeWith(selector, ...contextualSelectors);
|
696
|
+
}
|
697
|
+
}
|
698
|
+
function transformSelector(root, transformConfig) {
|
699
|
+
validate(root);
|
700
|
+
root.each(scopeSelector);
|
701
|
+
if (transformConfig.transformHost) {
|
702
|
+
root.each(transformHost);
|
703
|
+
}
|
704
|
+
}
|
705
|
+
|
706
|
+
function process$1(root, result) {
|
707
|
+
root.walkDecls((decl) => {
|
708
|
+
const valueRoot = postcssValueParser(decl.value);
|
709
|
+
let varFound = false;
|
710
|
+
valueRoot.walk(({ type, value }) => {
|
711
|
+
if (!varFound && type === 'function' && value === 'var') {
|
712
|
+
// Add the imported to results messages
|
713
|
+
const message = varFunctionMessage(value);
|
714
|
+
result.messages.push(message);
|
715
|
+
varFound = true;
|
716
|
+
}
|
717
|
+
});
|
718
|
+
});
|
719
|
+
}
|
720
|
+
|
721
|
+
/*
|
722
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
723
|
+
* All rights reserved.
|
724
|
+
* SPDX-License-Identifier: MIT
|
725
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
726
|
+
*/
|
727
|
+
function isValidDirValue(value) {
|
728
|
+
return value === 'ltr' || value === 'rtl';
|
729
|
+
}
|
730
|
+
function transformDirPseudoClass (root) {
|
731
|
+
root.nodes.forEach((selector) => {
|
732
|
+
selector.nodes.forEach((node) => {
|
733
|
+
if (!isDirPseudoClass(node)) {
|
734
|
+
return;
|
735
|
+
}
|
736
|
+
const value = node.nodes.toString().trim();
|
737
|
+
if (!isValidDirValue(value)) {
|
738
|
+
throw root.error(`:dir() pseudo class expects "ltr" or "rtl" for value, but received "${value}".`, {
|
739
|
+
index: node.sourceIndex,
|
740
|
+
word: node.value,
|
741
|
+
});
|
742
|
+
}
|
743
|
+
// Set placeholders for `:dir()` so we can keep it for native shadow and
|
744
|
+
// replace it with a polyfill for synthetic shadow.
|
745
|
+
//
|
746
|
+
// Native: `:dir(ltr)`
|
747
|
+
// Synthetic: `[dir="ltr"]`
|
748
|
+
//
|
749
|
+
// The placeholders look like this: `[__dirAttributeNativeLtr__]`
|
750
|
+
// The attribute has no value because it's simpler during serialization, and there
|
751
|
+
// are only two valid values: "ltr" and "rtl".
|
752
|
+
//
|
753
|
+
// Now consider a more complex selector: `.foo:dir(ltr):not(.bar)`.
|
754
|
+
// For native shadow, we need to leave it as-is. Whereas for synthetic shadow, we need
|
755
|
+
// to convert it to: `[dir="ltr"] .foo:not(.bar)`.
|
756
|
+
// I.e. we need to use a descendant selector (' ' combinator) relying on a `dir`
|
757
|
+
// attribute added to the host element. So we need two placeholders:
|
758
|
+
// `<synthetic_placeholder> .foo<native_placeholder>:not(.bar)`
|
759
|
+
const nativeAttribute = attribute({
|
760
|
+
attribute: value === 'ltr' ? DIR_ATTRIBUTE_NATIVE_LTR : DIR_ATTRIBUTE_NATIVE_RTL,
|
761
|
+
value: undefined,
|
762
|
+
raws: {},
|
763
|
+
});
|
764
|
+
const syntheticAttribute = attribute({
|
765
|
+
attribute: value === 'ltr' ? DIR_ATTRIBUTE_SYNTHETIC_LTR : DIR_ATTRIBUTE_SYNTHETIC_RTL,
|
766
|
+
value: undefined,
|
767
|
+
raws: {},
|
768
|
+
});
|
769
|
+
node.replaceWith(nativeAttribute);
|
770
|
+
// If the selector is not empty and if the first node in the selector is not already a
|
771
|
+
// " " combinator, we need to use the descendant selector format
|
772
|
+
const shouldAddDescendantCombinator = selector.first && !isCombinator(selector.first) && selector.first.value !== ' ';
|
773
|
+
if (shouldAddDescendantCombinator) {
|
774
|
+
selector.insertBefore(selector.first, combinator({
|
775
|
+
value: ' ',
|
776
|
+
}));
|
777
|
+
// Add the [dir] attribute in front of the " " combinator, i.e. as an ancestor
|
778
|
+
selector.insertBefore(selector.first, syntheticAttribute);
|
779
|
+
}
|
780
|
+
else {
|
781
|
+
// Otherwise there's no need for the descendant selector, so we can skip adding the
|
782
|
+
// space combinator and just put the synthetic placeholder next to the native one
|
783
|
+
selector.insertBefore(nativeAttribute, syntheticAttribute);
|
784
|
+
}
|
785
|
+
});
|
786
|
+
});
|
787
|
+
}
|
788
|
+
|
789
|
+
// Subset of prefixes for animation-related names that we expect people might be using.
|
790
|
+
// The most important is -webkit, which is actually part of the spec now. All -webkit prefixes
|
791
|
+
// are listed here: https://developer.mozilla.org/en-US/docs/Web/CSS/Webkit_Extensions
|
792
|
+
// -moz is also still supported as of Firefox 95.
|
793
|
+
// We could probably get away with just doing -webkit and -moz (since -ms never seems
|
794
|
+
// to have existed for keyframes/animations, and Opera has used Blink since 2013), but
|
795
|
+
// covering all the popular ones will at least make the compiled code more consistent
|
796
|
+
// for developers who are using all the variants.
|
797
|
+
// List based on a subset from https://github.com/wooorm/vendors/blob/2f489ad/index.js
|
798
|
+
const VENDOR_PREFIXES = ['moz', 'ms', 'o', 'webkit'];
|
799
|
+
// create a list like ['animation', '-webkit-animation', ...]
|
800
|
+
function getAllNames(name) {
|
801
|
+
return new Set([name, ...VENDOR_PREFIXES.map((prefix) => `-${prefix}-${name}`)]);
|
802
|
+
}
|
803
|
+
const ANIMATION = getAllNames('animation');
|
804
|
+
const ANIMATION_NAME = getAllNames('animation-name');
|
805
|
+
function process(root) {
|
806
|
+
const knownNames = new Set();
|
807
|
+
root.walkAtRules((atRule) => {
|
808
|
+
// Note that @-webkit-keyframes, @-moz-keyframes, etc. are not actually a thing supported
|
809
|
+
// in any browser, even though you'll see it on some StackOverflow answers.
|
810
|
+
if (atRule.name === 'keyframes') {
|
811
|
+
const { params } = atRule;
|
812
|
+
knownNames.add(params);
|
813
|
+
atRule.params = `${params}-${SHADOW_ATTRIBUTE}`;
|
814
|
+
}
|
815
|
+
});
|
816
|
+
root.walkRules((rule) => {
|
817
|
+
rule.walkDecls((decl) => {
|
818
|
+
if (ANIMATION.has(decl.prop)) {
|
819
|
+
// Use a simple heuristic of breaking up the tokens by whitespace. We could use
|
820
|
+
// a dedicated animation prop parser (e.g.
|
821
|
+
// https://github.com/hookhookun/parse-animation-shorthand) but it's
|
822
|
+
// probably overkill.
|
823
|
+
const tokens = decl.value
|
824
|
+
.trim()
|
825
|
+
.split(/\s+/g)
|
826
|
+
.map((token) => knownNames.has(token) ? `${token}-${SHADOW_ATTRIBUTE}` : token);
|
827
|
+
decl.value = tokens.join(' ');
|
828
|
+
}
|
829
|
+
else if (ANIMATION_NAME.has(decl.prop)) {
|
830
|
+
if (knownNames.has(decl.value)) {
|
831
|
+
decl.value = `${decl.value}-${SHADOW_ATTRIBUTE}`;
|
832
|
+
}
|
833
|
+
}
|
834
|
+
});
|
835
|
+
});
|
836
|
+
}
|
837
|
+
|
838
|
+
function shouldTransformSelector(rule) {
|
839
|
+
var _a;
|
840
|
+
// @keyframe at-rules are special, rules inside are not standard selectors and should not be
|
841
|
+
// scoped like any other rules.
|
842
|
+
return ((_a = rule.parent) === null || _a === void 0 ? void 0 : _a.type) !== 'atrule' || rule.parent.name !== 'keyframes';
|
843
|
+
}
|
844
|
+
function selectorProcessorFactory(transformConfig) {
|
845
|
+
return postCssSelector((root) => {
|
846
|
+
validateIdSelectors(root);
|
847
|
+
transformSelector(root, transformConfig);
|
848
|
+
transformDirPseudoClass(root);
|
849
|
+
});
|
850
|
+
}
|
851
|
+
function postCssLwcPlugin(options) {
|
852
|
+
// We need 2 types of selectors processors, since transforming the :host selector make the selector
|
853
|
+
// unusable when used in the context of the native shadow and vice-versa.
|
854
|
+
// This distinction also applies to light DOM in scoped (synthetic-like) vs unscoped (native-like) mode.
|
855
|
+
const nativeShadowSelectorProcessor = selectorProcessorFactory({
|
856
|
+
transformHost: false,
|
857
|
+
});
|
858
|
+
const syntheticShadowSelectorProcessor = selectorProcessorFactory({
|
859
|
+
transformHost: true,
|
860
|
+
});
|
861
|
+
return (root, result) => {
|
862
|
+
process$2(root, result, options.scoped);
|
863
|
+
process$1(root, result);
|
864
|
+
process(root);
|
865
|
+
root.walkRules((rule) => {
|
866
|
+
if (!shouldTransformSelector(rule)) {
|
867
|
+
return;
|
868
|
+
}
|
869
|
+
// Let transform the selector with the 2 processors.
|
870
|
+
const syntheticSelector = syntheticShadowSelectorProcessor.processSync(rule);
|
871
|
+
const nativeSelector = nativeShadowSelectorProcessor.processSync(rule);
|
872
|
+
rule.selector = syntheticSelector;
|
873
|
+
// If the resulting selector are different it means that the selector use the :host selector. In
|
874
|
+
// this case we need to duplicate the CSS rule and assign the other selector.
|
875
|
+
if (syntheticSelector !== nativeSelector) {
|
876
|
+
// The cloned selector is inserted before the currently processed selector to avoid processing
|
877
|
+
// again the cloned selector.
|
878
|
+
const currentRule = rule;
|
879
|
+
const clonedRule = rule.cloneBefore();
|
880
|
+
clonedRule.selector = nativeSelector;
|
881
|
+
// Safe a reference to each other
|
882
|
+
clonedRule._isNativeHost = true;
|
883
|
+
currentRule._isSyntheticHost = true;
|
884
|
+
}
|
885
|
+
});
|
886
|
+
};
|
887
|
+
}
|
888
|
+
|
889
|
+
/*
|
890
|
+
* Copyright (c) 2018, salesforce.com, inc.
|
891
|
+
* All rights reserved.
|
892
|
+
* SPDX-License-Identifier: MIT
|
893
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
894
|
+
*/
|
895
|
+
function transform(src, id, config = {}) {
|
896
|
+
if (src === '') {
|
897
|
+
return { code: 'export default undefined' };
|
898
|
+
}
|
899
|
+
const scoped = !!config.scoped;
|
900
|
+
const plugins = [postCssLwcPlugin({ scoped })];
|
901
|
+
const result = postcss(plugins).process(src, { from: id }).sync();
|
902
|
+
return { code: serialize(result, config) };
|
903
|
+
}
|
904
|
+
|
905
|
+
export { transform };
|
906
|
+
/** version: 2.45.4 */
|
907
|
+
//# sourceMappingURL=index.js.map
|