@danielx/civet 0.6.37 → 0.6.38

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/README.md CHANGED
@@ -26,7 +26,7 @@ The modern way to write TypeScript.
26
26
  [Gulp](integration/gulp),
27
27
  [ESM module resolution](source/esm.civet),
28
28
  [CJS](register.js),
29
- [Bun](source/bun-civet.coffee)
29
+ [Bun](source/bun-civet.civet)
30
30
  - Starter templates for [Solid](https://github.com/orenelbaum/solid-civet-template) and [Solid Start](https://github.com/orenelbaum/solid-start-civet-template)
31
31
 
32
32
  Quickstart Guide
package/dist/browser.js CHANGED
@@ -893,6 +893,28 @@ ${input.slice(result.pos)}
893
893
  }
894
894
  return expandedOps;
895
895
  }
896
+ function handleThisPrivateShorthands(value) {
897
+ if (value.privateShorthand) {
898
+ value = value.children[1].children[1];
899
+ return [value, false];
900
+ }
901
+ if (value.type === "MemberExpression" || value.type === "CallExpression") {
902
+ let suppressPrefix = value.thisShorthand;
903
+ value = {
904
+ ...value,
905
+ children: value.children.map((c, i) => {
906
+ if (i === 0) {
907
+ let s;
908
+ [c, s] = handleThisPrivateShorthands(c);
909
+ suppressPrefix || (suppressPrefix = s);
910
+ }
911
+ return c;
912
+ })
913
+ };
914
+ return [value, suppressPrefix];
915
+ }
916
+ return [value, value.thisShorthand];
917
+ }
896
918
  function processCallMemberExpression(node) {
897
919
  const { children } = node;
898
920
  for (let i = 0; i < children.length; i++) {
@@ -920,11 +942,15 @@ ${input.slice(result.pos)}
920
942
  throw new Error("Glob pattern cannot have method definition");
921
943
  }
922
944
  if (part.value && !["CallExpression", "MemberExpression", "Identifier"].includes(part.value.type)) {
923
- throw new Error("Glob pattern must have call or member expression value");
945
+ throw new Error(`Glob pattern must have call or member expression value, found ${JSON.stringify(part.value)}`);
924
946
  }
947
+ let suppressPrefix = false;
925
948
  let value = part.value ?? part.name;
926
949
  const wValue = getTrimmingSpace(part.value);
927
- value = prefix.concat(insertTrimmingSpace(value, ""));
950
+ [value, suppressPrefix] = handleThisPrivateShorthands(value);
951
+ if (!suppressPrefix) {
952
+ value = prefix.concat(insertTrimmingSpace(value, ""));
953
+ }
928
954
  if (wValue)
929
955
  value.unshift(wValue);
930
956
  if (part.type === "SpreadProperty") {
@@ -1138,14 +1164,14 @@ ${input.slice(result.pos)}
1138
1164
  chains.push(i);
1139
1165
  } else if (lowerPrecedenceOps.includes(op.token)) {
1140
1166
  processChains(op);
1141
- first = [];
1167
+ first = void 0;
1142
1168
  }
1143
1169
  i++;
1144
1170
  }
1145
1171
  processChains(op);
1146
1172
  return results;
1147
1173
  function processChains(op2) {
1148
- if (isRelationalOp(op2)) {
1174
+ if (first && isRelationalOp(op2)) {
1149
1175
  first = expandExistence(first);
1150
1176
  }
1151
1177
  if (chains.length > 1) {
@@ -1154,7 +1180,7 @@ ${input.slice(result.pos)}
1154
1180
  results.push(" ", "&&", " ");
1155
1181
  }
1156
1182
  const binop = binops[index];
1157
- let [pre, op3, post, exp] = binop;
1183
+ let [, , , exp] = binop;
1158
1184
  exp = binop[3] = expandExistence(exp);
1159
1185
  let endIndex;
1160
1186
  if (k < chains.length - 1) {
@@ -1167,10 +1193,13 @@ ${input.slice(result.pos)}
1167
1193
  return start = endIndex;
1168
1194
  });
1169
1195
  } else {
1170
- results.push(first, ...binops.slice(start, i + 1).flat());
1196
+ if (first) {
1197
+ results.push(first);
1198
+ }
1199
+ results.push(...binops.slice(start, i + 1).flat());
1171
1200
  start = i + 1;
1172
1201
  }
