@danielx/civet 0.7.16 → 0.7.18

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 (50) hide show
  1. package/README.md +2 -1
  2. package/dist/browser.js +28 -12
  3. package/dist/civet +139 -100
  4. package/dist/config.d.mts +1 -0
  5. package/dist/config.d.ts +1 -0
  6. package/dist/config.mjs +122 -0
  7. package/dist/esm.mjs +1 -1
  8. package/dist/main.js +28 -12
  9. package/dist/main.mjs +28 -12
  10. package/dist/unplugin/astro.d.ts +11 -0
  11. package/dist/unplugin/astro.js +49 -0
  12. package/dist/unplugin/astro.mjs +19 -0
  13. package/dist/unplugin/esbuild.d.ts +2 -0
  14. package/dist/unplugin/esbuild.js +36 -0
  15. package/dist/unplugin/esbuild.mjs +6 -0
  16. package/dist/unplugin/rollup.d.ts +2 -0
  17. package/dist/unplugin/rollup.js +36 -0
  18. package/dist/unplugin/rollup.mjs +6 -0
  19. package/dist/{unplugin.d.ts → unplugin/unplugin.d.ts} +7 -10
  20. package/dist/{unplugin.js → unplugin/unplugin.js} +81 -66
  21. package/dist/{unplugin-shared.mjs → unplugin/unplugin.mjs} +80 -65
  22. package/dist/unplugin/vite.d.ts +2 -0
  23. package/dist/unplugin/vite.js +36 -0
  24. package/dist/unplugin/vite.mjs +6 -0
  25. package/dist/unplugin/webpack.d.ts +2 -0
  26. package/dist/unplugin/webpack.js +36 -0
  27. package/dist/unplugin/webpack.mjs +6 -0
  28. package/package.json +27 -17
  29. package/dist/astro.d.mts +0 -16
  30. package/dist/astro.d.ts +0 -16
  31. package/dist/astro.js +0 -537
  32. package/dist/astro.mjs +0 -22
  33. package/dist/esbuild.d.mts +0 -8
  34. package/dist/esbuild.d.ts +0 -8
  35. package/dist/esbuild.js +0 -524
  36. package/dist/esbuild.mjs +0 -9
  37. package/dist/rollup.d.mts +0 -8
  38. package/dist/rollup.d.ts +0 -8
  39. package/dist/rollup.js +0 -524
  40. package/dist/rollup.mjs +0 -9
  41. package/dist/unplugin.d.mts +0 -26
  42. package/dist/unplugin.mjs +0 -10
  43. package/dist/vite.d.mts +0 -8
  44. package/dist/vite.d.ts +0 -8
  45. package/dist/vite.js +0 -524
  46. package/dist/vite.mjs +0 -9
  47. package/dist/webpack.d.mts +0 -7
  48. package/dist/webpack.d.ts +0 -7
  49. package/dist/webpack.js +0 -524
  50. package/dist/webpack.mjs +0 -9
package/README.md CHANGED
@@ -16,13 +16,14 @@ The modern way to write TypeScript.
16
16
  - [Civet VSCode Extension](https://marketplace.visualstudio.com/items?itemName=DanielX.civet)
17
17
  - [Discord Server](https://discord.gg/xkrW9GebBc)
18
18
  - Plugins for
19
- [Vite, esbuild, Astro, Rollup, Webpack, Rspack](integration/unplugin)
19
+ [Vite, esbuild, Astro, Rollup, Webpack, Rspack](source/unplugin),
20
20
  <!--
21
21
  [esbuild](source/esbuild-plugin.civet),
22
22
  [Vite](https://github.com/edemaine/vite-plugin-civet),
23
23
  -->
24
24
  [ESM/CJS loader](source/esm.civet),
25
25
  [Babel](source/babel-plugin.mjs),
26
+ [Jest](https://github.com/DanielXMoore/Civet/blob/main/integration/jest),
26
27
  [Gulp](integration/gulp),
27
28
  [Bun](source/bun-civet.civet)
28
29
  - Starter templates for [Solid](https://github.com/orenelbaum/solid-civet-template) and [Solid Start](https://github.com/orenelbaum/solid-start-civet-template)
package/dist/browser.js CHANGED
@@ -5653,6 +5653,7 @@ ${js}`
5653
5653
  }
5654
5654
  }
5655
5655
  }
5656
+ const refsToDeclare = /* @__PURE__ */ new Set();
5656
5657
  i = len3 - 1;
5657
5658
  while (i >= 0) {
5658
5659
  const lastAssignment = $1[i];
@@ -5686,6 +5687,7 @@ ${js}`
5686
5687
  }
5687
5688
  } else if (m2 = lhs.type, m2 === "ObjectBindingPattern" || m2 === "ArrayBindingPattern") {
5688
5689
  processBindingPatternLHS(lhs, tail);
5690
+ gatherRecursiveAll(lhs, ($5) => $5.type === "Ref").forEach(refsToDeclare.add.bind(refsToDeclare));
5689
5691
  }
5690
5692
  }
5691
5693
  i--;
@@ -5716,6 +5718,17 @@ ${js}`
5716
5718
  }
5717
5719
  i--;
5718
5720
  }
5721
+ if (refsToDeclare.size) {
5722
+ if (exp.hoistDec) {
5723
+ exp.hoistDec.children.push([...refsToDeclare].map(($6) => [",", $6]));
5724
+ } else {
5725
+ exp.hoistDec = {
5726
+ type: "Declaration",
5727
+ children: ["let ", [...refsToDeclare].map((r, i2) => i2 ? [",", r] : r)],
5728
+ names: []
5729
+ };
5730
+ }
5731
+ }
5719
5732
  exp.names = $1.flatMap(([, l]) => l.names || []);
5720
5733
  if (tail.length) {
5721
5734
  const index = exp.children.indexOf($2);
@@ -5852,7 +5865,7 @@ ${js}`
5852
5865
  });
