@plumeria/webpack-plugin 5.0.1 → 6.0.0
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.js +431 -210
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -14,15 +14,16 @@ if (process.env.NODE_ENV === 'production') {
|
|
|
14
14
|
fs_1.default.writeFileSync(VIRTUAL_FILE_PATH, '/** Placeholder file */', 'utf-8');
|
|
15
15
|
}
|
|
16
16
|
function loader(source) {
|
|
17
|
+
const loaderContext = this;
|
|
17
18
|
const callback = this.async();
|
|
19
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
18
20
|
if (this.resourcePath.includes('node_modules') ||
|
|
19
21
|
!source.includes('@plumeria/core')) {
|
|
20
22
|
return callback(null, source);
|
|
21
23
|
}
|
|
22
24
|
this.clearDependencies();
|
|
23
25
|
this.addDependency(this.resourcePath);
|
|
24
|
-
(0, utils_1.scanAll)(
|
|
25
|
-
const extractedSheets = [];
|
|
26
|
+
const scannedTables = (0, utils_1.scanAll)();
|
|
26
27
|
const fileStyles = {};
|
|
27
28
|
const ast = (0, core_1.parseSync)(source, {
|
|
28
29
|
syntax: 'typescript',
|
|
@@ -36,7 +37,8 @@ function loader(source) {
|
|
|
36
37
|
ImportDeclaration({ node }) {
|
|
37
38
|
const sourcePath = node.source.value;
|
|
38
39
|
const actualPath = (0, utils_1.resolveImportPath)(sourcePath, resourcePath);
|
|
39
|
-
if (actualPath
|
|
40
|
+
if (actualPath) {
|
|
41
|
+
loaderContext.addDependency(actualPath);
|
|
40
42
|
node.specifiers.forEach((specifier) => {
|
|
41
43
|
if (specifier.type === 'ImportSpecifier') {
|
|
42
44
|
const importedName = specifier.imported
|
|
@@ -44,48 +46,94 @@ function loader(source) {
|
|
|
44
46
|
: specifier.local.value;
|
|
45
47
|
const localName = specifier.local.value;
|
|
46
48
|
const uniqueKey = `${actualPath}-${importedName}`;
|
|
47
|
-
if (
|
|
48
|
-
importMap[localName] =
|
|
49
|
+
if (scannedTables.staticTable[uniqueKey]) {
|
|
50
|
+
importMap[localName] = scannedTables.staticTable[uniqueKey];
|
|
51
|
+
}
|
|
52
|
+
if (scannedTables.keyframesHashTable[uniqueKey]) {
|
|
53
|
+
importMap[localName] =
|
|
54
|
+
scannedTables.keyframesHashTable[uniqueKey];
|
|
55
|
+
}
|
|
56
|
+
if (scannedTables.viewTransitionHashTable[uniqueKey]) {
|
|
57
|
+
importMap[localName] =
|
|
58
|
+
scannedTables.viewTransitionHashTable[uniqueKey];
|
|
49
59
|
}
|
|
50
|
-
if (
|
|
51
|
-
importMap[localName] =
|
|
60
|
+
if (scannedTables.themeTable[uniqueKey]) {
|
|
61
|
+
importMap[localName] = scannedTables.themeTable[uniqueKey];
|
|
52
62
|
}
|
|
53
|
-
if (
|
|
54
|
-
importMap[localName] =
|
|
63
|
+
if (scannedTables.createHashTable[uniqueKey]) {
|
|
64
|
+
importMap[localName] = scannedTables.createHashTable[uniqueKey];
|
|
55
65
|
}
|
|
56
|
-
if (
|
|
57
|
-
importMap[localName] =
|
|
66
|
+
if (scannedTables.variantsHashTable[uniqueKey]) {
|
|
67
|
+
importMap[localName] = scannedTables.variantsHashTable[uniqueKey];
|
|
58
68
|
}
|
|
59
69
|
}
|
|
60
70
|
});
|
|
61
71
|
}
|
|
62
72
|
},
|
|
63
73
|
});
|
|
64
|
-
const mergedStaticTable = {
|
|
74
|
+
const mergedStaticTable = {};
|
|
75
|
+
for (const key of Object.keys(scannedTables.staticTable)) {
|
|
76
|
+
mergedStaticTable[key] = scannedTables.staticTable[key];
|
|
77
|
+
}
|
|
65
78
|
for (const key of Object.keys(localConsts)) {
|
|
66
79
|
mergedStaticTable[key] = localConsts[key];
|
|
67
80
|
}
|
|
68
81
|
for (const key of Object.keys(importMap)) {
|
|
69
82
|
mergedStaticTable[key] = importMap[key];
|
|
70
83
|
}
|
|
71
|
-
const mergedKeyframesTable = {
|
|
84
|
+
const mergedKeyframesTable = {};
|
|
85
|
+
for (const key of Object.keys(scannedTables.keyframesHashTable)) {
|
|
86
|
+
mergedKeyframesTable[key] = scannedTables.keyframesHashTable[key];
|
|
87
|
+
}
|
|
72
88
|
for (const key of Object.keys(importMap)) {
|
|
73
89
|
mergedKeyframesTable[key] = importMap[key];
|
|
74
90
|
}
|
|
75
|
-
const mergedViewTransitionTable = {
|
|
91
|
+
const mergedViewTransitionTable = {};
|
|
92
|
+
for (const key of Object.keys(scannedTables.viewTransitionHashTable)) {
|
|
93
|
+
mergedViewTransitionTable[key] = scannedTables.viewTransitionHashTable[key];
|
|
94
|
+
}
|
|
76
95
|
for (const key of Object.keys(importMap)) {
|
|
77
96
|
mergedViewTransitionTable[key] = importMap[key];
|
|
78
97
|
}
|
|
79
|
-
const mergedThemeTable = {
|
|
98
|
+
const mergedThemeTable = {};
|
|
99
|
+
for (const key of Object.keys(scannedTables.themeTable)) {
|
|
100
|
+
mergedThemeTable[key] = scannedTables.themeTable[key];
|
|
101
|
+
}
|
|
80
102
|
for (const key of Object.keys(importMap)) {
|
|
81
103
|
mergedThemeTable[key] = importMap[key];
|
|
82
104
|
}
|
|
83
|
-
const
|
|
105
|
+
const mergedCreateTable = {};
|
|
106
|
+
for (const key of Object.keys(scannedTables.createHashTable)) {
|
|
107
|
+
mergedCreateTable[key] = scannedTables.createHashTable[key];
|
|
108
|
+
}
|
|
109
|
+
for (const key of Object.keys(importMap)) {
|
|
110
|
+
mergedCreateTable[key] = importMap[key];
|
|
111
|
+
}
|
|
112
|
+
const mergedVariantsTable = {};
|
|
113
|
+
for (const key of Object.keys(scannedTables.variantsHashTable)) {
|
|
114
|
+
mergedVariantsTable[key] = scannedTables.variantsHashTable[key];
|
|
115
|
+
}
|
|
116
|
+
for (const key of Object.keys(importMap)) {
|
|
117
|
+
mergedVariantsTable[key] = importMap[key];
|
|
118
|
+
}
|
|
84
119
|
const localCreateStyles = {};
|
|
85
120
|
const replacements = [];
|
|
121
|
+
const extractedSheets = [];
|
|
86
122
|
const processedDecls = new Set();
|
|
87
|
-
const excludedSpans = new Set();
|
|
88
123
|
const idSpans = new Set();
|
|
124
|
+
const excludedSpans = new Set();
|
|
125
|
+
const checkVariantAssignment = (decl) => {
|
|
126
|
+
if (decl.init &&
|
|
127
|
+
utils_1.t.isCallExpression(decl.init) &&
|
|
128
|
+
utils_1.t.isIdentifier(decl.init.callee)) {
|
|
129
|
+
const varName = decl.init.callee.value;
|
|
130
|
+
if ((localCreateStyles[varName] &&
|
|
131
|
+
localCreateStyles[varName].type === 'variant') ||
|
|
132
|
+
mergedVariantsTable[varName]) {
|
|
133
|
+
throw new Error(`Plumeria: Assigning the return value of "css.variants" to a variable is not supported.\nPlease pass the variant function directly to "css.props". Found assignment to: ${utils_1.t.isIdentifier(decl.id) ? decl.id.value : 'unknown'}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
89
137
|
const registerStyle = (node, declSpan, isExported) => {
|
|
90
138
|
if (utils_1.t.isIdentifier(node.id) &&
|
|
91
139
|
node.init &&
|
|
@@ -97,15 +145,17 @@ function loader(source) {
|
|
|
97
145
|
const propName = node.init.callee.property.value;
|
|
98
146
|
if (propName === 'create' &&
|
|
99
147
|
utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
|
|
100
|
-
const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
|
|
148
|
+
const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
|
|
101
149
|
if (obj) {
|
|
102
150
|
const hashMap = {};
|
|
103
151
|
Object.entries(obj).forEach(([key, style]) => {
|
|
104
152
|
const records = (0, utils_1.getStyleRecords)(key, style, 2);
|
|
105
|
-
(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
153
|
+
if (!isProduction) {
|
|
154
|
+
(0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
|
|
155
|
+
records.forEach((r) => {
|
|
156
|
+
extractedSheets.push(r.sheet);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
109
159
|
const atomMap = {};
|
|
110
160
|
records.forEach((r) => (atomMap[r.key] = r.hash));
|
|
111
161
|
hashMap[key] = atomMap;
|
|
@@ -118,7 +168,6 @@ function loader(source) {
|
|
|
118
168
|
type: 'create',
|
|
119
169
|
obj,
|
|
120
170
|
hashMap,
|
|
121
|
-
hasDynamicAccess: false,
|
|
122
171
|
isExported,
|
|
123
172
|
initSpan: {
|
|
124
173
|
start: node.init.span.start - ast.span.start,
|
|
@@ -139,7 +188,36 @@ function loader(source) {
|
|
|
139
188
|
type: 'constant',
|
|
140
189
|
obj: {},
|
|
141
190
|
hashMap: {},
|
|
142
|
-
|
|
191
|
+
isExported,
|
|
192
|
+
initSpan: {
|
|
193
|
+
start: node.init.span.start - ast.span.start,
|
|
194
|
+
end: node.init.span.end - ast.span.start,
|
|
195
|
+
},
|
|
196
|
+
declSpan: {
|
|
197
|
+
start: declSpan.start - ast.span.start,
|
|
198
|
+
end: declSpan.end - ast.span.start,
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
else if (propName === 'variants' &&
|
|
203
|
+
utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
|
|
204
|
+
const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, (name) => {
|
|
205
|
+
if (localCreateStyles[name]) {
|
|
206
|
+
return localCreateStyles[name].obj;
|
|
207
|
+
}
|
|
208
|
+
if (mergedCreateTable[name]) {
|
|
209
|
+
const hash = mergedCreateTable[name];
|
|
210
|
+
if (scannedTables.createObjectTable[hash]) {
|
|
211
|
+
return scannedTables.createObjectTable[hash];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return undefined;
|
|
215
|
+
});
|
|
216
|
+
localCreateStyles[node.id.value] = {
|
|
217
|
+
name: node.id.value,
|
|
218
|
+
type: 'variant',
|
|
219
|
+
obj,
|
|
220
|
+
hashMap: {},
|
|
143
221
|
isExported,
|
|
144
222
|
initSpan: {
|
|
145
223
|
start: node.init.span.start - ast.span.start,
|
|
@@ -154,10 +232,23 @@ function loader(source) {
|
|
|
154
232
|
}
|
|
155
233
|
};
|
|
156
234
|
(0, utils_1.traverse)(ast, {
|
|
235
|
+
ImportDeclaration({ node }) {
|
|
236
|
+
if (node.specifiers) {
|
|
237
|
+
node.specifiers.forEach((specifier) => {
|
|
238
|
+
if (specifier.local) {
|
|
239
|
+
excludedSpans.add(specifier.local.span.start);
|
|
240
|
+
}
|
|
241
|
+
if (specifier.imported) {
|
|
242
|
+
excludedSpans.add(specifier.imported.span.start);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
},
|
|
157
247
|
ExportDeclaration({ node }) {
|
|
158
248
|
if (utils_1.t.isVariableDeclaration(node.declaration)) {
|
|
159
249
|
processedDecls.add(node.declaration);
|
|
160
250
|
node.declaration.declarations.forEach((decl) => {
|
|
251
|
+
checkVariantAssignment(decl);
|
|
161
252
|
registerStyle(decl, node.span, true);
|
|
162
253
|
});
|
|
163
254
|
}
|
|
@@ -166,25 +257,10 @@ function loader(source) {
|
|
|
166
257
|
if (processedDecls.has(node))
|
|
167
258
|
return;
|
|
168
259
|
node.declarations.forEach((decl) => {
|
|
260
|
+
checkVariantAssignment(decl);
|
|
169
261
|
registerStyle(decl, node.span, false);
|
|
170
262
|
});
|
|
171
263
|
},
|
|
172
|
-
MemberExpression({ node }) {
|
|
173
|
-
if (utils_1.t.isIdentifier(node.object)) {
|
|
174
|
-
const styleInfo = localCreateStyles[node.object.value];
|
|
175
|
-
if (styleInfo) {
|
|
176
|
-
if (utils_1.t.isIdentifier(node.property)) {
|
|
177
|
-
const hash = styleInfo.hashMap[node.property.value];
|
|
178
|
-
if (!hash && styleInfo.type !== 'constant') {
|
|
179
|
-
styleInfo.hasDynamicAccess = true;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
styleInfo.hasDynamicAccess = true;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
264
|
CallExpression({ node }) {
|
|
189
265
|
const callee = node.callee;
|
|
190
266
|
if (utils_1.t.isMemberExpression(callee) &&
|
|
@@ -195,9 +271,9 @@ function loader(source) {
|
|
|
195
271
|
if (propName === 'keyframes' &&
|
|
196
272
|
args.length > 0 &&
|
|
197
273
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
198
|
-
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
|
|
274
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
|
|
199
275
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
200
|
-
|
|
276
|
+
scannedTables.keyframesObjectTable[hash] = obj;
|
|
201
277
|
replacements.push({
|
|
202
278
|
start: node.span.start - ast.span.start,
|
|
203
279
|
end: node.span.end - ast.span.start,
|
|
@@ -207,11 +283,13 @@ function loader(source) {
|
|
|
207
283
|
else if (propName === 'viewTransition' &&
|
|
208
284
|
args.length > 0 &&
|
|
209
285
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
210
|
-
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
|
|
286
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
|
|
211
287
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
212
|
-
|
|
213
|
-
(
|
|
214
|
-
|
|
288
|
+
scannedTables.viewTransitionObjectTable[hash] = obj;
|
|
289
|
+
if (!isProduction) {
|
|
290
|
+
(0, utils_1.extractOndemandStyles)(obj, extractedSheets, scannedTables);
|
|
291
|
+
(0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets, scannedTables);
|
|
292
|
+
}
|
|
215
293
|
replacements.push({
|
|
216
294
|
start: node.span.start - ast.span.start,
|
|
217
295
|
end: node.span.end - ast.span.start,
|
|
@@ -221,43 +299,69 @@ function loader(source) {
|
|
|
221
299
|
else if (propName === 'createTheme' &&
|
|
222
300
|
args.length > 0 &&
|
|
223
301
|
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
224
|
-
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
|
|
302
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
|
|
303
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
304
|
+
scannedTables.createThemeObjectTable[hash] = obj;
|
|
305
|
+
}
|
|
306
|
+
else if (propName === 'create' &&
|
|
307
|
+
args.length > 0 &&
|
|
308
|
+
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
309
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
|
|
225
310
|
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
226
|
-
|
|
311
|
+
scannedTables.createObjectTable[hash] = obj;
|
|
312
|
+
Object.entries(obj).forEach(([key, style]) => {
|
|
313
|
+
if (typeof style === 'object' && style !== null) {
|
|
314
|
+
const records = (0, utils_1.getStyleRecords)(key, style, 2);
|
|
315
|
+
if (!isProduction) {
|
|
316
|
+
(0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
|
|
317
|
+
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
});
|
|
227
321
|
}
|
|
228
322
|
}
|
|
229
323
|
},
|
|
230
324
|
});
|
|
231
325
|
(0, utils_1.traverse)(ast, {
|
|
232
326
|
MemberExpression({ node }) {
|
|
233
|
-
if (excludedSpans.has(node.span.start))
|
|
234
|
-
return;
|
|
235
327
|
if (utils_1.t.isIdentifier(node.object) && utils_1.t.isIdentifier(node.property)) {
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
328
|
+
const varName = node.object.value;
|
|
329
|
+
const propName = node.property.value;
|
|
330
|
+
const styleInfo = localCreateStyles[varName];
|
|
331
|
+
if (styleInfo) {
|
|
332
|
+
const atomMap = styleInfo.hashMap[propName];
|
|
239
333
|
if (atomMap) {
|
|
240
334
|
replacements.push({
|
|
241
335
|
start: node.span.start - ast.span.start,
|
|
242
336
|
end: node.span.end - ast.span.start,
|
|
243
337
|
content: JSON.stringify(atomMap),
|
|
244
338
|
});
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const hash = mergedCreateTable[varName];
|
|
343
|
+
if (hash) {
|
|
344
|
+
const obj = scannedTables.createObjectTable[hash];
|
|
345
|
+
if (obj && obj[propName]) {
|
|
346
|
+
const style = obj[propName];
|
|
347
|
+
if (typeof style === 'object' && style !== null) {
|
|
348
|
+
const records = (0, utils_1.getStyleRecords)(propName, style, 2);
|
|
349
|
+
if (!isProduction) {
|
|
350
|
+
(0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
|
|
351
|
+
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
352
|
+
}
|
|
353
|
+
const atomMap = {};
|
|
354
|
+
records.forEach((r) => (atomMap[r.key] = r.hash));
|
|
355
|
+
if (Object.keys(atomMap).length > 0) {
|
|
356
|
+
replacements.push({
|
|
357
|
+
start: node.span.start - ast.span.start,
|
|
358
|
+
end: node.span.end - ast.span.start,
|
|
359
|
+
content: JSON.stringify(atomMap),
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
245
363
|
}
|
|
246
364
|
}
|
|
247
|
-
}
|
|
248
|
-
},
|
|
249
|
-
Identifier({ node }) {
|
|
250
|
-
if (excludedSpans.has(node.span.start))
|
|
251
|
-
return;
|
|
252
|
-
if (idSpans.has(node.span.start))
|
|
253
|
-
return;
|
|
254
|
-
const styleInfo = localCreateStyles[node.value];
|
|
255
|
-
if (styleInfo && !styleInfo.hasDynamicAccess) {
|
|
256
|
-
replacements.push({
|
|
257
|
-
start: node.span.start - ast.span.start,
|
|
258
|
-
end: node.span.end - ast.span.start,
|
|
259
|
-
content: JSON.stringify(styleInfo.hashMap),
|
|
260
|
-
});
|
|
261
365
|
}
|
|
262
366
|
},
|
|
263
367
|
CallExpression({ node }) {
|
|
@@ -266,182 +370,299 @@ function loader(source) {
|
|
|
266
370
|
utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
|
|
267
371
|
utils_1.t.isIdentifier(callee.property, { name: 'props' })) {
|
|
268
372
|
const args = node.arguments;
|
|
269
|
-
const
|
|
270
|
-
if (utils_1.t.isObjectExpression(expr)
|
|
271
|
-
utils_1.
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
utils_1.t.isNullLiteral(expr))
|
|
275
|
-
return true;
|
|
276
|
-
if (utils_1.t.isMemberExpression(expr) &&
|
|
373
|
+
const resolveStyleObject = (expr) => {
|
|
374
|
+
if (utils_1.t.isObjectExpression(expr)) {
|
|
375
|
+
return (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
|
|
376
|
+
}
|
|
377
|
+
else if (utils_1.t.isMemberExpression(expr) &&
|
|
277
378
|
utils_1.t.isIdentifier(expr.object) &&
|
|
278
|
-
utils_1.t.isIdentifier(expr.property)) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
379
|
+
(utils_1.t.isIdentifier(expr.property) || expr.property.type === 'Computed')) {
|
|
380
|
+
if (expr.property.type === 'Computed') {
|
|
381
|
+
return {};
|
|
382
|
+
}
|
|
383
|
+
const varName = expr.object
|
|
384
|
+
.value;
|
|
385
|
+
const propName = expr.property
|
|
386
|
+
.value;
|
|
387
|
+
const styleInfo = localCreateStyles[varName];
|
|
388
|
+
if (styleInfo && styleInfo.obj[propName]) {
|
|
389
|
+
const style = styleInfo.obj[propName];
|
|
390
|
+
if (typeof style === 'object' && style !== null) {
|
|
391
|
+
return style;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const hash = mergedCreateTable[varName];
|
|
395
|
+
if (hash) {
|
|
396
|
+
const obj = scannedTables.createObjectTable[hash];
|
|
397
|
+
if (obj && obj[propName]) {
|
|
398
|
+
const style = obj[propName];
|
|
399
|
+
if (typeof style === 'object' && style !== null) {
|
|
400
|
+
return style;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
283
404
|
}
|
|
284
|
-
if (utils_1.t.isIdentifier(expr)) {
|
|
285
|
-
const
|
|
286
|
-
|
|
405
|
+
else if (utils_1.t.isIdentifier(expr)) {
|
|
406
|
+
const varName = expr.value;
|
|
407
|
+
const styleInfo = localCreateStyles[varName];
|
|
408
|
+
if (styleInfo && styleInfo.obj) {
|
|
409
|
+
return styleInfo.obj;
|
|
410
|
+
}
|
|
411
|
+
const hash = mergedCreateTable[varName];
|
|
412
|
+
if (hash) {
|
|
413
|
+
const obj = scannedTables.createObjectTable[hash];
|
|
414
|
+
if (obj && typeof obj === 'object') {
|
|
415
|
+
return obj;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (localCreateStyles[varName]) {
|
|
419
|
+
return localCreateStyles[varName].obj;
|
|
420
|
+
}
|
|
421
|
+
const vHash = mergedVariantsTable[varName];
|
|
422
|
+
if (vHash) {
|
|
423
|
+
return scannedTables.variantsObjectTable[vHash];
|
|
424
|
+
}
|
|
287
425
|
}
|
|
288
|
-
return
|
|
426
|
+
return null;
|
|
289
427
|
};
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
428
|
+
const conditionals = [];
|
|
429
|
+
let groupIdCounter = 0;
|
|
430
|
+
let baseStyle = {};
|
|
431
|
+
let isOptimizable = true;
|
|
432
|
+
for (const arg of args) {
|
|
433
|
+
const expr = arg.expression;
|
|
434
|
+
if (utils_1.t.isCallExpression(expr) && utils_1.t.isIdentifier(expr.callee)) {
|
|
435
|
+
const varName = expr.callee.value;
|
|
436
|
+
let variantObj;
|
|
437
|
+
if (localCreateStyles[varName] && localCreateStyles[varName].obj) {
|
|
438
|
+
variantObj = localCreateStyles[varName].obj;
|
|
439
|
+
}
|
|
440
|
+
else if (mergedVariantsTable[varName]) {
|
|
441
|
+
const hash = mergedVariantsTable[varName];
|
|
442
|
+
if (scannedTables.variantsObjectTable[hash]) {
|
|
443
|
+
variantObj = scannedTables.variantsObjectTable[hash];
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (variantObj) {
|
|
447
|
+
const callArgs = expr.arguments;
|
|
448
|
+
if (callArgs.length === 1 && !callArgs[0].spread) {
|
|
449
|
+
const arg = callArgs[0].expression;
|
|
450
|
+
if (arg.type === 'ObjectExpression') {
|
|
451
|
+
for (const prop of arg.properties) {
|
|
452
|
+
let groupName;
|
|
453
|
+
let valExpr;
|
|
454
|
+
if (prop.type === 'KeyValueProperty' &&
|
|
455
|
+
prop.key.type === 'Identifier') {
|
|
456
|
+
groupName = prop.key.value;
|
|
457
|
+
valExpr = prop.value;
|
|
458
|
+
}
|
|
459
|
+
else if (prop.type === 'Identifier') {
|
|
460
|
+
groupName = prop.value;
|
|
461
|
+
valExpr = prop;
|
|
462
|
+
}
|
|
463
|
+
if (groupName && valExpr) {
|
|
464
|
+
const groupVariants = variantObj[groupName];
|
|
465
|
+
if (!groupVariants)
|
|
466
|
+
continue;
|
|
467
|
+
const currentGroupId = ++groupIdCounter;
|
|
468
|
+
const valStart = valExpr.span.start - ast.span.start;
|
|
469
|
+
const valEnd = valExpr.span.end - ast.span.start;
|
|
470
|
+
const valSource = source.substring(valStart, valEnd);
|
|
471
|
+
if (valExpr.type === 'StringLiteral') {
|
|
472
|
+
if (groupVariants[valExpr.value]) {
|
|
473
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, groupVariants[valExpr.value]);
|
|
474
|
+
}
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
Object.entries(groupVariants).forEach(([optionName, style]) => {
|
|
478
|
+
conditionals.push({
|
|
479
|
+
test: valExpr,
|
|
480
|
+
testString: `${valSource} === '${optionName}'`,
|
|
481
|
+
truthy: style,
|
|
482
|
+
falsy: {},
|
|
483
|
+
groupId: currentGroupId,
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
const argStart = arg.span.start - ast.span.start;
|
|
491
|
+
const argEnd = arg.span.end - ast.span.start;
|
|
492
|
+
const argSource = source.substring(argStart, argEnd);
|
|
493
|
+
if (utils_1.t.isStringLiteral(arg)) {
|
|
494
|
+
if (variantObj[arg.value]) {
|
|
495
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
|
|
496
|
+
}
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
const currentGroupId = ++groupIdCounter;
|
|
500
|
+
Object.entries(variantObj).forEach(([key, style]) => {
|
|
501
|
+
conditionals.push({
|
|
502
|
+
test: arg,
|
|
503
|
+
testString: `${argSource} === '${key}'`,
|
|
504
|
+
truthy: style,
|
|
505
|
+
falsy: {},
|
|
506
|
+
groupId: currentGroupId,
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
isOptimizable = false;
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
295
514
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
515
|
+
const staticStyle = resolveStyleObject(expr);
|
|
516
|
+
if (staticStyle) {
|
|
517
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
|
|
518
|
+
continue;
|
|
300
519
|
}
|
|
301
|
-
if (
|
|
302
|
-
|
|
303
|
-
|
|
520
|
+
else if (expr.type === 'ConditionalExpression') {
|
|
521
|
+
const truthyStyle = resolveStyleObject(expr.consequent);
|
|
522
|
+
const falsyStyle = resolveStyleObject(expr.alternate);
|
|
523
|
+
if (truthyStyle !== null && falsyStyle !== null) {
|
|
524
|
+
conditionals.push({
|
|
525
|
+
test: expr.test,
|
|
526
|
+
truthy: truthyStyle,
|
|
527
|
+
falsy: falsyStyle,
|
|
528
|
+
});
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
304
531
|
}
|
|
305
|
-
if (
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
532
|
+
else if (expr.type === 'BinaryExpression' &&
|
|
533
|
+
expr.operator === '&&') {
|
|
534
|
+
const truthyStyle = resolveStyleObject(expr.right);
|
|
535
|
+
if (truthyStyle !== null) {
|
|
536
|
+
conditionals.push({
|
|
537
|
+
test: expr.left,
|
|
538
|
+
truthy: truthyStyle,
|
|
539
|
+
falsy: {},
|
|
540
|
+
});
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
310
543
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const expr = arg.expression;
|
|
318
|
-
if (utils_1.t.isObjectExpression(expr)) {
|
|
319
|
-
const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
|
|
320
|
-
return obj ? (0, utils_1.deepMerge)(acc, obj) : acc;
|
|
544
|
+
else if (expr.type === 'ParenthesisExpression') {
|
|
545
|
+
const inner = expr.expression;
|
|
546
|
+
const innerStatic = resolveStyleObject(inner);
|
|
547
|
+
if (innerStatic) {
|
|
548
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, innerStatic);
|
|
549
|
+
continue;
|
|
321
550
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
551
|
+
}
|
|
552
|
+
isOptimizable = false;
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
if (isOptimizable &&
|
|
556
|
+
(args.length > 0 || Object.keys(baseStyle).length > 0)) {
|
|
557
|
+
if (conditionals.length === 0) {
|
|
558
|
+
if (!isProduction) {
|
|
559
|
+
(0, utils_1.extractOndemandStyles)(baseStyle, extractedSheets, scannedTables);
|
|
329
560
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
561
|
+
const hash = (0, zss_engine_1.genBase36Hash)(baseStyle, 1, 8);
|
|
562
|
+
const records = (0, utils_1.getStyleRecords)(hash, baseStyle, 2);
|
|
563
|
+
if (!isProduction) {
|
|
564
|
+
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
333
565
|
}
|
|
334
|
-
|
|
335
|
-
}, {});
|
|
336
|
-
if (Object.keys(merged).length > 0) {
|
|
337
|
-
(0, utils_1.extractOndemandStyles)(merged, extractedSheets);
|
|
338
|
-
const hash = (0, zss_engine_1.genBase36Hash)(merged, 1, 8);
|
|
339
|
-
const records = (0, utils_1.getStyleRecords)(hash, merged, 2);
|
|
340
|
-
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
341
|
-
const resultHash = records
|
|
342
|
-
.map((r) => r.hash)
|
|
343
|
-
.join(' ');
|
|
566
|
+
const className = records.map((r) => r.hash).join(' ');
|
|
344
567
|
replacements.push({
|
|
345
568
|
start: node.span.start - ast.span.start,
|
|
346
569
|
end: node.span.end - ast.span.start,
|
|
347
|
-
content: JSON.stringify(
|
|
570
|
+
content: JSON.stringify(className),
|
|
348
571
|
});
|
|
349
572
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
573
|
+
else {
|
|
574
|
+
const table = {};
|
|
575
|
+
const combinations = 1 << conditionals.length;
|
|
576
|
+
for (let i = 0; i < combinations; i++) {
|
|
577
|
+
const currentClassNames = [];
|
|
578
|
+
const seenGroups = new Set();
|
|
579
|
+
let impossible = false;
|
|
580
|
+
if (Object.keys(baseStyle).length > 0) {
|
|
581
|
+
if (!isProduction) {
|
|
582
|
+
(0, utils_1.extractOndemandStyles)(baseStyle, extractedSheets, scannedTables);
|
|
583
|
+
}
|
|
584
|
+
const hash = (0, zss_engine_1.genBase36Hash)(baseStyle, 1, 8);
|
|
585
|
+
const records = (0, utils_1.getStyleRecords)(hash, baseStyle, 2);
|
|
586
|
+
if (!isProduction) {
|
|
587
|
+
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
588
|
+
}
|
|
589
|
+
currentClassNames.push(...records.map((r) => r.hash));
|
|
357
590
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
replacements.push({
|
|
384
|
-
start: expr.span.start - ast.span.start,
|
|
385
|
-
end: expr.span.end - ast.span.start,
|
|
386
|
-
content: JSON.stringify(atomMap),
|
|
387
|
-
});
|
|
591
|
+
for (let j = 0; j < conditionals.length; j++) {
|
|
592
|
+
let targetStyle = {};
|
|
593
|
+
if ((i >> j) & 1) {
|
|
594
|
+
if (conditionals[j].groupId !== undefined) {
|
|
595
|
+
if (seenGroups.has(conditionals[j].groupId)) {
|
|
596
|
+
impossible = true;
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
seenGroups.add(conditionals[j].groupId);
|
|
600
|
+
}
|
|
601
|
+
targetStyle = conditionals[j].truthy;
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
targetStyle = conditionals[j].falsy;
|
|
605
|
+
}
|
|
606
|
+
if (Object.keys(targetStyle).length > 0) {
|
|
607
|
+
if (!isProduction) {
|
|
608
|
+
(0, utils_1.extractOndemandStyles)(targetStyle, extractedSheets, scannedTables);
|
|
609
|
+
}
|
|
610
|
+
const hash = (0, zss_engine_1.genBase36Hash)(targetStyle, 1, 8);
|
|
611
|
+
const records = (0, utils_1.getStyleRecords)(hash, targetStyle, 2);
|
|
612
|
+
if (!isProduction) {
|
|
613
|
+
records.forEach((r) => extractedSheets.push(r.sheet));
|
|
614
|
+
}
|
|
615
|
+
currentClassNames.push(...records.map((r) => r.hash));
|
|
388
616
|
}
|
|
389
617
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (info) {
|
|
394
|
-
excludedSpans.add(expr.span.start);
|
|
395
|
-
replacements.push({
|
|
396
|
-
start: expr.span.start - ast.span.start,
|
|
397
|
-
end: expr.span.end - ast.span.start,
|
|
398
|
-
content: JSON.stringify(info.hashMap),
|
|
399
|
-
});
|
|
618
|
+
if (impossible) {
|
|
619
|
+
table[i] = '';
|
|
620
|
+
continue;
|
|
400
621
|
}
|
|
622
|
+
table[i] = currentClassNames.join(' ');
|
|
401
623
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
624
|
+
let indexExpr = '';
|
|
625
|
+
if (conditionals.length === 0) {
|
|
626
|
+
indexExpr = '0';
|
|
405
627
|
}
|
|
406
|
-
else
|
|
407
|
-
(
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
628
|
+
else {
|
|
629
|
+
const parts = conditionals.map((c, idx) => {
|
|
630
|
+
if (c.testString) {
|
|
631
|
+
return `(!!(${c.testString}) << ${idx})`;
|
|
632
|
+
}
|
|
633
|
+
const start = c.test.span.start - ast.span.start;
|
|
634
|
+
const end = c.test.span.end - ast.span.start;
|
|
635
|
+
const testStr = source.substring(start, end);
|
|
636
|
+
return `(!!(${testStr}) << ${idx})`;
|
|
637
|
+
});
|
|
638
|
+
indexExpr = parts.join(' | ');
|
|
412
639
|
}
|
|
413
|
-
|
|
414
|
-
|
|
640
|
+
const tableStr = JSON.stringify(table);
|
|
641
|
+
const replacement = `${tableStr}[${indexExpr}]`;
|
|
642
|
+
replacements.push({
|
|
643
|
+
start: node.span.start - ast.span.start,
|
|
644
|
+
end: node.span.end - ast.span.start,
|
|
645
|
+
content: replacement,
|
|
646
|
+
});
|
|
647
|
+
}
|
|
415
648
|
}
|
|
416
649
|
}
|
|
417
650
|
},
|
|
418
651
|
});
|
|
419
652
|
Object.values(localCreateStyles).forEach((info) => {
|
|
420
653
|
if (info.isExported) {
|
|
421
|
-
const content = isTSFile || info.hasDynamicAccess
|
|
422
|
-
? JSON.stringify(info.hashMap)
|
|
423
|
-
: JSON.stringify('');
|
|
424
654
|
replacements.push({
|
|
425
|
-
start: info.
|
|
426
|
-
end: info.
|
|
427
|
-
content,
|
|
655
|
+
start: info.declSpan.start,
|
|
656
|
+
end: info.declSpan.end,
|
|
657
|
+
content: JSON.stringify(''),
|
|
428
658
|
});
|
|
429
659
|
}
|
|
430
660
|
else {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
else {
|
|
439
|
-
replacements.push({
|
|
440
|
-
start: info.declSpan.start,
|
|
441
|
-
end: info.declSpan.end,
|
|
442
|
-
content: '',
|
|
443
|
-
});
|
|
444
|
-
}
|
|
661
|
+
replacements.push({
|
|
662
|
+
start: info.declSpan.start,
|
|
663
|
+
end: info.declSpan.end,
|
|
664
|
+
content: '',
|
|
665
|
+
});
|
|
445
666
|
}
|
|
446
667
|
});
|
|
447
668
|
if (extractedSheets.length > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/webpack-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Plumeria Webpack plugin",
|
|
5
5
|
"author": "Refirst 11",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"zero-virtual.css"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@plumeria/utils": "^
|
|
25
|
+
"@plumeria/utils": "^6.0.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@swc/core": "1.15.8",
|