@danielx/civet 0.8.11 → 0.8.13

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.
@@ -1,6 +1,6 @@
1
1
  // unplugin-civet:C:\Users\edemaine\Projects\Civet\source\unplugin\unplugin.civet.jsx
2
2
  import { createUnplugin } from "unplugin";
3
- import civet, { SourceMap } from "@danielx/civet";
3
+ import civet, { lib, SourceMap } from "@danielx/civet";
4
4
  import { findInDir, loadConfig } from "@danielx/civet/config";
5
5
  import {
6
6
  remapRange,
@@ -36,8 +36,8 @@ function cleanCivetId(id) {
36
36
  function tryStatSync(file) {
37
37
  try {
38
38
  return fs.statSync(file, { throwIfNoEntry: false });
39
- } catch {
40
- return void 0;
39
+ } catch (e) {
40
+ return;
41
41
  }
42
42
  }
43
43
  function slash(p) {
@@ -48,30 +48,44 @@ function normalizePath(id) {
48
48
  }
49
49
  function tryFsResolve(file) {
50
50
  const fileStat = tryStatSync(file);
51
- if (fileStat?.isFile())
51
+ if (fileStat?.isFile()) {
52
52
  return normalizePath(file);
53
- return void 0;
53
+ }
54
+ ;
55
+ return;
54
56
  }
55
57
  function resolveAbsolutePath(rootDir, id, implicitExtension) {
56
58
  const file = path.join(rootDir, id);
57
59
  return tryFsResolve(file) || implicitExtension && implicitCivet(file) || tryFsResolve(id) || implicitExtension && implicitCivet(id);
58
60
  }
59
61
  function implicitCivet(file) {
60
- if (tryFsResolve(file))
62
+ if (tryFsResolve(file)) {
61
63
  return;
64
+ }
62
65
  const civet2 = file + ".civet";
63
- if (tryFsResolve(civet2))
66
+ if (tryFsResolve(civet2)) {
64
67
  return civet2;
68
+ }
69
+ ;
65
70
  return;
66
71
  }
67
72
  var rawPlugin = (options = {}, meta) => {
68
73
  if (options.dts)
69
74
  options.emitDeclaration = options.dts;
70
- if (options.js)
71
- options.ts = "civet";
72
75
  let compileOptions = {};
76
+ let ts = options.ts;
77
+ if (options.js)
78
+ ts = "civet";
79
+ if (!(ts != null)) {
80
+ 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
+ ts = "civet";
82
+ }
83
+ if (!["civet", "esbuild", "tsc", "preserve"].includes(ts)) {
84
+ console.log(`WARNING: Invalid option ts: ${JSON.stringify(ts)}; switching to "civet"`);
85
+ ts = "civet";
86
+ }
73
87
  const transformTS = options.emitDeclaration || options.typecheck;
74
- const outExt = options.outputExtension ?? (options.ts === "preserve" ? ".tsx" : ".jsx");
88
+ const outExt = options.outputExtension ?? (ts === "preserve" ? ".tsx" : ".jsx");
75
89
  const implicitExtension = options.implicitExtension ?? true;
76
90
  let aliasResolver;
77
91
  let fsMap = /* @__PURE__ */ new Map();
@@ -81,7 +95,14 @@ var rawPlugin = (options = {}, meta) => {
81
95
  let esbuildOptions;
82
96
  let configErrors;
83
97
  let configFileNames;
84
- const tsPromise = transformTS || options.ts === "tsc" ? import("typescript").then(($) => $.default) : null;
98
+ let ref;
99
+ if (transformTS || ts === "tsc") {
100
+ ref = import("typescript").then(($1) => $1.default);
101
+ } else {
102
+ ref = void 0;
103
+ }
104
+ ;
105
+ const tsPromise = ref;
85
106
  const getFormatHost = (sys) => {
86
107
  return {
87
108
  getCurrentDirectory: () => sys.getCurrentDirectory(),
@@ -102,39 +123,40 @@ var rawPlugin = (options = {}, meta) => {
102
123
  ...compileOptions.parseOptions,
103
124
  ...options.parseOptions
104
125
  };
105
- if (transformTS || options.ts === "tsc") {
126
+ if (transformTS || ts === "tsc") {
106
127
  let mogrify = function(key) {
107
128
  if (key in config && Array.isArray(config[key])) {
108
129
  return config[key] = config[key].map((item) => {
109
- if (typeof item !== "string")
130
+ if (!(typeof item === "string")) {
110
131
  return item;
132
+ }
111
133
  return item.replace(/\.civet\b(?!\.)/g, ".civet.tsx");
112
134
  });
113
135
  }
114
136
  ;
115
137
  return;
116
138
  };
117
- const ts = await tsPromise;
118
- const tsConfigPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists);
139
+ const ts2 = await tsPromise;
140
+ const tsConfigPath = ts2.findConfigFile(process.cwd(), ts2.sys.fileExists);
119
141
  if (!tsConfigPath) {
120
142
  throw new Error("Could not find 'tsconfig.json'");
121
143
  }
122
- const { config, error } = ts.readConfigFile(
144
+ const { config, error } = ts2.readConfigFile(
123
145
  tsConfigPath,
124
- ts.sys.readFile
146
+ ts2.sys.readFile
125
147
  );
126
148
  if (error) {
127
- console.error(ts.formatDiagnostic(error, getFormatHost(ts.sys)));
149
+ console.error(ts2.formatDiagnostic(error, getFormatHost(ts2.sys)));
128
150
  throw error;
129
151
  }
130
152
  mogrify("files");
131
- const system = { ...ts.sys };
153
+ const system = { ...ts2.sys };
132
154
  const { readDirectory: systemReadDirectory } = system;
133
155
  system.readDirectory = (path2, extensions, excludes, includes, depth) => {
134
156
  extensions = [...extensions ?? [], ".civet"];
135
- return systemReadDirectory(path2, extensions, excludes, includes, depth).map(($1) => $1.endsWith(".civet") ? $1 + ".tsx" : $1);
157
+ return systemReadDirectory(path2, extensions, excludes, includes, depth).map(($2) => $2.endsWith(".civet") ? $2 + ".tsx" : $2);
136
158
  };
137
- const configContents = ts.parseJsonConfigFileContent(
159
+ const configContents = ts2.parseJsonConfigFileContent(
138
160
  config,
139
161
  system,
140
162
  process.cwd()
@@ -143,10 +165,10 @@ var rawPlugin = (options = {}, meta) => {
143
165
  configFileNames = configContents.fileNames;
144
166
  compilerOptions = {
145
167
  ...configContents.options,
146
- target: ts.ScriptTarget.ESNext,
168
+ target: ts2.ScriptTarget.ESNext,
147
169
  composite: false
148
170
  };
149
- compilerOptions.jsx ??= ts.JsxEmit.Preserve;
171
+ compilerOptions.jsx ??= ts2.JsxEmit.Preserve;
150
172
  compilerOptionsWithSourceMap = {
151
173
  ...compilerOptions,
152
174
  sourceMap: true
@@ -156,8 +178,8 @@ var rawPlugin = (options = {}, meta) => {
156
178
  },
157
179
  async buildEnd(useConfigFileNames = false) {
158
180
  if (transformTS) {
159
- const ts = await tsPromise;
160
- const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts);
181
+ const ts2 = await tsPromise;
182
+ const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts2);
161
183
  const {
162
184
  fileExists: systemFileExists,
163
185
  readFile: systemReadFile,
@@ -171,12 +193,17 @@ var rawPlugin = (options = {}, meta) => {
171
193
  return systemFileExists(filename.slice(0, -4));
172
194
  };
173
195
  system.readDirectory = (path2) => {
174
- return systemReadDirectory(path2).map(($2) => $2.endsWith(".civet") ? $2 + ".tsx" : $2);
196
+ return systemReadDirectory(path2).map(($3) => $3.endsWith(".civet") ? $3 + ".tsx" : $3);
197
+ };
198
+ const tsCompileOptions = {
199
+ ...compileOptions,
200
+ rewriteCivetImports: false,
201
+ rewriteTsImports: true
175
202
  };
176
203
  system.readFile = (filename, encoding = "utf-8") => {
177
204
  if (path.basename(filename) === "package.json") {
178
205
  let recurse = function(node) {
179
- if (node && typeof node === "object") {
206
+ if (node != null && typeof node === "object") {
180
207
  for (const key in node) {
181
208
  const value = node[key];
182
209
  if (typeof value === "string") {
@@ -191,8 +218,9 @@ var rawPlugin = (options = {}, meta) => {
191
218
  }
192
219
  };
193
220
  const json = systemReadFile(filename, encoding);
194
- if (!json)
221
+ if (!json) {
195
222
  return json;
223
+ }
196
224
  const parsed = JSON.parse(json);
197
225
  let modified = false;
198
226
  recurse(parsed.imports);
@@ -206,58 +234,58 @@ var rawPlugin = (options = {}, meta) => {
206
234
  const rawCivetSource = fs.readFileSync(civetFilename, {
207
235
  encoding
208
236
  });
209
- const compiledTS = civet.compile(rawCivetSource, {
210
- ...compileOptions,
237
+ const { code: compiledTS, sourceMap } = civet.compile(rawCivetSource, {
238
+ ...tsCompileOptions,
211
239
  filename,
212
240
  js: false,
241
+ sourceMap: true,
213
242
  sync: true
214
243
  // TS readFile API seems to need to be synchronous
215
244
  });
216
245
  fsMap.set(filename, compiledTS);
246
+ sourceMaps.set(filename, sourceMap);
217
247
  return compiledTS;
218
248
  };
219
249
  const host = tsvfs.createVirtualCompilerHost(
220
250
  system,
221
251
  compilerOptions,
222
- ts
252
+ ts2
223
253
  );
224
- const program = ts.createProgram({
254
+ const program = ts2.createProgram({
225
255
  rootNames: useConfigFileNames ? configFileNames : [...fsMap.keys()],
226
256
  options: compilerOptions,
227
257
  host: host.compilerHost
228
258
  });
229
- const diagnostics = ts.getPreEmitDiagnostics(program).map(
230
- (diagnostic) => {
231
- const file = diagnostic.file;
232
- if (!file)
233
- return diagnostic;
234
- const sourceMap = sourceMaps.get(file.fileName);
235
- if (!sourceMap)
236
- return diagnostic;
237
- const sourcemapLines = sourceMap.data.lines;
238
- const range = remapRange(
239
- {
240
- start: diagnostic.start || 0,
241
- end: (diagnostic.start || 0) + (diagnostic.length || 1)
242
- },
243
- sourcemapLines
244
- );
245
- return {
246
- ...diagnostic,
247
- messageText: flattenDiagnosticMessageText(diagnostic.messageText),
248
- length: diagnostic.length,
249
- start: range.start
250
- };
251
- }
252
- );
259
+ const diagnostics = ts2.getPreEmitDiagnostics(program).map((diagnostic) => {
260
+ const file = diagnostic.file;
261
+ if (!file)
262
+ return diagnostic;
263
+ const sourceMap = sourceMaps.get(file.fileName);
264
+ if (!sourceMap)
265
+ return diagnostic;
266
+ const sourcemapLines = sourceMap.data.lines;
267
+ const range = remapRange(
268
+ {
269
+ start: diagnostic.start || 0,
270
+ end: (diagnostic.start || 0) + (diagnostic.length || 1)
271
+ },
272
+ sourcemapLines
273
+ );
274
+ return {
275
+ ...diagnostic,
276
+ messageText: flattenDiagnosticMessageText(diagnostic.messageText),
277
+ length: diagnostic.length,
278
+ start: range.start
279
+ };
280
+ });
253
281
  if (configErrors?.length) {
254
282
  diagnostics.unshift(...configErrors);
255
283
  }
256
284
  if (diagnostics.length > 0) {
257
285
  console.error(
258
- ts.formatDiagnosticsWithColorAndContext(
286
+ ts2.formatDiagnosticsWithColorAndContext(
259
287
  diagnostics,
260
- getFormatHost(ts.sys)
288
+ getFormatHost(ts2.sys)
261
289
  )
262
290
  );
263
291
  if (options.typecheck) {
@@ -305,9 +333,9 @@ var rawPlugin = (options = {}, meta) => {
305
333
  } else {
306
334
  console.log(`WARNING: No .d.ts extension in ${filePath}`);
307
335
  }
308
- let ref;
309
- if (ref = filePath.match(civetExtension)) {
310
- const match = ref;
336
+ let ref1;
337
+ if (ref1 = filePath.match(civetExtension)) {
338
+ const match = ref1;
311
339
  filePath = filePath.slice(0, -match[0].length);
312
340
  } else {
313
341
  console.log(`WARNING: No .civet extension in ${filePath}`);
@@ -343,6 +371,7 @@ var rawPlugin = (options = {}, meta) => {
343
371
  }
344
372
  }
345
373
  },
374
+ // forceDtsEmit
346
375
  resolveId(id, importer, options2) {
347
376
  if (aliasResolver != null) {
348
377
  id = aliasResolver(id);
@@ -354,14 +383,14 @@ var rawPlugin = (options = {}, meta) => {
354
383
  }
355
384
  let postfix;
356
385
  ({ id, postfix } = cleanCivetId(id));
357
- let ref1;
386
+ let ref2;
358
387
  if (path.isAbsolute(id)) {
359
- ref1 = resolveAbsolutePath(rootDir, id, implicitExtension);
388
+ ref2 = resolveAbsolutePath(rootDir, id, implicitExtension);
360
389
  } else {
361
- ref1 = path.resolve(path.dirname(importer ?? ""), id);
390
+ ref2 = path.resolve(path.dirname(importer ?? ""), id);
362
391
  }
363
392
  ;
364
- let resolvedId = ref1;
393
+ let resolvedId = ref2;
365
394
  if (!resolvedId)
366
395
  return null;
367
396
  if (!resolvedId.endsWith(".civet")) {
@@ -382,8 +411,9 @@ var rawPlugin = (options = {}, meta) => {
382
411
  },
383
412
  async load(id) {
384
413
  const match = isCivetTranspiled.exec(id);
385
- if (!match)
414
+ if (!match) {
386
415
  return null;
416
+ }
387
417
  const basename = id.slice(0, match.index + match[1].length);
388
418
  const filename = path.resolve(rootDir, basename);
389
419
  let mtime;
@@ -397,82 +427,96 @@ var rawPlugin = (options = {}, meta) => {
397
427
  const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
398
428
  this.addWatchFile(filename);
399
429
  let compiled;
430
+ let sourceMap;
400
431
  const civetOptions = {
401
432
  ...compileOptions,
402
433
  filename: id,
403
- sourceMap: true
434
+ errors: []
404
435
  };
405
- if (options.ts === "civet" && !transformTS) {
406
- compiled = await civet.compile(rawCivetSource, {
436
+ function checkErrors() {
437
+ if (civetOptions.errors.length) {
438
+ throw new civet.ParseErrors(civetOptions.errors);
439
+ }
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, {
407
450
  ...civetOptions,
408
- js: true
451
+ js: true,
452
+ sourceMap: civetSourceMap
409
453
  });
454
+ sourceMap = civetSourceMap;
455
+ checkErrors();
410
456
  } else {
411
- const compiledTS = await civet.compile(rawCivetSource, {
457
+ const compiledTS = await civet.generate(ast, {
412
458
  ...civetOptions,
413
- js: false
459
+ js: false,
460
+ sourceMap: civetSourceMap
414
461
  });
415
- const resolved = filename + outExt;
416
- sourceMaps.set(
417
- resolved,
418
- compiledTS.sourceMap
419
- );
420
- if (transformTS) {
421
- const tsx = filename + ".tsx";
422
- fsMap.set(tsx, compiledTS.code);
423
- const slashed = slash(tsx);
424
- if (tsx !== slashed)
425
- fsMap.set(slashed, compiledTS.code);
426
- }
427
- switch (options.ts) {
462
+ checkErrors();
463
+ switch (ts) {
428
464
  case "esbuild": {
429
465
  const esbuildTransform = (await import("esbuild")).transform;
430
- const result = await esbuildTransform(compiledTS.code, {
466
+ const result = await esbuildTransform(compiledTS, {
431
467
  jsx: "preserve",
432
468
  loader: "tsx",
433
469
  sourcefile: id,
434
470
  sourcemap: "external"
435
471
  });
436
- compiled = { code: result.code, sourceMap: result.map };
472
+ compiled = result.code;
473
+ sourceMap = result.map;
437
474
  break;
438
475
  }
439
476
  case "tsc": {
440
477
  const tsTranspile = (await tsPromise).transpileModule;
441
- const result = tsTranspile(
442
- compiledTS.code,
443
- { compilerOptions: compilerOptionsWithSourceMap }
444
- );
445
- compiled = {
446
- code: result.outputText,
447
- sourceMap: result.sourceMapText
448
- };
478
+ const result = tsTranspile(compiledTS, {
479
+ compilerOptions: compilerOptionsWithSourceMap
480
+ });
481
+ compiled = result.outputText;
482
+ sourceMap = result.sourceMapText;
449
483
  break;
450
484
  }
451
485
  case "preserve": {
452
486
  compiled = compiledTS;
453
487
  break;
454
488
  }
455
- case "civet":
456
- default: {
457
- compiled = await civet.compile(rawCivetSource, {
458
- ...civetOptions,
459
- js: true
460
- });
461
- if (options.ts == void 0) {
462
- console.log(
463
- '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.'
464
- );
465
- }
466
- break;
489
+ }
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");
467
497
  }
468
498
  }
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);
512
+ }
469
513
  }
470
- const jsonSourceMap = compiled.sourceMap && (typeof compiled.sourceMap === "string" ? JSON.parse(compiled.sourceMap) : compiled.sourceMap.json(
514
+ const jsonSourceMap = sourceMap && (typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap.json(
471
515
  path.basename(id.replace(/\.[jt]sx$/, "")),
472
516
  path.basename(id)
473
517
  ));
474
518
  let transformed = {
475
- code: compiled.code,
519
+ code: compiled,
476
520
  map: jsonSourceMap
477
521
  };
478
522
  if (options.transformOutput) {
@@ -483,7 +527,7 @@ var rawPlugin = (options = {}, meta) => {
483
527
  },
484
528
  esbuild: {
485
529
  config(options2) {
486
- return esbuildOptions = options2;
530
+ esbuildOptions = options2;
487
531
  }
488
532
  },
489
533
  vite: {
@@ -530,9 +574,9 @@ var rawPlugin = (options = {}, meta) => {
530
574
  compiler.options.resolve.extensions.unshift(".civet");
531
575
  }
532
576
  return aliasResolver = (id) => {
533
- let ref2;
534
- for (const key in ref2 = compiler.options.resolve.alias) {
535
- const value = ref2[key];
577
+ let ref4;
578
+ for (const key in ref4 = compiler.options.resolve.alias) {
579
+ const value = ref4[key];
536
580
  if (key.endsWith("$")) {
537
581
  if (id === key.slice(0, -1)) {
538
582
  return typeof value === "string" ? value : "\0";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.8.11",
4
+ "version": "0.8.13",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",
@@ -95,7 +95,7 @@
95
95
  "unplugin": "^1.12.2"
96
96
  },
97
97
  "devDependencies": {
98
- "@danielx/civet": "0.8.5",
98
+ "@danielx/civet": "0.8.11",
99
99
  "@danielx/hera": "^0.8.16",
100
100
  "@prettier/sync": "^0.5.2",
101
101
  "@types/assert": "^1.5.6",