5853
5866
  }
5854
5867
  function processStatementExpressions(statements) {
5855
- gatherRecursiveAll(statements, ($5) => $5.type === "StatementExpression").forEach((_exp) => {
5868
+ gatherRecursiveAll(statements, ($7) => $7.type === "StatementExpression").forEach((_exp) => {
5856
5869
  const exp = _exp;
5857
5870
  const { statement } = exp;
5858
5871
  let ref11;
@@ -5982,11 +5995,11 @@ ${js}`
5982
5995
  function processPlaceholders(statements) {
5983
5996
  const placeholderMap = /* @__PURE__ */ new Map();
5984
5997
  const liftedIfs = /* @__PURE__ */ new Set();
5985
- gatherRecursiveAll(statements, ($6) => $6.type === "Placeholder").forEach((_exp) => {
5998
+ gatherRecursiveAll(statements, ($8) => $8.type === "Placeholder").forEach((_exp) => {
5986
5999
  const exp = _exp;
5987
6000
  let ancestor;
5988
6001
  if (exp.subtype === ".") {
5989
- ({ ancestor } = findAncestor(exp, ($7) => $7.type === "Call"));
6002
+ ({ ancestor } = findAncestor(exp, ($9) => $9.type === "Call"));
5990
6003
  ancestor = ancestor?.parent;
5991
6004
  while (ancestor?.parent?.type === "UnaryExpression" || ancestor?.parent?.type === "NewExpression") {
5992
6005
  ancestor = ancestor.parent;
@@ -7108,8 +7121,8 @@ ${js}`
7108
7121
  var $L61 = (0, import_lib3.$L)("?=");
7109
7122
  var $L62 = (0, import_lib3.$L)("and=");
7110
7123
  var $L63 = (0, import_lib3.$L)("or=");
7111
- var $L64 = (0, import_lib3.$L)("**");
7112
- var $L65 = (0, import_lib3.$L)("*");
7124
+ var $L64 = (0, import_lib3.$L)("*");
7125
+ var $L65 = (0, import_lib3.$L)("**");
7113
7126
  var $L66 = (0, import_lib3.$L)("/");
7114
7127
  var $L67 = (0, import_lib3.$L)("%%");
7115
7128
  var $L68 = (0, import_lib3.$L)("%");
@@ -8147,13 +8160,15 @@ ${js}`
8147
8160
  function TernaryRest(ctx, state2) {
8148
8161
  return (0, import_lib3.$EVENT_C)(ctx, state2, "TernaryRest", TernaryRest$$);
8149
8162
  }
8150
- var NestedTernaryRest$0 = (0, import_lib3.$TS)((0, import_lib3.$S)(PushIndent, (0, import_lib3.$E)((0, import_lib3.$S)(Nested, QuestionMark, MaybeNestedExtendedExpression, Nested, Colon, MaybeNestedExtendedExpression)), PopIndent), function($skip, $loc, $0, $1, $2, $3) {
8163
+ var NestedTernaryRest$0 = (0, import_lib3.$S)(Nested, QuestionMark, MaybeNestedExtendedExpression, Nested, Colon, MaybeNestedExtendedExpression);
8164
+ var NestedTernaryRest$1 = (0, import_lib3.$TS)((0, import_lib3.$S)(PushIndent, (0, import_lib3.$E)((0, import_lib3.$S)(Nested, QuestionMark, MaybeNestedExtendedExpression, Nested, Colon, MaybeNestedExtendedExpression)), PopIndent), function($skip, $loc, $0, $1, $2, $3) {
8151
8165
  if ($2)
8152
8166
  return $2;
8153
8167
  return $skip;
8154
8168
  });
8169
+ var NestedTernaryRest$$ = [NestedTernaryRest$0, NestedTernaryRest$1];
8155
8170
  function NestedTernaryRest(ctx, state2) {
8156
- return (0, import_lib3.$EVENT)(ctx, state2, "NestedTernaryRest", NestedTernaryRest$0);
8171
+ return (0, import_lib3.$EVENT_C)(ctx, state2, "NestedTernaryRest", NestedTernaryRest$$);
8157
8172
  }
8158
8173
  var ShortCircuitExpression$0 = BinaryOpExpression;
8159
8174
  function ShortCircuitExpression(ctx, state2) {
@@ -11501,11 +11516,12 @@ ${js}`
11501
11516
  ws.push(...$2);
11502
11517
  return [ws, $3];
11503
11518
  });
11504
- var NotDedentedBinaryOp$1 = (0, import_lib3.$TS)((0, import_lib3.$S)(Nested, (0, import_lib3.$E)(_), (0, import_lib3.$N)(Identifier), BinaryOp), function($skip, $loc, $0, $1, $2, $3, $4) {
11519
+ var NotDedentedBinaryOp$1 = (0, import_lib3.$TS)((0, import_lib3.$S)(Nested, (0, import_lib3.$E)(_), (0, import_lib3.$N)(Identifier), (0, import_lib3.$C)((0, import_lib3.$N)((0, import_lib3.$EXPECT)($L64, 'NotDedentedBinaryOp "*"')), (0, import_lib3.$N)(ImportDeclaration)), BinaryOp), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11520
+ var op = $5;
11505
11521
  const ws = [...$1];
11506
11522
  if ($2)
11507
11523
  ws.push(...$2);
11508
- return [ws, $4];
11524
+ return [ws, op];
11509
11525
  });
11510
11526
  var NotDedentedBinaryOp$$ = [NotDedentedBinaryOp$0, NotDedentedBinaryOp$1];
11511
11527
  function NotDedentedBinaryOp(ctx, state2) {
@@ -11559,8 +11575,8 @@ ${js}`
11559
11575
  function _BinaryOp(ctx, state2) {
11560
11576
  return (0, import_lib3.$EVENT_C)(ctx, state2, "_BinaryOp", _BinaryOp$$);
11561
11577
  }
11562
- var BinaryOpSymbol$0 = (0, import_lib3.$EXPECT)($L64, 'BinaryOpSymbol "**"');
11563
- var BinaryOpSymbol$1 = (0, import_lib3.$EXPECT)($L65, 'BinaryOpSymbol "*"');
11578
+ var BinaryOpSymbol$0 = (0, import_lib3.$EXPECT)($L65, 'BinaryOpSymbol "**"');
11579
+ var BinaryOpSymbol$1 = (0, import_lib3.$EXPECT)($L64, 'BinaryOpSymbol "*"');
11564
11580
  var BinaryOpSymbol$2 = (0, import_lib3.$EXPECT)($L66, 'BinaryOpSymbol "/"');
11565
11581
  var BinaryOpSymbol$3 = (0, import_lib3.$TV)((0, import_lib3.$EXPECT)($L67, 'BinaryOpSymbol "%%"'), function($skip, $loc, $0, $1) {
11566
11582
  return {
@@ -14495,7 +14511,7 @@ ${js}`
14495
14511
  function SingleQuote(ctx, state2) {
14496
14512
  return (0, import_lib3.$EVENT)(ctx, state2, "SingleQuote", SingleQuote$0);
14497
14513
  }
14498
- var Star$0 = (0, import_lib3.$TV)((0, import_lib3.$EXPECT)($L65, 'Star "*"'), function($skip, $loc, $0, $1) {
14514
+ var Star$0 = (0, import_lib3.$TV)((0, import_lib3.$EXPECT)($L64, 'Star "*"'), function($skip, $loc, $0, $1) {
14499
14515
  return { $loc, token: $1 };
14500
14516
  });
14501
14517
  function Star(ctx, state2) {
package/dist/civet CHANGED
@@ -39,68 +39,34 @@ __export(cli_exports, {
39
39
  module.exports = __toCommonJS(cli_exports);
40
40
  var import_main = require("./main.js");
41
41
  var import_config = require("./config.js");
42
- var import_unplugin = require("./unplugin");
42
+ var import_unplugin = require("./unplugin/unplugin.js");
43
43
  var import_promises = __toESM(require("node:fs/promises"));
44
44
  var import_node_path = __toESM(require("node:path"));
45
+ var import_meta = {};
45
46
  var unplugin;
46
- function version() {
47
- return require("../package.json").version;
48
- }
49
- if (process.argv.some((a) => a === "--version" || a === "-version" || a === "-v")) {
50
- console.log(version());
51
- process.exit(0);
52
- }
53
- if (process.argv.some((a1) => a1 === "--help" || a1 === "-help" || a1 === "-h")) {
54
- process.stderr.write(` \u2584\u2584\xB7 \u25AA \u258C \u2590\xB7\u2584\u2584\u2584 .\u2584\u2584\u2584\u2584\u2584
55
- \u2590\u2588 \u258C\u25AA\u2588\u2588 \u25AA\u2588\xB7\u2588\u258C\u2580\u2584.\u2580\xB7\u2022\u2588\u2588 _._ _,-'""\`-._
56
- \u2588\u2588 \u2584\u2584\u2590\u2588\xB7\u2590\u2588\u2590\u2588\u2022\u2590\u2580\u2580\u25AA\u2584 \u2590\u2588.\u25AA (,-.\`._,'( |\\\`-/|
57
- \u2590\u2588\u2588\u2588\u258C\u2590\u2588\u258C \u2588\u2588\u2588 \u2590\u2588\u2584\u2584\u258C \u2590\u2588\u258C\xB7 \`-.-' \\ )-\`( , o o)
58
- \xB7\u2580\u2580\u2580 \u2580\u2580\u2580. \u2580 \u2580\u2580\u2580 \u2580\u2580\u2580 \`- \\\`_\`"'-
59
-
60
-
61
- Usage:
62
-
63
- civet # REPL for executing code
64
- civet -c # REPL for transpiling code
65
- civet --ast # REPL for parsing code
66
- civet [options] input.civet # run input.civet
67
- civet [options] -c input.civet # -> input.civet.tsx
68
- civet [options] -c input.civet -o .ts # -> input.ts
69
- civet [options] -c input.civet -o dir # -> dir/input.civet.tsx
70
- civet [options] -c input.civet -o dir/.ts # -> dir/input.ts
71
- civet [options] -c input.civet -o output.ts # -> output.ts
72
- civet [options] < input.civet > output.ts # pipe form
73
-
74
- Options:
75
- --help Show this help message
76
- --version Show the version number
77
- -o / --output XX Specify output directory and/or extension, or filename
78
- -c / --compile Compile input files to TypeScript (or JavaScript)
79
- --config XX Specify a config file (default scans for a config.civet, civet.json, civetconfig.civet or civetconfig.json file, optionally in a .config directory, or starting with a .)
80
- --civet XX Specify civet compiler flag, as in "civet XX" prologue
81
- --comptime Enable execution of code during compilation via comptime
82
- --no-config Don't scan for a config file
83
- --js Strip out all type annotations; default to .jsx extension
84
- --ast Print the AST instead of the compiled code
85
- --inline-map Generate a sourcemap
86
- --no-cache Disable compiler caching (slow, for debugging)
87
- --typecheck Run TypeScript and output diagnostics
88
- --emit-declaration Run TypeScript and emit .d.ts files (if no errors)
89
-
90
- You can use - to read from stdin or (prefixed by -o) write to stdout.
91
-
92
- By default, .civet imports get rewritten to use the output extension.
93
- You can override this behavior via: --civet rewriteCivetImports=.ext
94
-
95
- `);
96
- process.exit(0);
47
+ async function version() {
48
+ if (import_meta.url) {
49
+ const { createRequire } = await import("node:module");
50
+ return createRequire(import_meta.url)("../package.json").version;
51
+ } else {
52
+ return require("../package.json").version;
53
+ }
97
54
  }
98
55
  var encoding = "utf8";
99
- function parseArgs(args) {
56
+ async function parseArgs(args, isTTY = process.stdin.isTTY) {
100
57
  const options = {};
101
58
  const isRun = () => !(options.ast || options.compile || options.typecheck || options.emitDeclaration);
102
59
  let filenames = [];
103
60
  let scriptArgs = [];
61
+ if (args.includes("-version")) {
62
+ options.version = true;
63
+ }
64
+ if (args.includes("-help")) {
65
+ options.help = true;
66
+ }
67
+ if (options.version || options.help) {
68
+ return { filenames, scriptArgs, options };
69
+ }
104
70
  let i = 0;
105
71
  let errors = 0;
106
72
  function endOfArgs(j) {
@@ -128,6 +94,16 @@ function parseArgs(args) {
128
94
  continue;
129
95
  }
130
96
  switch (arg) {
97
+ case "-v":
98
+ case "--version": {
99
+ options.version = true;
100
+ break;
101
+ }
102
+ case "-h":
103
+ case "--help": {
104
+ options.help = true;
105
+ break;
106
+ }
131
107
  case "-c":
132
108
  case "--compile": {
133
109
  options.compile = true;
@@ -150,7 +126,13 @@ function parseArgs(args) {
150
126
  Object.assign(
151
127
  options.parseOptions ??= {},
152
128
  (0, import_main.parse)(`civet ${args[++i]}`, {
153
- startRule: "CivetPrologueContent"
129
+ startRule: "CivetPrologueContent",
130
+ filename: "--civet argument",
131
+ events: {
132
+ // remove cache in case we've compiled Civet before
133
+ enter: void 0,
134
+ exit: void 0
135
+ }
154
136
  }).config
155
137
  );
156
138
  break;
@@ -213,9 +195,11 @@ function parseArgs(args) {
213
195
  }
214
196
  i++;
215
197
  }
216
- options.typescript = Boolean(options.typecheck || options.emitDeclaration);
198
+ if (options.typecheck || options.emitDeclaration) {
199
+ options.typescript = true;
200
+ }
217
201
  if (!(filenames.length || options.typescript)) {
218
- if (process.stdin.isTTY) {
202
+ if (isTTY) {
219
203
  options.repl = true;
220
204
  } else {
221
205
  options.compile = true;
@@ -223,6 +207,28 @@ function parseArgs(args) {
223
207
  filenames = ["-"];
224
208
  }
225
209
  }
210
+ if (options.output && options.output !== "-") {
211
+ const optionsPath = import_node_path.default.parse(options.output);
212
+ let stat;
213
+ try {
214
+ stat = await import_promises.default.stat(options.output);
215
+ } catch {
216
+ stat = null;
217
+ }
218
+ if (stat?.isDirectory() || options.output.endsWith(import_node_path.default.sep) || options.output.endsWith("/")) {
219
+ options.outputDir = options.output;
220
+ } else if (/^(\.[^]+)+$/.test(optionsPath.base)) {
221
+ options.outputExt = optionsPath.base;
222
+ if (optionsPath.dir) {
223
+ options.outputDir = optionsPath.dir;
224
+ }
225
+ } else {
226
+ options.outputPath = optionsPath;
227
+ options.outputExt = optionsPath.ext;
228
+ }
229
+ }
230
+ ;
231
+ (options.parseOptions ??= {}).rewriteCivetImports ??= options.outputExt ?? ".civet.jsx";
226
232
  if (errors) {
227
233
  process.exit(Math.min(255, errors));
228
234
  }
@@ -275,7 +281,7 @@ async function* readFiles(filenames) {
275
281
  ;
276
282
  return results1;
277
283
  }
278
- async function repl(options) {
284
+ async function repl(args, options) {
279
285
  const vm = await import("node:vm");
280
286
  let importModuleDynamically = vm.constants?.USE_MAIN_CONTEXT_DEFAULT_LOADER;
281
287
  if (!importModuleDynamically) {
@@ -294,15 +300,15 @@ async function repl(options) {
294
300
  execArgv.push(process.env.NODE_OPTIONS);
295
301
  }
296
302
  const { fork } = await import("node:child_process");
297
- fork(__filename, process.argv.slice(2), {
303
+ fork(__filename, args, {
298
304
  execArgv,
299
305
  stdio: "inherit"
300
306
  });
301
307
  return;
302
308
  }
303
309
  }
304
- require("../register.js");
305
- console.log(`Civet ${version()} REPL. Enter a blank line to ${(() => {
310
+ await import("../register.js");
311
+ console.log(`Civet ${await version()} REPL. Enter a blank line to ${(() => {
306
312
  switch (false) {
307
313
  case !options.ast: {
308
314
  return "parse";
@@ -391,7 +397,10 @@ ${" ".repeat(error.column - 1)}^ ${error.header}`);
391
397
  }
392
398
  const topLevelAwait = import_main.lib.hasAwait(ast) || import_main.lib.hasImportDeclaration(ast);
393
399
  if (topLevelAwait) {
394
- const [prologue, rest] = (0, import_main.parse)(input, { startRule: "ProloguePrefix" });
400
+ const [prologue, rest] = (0, import_main.parse)(input, {
401
+ startRule: "ProloguePrefix",
402
+ filename: "--civet argument"
403
+ });
395
404
  const prefix = input.slice(0, -rest.length);
396
405
  const coffee = prologue.some((p) => p.type === "CivetPrologue" && (p.config.coffeeCompat || p.config.coffeeDo));
397
406
  ast = await (0, import_main.compile)(
@@ -452,17 +461,72 @@ ${" ".repeat(error.column - 1)}^ ${error.header}`);
452
461
  });
453
462
  return r;
454
463
  }
455
- async function cli() {
456
- const argv = process.argv;
457
- let { filenames, scriptArgs, options } = parseArgs(argv.slice(2));
464
+ async function cli(args = process.argv.slice(2)) {
465
+ let { filenames, scriptArgs, options } = await parseArgs(args);
466
+ if (options.version) {
467
+ console.log(await version());
468
+ process.exit(0);
469
+ }
470
+ if (options.help) {
471
+ process.stderr.write(` \u2584\u2584\xB7 \u25AA \u258C \u2590\xB7\u2584\u2584\u2584 .\u2584\u2584\u2584\u2584\u2584
472
+ \u2590\u2588 \u258C\u25AA\u2588\u2588 \u25AA\u2588\xB7\u2588\u258C\u2580\u2584.\u2580\xB7\u2022\u2588\u2588 _._ _,-'""\`-._
473
+ \u2588\u2588 \u2584\u2584\u2590\u2588\xB7\u2590\u2588\u2590\u2588\u2022\u2590\u2580\u2580\u25AA\u2584 \u2590\u2588.\u25AA (,-.\`._,'( |\\\`-/|
474
+ \u2590\u2588\u2588\u2588\u258C\u2590\u2588\u258C \u2588\u2588\u2588 \u2590\u2588\u2584\u2584\u258C \u2590\u2588\u258C\xB7 \`-.-' \\ )-\`( , o o)
475
+ \xB7\u2580\u2580\u2580 \u2580\u2580\u2580. \u2580 \u2580\u2580\u2580 \u2580\u2580\u2580 \`- \\\`_\`"'-
476
+
477
+
478
+ Usage:
479
+
480
+ civet # REPL for executing code
481
+ civet -c # REPL for transpiling code
482
+ civet --ast # REPL for parsing code
483
+ civet [options] input.civet # run input.civet
484
+ civet [options] -c input.civet # -> input.civet.tsx
485
+ civet [options] -c input.civet -o .ts # -> input.ts
486
+ civet [options] -c input.civet -o dir # -> dir/input.civet.tsx
487
+ civet [options] -c input.civet -o dir/.ts # -> dir/input.ts
488
+ civet [options] -c input.civet -o output.ts # -> output.ts
489
+ civet [options] < input.civet > output.ts # pipe form
490
+
491
+ Options:
492
+ --help Show this help message
493
+ --version Show the version number
494
+ -o / --output XX Specify output directory and/or extension, or filename
495
+ -c / --compile Compile input files to TypeScript (or JavaScript)
496
+ --config XX Specify a config file (default scans for a config.civet, civet.json, civetconfig.civet or civetconfig.json file, optionally in a .config directory, or starting with a .)
497
+ --civet XX Specify civet compiler flag, as in "civet XX" prologue
498
+ --comptime Enable execution of code during compilation via comptime
499
+ --no-config Don't scan for a config file
500
+ --js Strip out all type annotations; default to .jsx extension
501
+ --ast Print the AST instead of the compiled code
502
+ --inline-map Generate a sourcemap
503
+ --no-cache Disable compiler caching (slow, for debugging)
504
+ --typecheck Run TypeScript and output diagnostics
505
+ --emit-declaration Run TypeScript and emit .d.ts files (if no errors)
506
+
507
+ You can use - to read from stdin or (prefixed by -o) write to stdout.
508
+
509
+ By default, .civet imports get rewritten to use the output extension.
510
+ You can override this behavior via: --civet rewriteCivetImports=.ext
511
+
512
+ `);
513
+ process.exit(0);
514
+ }
458
515
  if (options.config !== false) {
459
516
  options.config ??= await (0, import_config.findConfig)(process.cwd());
460
517
  }
461
518
  if (options.config) {
519
+ const parsed = await (0, import_config.loadConfig)(options.config);
462
520
  options = {
463
- ...await (0, import_config.loadConfig)(options.config),
521
+ ...parsed,
464
522
  ...options
465
523
  };
524
+ if (parsed.parseOptions && options.parseOptions) {
525
+ options.parseOptions = {
526
+ ...parsed.parseOptions,
527
+ ...options.parseOptions
528
+ };
529
+ }
466
530
  }
467
531
  if (options.typescript) {
468
532
  const unpluginOptions = {
@@ -479,30 +543,7 @@ async function cli() {
479
543
  options.inlineMap = true;
480
544
  }
481
545
  if (options.repl) {
482
- return repl(options);
483
- }
484
- let outputDir, outputExt, outputPath;
485
- if (options.output) {
486
- const optionsPath = import_node_path.default.parse(options.output);
487
- let stat;
488
- try {
489
- stat = await import_promises.default.stat(options.output);
490
- } catch {
491
- stat = null;
492
- }
493
- if (stat?.isDirectory() || options.output.endsWith(import_node_path.default.sep) || options.output.endsWith("/")) {
494
- outputDir = options.output;
495
- } else if (/^(\.[^.]+)+$/.test(optionsPath.base)) {
496
- outputExt = optionsPath.base;
497
- if (optionsPath.dir) {
498
- outputDir = optionsPath.dir;
499
- }
500
- } else {
501
- outputPath = optionsPath;
502
- }
503
- if (options.output !== "-") {
504
- (options.parseOptions ??= {}).rewriteCivetImports ??= outputExt ?? ".civet" + (options.js ? ".jsx" : ".tsx");
505
- }
546
+ return repl(args, options);
506
547
  }
507
548
  let errors = 0;
508
549
  for await (const { filename, error, content, stdin } of readFiles(filenames)) {
@@ -549,14 +590,14 @@ async function cli() {
549
590
  } else {
550
591
  targetPath.ext += ".tsx";
551
592
  }
552
- if (outputDir != null) {
553
- targetPath.dir = outputDir;
593
+ if (options.outputDir != null) {
594
+ targetPath.dir = options.outputDir;
554
595
  }
555
- if (outputExt != null) {
556
- targetPath.ext = outputExt;
596
+ if (options.outputExt != null) {
597
+ targetPath.ext = options.outputExt;
557
598
  }
558
- if (outputPath != null) {
559
- targetPath = outputPath;
599
+ if (options.outputPath != null) {
600
+ targetPath = options.outputPath;
560
601
  }
561
602
  if (targetPath.dir) {
562
603
  await import_promises.default.mkdir(targetPath.dir, { recursive: true });
@@ -619,14 +660,14 @@ async function cli() {
619
660
  return process.exit(code ?? 1);
620
661
  });
621
662
  } else {
622
- require("../register.js");
663
+ await import("../register.js");
623
664
  try {
624
665
  module.filename = await import_promises.default.realpath(filename);
625
666
  } catch {
626
667
  module.filename = filename;
627
668
  }
628
669
  process.argv = ["civet", module.filename, ...scriptArgs];
629
- module.paths = require("module")._nodeModulePaths(import_node_path.default.dirname(module.filename));
670
+ module.paths = (await import("node:module"))._nodeModulePaths(import_node_path.default.dirname(module.filename));
630
671
  try {
631
672
  module._compile(output, module.filename);
632
673
  } catch (error2) {
@@ -659,9 +700,6 @@ async function cli() {
659
700
  ;
660
701
  return;
661
702
  }
662
- if (require.main === module) {
663
- cli();
664
- }
665
703
  // Annotate the CommonJS export names for ESM import in node:
666
704
  0 && (module.exports = {
667
705
  cli,
@@ -669,3 +707,4 @@ if (require.main === module) {
669
707
  repl,
670
708
  version
671
709
  });
710
+ cli()
@@ -0,0 +1 @@
1
+ /// <reference path="./types.d.ts" />
@@ -0,0 +1 @@
1
+ /// <reference path="./types.d.ts" />
@@ -0,0 +1,122 @@
1
+ // source/config.civet
2
+ import path from "path";
3
+ import fs from "fs/promises";
4
+ import { compile } from "./main.mjs";
5
+ var configNames = [
6
+ "\u{1F408}",
7
+ "civetconfig",
8
+ "civet.config",
9
+ "package"
10
+ ];
11
+ var configExtensions = [
12
+ ".civet",
13
+ ".js",
14
+ ".yaml",
15
+ ".yml",
16
+ ".json"
17
+ ];
18
+ var configDir = ".config";
19
+ async function findInDir(dirPath) {
20
+ const entries = new Set(await fs.readdir(dirPath));
21
+ const pathFor = (name) => path.join(dirPath, name);
22
+ if (entries.has(configDir) && await (async () => {
23
+ try {
24
+ return (await fs.stat(pathFor(configDir))).isDirectory();
25
+ } catch (e) {
26
+ return;
27
+ }
28
+ })()) {
29
+ const found = await findInDir(pathFor(configDir));
30
+ if (found) {
31
+ return found;
32
+ }
33
+ }
34
+ for (let i = 0, len = configNames.length; i < len; i++) {
35
+ const configName = configNames[i];
36
+ for (let i1 = 0, len1 = configExtensions.length; i1 < len1; i1++) {
37
+ const extension = configExtensions[i1];
38
+ for (let ref = ["." + configName + extension, configName + extension], i2 = 0, len2 = ref.length; i2 < len2; i2++) {
39
+ const entry = ref[i2];
40
+ if (entries.has(entry) && await (async () => {
41
+ try {
42
+ return (await fs.stat(pathFor(entry))).isFile();
43
+ } catch (e) {
44
+ return;
45
+ }
46
+ })()) {
47
+ return pathFor(entry);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ return;
53
+ }
54
+ async function findConfig(startDir) {
55
+ let curr = startDir;
56
+ let parent = path.dirname(curr);
57
+ while (curr !== parent) {
58
+ const configPath = await findInDir(curr);
59
+ if (configPath) {
60
+ return configPath;
61
+ }
62
+ curr = parent;
63
+ parent = path.dirname(curr);
64
+ }
65
+ return;
66
+ }
67
+ async function loadConfig(pathname) {
68
+ const config = await fs.readFile(pathname, "utf8");
69
+ let data = {};
70
+ if (pathname.endsWith(".json")) {
71
+ let json;
72
+ try {
73
+ json = JSON.parse(config);
74
+ } catch (e) {
75
+ throw new Error(`Error parsing JSON config file ${pathname}: ${e}`);
76
+ }
77
+ if ("civetConfig" in json) {
78
+ data = json.civetConfig;
79
+ } else if (path.basename(pathname).startsWith("package")) {
80
+ return {};
81
+ } else {
82
+ data = json;
83
+ }
84
+ } else if (/\.ya?ml$/.test(pathname)) {
85
+ try {
86
+ const { default: YAML } = await import("yaml");
87
+ const yaml = YAML.parse(config);
88
+ data = yaml.civetConfig ?? yaml;
89
+ } catch (e) {
90
+ throw new Error(`Error parsing YAML config file ${pathname}: ${e}`);
91
+ }
92
+ } else {
93
+ let js;
94
+ if (pathname.endsWith(".civet")) {
95
+ try {
96
+ js = await compile(config, {
97
+ js: true
98
+ });
99
+ } catch (e) {
100
+ throw new Error(`Error compiling Civet config file ${pathname}: ${e}`);
101
+ }
102
+ } else {
103
+ js = config;
104
+ }
105
+ try {
106
+ const exports = await import(`data:text/javascript,${encodeURIComponent(js)}`);
107
+ data = exports?.default;
108
+ } catch (e) {
109
+ throw new Error(`Error running Civet config file ${pathname}: ${e}`);
110
+ }
111
+ }
112
+ if (!(data != null && typeof data === "object" && !Array.isArray(data))) {
113
+ throw new Error(`Civet config file must export an object, not ${Array.isArray(data) ? "array" : data != null ? typeof data : "null"}`);
114
+ }
115
+ delete data?.parseOptions?.comptime;
116
+ return data;
117
+ }
118
+ export {
119
+ findConfig,
120
+ findInDir,
121
+ loadConfig
122
+ };
package/dist/esm.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // source/esm.civet
2
2
  import { readFileSync } from "fs";
3
3
  import { pathToFileURL, fileURLToPath } from "url";
4
- import Civet from "./main.js";
4
+ import Civet from "./main.mjs";
5
5
  var { compile, SourceMap } = Civet;
6
6
  var baseURL = pathToFileURL(process.cwd() + "/").href;
7
7
  var extensionsRegex = /\.civet$/;