@steambrew/ttc 3.1.1 → 3.1.2

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 CHANGED
@@ -275,6 +275,7 @@ function constSysfsExpr(options = {}) {
275
275
  return null;
276
276
  const magicString = new MagicString(code);
277
277
  let hasReplaced = false;
278
+ let constSysfsImport = null;
278
279
  try {
279
280
  const stringVariables = new Map();
280
281
  const ast = parser.parse(code, {
@@ -289,6 +290,25 @@ function constSysfsExpr(options = {}) {
289
290
  stringVariables.set(id.name, init.value);
290
291
  }
291
292
  },
293
+ ImportDeclaration(nodePath) {
294
+ const decl = nodePath.node;
295
+ const specifiers = decl.specifiers;
296
+ const idx = specifiers.findIndex((s) => s.type === 'ImportSpecifier' && (s.imported?.name === 'constSysfsExpr' || s.local?.name === 'constSysfsExpr'));
297
+ if (idx !== -1 && typeof decl.start === 'number' && typeof decl.end === 'number') {
298
+ const spec = specifiers[idx];
299
+ if (typeof spec.start === 'number' && typeof spec.end === 'number') {
300
+ constSysfsImport = {
301
+ specifierStart: spec.start,
302
+ specifierEnd: spec.end,
303
+ declStart: decl.start,
304
+ declEnd: decl.end,
305
+ isOnlySpecifier: specifiers.length === 1,
306
+ prevSpecifierEnd: idx > 0 && typeof specifiers[idx - 1].end === 'number' ? specifiers[idx - 1].end : null,
307
+ nextSpecifierStart: idx < specifiers.length - 1 && typeof specifiers[idx + 1].start === 'number' ? specifiers[idx + 1].start : null,
308
+ };
309
+ }
310
+ }
311
+ },
292
312
  });
293
313
  traverse(ast, {
294
314
  CallExpression: (nodePath) => {
@@ -427,7 +447,7 @@ function constSysfsExpr(options = {}) {
427
447
  this.addWatchFile(singleFilePath);
428
448
  }
429
449
  catch (fileError) {
430
- let message = String(fileError instanceof Error ? fileError.message : fileError ?? 'Unknown file read error');
450
+ let message = String(fileError instanceof Error ? fileError.message : (fileError ?? 'Unknown file read error'));
431
451
  this.error(`Error reading file ${singleFilePath}: ${message}`, node.loc?.start.index);
432
452
  return;
433
453
  }
@@ -451,7 +471,7 @@ function constSysfsExpr(options = {}) {
451
471
  this.addWatchFile(fullPath);
452
472
  }
453
473
  catch (fileError) {
454
- let message = String(fileError instanceof Error ? fileError.message : fileError ?? 'Unknown file read error');
474
+ let message = String(fileError instanceof Error ? fileError.message : (fileError ?? 'Unknown file read error'));
455
475
  this.warn(`Error reading file ${fullPath}: ${message}`);
456
476
  }
457
477
  }
@@ -463,7 +483,7 @@ function constSysfsExpr(options = {}) {
463
483
  count++;
464
484
  }
465
485
  catch (error) {
466
- const message = String(error instanceof Error ? error.message : error ?? 'Unknown error during file processing');
486
+ const message = String(error instanceof Error ? error.message : (error ?? 'Unknown error during file processing'));
467
487
  this.error(`Could not process files for constSysfsExpr: ${message}`, node.loc?.start.index);
468
488
  return;
469
489
  }
@@ -472,10 +492,27 @@ function constSysfsExpr(options = {}) {
472
492
  });
473
493
  }
474
494
  catch (error) {
475
- const message = String(error instanceof Error ? error.message : error ?? 'Unknown parsing error');
495
+ const message = String(error instanceof Error ? error.message : (error ?? 'Unknown parsing error'));
476
496
  this.error(`Failed to parse ${id}: ${message}`);
477
497
  return null;
478
498
  }
499
+ if (constSysfsImport !== null && hasReplaced) {
500
+ const info = constSysfsImport;
501
+ if (info.isOnlySpecifier) {
502
+ let endPos = info.declEnd;
503
+ if (code[endPos] === '\n')
504
+ endPos++;
505
+ else if (code[endPos] === '\r' && code[endPos + 1] === '\n')
506
+ endPos += 2;
507
+ magicString.remove(info.declStart, endPos);
508
+ }
509
+ else if (info.nextSpecifierStart !== null) {
510
+ magicString.remove(info.specifierStart, info.nextSpecifierStart);
511
+ }
512
+ else if (info.prevSpecifierEnd !== null) {
513
+ magicString.remove(info.prevSpecifierEnd, info.specifierEnd);
514
+ }
515
+ }
479
516
  // If no replacements were made, return null
480
517
  if (!hasReplaced) {
481
518
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steambrew/ttc",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -33,6 +33,16 @@ interface FileInfo {
33
33
  fileName: string;
34
34
  }
35
35
 
36
+ interface ImportSpecifierInfo {
37
+ specifierStart: number;
38
+ specifierEnd: number;
39
+ declStart: number;
40
+ declEnd: number;
41
+ isOnlySpecifier: boolean;
42
+ prevSpecifierEnd: number | null;
43
+ nextSpecifierStart: number | null;
44
+ }
45
+
36
46
  export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsPlugin {
37
47
  const filter = createFilter(options.include, options.exclude);
38
48
  const pluginName = 'millennium-const-sysfs-expr';
@@ -47,6 +57,7 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
47
57
 
48
58
  const magicString = new MagicString(code);
49
59
  let hasReplaced = false;
60
+ let constSysfsImport: ImportSpecifierInfo | null = null;
50
61
 
51
62
  try {
52
63
  const stringVariables = new Map<string, string>();
@@ -64,6 +75,28 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
64
75
  stringVariables.set(id.name, init.value);
65
76
  }
66
77
  },
78
+ ImportDeclaration(nodePath) {
79
+ const decl = nodePath.node;
80
+ const specifiers = decl.specifiers;
81
+ const idx = specifiers.findIndex(
82
+ (s) => s.type === 'ImportSpecifier' && ((s as any).imported?.name === 'constSysfsExpr' || (s as any).local?.name === 'constSysfsExpr'),
83
+ );
84
+ if (idx !== -1 && typeof decl.start === 'number' && typeof decl.end === 'number') {
85
+ const spec = specifiers[idx];
86
+ if (typeof spec.start === 'number' && typeof spec.end === 'number') {
87
+ constSysfsImport = {
88
+ specifierStart: spec.start,
89
+ specifierEnd: spec.end,
90
+ declStart: decl.start,
91
+ declEnd: decl.end,
92
+ isOnlySpecifier: specifiers.length === 1,
93
+ prevSpecifierEnd: idx > 0 && typeof specifiers[idx - 1].end === 'number' ? (specifiers[idx - 1].end as number) : null,
94
+ nextSpecifierStart:
95
+ idx < specifiers.length - 1 && typeof specifiers[idx + 1].start === 'number' ? (specifiers[idx + 1].start as number) : null,
96
+ };
97
+ }
98
+ }
99
+ },
67
100
  });
