@danielx/civet 0.9.0 → 0.9.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.
@@ -21,7 +21,7 @@ DiagnosticCategory[DiagnosticCategory["Error"] = 1] = "Error";
21
21
  DiagnosticCategory[DiagnosticCategory["Suggestion"] = 2] = "Suggestion";
22
22
  DiagnosticCategory[DiagnosticCategory["Message"] = 3] = "Message";
23
23
  var civetExtension = /\.civet$/;
24
- var isCivetTranspiled = /(\.civet)(\.[jt]sx)([?#].*)?$/;
24
+ var isCivetTranspiled = /(\.civet)(\.[jt]sx)?([?#].*)?$/;
25
25
  var postfixRE = /[?#].*$/s;
26
26
  var isWindows = os.platform() === "win32";
27
27
  var windowsSlashRE = /\\/g;
@@ -70,12 +70,10 @@ function implicitCivet(file) {
70
70
  return;
71
71
  }
72
72
  var rawPlugin = (options = {}, meta) => {
73
- if (options.dts)
74
- options.emitDeclaration = options.dts;
73
+ if (options.dts) options.emitDeclaration = options.dts;
75
74
  let compileOptions = {};
76
75
  let ts = options.ts;
77
- if (options.js)
78
- ts = "civet";
76
+ if (options.js) ts = "civet";
79
77
  if (!(ts != null)) {
80
78
  console.log('WARNING: You are using the default mode for `options.ts` which is `"civet"`. This mode does not support all TS features. If this is intentional, you should explicitly set `options.ts` to `"civet"`, or choose a different mode.');
81
79
  ts = "civet";
@@ -110,7 +108,7 @@ var rawPlugin = (options = {}, meta) => {
110
108
  getCanonicalFileName: sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
111
109
  };
112
110
  };
113
- const cache = options.cache ? /* @__PURE__ */ new Map() : void 0;
111
+ const cache = !(options.cache === false) ? /* @__PURE__ */ new Map() : void 0;
114
112
  const plugin = {
115
113
  name: "unplugin-civet",
116
114
  enforce: "pre",
@@ -123,6 +121,9 @@ var rawPlugin = (options = {}, meta) => {
123
121
  ...compileOptions.parseOptions,
124
122
  ...options.parseOptions
125
123
  };
124
+ if (options.threads != null) {
125
+ compileOptions.threads = options.threads;
126
+ }
126
127
  if (transformTS || ts === "tsc") {
127
128
  let mogrify = function(key) {
128
129
  if (key in config && Array.isArray(config[key])) {
@@ -186,10 +187,8 @@ var rawPlugin = (options = {}, meta) => {
186
187
  readDirectory: systemReadDirectory
187
188
  } = system;
188
189
  system.fileExists = (filename) => {
189
- if (!filename.endsWith(".civet.tsx"))
190
- return systemFileExists(filename);
191
- if (fsMap.has(filename))
192
- return true;
190
+ if (!filename.endsWith(".civet.tsx")) return systemFileExists(filename);
191
+ if (fsMap.has(filename)) return true;
193
192
  return systemFileExists(filename.slice(0, -4));
194
193
  };
195
194
  system.readDirectory = (path2) => {
@@ -226,10 +225,8 @@ var rawPlugin = (options = {}, meta) => {
226
225
  recurse(parsed.imports);
227
226
  return modified ? JSON.stringify(parsed) : json;
228
227
  }
229
- if (!filename.endsWith(".civet.tsx"))
230
- return systemReadFile(filename, encoding);
231
- if (fsMap.has(filename))
232
- return fsMap.get(filename);
228
+ if (!filename.endsWith(".civet.tsx")) return systemReadFile(filename, encoding);
229
+ if (fsMap.has(filename)) return fsMap.get(filename);
233
230
  const civetFilename = filename.slice(0, -4);
234
231
  const rawCivetSource = fs.readFileSync(civetFilename, {
235
232
  encoding
@@ -258,11 +255,9 @@ var rawPlugin = (options = {}, meta) => {
258
255
  });
259
256
  const diagnostics = ts2.getPreEmitDiagnostics(program).map((diagnostic) => {
260
257
  const file = diagnostic.file;
261
- if (!file)
262
- return diagnostic;
258
+ if (!file) return diagnostic;
263
259
  const sourceMap = sourceMaps.get(file.fileName);
264
- if (!sourceMap)
265
- return diagnostic;
260
+ if (!sourceMap) return diagnostic;
266
261
  const sourcemapLines = sourceMap.data.lines;
267
262
  const range = remapRange(
268
263
  {
@@ -291,14 +286,10 @@ var rawPlugin = (options = {}, meta) => {
291
286
  if (options.typecheck) {
292
287
  let failures = [];
293
288
  if (typeof options.typecheck === "string") {
294
- if (options.typecheck.includes("error"))
295
- failures.push(DiagnosticCategory.Error);
296
- if (options.typecheck.includes("warning"))
297
- failures.push(DiagnosticCategory.Warning);
298
- if (options.typecheck.includes("suggestion"))
299
- failures.push(DiagnosticCategory.Suggestion);
300
- if (options.typecheck.includes("message"))
301
- failures.push(DiagnosticCategory.Message);
289
+ if (options.typecheck.includes("error")) failures.push(DiagnosticCategory.Error);
290
+ if (options.typecheck.includes("warning")) failures.push(DiagnosticCategory.Warning);
291
+ if (options.typecheck.includes("suggestion")) failures.push(DiagnosticCategory.Suggestion);
292
+ if (options.typecheck.includes("message")) failures.push(DiagnosticCategory.Message);
302
293
  if (options.typecheck.includes("all")) {
303
294
  failures = { includes: () => true };
304
295
  }
@@ -351,7 +342,9 @@ var rawPlugin = (options = {}, meta) => {
351
342
  path.dirname(
352
343
  path.resolve(esbuildOptions.outdir, pathFromDistDir)
353
344
  ),
354
- { recursive: true }
345
+ {
346
+ recursive: true
347
+ }
355
348
  );
356
349
  }
357
350
  return this.emitFile({
@@ -376,8 +369,7 @@ var rawPlugin = (options = {}, meta) => {
376
369
  if (aliasResolver != null) {
377
370
  id = aliasResolver(id);
378
371
  }
379
- if (/\0/.test(id))
380
- return null;
372
+ if (/\0/.test(id)) return null;
381
373
  if (plugin.__virtualModulePrefix && importer?.startsWith(plugin.__virtualModulePrefix)) {
382
374
  importer = decodeURIComponent(importer.slice(plugin.__virtualModulePrefix.length));
383
375
  }
@@ -391,14 +383,11 @@ var rawPlugin = (options = {}, meta) => {
391
383
  }
392
384
  ;
393
385
  let resolvedId = ref2;
394
- if (!resolvedId)
395
- return null;
386
+ if (!resolvedId) return null;
396
387
  if (!resolvedId.endsWith(".civet")) {
397
- if (!implicitExtension)
398
- return null;
388
+ if (!implicitExtension) return null;
399
389
  const implicitId = implicitCivet(resolvedId);
400
- if (!implicitId)
401
- return null;
390
+ if (!implicitId) return null;
402
391
  resolvedId = implicitId;
403
392
  }
404
393
  if (options2.scan && meta.framework === "vite") {
@@ -416,114 +405,130 @@ var rawPlugin = (options = {}, meta) => {
416
405
  }
417
406
  const basename = id.slice(0, match.index + match[1].length);
418
407
  const filename = path.resolve(rootDir, basename);
419
- let mtime;
420
- if (cache) {
408
+ let mtime, cached, resolve;
409
+ if (cache != null) {
421
410
  mtime = (await fs.promises.stat(filename)).mtimeMs;
422
- const cached = cache?.get(filename);
411
+ cached = cache?.get(filename);
423
412
  if (cached && cached.mtime === mtime) {
424
- return cached.result;
425
- }
426
- }
427
- const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
428
- this.addWatchFile(filename);
429
- let compiled;
430
- let sourceMap;
431
- const civetOptions = {
432
- ...compileOptions,
433
- filename: id,
434
- errors: []
435
- };
436
- function checkErrors() {
437
- if (civetOptions.errors.length) {
438
- throw new civet.ParseErrors(civetOptions.errors);
413
+ if (cached.promise) {
414
+ await cached.promise;
415
+ }
416
+ if (cached.result) {
417
+ return cached.result;
418
+ }
439
419
  }
440
- ;
441
- return;
442
- }
443
- const ast = await civet.compile(rawCivetSource, {
444
- ...civetOptions,
445
- ast: true
446
- });
447
- const civetSourceMap = SourceMap(rawCivetSource);
448
- if (ts === "civet") {
449
- compiled = await civet.generate(ast, {
450
- ...civetOptions,
451
- js: true,
452
- sourceMap: civetSourceMap
420
+ const promise = new Promise((r) => {
421
+ resolve = r;
453
422
  });
454
- sourceMap = civetSourceMap;
455
- checkErrors();
456
- } else {
457
- const compiledTS = await civet.generate(ast, {
423
+ cache.set(filename, cached = { mtime, promise });
424
+ }
425
+ try {
426
+ let checkErrors = function() {
427
+ if (civetOptions.errors.length) {
428
+ throw new civet.ParseErrors(civetOptions.errors);
429
+ }
430
+ ;
431
+ return;
432
+ };
433
+ this.addWatchFile(filename);
434
+ let compiled;
435
+ let sourceMap;
436
+ const civetOptions = {
437
+ ...compileOptions,
438
+ filename: id,
439
+ errors: []
440
+ };
441
+ const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
442
+ const ast = await civet.compile(rawCivetSource, {
458
443
  ...civetOptions,
459
- js: false,
460
- sourceMap: civetSourceMap
444
+ ast: true
461
445
  });
462
- checkErrors();
463
- switch (ts) {
464
- case "esbuild": {
465
- const esbuildTransform = (await import("esbuild")).transform;
466
- const result = await esbuildTransform(compiledTS, {
467
- jsx: "preserve",
468
- loader: "tsx",
469
- sourcefile: id,
470
- sourcemap: "external"
471
- });
472
- compiled = result.code;
473
- sourceMap = result.map;
474
- break;
446
+ const civetSourceMap = SourceMap(rawCivetSource);
447
+ if (ts === "civet") {
448
+ compiled = await civet.generate(ast, {
449
+ ...civetOptions,
450
+ js: true,
451
+ sourceMap: civetSourceMap
452
+ });
453
+ sourceMap = civetSourceMap;
454
+ checkErrors();
455
+ } else {
456
+ const compiledTS = await civet.generate(ast, {
457
+ ...civetOptions,
458
+ js: false,
459
+ sourceMap: civetSourceMap
460
+ });
461
+ checkErrors();
462
+ switch (ts) {
463
+ case "esbuild": {
464
+ const esbuildTransform = (await import("esbuild")).transform;
465
+ const result = await esbuildTransform(compiledTS, {
466
+ jsx: "preserve",
467
+ loader: "tsx",
468
+ sourcefile: id,
469
+ sourcemap: "external"
470
+ });
471
+ compiled = result.code;
472
+ sourceMap = result.map;
473
+ break;
474
+ }
475
+ case "tsc": {
476
+ const tsTranspile = (await tsPromise).transpileModule;
477
+ const result = tsTranspile(compiledTS, {
478
+ compilerOptions: compilerOptionsWithSourceMap
479
+ });
480
+ compiled = result.outputText;
481
+ sourceMap = result.sourceMapText;
482
+ break;
483
+ }
484
+ case "preserve": {
485
+ compiled = compiledTS;
486
+ break;
487
+ }
475
488
  }
476
- case "tsc": {
477
- const tsTranspile = (await tsPromise).transpileModule;
478
- const result = tsTranspile(compiledTS, {
479
- compilerOptions: compilerOptionsWithSourceMap
480
- });
481
- compiled = result.outputText;
482
- sourceMap = result.sourceMapText;
483
- break;
489
+ }
490
+ if (transformTS) {
491
+ for (let ref3 = lib.gatherRecursive(ast, ($) => $.type === "ModuleSpecifier"), i = 0, len = ref3.length; i < len; i++) {
492
+ const _spec = ref3[i];
493
+ const spec = _spec;
494
+ if (spec.module?.input) {
495
+ spec.module.token = spec.module.input.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
496
+ }
484
497
  }
485
- case "preserve": {
486
- compiled = compiledTS;
487
- break;
498
+ const compiledTS = await civet.generate(ast, {
499
+ ...civetOptions,
500
+ js: false,
501
+ sourceMap: civetSourceMap
502
+ });
503
+ checkErrors();
504
+ const tsx = filename + ".tsx";
505
+ fsMap.set(tsx, compiledTS);
506
+ sourceMaps.set(tsx, civetSourceMap);
507
+ const slashed = slash(tsx);
508
+ if (!(tsx === slashed)) {
509
+ fsMap.set(slashed, compiledTS);
510
+ sourceMaps.set(slashed, civetSourceMap);
488
511
  }
489
512
  }
490
- }
491
- if (transformTS) {
492
- for (let ref3 = lib.gatherRecursive(ast, ($) => $.type === "ModuleSpecifier"), i = 0, len = ref3.length; i < len; i++) {
493
- const _spec = ref3[i];
494
- const spec = _spec;
495
- if (spec.module?.input) {
496
- spec.module.token = spec.module.input.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
497
- }
513
+ const jsonSourceMap = sourceMap && (typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap.json(
514
+ path.basename(id.replace(/\.[jt]sx$/, "")),
515
+ path.basename(id)
516
+ ));
517
+ let transformed = {
518
+ code: compiled,
519
+ map: jsonSourceMap
520
+ };
521
+ if (options.transformOutput) {
522
+ transformed = await options.transformOutput(transformed.code, id);
498
523
  }
499
- const compiledTS = await civet.generate(ast, {
500
- ...civetOptions,
501
- js: false,
502
- sourceMap: civetSourceMap
503
- });
504
- checkErrors();
505
- const tsx = filename + ".tsx";
506
- fsMap.set(tsx, compiledTS);
507
- sourceMaps.set(tsx, civetSourceMap);
508
- const slashed = slash(tsx);
509
- if (!(tsx === slashed)) {
510
- fsMap.set(slashed, compiledTS);
511
- sourceMaps.set(slashed, civetSourceMap);
524
+ if (cached != null) {
525
+ cached.result = transformed;
526
+ delete cached.promise;
512
527
  }
528
+ return transformed;
529
+ } finally {
530
+ resolve?.();
513
531
  }
514
- const jsonSourceMap = sourceMap && (typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap.json(
515
- path.basename(id.replace(/\.[jt]sx$/, "")),
516
- path.basename(id)
517
- ));
518
- let transformed = {
519
- code: compiled,
520
- map: jsonSourceMap
521
- };
522
- if (options.transformOutput) {
523
- transformed = await options.transformOutput(transformed.code, id);
524
- }
525
- cache?.set(filename, { mtime, result: transformed });
526
- return transformed;
527
532
  },
528
533
  esbuild: {
529
534
  config(options2) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.9.0",
4
+ "version": "0.9.2",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",
@@ -92,22 +92,22 @@
92
92
  "dependencies": {
93
93
  "@cspotcode/source-map-support": "^0.8.1",
94
94
  "@typescript/vfs": "^1.6.0",
95
- "unplugin": "^1.12.2"
95
+ "unplugin": "^2.1.0"
96
96
  },
97
97
  "devDependencies": {
98
- "@danielx/civet": "0.8.11",
98
+ "@danielx/civet": "0.9.0",
99
99
  "@danielx/hera": "^0.8.16",
100
100
  "@prettier/sync": "^0.5.2",
101
101
  "@types/assert": "^1.5.6",
102
102
  "@types/mocha": "^10.0.8",
103
- "@types/node": "^20.12.2",
103
+ "@types/node": "^22.10.2",
104
104
  "c8": "^7.12.0",
105
- "esbuild": "0.20.0",
105
+ "esbuild": "0.24.0",
106
106
  "marked": "^4.2.4",
107
107
  "mocha": "^10.7.3",
108
108
  "prettier": "^3.2.5",
109
109
  "terser": "^5.16.1",
110
- "ts-node": "^10.9.1",
110
+ "ts-node": "^10.9.2",
111
111
  "tslib": "^2.4.0",
112
112
  "typescript": "^5.5.2",
113
113
  "vite": "^4.4.12",