1173
- return chains.length = 0;
1202
+ chains.length = 0;
1174
1203
  }
1175
1204
  function expandExistence(exp) {
1176
1205
  const existence = isExistence(exp);
@@ -1252,6 +1281,23 @@ ${input.slice(result.pos)}
1252
1281
  }
1253
1282
  }
1254
1283
  }
1284
+ function replaceBlockExpression(node, child, replacement) {
1285
+ let found = false;
1286
+ const { expressions } = node;
1287
+ for (let i = 0, l = expressions.length; i < l; i++) {
1288
+ const statement = expressions[i];
1289
+ const [, s] = statement;
1290
+ if (s === child) {
1291
+ statement[1] = replacement;
1292
+ replacement.parent = node;
1293
+ found = true;
1294
+ break;
1295
+ }
1296
+ }
1297
+ if (!found) {
1298
+ throw new Error("Could not find child to replace");
1299
+ }
1300
+ }
1255
1301
  function findChildIndex(parent, child) {
1256
1302
  const children = Array.isArray(parent) ? parent : parent.children;
1257
1303
  const len = children.length;
@@ -2138,12 +2184,12 @@ ${input.slice(result.pos)}
2138
2184
  }
2139
2185
  function processDeclarationConditions(node) {
2140
2186
  gatherRecursiveAll(node, (n) => {
2141
- return n.type === "IfStatement" || n.type === "IterationStatement";
2187
+ return n.type === "IfStatement" || n.type === "IterationStatement" || n.type === "SwitchStatement";
2142
2188
  }).forEach(processDeclarationConditionStatement);
2143
2189
  }
2144
2190
  function processDeclarationConditionStatement(s) {
2145
2191
  const { condition } = s;
2146
- if (!condition) {
2192
+ if (!condition?.expression) {
2147
2193
  return;
2148
2194
  }
2149
2195
  processDeclarationCondition(condition.expression);
@@ -2184,6 +2230,27 @@ ${input.slice(result.pos)}
2184
2230
  updateParentPointers(newBlock, s);
2185
2231
  break;
2186
2232
  }
2233
+ case "SwitchStatement": {
2234
+ const { blockPrefix, ref: ref2 } = condition.expression;
2235
+ if (!blockPrefix) {
2236
+ return;
2237
+ }
2238
+ s.condition = {
2239
+ type: "ParenthesizedExpression",
2240
+ children: ["(", ref2, ")"],
2241
+ expression: ref2,
2242
+ parent: s
2243
+ };
2244
+ s.children[1] = s.condition;
2245
+ const block = blockWithPrefix([["", [{
2246
+ type: "Declaration",
2247
+ children: ["let ", ...condition.expression.children]
2248
+ }], ";"], ...blockPrefix], makeEmptyBlock());
2249
+ replaceBlockExpression(s.parent, s, block);
2250
+ block.expressions.push(["", s]);
2251
+ s.parent = block;
2252
+ break;
2253
+ }
2187
2254
  }
2188
2255
  }
2189
2256
  function implicitFunctionBlock(f) {
@@ -2632,12 +2699,12 @@ ${input.slice(result.pos)}
2632
2699
  }
2633
2700
  if (errors || !isPattern)
2634
2701
  return;
2635
- let { expression } = s;
2636
- if (expression.type === "ParenthesizedExpression") {
2637
- expression = expression.expression;
2702
+ let { condition } = s;
2703
+ if (condition.type === "ParenthesizedExpression") {
2704
+ condition = condition.expression;
2638
2705
  }
2639
- let hoistDec, refAssignment = [], ref = maybeRef(expression, "m");
2640
- if (ref !== expression) {
2706
+ let hoistDec, refAssignment = [], ref = maybeRef(condition, "m");
2707
+ if (ref !== condition) {
2641
2708
  hoistDec = {
2642
2709
  type: "Declaration",
2643
2710
  children: ["let ", ref],
@@ -2645,7 +2712,7 @@ ${input.slice(result.pos)}
2645
2712
  };
2646
2713
  refAssignment = [{
2647
2714
  type: "AssignmentExpression",
2648
- children: [ref, " = ", expression]
2715
+ children: [ref, " = ", condition]
2649
2716
  }, ","];
2650
2717
  }
2651
2718
  let prev = [], root = prev;
@@ -2673,7 +2740,7 @@ ${input.slice(result.pos)}
2673
2740
  return conditionArray;
2674
2741
  return [" || ", ...conditionArray];
2675
2742
  });