68
101
 
69
102
  traverse(ast, {
@@ -177,14 +210,13 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
177
210
  }
178
211
 
179
212
  try {
180
-
181
213
  const searchBasePath = callOptions.basePath
182
214
  ? path.isAbsolute(callOptions.basePath)
183
215
  ? callOptions.basePath
184
216
  : path.resolve(path.dirname(id), callOptions.basePath)
185
217
  : path.isAbsolute(pathOrPattern) && !/[?*+!@()[\]{}]/.test(pathOrPattern)
186
- ? path.dirname(pathOrPattern)
187
- : path.resolve(path.dirname(id), path.dirname(pathOrPattern));
218
+ ? path.dirname(pathOrPattern)
219
+ : path.resolve(path.dirname(id), path.dirname(pathOrPattern));
188
220
 
189
221
  let embeddedContent: string;
190
222
 
@@ -206,15 +238,13 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
206
238
  fileName: path.relative(searchBasePath, singleFilePath),
207
239
  };
208
240
  embeddedContent = JSON.stringify(fileInfo);
209
- this.addWatchFile(singleFilePath);
210
- } catch (fileError: unknown) {
211
- let message = String(fileError instanceof Error ? fileError.message : fileError ?? 'Unknown file read error');
241
+ this.addWatchFile(singleFilePath);
242
+ } catch (fileError: unknown) {
243
+ let message = String(fileError instanceof Error ? fileError.message : (fileError ?? 'Unknown file read error'));
212
244
  this.error(`Error reading file ${singleFilePath}: ${message}`, node.loc?.start.index);
213
245
  return;
214
246
  }
215
247
  } else {
216
-
217
-
218
248
  const matchingFiles = glob.sync(pathOrPattern, {
219
249
  cwd: searchBasePath,
220
250
  nodir: true,
@@ -233,7 +263,7 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
233
263
  });
234
264
  this.addWatchFile(fullPath);
235
265
  } catch (fileError: unknown) {
236
- let message = String(fileError instanceof Error ? fileError.message : fileError ?? 'Unknown file read error');
266
+ let message = String(fileError instanceof Error ? fileError.message : (fileError ?? 'Unknown file read error'));
237
267
  this.warn(`Error reading file ${fullPath}: ${message}`);
238
268
  }
239
269
  }
@@ -245,7 +275,7 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
245
275
  hasReplaced = true;
246
276
  count++;
247
277
  } catch (error: unknown) {
248
- const message = String(error instanceof Error ? error.message : error ?? 'Unknown error during file processing');
278
+ const message = String(error instanceof Error ? error.message : (error ?? 'Unknown error during file processing'));
249
279
  this.error(`Could not process files for constSysfsExpr: ${message}`, node.loc?.start.index);
250
280
  return;
251
281
  }
@@ -253,11 +283,25 @@ export default function constSysfsExpr(options: EmbedPluginOptions = {}): SysfsP
253
283
  },
254
284
  });
255
285
  } catch (error: unknown) {
256
- const message = String(error instanceof Error ? error.message : error ?? 'Unknown parsing error');
286
+ const message = String(error instanceof Error ? error.message : (error ?? 'Unknown parsing error'));
257
287
  this.error(`Failed to parse ${id}: ${message}`);
258
288
  return null;
259
289
  }
260
290
 
291
+ if (constSysfsImport !== null && hasReplaced) {
292
+ const info = constSysfsImport as ImportSpecifierInfo;
293
+ if (info.isOnlySpecifier) {
294
+ let endPos = info.declEnd;
295
+ if (code[endPos] === '\n') endPos++;
296
+ else if (code[endPos] === '\r' && code[endPos + 1] === '\n') endPos += 2;
297
+ magicString.remove(info.declStart, endPos);
298
+ } else if (info.nextSpecifierStart !== null) {
299
+ magicString.remove(info.specifierStart, info.nextSpecifierStart);
300
+ } else if (info.prevSpecifierEnd !== null) {
301
+ magicString.remove(info.prevSpecifierEnd, info.specifierEnd);
302
+ }
303
+ }
304
+
261
305
  // If no replacements were made, return null
262
306
  if (!hasReplaced) {
263
307
  return null;