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