2676
- const condition = {
2743
+ const condition2 = {
2677
2744
  type: "ParenthesizedExpression",
2678
2745
  children: ["(", ...refAssignment, conditionExpression, ")"],
2679
2746
  expression: conditionExpression
@@ -2708,7 +2775,7 @@ ${input.slice(result.pos)}
2708
2775
  next.push("\n", "else ");
2709
2776
  prev.push(["", {
2710
2777
  type: "IfStatement",
2711
- children: ["if", condition, block, next],
2778
+ children: ["if", condition2, block, next],
2712
2779
  then: block,
2713
2780
  else: next,
2714
2781
  hoistDec
@@ -5361,7 +5428,8 @@ ${input.slice(result.pos)}
5361
5428
  type: "PropertyAccess",
5362
5429
  name: id,
5363
5430
  children: [".", id]
5364
- }]
5431
+ }],
5432
+ thisShorthand: true
5365
5433
  };
5366
5434
  });
5367
5435
  var ThisLiteral$2 = AtThis;
@@ -5382,7 +5450,8 @@ ${input.slice(result.pos)}
5382
5450
  type: "PropertyAccess",
5383
5451
  name: id.name,
5384
5452
  children: [".", id]
5385
- }]
5453
+ }],
5454
+ privateShorthand: true
5386
5455
  };
5387
5456
  });
5388
5457
  var PrivateThis$$ = [PrivateThis$0, PrivateThis$1];
@@ -7234,20 +7303,7 @@ ${input.slice(result.pos)}
7234
7303
  function ObjectPropertyDelimiter(ctx, state) {
7235
7304
  return $EVENT_C(ctx, state, "ObjectPropertyDelimiter", ObjectPropertyDelimiter$$);
7236
7305
  }
