@plumeria/webpack-plugin 3.0.1 → 3.1.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.
Files changed (2) hide show
  1. package/dist/index.js +271 -96
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -37,37 +37,108 @@ function loader(source) {
37
37
  });
38
38
  const localConsts = (0, utils_1.collectLocalConsts)(ast);
39
39
  Object.assign(utils_1.tables.staticTable, localConsts);
40
+ const isTSFile = this.resourcePath.endsWith('.ts') && !this.resourcePath.endsWith('.tsx');
40
41
  const localCreateStyles = {};
41
42
  const replacements = [];
42
- (0, utils_1.traverse)(ast, {
43
- VariableDeclarator({ node }) {
44
- if (node.id.type === 'Identifier' &&
45
- node.init &&
46
- utils_1.t.isCallExpression(node.init) &&
47
- utils_1.t.isMemberExpression(node.init.callee) &&
48
- utils_1.t.isIdentifier(node.init.callee.object, { name: 'css' }) &&
49
- utils_1.t.isIdentifier(node.init.callee.property, { name: 'create' }) &&
50
- node.init.arguments.length === 1 &&
43
+ const processedDecls = new Set();
44
+ const excludedSpans = new Set();
45
+ const idSpans = new Set();
46
+ const registerStyle = (node, declSpan, isExported) => {
47
+ if (utils_1.t.isIdentifier(node.id) &&
48
+ node.init &&
49
+ utils_1.t.isCallExpression(node.init) &&
50
+ utils_1.t.isMemberExpression(node.init.callee) &&
51
+ utils_1.t.isIdentifier(node.init.callee.object, { name: 'css' }) &&
52
+ utils_1.t.isIdentifier(node.init.callee.property) &&
53
+ node.init.arguments.length >= 1) {
54
+ const propName = node.init.callee.property.value;
55
+ if (propName === 'create' &&
51
56
  utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
52
57
  const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
53
58
  if (obj) {
54
- localCreateStyles[node.id.value] = obj;
55
59
  const hashMap = {};
56
60
  Object.entries(obj).forEach(([key, style]) => {
57
61
  const records = (0, utils_1.getStyleRecords)(key, style, 2);
58
- const propMap = {};
59
62
  (0, utils_1.extractOndemandStyles)(style, extractedSheets);
60
63
  records.forEach((r) => {
61
- propMap[r.key] = r.hash;
62
64
  extractedSheets.push(r.sheet);
63
65
  });
64
- hashMap[key] = records.map((r) => r.hash).join(' ');
66
+ const atomMap = {};
67
+ records.forEach((r) => (atomMap[r.key] = r.hash));
68
+ hashMap[key] = atomMap;
65
69
  });
66
- replacements.push({
70
+ if (utils_1.t.isIdentifier(node.id)) {
71
+ idSpans.add(node.id.span.start);
72
+ }
73
+ localCreateStyles[node.id.value] = {
74
+ name: node.id.value,
75
+ type: 'create',
76
+ obj,
77
+ hashMap,
78
+ hasDynamicAccess: false,
79
+ isExported,
80
+ initSpan: {
81
+ start: node.init.span.start - ast.span.start,
82
+ end: node.init.span.end - ast.span.start,
83
+ },
84
+ declSpan: {
85
+ start: declSpan.start - ast.span.start,
86
+ end: declSpan.end - ast.span.start,
87
+ },
88
+ };
89
+ }
90
+ }
91
+ else if ((propName === 'createTheme' || propName === 'createStatic') &&
92
+ (utils_1.t.isObjectExpression(node.init.arguments[0].expression) ||
93
+ utils_1.t.isStringLiteral(node.init.arguments[0].expression))) {
94
+ localCreateStyles[node.id.value] = {
95
+ name: node.id.value,
96
+ type: 'constant',
97
+ obj: {},
98
+ hashMap: {},
99
+ hasDynamicAccess: false,
100
+ isExported,
101
+ initSpan: {
67
102
  start: node.init.span.start - ast.span.start,
68
103
  end: node.init.span.end - ast.span.start,
69
- content: JSON.stringify(hashMap),
70
- });
104
+ },
105
+ declSpan: {
106
+ start: declSpan.start - ast.span.start,
107
+ end: declSpan.end - ast.span.start,
108
+ },
109
+ };
110
+ }
111
+ }
112
+ };
113
+ (0, utils_1.traverse)(ast, {
114
+ ExportDeclaration({ node }) {
115
+ if (utils_1.t.isVariableDeclaration(node.declaration)) {
116
+ processedDecls.add(node.declaration);
117
+ node.declaration.declarations.forEach((decl) => {
118
+ registerStyle(decl, node.span, true);
119
+ });
120
+ }
121
+ },
122
+ VariableDeclaration({ node }) {
123
+ if (processedDecls.has(node))
124
+ return;
125
+ node.declarations.forEach((decl) => {
126
+ registerStyle(decl, node.span, false);
127
+ });
128
+ },
129
+ MemberExpression({ node }) {
130
+ if (utils_1.t.isIdentifier(node.object)) {
131
+ const styleInfo = localCreateStyles[node.object.value];
132
+ if (styleInfo) {
133
+ if (utils_1.t.isIdentifier(node.property)) {
134
+ const hash = styleInfo.hashMap[node.property.value];
135
+ if (!hash && styleInfo.type !== 'constant') {
136
+ styleInfo.hasDynamicAccess = true;
137
+ }
138
+ }
139
+ else {
140
+ styleInfo.hasDynamicAccess = true;
141
+ }
71
142
  }
72
143
  }
73
144
  },
@@ -76,66 +147,9 @@ function loader(source) {
76
147
  if (utils_1.t.isMemberExpression(callee) &&
77
148
  utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
78
149
  utils_1.t.isIdentifier(callee.property)) {
150
+ const propName = callee.property.value;
79
151
  const args = node.arguments;
80
- if (callee.property.value === 'props') {
81
- const merged = {};
82
- let allStatic = true;
83
- args.forEach((arg) => {
84
- const expr = arg.expression;
85
- if (utils_1.t.isObjectExpression(expr)) {
86
- const obj = (0, utils_1.objectExpressionToObject)(expr, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
87
- if (obj) {
88
- Object.assign(merged, obj);
89
- }
90
- else {
91
- allStatic = false;
92
- }
93
- }
94
- else if (utils_1.t.isMemberExpression(expr)) {
95
- if (utils_1.t.isIdentifier(expr.object) &&
96
- utils_1.t.isIdentifier(expr.property)) {
97
- const varName = expr.object.value;
98
- const propName = expr.property.value;
99
- const styleSet = localCreateStyles[varName];
100
- if (styleSet && styleSet[propName]) {
101
- Object.assign(merged, styleSet[propName]);
102
- }
103
- else {
104
- allStatic = false;
105
- }
106
- }
107
- else {
108
- allStatic = false;
109
- }
110
- }
111
- else if (utils_1.t.isIdentifier(expr)) {
112
- const obj = localCreateStyles[expr.value];
113
- if (obj) {
114
- Object.assign(merged, obj);
115
- }
116
- else {
117
- allStatic = false;
118
- }
119
- }
120
- else {
121
- allStatic = false;
122
- }
123
- });
124
- if (allStatic && Object.keys(merged).length > 0) {
125
- (0, utils_1.extractOndemandStyles)(merged, extractedSheets);
126
- const hash = (0, zss_engine_1.genBase36Hash)(merged, 1, 8);
127
- const records = (0, utils_1.getStyleRecords)(hash, merged, 2);
128
- records.forEach((r) => {
129
- extractedSheets.push(r.sheet);
130
- });
131
- replacements.push({
132
- start: node.span.start - ast.span.start,
133
- end: node.span.end - ast.span.start,
134
- content: JSON.stringify(records.map((r) => r.hash).join(' ')),
135
- });
136
- }
137
- }
138
- else if (callee.property.value === 'keyframes' &&
152
+ if (propName === 'keyframes' &&
139
153
  args.length > 0 &&
140
154
  utils_1.t.isObjectExpression(args[0].expression)) {
141
155
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
@@ -147,7 +161,7 @@ function loader(source) {
147
161
  content: JSON.stringify(`kf-${hash}`),
148
162
  });
149
163
  }
150
- else if (callee.property.value === 'viewTransition' &&
164
+ else if (propName === 'viewTransition' &&
151
165
  args.length > 0 &&
152
166
  utils_1.t.isObjectExpression(args[0].expression)) {
153
167
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
@@ -159,30 +173,194 @@ function loader(source) {
159
173
  content: JSON.stringify(`vt-${hash}`),
160
174
  });
161
175
  }
162
- else if (callee.property.value === 'createTheme' &&
176
+ else if (propName === 'createTheme' &&
163
177
  args.length > 0 &&
164
178
  utils_1.t.isObjectExpression(args[0].expression)) {
165
179
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
166
180
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
167
181
  utils_1.tables.createThemeObjectTable[hash] = obj;
168
- replacements.push({
169
- start: node.span.start - ast.span.start,
170
- end: node.span.end - ast.span.start,
171
- content: JSON.stringify(''),
172
- });
173
182
  }
174
- else if (callee.property.value === 'createStatic' &&
175
- args.length > 0 &&
176
- utils_1.t.isStringLiteral(args[0].expression)) {
177
- replacements.push({
178
- start: node.span.start - ast.span.start,
179
- end: node.span.end - ast.span.start,
180
- content: JSON.stringify(''),
183
+ }
184
+ },
185
+ });
186
+ (0, utils_1.traverse)(ast, {
187
+ MemberExpression({ node }) {
188
+ if (excludedSpans.has(node.span.start))
189
+ return;
190
+ if (utils_1.t.isIdentifier(node.object) && utils_1.t.isIdentifier(node.property)) {
191
+ const styleInfo = localCreateStyles[node.object.value];
192
+ if (styleInfo && !styleInfo.hasDynamicAccess) {
193
+ const atomMap = styleInfo.hashMap[node.property.value];
194
+ if (atomMap) {
195
+ const combinedHash = Object.values(atomMap).join(' ');
196
+ replacements.push({
197
+ start: node.span.start - ast.span.start,
198
+ end: node.span.end - ast.span.start,
199
+ content: JSON.stringify(combinedHash),
200
+ });
201
+ }
202
+ }
203
+ }
204
+ },
205
+ Identifier({ node }) {
206
+ if (excludedSpans.has(node.span.start))
207
+ return;
208
+ if (idSpans.has(node.span.start))
209
+ return;
210
+ const styleInfo = localCreateStyles[node.value];
211
+ if (styleInfo && !styleInfo.hasDynamicAccess) {
212
+ const fullHashMap = {};
213
+ Object.entries(styleInfo.hashMap).forEach(([key, atomMap]) => {
214
+ fullHashMap[key] = Object.values(atomMap).join(' ');
215
+ });
216
+ replacements.push({
217
+ start: node.span.start - ast.span.start,
218
+ end: node.span.end - ast.span.start,
219
+ content: JSON.stringify(fullHashMap),
220
+ });
221
+ }
222
+ },
223
+ CallExpression({ node }) {
224
+ const callee = node.callee;
225
+ if (utils_1.t.isMemberExpression(callee) &&
226
+ utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
227
+ utils_1.t.isIdentifier(callee.property, { name: 'props' })) {
228
+ const args = node.arguments;
229
+ const checkStatic = (expr) => {
230
+ if (utils_1.t.isObjectExpression(expr) ||
231
+ utils_1.t.isStringLiteral(expr) ||
232
+ utils_1.t.isNumericLiteral(expr) ||
233
+ utils_1.t.isBooleanLiteral(expr) ||
234
+ utils_1.t.isNullLiteral(expr))
235
+ return true;
236
+ if (utils_1.t.isMemberExpression(expr) &&
237
+ utils_1.t.isIdentifier(expr.object) &&
238
+ utils_1.t.isIdentifier(expr.property)) {
239
+ const styleInfo = localCreateStyles[expr.object.value];
240
+ return !!(styleInfo &&
241
+ !styleInfo.hasDynamicAccess &&
242
+ styleInfo.hashMap[expr.property.value]);
243
+ }
244
+ if (utils_1.t.isIdentifier(expr)) {
245
+ const styleInfo = localCreateStyles[expr.value];
246
+ return !!(styleInfo && !styleInfo.hasDynamicAccess);
247
+ }
248
+ return false;
249
+ };
250
+ const allStatic = args.every((arg) => checkStatic(arg.expression));
251
+ if (allStatic) {
252
+ const merged = {};
253
+ args.forEach((arg) => {
254
+ const expr = arg.expression;
255
+ if (utils_1.t.isObjectExpression(expr)) {
256
+ const obj = (0, utils_1.objectExpressionToObject)(expr, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
257
+ obj && Object.assign(merged, obj);
258
+ }
259
+ else if (utils_1.t.isMemberExpression(expr) &&
260
+ utils_1.t.isIdentifier(expr.object) &&
261
+ utils_1.t.isIdentifier(expr.property)) {
262
+ const styleInfo = localCreateStyles[expr.object.value];
263
+ if (styleInfo) {
264
+ Object.assign(merged, styleInfo.obj[expr.property.value]);
265
+ }
266
+ }
267
+ else if (utils_1.t.isIdentifier(expr)) {
268
+ const styleInfo = localCreateStyles[expr.value];
269
+ if (styleInfo) {
270
+ Object.assign(merged, styleInfo.obj);
271
+ }
272
+ }
181
273
  });
274
+ if (Object.keys(merged).length > 0) {
275
+ (0, utils_1.extractOndemandStyles)(merged, extractedSheets);
276
+ const hash = (0, zss_engine_1.genBase36Hash)(merged, 1, 8);
277
+ const records = (0, utils_1.getStyleRecords)(hash, merged, 2);
278
+ records.forEach((r) => extractedSheets.push(r.sheet));
279
+ const resultHash = records
280
+ .map((r) => r.hash)
281
+ .join(' ');
282
+ replacements.push({
283
+ start: node.span.start - ast.span.start,
284
+ end: node.span.end - ast.span.start,
285
+ content: JSON.stringify(resultHash),
286
+ });
287
+ }
288
+ }
289
+ else {
290
+ const processExpr = (expr) => {
291
+ if (utils_1.t.isMemberExpression(expr) &&
292
+ utils_1.t.isIdentifier(expr.object) &&
293
+ utils_1.t.isIdentifier(expr.property)) {
294
+ const info = localCreateStyles[expr.object.value];
295
+ if (info) {
296
+ const atomMap = info.hashMap[expr.property.value];
297
+ if (atomMap) {
298
+ excludedSpans.add(expr.span.start);
299
+ replacements.push({
300
+ start: expr.span.start - ast.span.start,
301
+ end: expr.span.end - ast.span.start,
302
+ content: JSON.stringify(atomMap),
303
+ });
304
+ }
305
+ }
306
+ }
307
+ else if (utils_1.t.isIdentifier(expr)) {
308
+ const info = localCreateStyles[expr.value];
309
+ if (info) {
310
+ info.hasDynamicAccess = true;
311
+ excludedSpans.add(expr.span.start);
312
+ replacements.push({
313
+ start: expr.span.start - ast.span.start,
314
+ end: expr.span.end - ast.span.start,
315
+ content: JSON.stringify(info.hashMap),
316
+ });
317
+ }
318
+ }
319
+ else if (utils_1.t.isConditionalExpression(expr)) {
320
+ processExpr(expr.consequent);
321
+ processExpr(expr.alternate);
322
+ }
323
+ else if (utils_1.t.isBinaryExpression(expr) &&
324
+ (expr.operator === '&&' ||
325
+ expr.operator === '||' ||
326
+ expr.operator === '??')) {
327
+ processExpr(expr.left);
328
+ processExpr(expr.right);
329
+ }
330
+ };
331
+ args.forEach((arg) => processExpr(arg.expression));
182
332
  }
183
333
  }
184
334
  },
185
335
  });
336
+ Object.values(localCreateStyles).forEach((info) => {
337
+ if (info.isExported) {
338
+ const content = isTSFile || info.hasDynamicAccess
339
+ ? JSON.stringify(info.hashMap)
340
+ : JSON.stringify('');
341
+ replacements.push({
342
+ start: info.initSpan.start,
343
+ end: info.initSpan.end,
344
+ content,
345
+ });
346
+ }
347
+ else {
348
+ if (info.hasDynamicAccess) {
349
+ replacements.push({
350
+ start: info.initSpan.start,
351
+ end: info.initSpan.end,
352
+ content: JSON.stringify(info.hashMap),
353
+ });
354
+ }
355
+ else {
356
+ replacements.push({
357
+ start: info.declSpan.start,
358
+ end: info.declSpan.end,
359
+ content: '',
360
+ });
361
+ }
362
+ }
363
+ });
186
364
  if (extractedSheets.length > 0) {
187
365
  fileStyles.baseStyles =
188
366
  (fileStyles.baseStyles || '') + extractedSheets.join('');
@@ -191,8 +369,10 @@ function loader(source) {
191
369
  let offset = 0;
192
370
  const parts = [];
193
371
  replacements
194
- .sort((a, b) => a.start - b.start)
372
+ .sort((a, b) => a.start - b.start || b.end - a.end)
195
373
  .forEach((r) => {
374
+ if (r.start < offset)
375
+ return;
196
376
  parts.push(buffer.subarray(offset, r.start));
197
377
  parts.push(Buffer.from(r.content));
198
378
  offset = r.end;
@@ -210,12 +390,7 @@ function loader(source) {
210
390
  }
211
391
  const virtualCssRequest = stringifyRequest(this, `${VIRTUAL_CSS_PATH}`);
212
392
  const postfix = `\nimport ${virtualCssRequest};`;
213
- let css = '';
214
- try {
215
- css = fs_1.default.readFileSync(VIRTUAL_FILE_PATH, 'utf-8');
216
- }
217
- catch (e) {
218
- }
393
+ const css = fs_1.default.readFileSync(VIRTUAL_FILE_PATH, 'utf-8');
219
394
  function generateOrderedCSS(styles) {
220
395
  const sections = [];
221
396
  if (styles.keyframeStyles?.trim()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/webpack-plugin",
3
- "version": "3.0.1",
3
+ "version": "3.1.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": "^3.0.1"
25
+ "@plumeria/utils": "^3.1.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.2",