7237
- var PropertyDefinition$0 = $TS($S($E(_), AtThis, IdentifierReference), function($skip, $loc, $0, $1, $2, $3) {
7238
- var ws = $1;
7239
- var at = $2;
7240
- var id = $3;
7241
- const value = [at, ".", id];
7242
- return {
7243
- type: "Property",
7244
- children: [ws, id, ": ", ...value],
7245
- name: id,
7246
- names: id.names,
7247
- value
7248
- };
7249
- });
7250
- var PropertyDefinition$1 = $TS($S($E(_), NamedProperty), function($skip, $loc, $0, $1, $2) {
7306
+ var PropertyDefinition$0 = $TS($S($E(_), NamedProperty), function($skip, $loc, $0, $1, $2) {
7251
7307
  var ws = $1;
7252
7308
  var prop = $2;
7253
7309
  return {
@@ -7255,7 +7311,7 @@ ${input.slice(result.pos)}
7255
7311
  children: [ws, ...prop.children]
7256
7312
  };
7257
7313
  });
7258
- var PropertyDefinition$2 = $TS($S($E(_), $TEXT($EXPECT($R6, "PropertyDefinition /[!+-]/")), PropertyName), function($skip, $loc, $0, $1, $2, $3) {
7314
+ var PropertyDefinition$1 = $TS($S($E(_), $TEXT($EXPECT($R6, "PropertyDefinition /[!+-]/")), PropertyName), function($skip, $loc, $0, $1, $2, $3) {
7259
7315
  var ws = $1;
7260
7316
  var toggle = $2;
7261
7317
  var id = $3;
@@ -7268,7 +7324,7 @@ ${input.slice(result.pos)}
7268
7324
  value
7269
7325
  };
7270
7326
  });
7271
- var PropertyDefinition$3 = $TS($S($E(_), MethodDefinition), function($skip, $loc, $0, $1, $2) {
7327
+ var PropertyDefinition$2 = $TS($S($E(_), MethodDefinition), function($skip, $loc, $0, $1, $2) {
7272
7328
  var ws = $1;
7273
7329
  var def = $2;
7274
7330
  if (!def.block || def.block.empty)
@@ -7278,7 +7334,7 @@ ${input.slice(result.pos)}
7278
7334
  children: [ws, ...def.children]
7279
7335
  };
7280
7336
  });
7281
- var PropertyDefinition$4 = $TS($S($E(_), DotDotDot, ExtendedExpression), function($skip, $loc, $0, $1, $2, $3) {
7337
+ var PropertyDefinition$3 = $TS($S($E(_), DotDotDot, ExtendedExpression), function($skip, $loc, $0, $1, $2, $3) {
7282
7338
  var ws = $1;
7283
7339
  var dots = $2;
7284
7340
  var exp = $3;
@@ -7290,7 +7346,7 @@ ${input.slice(result.pos)}
7290
7346
  value: exp
7291
7347
  };
7292
7348
  });
7293
- var PropertyDefinition$5 = $TS($S($E(_), $N(EOS), CallExpression), function($skip, $loc, $0, $1, $2, $3) {
7349
+ var PropertyDefinition$4 = $TS($S($E(_), $N(EOS), CallExpression), function($skip, $loc, $0, $1, $2, $3) {
7294
7350
  var ws = $1;
7295
7351
  var value = $3;
7296
7352
  switch (value.type) {
@@ -7349,16 +7405,18 @@ ${input.slice(result.pos)}
7349
7405
  if (!name)
7350
7406
  return $skip;
7351
7407
  }
7408
+ if (name[0] === "#")
7409
+ name = name.slice(1);
7352
7410
  return {
7353
7411
  type: "Property",
7354
7412
  children: [ws, name, ": ", value],
7355
7413
  name,
7356
- value,
7357
7414
  names: [],
7415
+ value,
7358
7416
  hoistDec
7359
7417
  };
7360
7418
  });
7361
- var PropertyDefinition$$ = [PropertyDefinition$0, PropertyDefinition$1, PropertyDefinition$2, PropertyDefinition$3, PropertyDefinition$4, PropertyDefinition$5];
7419
+ var PropertyDefinition$$ = [PropertyDefinition$0, PropertyDefinition$1, PropertyDefinition$2, PropertyDefinition$3, PropertyDefinition$4];
7362
7420
  function PropertyDefinition(ctx, state) {
7363
7421
  return $EVENT_C(ctx, state, "PropertyDefinition", PropertyDefinition$$);
7364
7422
  }
@@ -8722,7 +8780,7 @@ ${input.slice(result.pos)}
8722
8780
  return {
8723
8781
  type: "SwitchStatement",
8724
8782
  children: $0,
8725
- expression: condition,
8783
+ condition,
8726
8784
  caseBlock
8727
8785
  };
8728
8786
  });
@@ -13418,6 +13476,7 @@ ${input.slice(result.pos)}
13418
13476
  generate: () => generate_default,
13419
13477
  isCompileError: () => isCompileError,
13420
13478
  parse: () => parse,
13479
+ prune: () => prune,
13421
13480
  util: () => util_exports
13422
13481
  });
13423
13482
  var import_parser = __toESM(require_parser());
@@ -13585,7 +13644,10 @@ ${input.slice(result.pos)}
13585
13644
  sources: [srcFileName],
13586
13645
  mappings: this.renderMappings(),
13587
13646
  names: [],
13588
- sourcesContent: [sourceString]
13647
+ sourcesContent: [sourceString],
13648
+ toString: function() {
13649
+ return JSON.stringify(this);
13650
+ }
13589
13651
  };
13590
13652
  },
13591
13653
  updateSourceMap: function(outputStr, inputPos) {
package/dist/civet CHANGED
@@ -1,20 +1,39 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
+ // If the importer is in node compatibility mode or this is not an ESM
19
+ // file that has been converted to a CommonJS file using a Babel-
20
+ // compatible transform (i.e. "__esModule" has not been set), then set
21
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
3
25
 
4
26
  // source/cli.civet
5
27
  var import_main = require("./main.js");
6
28
  var import_config = require("./config.js");
7
- var prune;
29
+ var import_promises = __toESM(require("fs/promises"));
30
+ var import_path = __toESM(require("path"));
8
31
  var version;
9
- var encoding;
10
- var fs;
11
- var path;
12
32
  var parseArgs;
13
33
  var readFiles;
14
34
  var repl;
15
35
  var cli;
16
36
  var indexOf = [].indexOf;
17
- ({ prune } = import_main.generate);
18
37
  version = function() {
19
38
  return require("../package.json").version;
20
39
  };
@@ -60,9 +79,7 @@ if (process.argv.includes("--help")) {
60
79
  `);
61
80
  process.exit(0);
62
81
  }
63
- encoding = "utf8";
64
- fs = require("fs/promises");
65
- path = require("path");
82
+ var encoding = "utf8";
66
83
  parseArgs = function(args) {
67
84
  var options, filenames, scriptArgs, i, endOfArgs, arg;
68
85
  options = {};
@@ -167,7 +184,7 @@ readFiles = async function* (filenames, options) {
167
184
  process.stdin.setEncoding(encoding);
168
185
  filename = "<stdin>";
169
186
  try {
170
- filename = await fs.realpath("/dev/stdin");
187
+ filename = await import_promises.default.realpath("/dev/stdin");
171
188
  } catch (e) {
172
189
  }
173
190
  if (process.stdin.isTTY) {
@@ -196,7 +213,7 @@ readFiles = async function* (filenames, options) {
196
213
  })()).join("");
197
214
  }
198
215
  } else {
199
- content = await fs.readFile(filename, { encoding });
216
+ content = await import_promises.default.readFile(filename, { encoding });
200
217
  }
201
218
  results1.push(yield { filename, content, stdin });
202
219
  } catch (error) {
@@ -284,7 +301,7 @@ repl = function(options) {
284
301
  });
285
302
  };
286
303
  cli = async function() {
287
- var argv, filenames, scriptArgs, options, filename, error, content, stdin, output, outputPath, outputFilename, optionsPath, stat, esm, child;
304
+ var argv, filenames, scriptArgs, options, filename, error, content, stdin, output, outputPath, outputFilename, optionsPath, stat, esm, execArgv, debugRe, isDebug, child;
288
305
  argv = process.argv;
289
306
  ({ filenames, scriptArgs, options } = parseArgs(argv.slice(2)));
290
307
  if (options.config !== false) {
@@ -330,7 +347,7 @@ cli = async function() {
330
347
  if (stdin && !options.output || options.output === "-") {
331
348
  results3.push(process.stdout.write(output));
332
349
  } else {
333
- outputPath = path.parse(filename);
350
+ outputPath = import_path.default.parse(filename);
334
351
  delete outputPath.base;
335
352
  if (options.js) {
336
353
  outputPath.ext += ".jsx";
@@ -338,13 +355,13 @@ cli = async function() {
338
355
  outputPath.ext += ".tsx";
339
356
  }
340
357
  if (options.output) {
341
- optionsPath = path.parse(options.output);
358
+ optionsPath = import_path.default.parse(options.output);
342
359
  try {
343
- stat = await fs.stat(options.output);
360
+ stat = await import_promises.default.stat(options.output);
344
361
  } catch {
345
362
  stat = null;
346
363
  }
347
- if (stat?.isDirectory() || options.output.endsWith(path.sep) || options.output.endsWith("/")) {
364
+ if (stat?.isDirectory() || options.output.endsWith(import_path.default.sep) || options.output.endsWith("/")) {
348
365
  outputPath.dir = options.output;
349
366
  } else if (/^(\.[^.]+)+$/.test(optionsPath.base)) {
350
367
  outputPath.ext = optionsPath.base;
@@ -356,11 +373,11 @@ cli = async function() {
356
373
  }
357
374
  }
358
375
  if (outputPath.dir) {
359
- fs.mkdir(outputPath.dir, { recursive: true });
376
+ import_promises.default.mkdir(outputPath.dir, { recursive: true });
360
377
  }
361
- outputFilename = path.format(outputPath);
378
+ outputFilename = import_path.default.format(outputPath);
362
379
  try {
363
- results3.push(await fs.writeFile(outputFilename, output));
380
+ results3.push(await import_promises.default.writeFile(outputFilename, output));
364
381
  } catch (error2) {
365
382
  console.error(`${outputFilename} failed to write:`);
366
383
  results3.push(console.error(error2));
@@ -372,32 +389,45 @@ cli = async function() {
372
389
  if (stdin) {
373
390
  filename = `.stdin-${process.pid}.civet`;
374
391
  try {
375
- await fs.writeFile(filename, content, { encoding });
392
+ await import_promises.default.writeFile(filename, content, { encoding });
376
393
  } catch (e) {
377
394
  console.error(`Could not write ${filename} for Civet ESM mode:`);
378
395
  console.error(e);
379
396
  process.exit(1);
380
397
  }
381
398
  }
382
- child = require("child_process").spawnSync(argv[0], [
383
- "--loader",
384
- "@danielx/civet/esm",
385
- filename,
386
- ...scriptArgs
387
- ], { stdio: "inherit" });
388
- if (stdin) {
389
- await fs.unlink(filename);
399
+ ;
400
+ const { fork } = await import("child_process");
401
+ execArgv = ["--loader", "@danielx/civet/esm"];
402
+ debugRe = /--debug|--inspect/;
403
+ isDebug = typeof v8debug === "object" || debugRe.test(process.execArgv.join(" ")) || debugRe.test(process.env.NODE_OPTIONS);
404
+ if (process.env.NODE_OPTIONS) {
405
+ execArgv.push(process.env.NODE_OPTIONS);
406
+ }
407
+ if (isDebug) {
408
+ execArgv.push("--inspect=" + (process.debugPort + 1));
390
409
  }
391
- results3.push(process.exit(child.status));
410
+ child = fork(filename, [
411
+ ...scriptArgs
412
+ ], {
413
+ execArgv,
414
+ stdio: "inherit"
415
+ });
416
+ results3.push(child.on("exit", async function(code) {
417
+ if (stdin) {
418
+ await import_promises.default.unlink(filename);
419
+ }
420
+ return process.exit(code ?? 1);
421
+ }));
392
422
  } else {
393
423
  require("../register.js");
394
424
  try {
395
- module.filename = await fs.realpath(filename);
425
+ module.filename = await import_promises.default.realpath(filename);
396
426
  } catch {
397
427
  module.filename = filename;
398
428
  }
399
429
  process.argv = ["civet", module.filename, ...scriptArgs];
400
- module.paths = require("module")._nodeModulePaths(path.dirname(module.filename));
430
+ module.paths = require("module")._nodeModulePaths(import_path.default.dirname(module.filename));
401
431
  try {
402
432
  results3.push(module._compile(output, module.filename));
403
433
  } catch (error2) {
package/dist/esbuild.js CHANGED
@@ -37,6 +37,7 @@ module.exports = __toCommonJS(esbuild_exports);
37
37
  // src/index.ts
38
38
  var import_unplugin = require("unplugin");
39
39
  var import_civet = __toESM(require("@danielx/civet"));
40
+ var import_ts_diagnostic = require("@danielx/civet/ts-diagnostic");
40
41
  var fs = __toESM(require("fs"));
41
42
  var import_path = __toESM(require("path"));
42
43
  var import_typescript = __toESM(require("typescript"));
@@ -52,15 +53,19 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
52
53
  if (options.dts && options.js) {
53
54
  throw new Error("Can't have both `dts` and `js` be set to `true`.");
54
55
  }
55
- const transpileToJS = options.js ?? !options.dts;
56
+ if (options.typecheck && options.js) {
57
+ throw new Error("Can't have both `typecheck` and `js` be set to `true`.");
58
+ }
59
+ const transpileToJS = options.js ?? !(options.dts || options.typecheck);
56
60
  const outExt = options.outputExtension ?? (transpileToJS ? ".jsx" : ".tsx");
57
61
  let fsMap = /* @__PURE__ */ new Map();
62
+ const sourceMaps = /* @__PURE__ */ new Map();
58
63
  let compilerOptions;
59
64
  return {
60
65
  name: "unplugin-civet",
61
66
  enforce: "pre",
62
67
  async buildStart() {
63
- if (options.dts) {
68
+ if (options.dts || options.typecheck) {
64
69
  const configPath = import_typescript.default.findConfigFile(process.cwd(), import_typescript.default.sys.fileExists);
65
70
  if (!configPath) {
66
71
  throw new Error("Could not find 'tsconfig.json'");
@@ -86,7 +91,7 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
86
91
  }
87
92
  },
88
93
  buildEnd() {
89
- if (options.dts) {
94
+ if (options.dts || options.typecheck) {
90
95
  const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), import_typescript.default);
91
96
  const host = tsvfs.createVirtualCompilerHost(
92
97
  system,
@@ -98,20 +103,55 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
98
103
  options: compilerOptions,
99
104
  host: host.compilerHost
100
105
  });
101
- for (const file of fsMap.keys()) {
102
- const sourceFile = program.getSourceFile(file);
103
- program.emit(
104
- sourceFile,
105
- (filePath, content) => {
106
- this.emitFile({
107
- source: content,
108
- fileName: import_path.default.relative(process.cwd(), filePath),
109
- type: "asset"
110
- });
106
+ const diagnostics = import_typescript.default.getPreEmitDiagnostics(program).map((diagnostic) => {
107
+ const file = diagnostic.file;
108
+ if (!file)
109
+ return diagnostic;
110
+ const sourceMap = sourceMaps.get(file.fileName);
111
+ if (!sourceMap)
112
+ return diagnostic;
113
+ const sourcemapLines = sourceMap.data.lines;
114
+ const range = (0, import_ts_diagnostic.remapRange)(
115
+ {
116
+ start: diagnostic.start || 0,
117
+ end: (diagnostic.start || 0) + (diagnostic.length || 1)
111
118
  },
112
- void 0,
113
- true
119
+ sourcemapLines
114
120
  );
121
+ return {
122
+ ...diagnostic,
123
+ messageText: (0, import_ts_diagnostic.flattenDiagnosticMessageText)(diagnostic.messageText),
124
+ length: diagnostic.length,
125
+ start: range.start
126
+ };
127
+ });
128
+ if (diagnostics.length > 0) {
129
+ console.error(
130
+ import_typescript.default.formatDiagnosticsWithColorAndContext(diagnostics, formatHost)
131
+ );
132
+ }
133
+ if (options.dts) {
134
+ for (const file of fsMap.keys()) {
135
+ const sourceFile = program.getSourceFile(file);
136
+ program.emit(
137
+ sourceFile,
138
+ async (filePath, content) => {
139
+ const dir = import_path.default.dirname(filePath);
140
+ await fs.promises.mkdir(dir, { recursive: true });
141
+ const pathFromDistDir = import_path.default.relative(
142
+ compilerOptions.outDir ?? process.cwd(),
143
+ filePath
144
+ );
145
+ this.emitFile({
146
+ source: content,
147
+ fileName: pathFromDistDir,
148
+ type: "asset"
149
+ });
150
+ },
151
+ void 0,
152
+ true
153
+ );
154
+ }
115
155
  }
116
156
  }
117
157
  },
@@ -135,13 +175,20 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
135
175
  return null;
136
176
  const filename = import_path.default.resolve(process.cwd(), id.slice(0, -outExt.length));
137
177
  const code = await fs.promises.readFile(filename, "utf-8");
178
+ const compiled = import_civet.default.compile(code, {
179
+ // inlineMap: true,
180
+ filename: id,
181
+ js: transpileToJS,
182
+ sourceMap: true
183
+ });
184
+ sourceMaps.set(import_path.default.resolve(process.cwd(), id), compiled.sourceMap);
185
+ const jsonSourceMap = compiled.sourceMap.json(
186
+ import_path.default.basename(id.replace(/\.[jt]sx$/, "")),
187
+ import_path.default.basename(id)
188
+ );
138
189
  let transformed = {
139
- code: import_civet.default.compile(code, {
140
- inlineMap: true,
141
- filename: id,
142
- js: transpileToJS
143
- }),
144
- map: null
190
+ code: compiled.code,
191
+ map: jsonSourceMap
145
192
  };
146
193
  if (options.transformOutput)
147
194
  transformed = await options.transformOutput(transformed.code, id);
@@ -150,8 +197,12 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
150
197
  transform(code, id) {
151
198
  if (!/\.civet\.tsx?$/.test(id))
152
199
  return null;
153
- if (options.dts) {
154
- fsMap.set(import_path.default.resolve(process.cwd(), id), code);
200
+ if (options.dts || options.typecheck) {
201
+ const resolved = import_path.default.resolve(process.cwd(), id);
202
+ fsMap.set(resolved, code);
203
+ const slash = resolved.replace(/\\/g, "/");
204
+ if (resolved !== slash)
205
+ fsMap.set(slash, code);
155
206
  }
156
207
  return null;
157
208
  },