@danielx/civet 0.9.1 → 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.
@@ -0,0 +1,29 @@
1
+ // unplugin-civet:C:\Users\edemaine\Projects\Civet\source\node-worker.civet.jsx
2
+ import { parentPort } from "node:worker_threads";
3
+ import module from "node:module";
4
+ try {
5
+ module.enableCompileCache();
6
+ } catch (e) {
7
+ }
8
+ (async () => {
9
+ {
10
+ const { compile } = await import("./main.mjs");
11
+ return parentPort.on("message", async ({ id, op, args }) => {
12
+ try {
13
+ let result;
14
+ switch (op) {
15
+ case "compile": {
16
+ result = await compile(...args);
17
+ break;
18
+ }
19
+ default: {
20
+ throw `Unknown operation: ${op}`;
21
+ }
22
+ }
23
+ return parentPort.postMessage({ id, result });
24
+ } catch (error) {
25
+ return parentPort.postMessage({ id, error });
26
+ }
27
+ });
28
+ }
29
+ })();
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -16,20 +15,18 @@ var __copyProps = (to, from, except, desc) => {
16
15
  return to;
17
16
  };
18
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var ts_diagnostic_exports = {};
20
- __export(ts_diagnostic_exports, {
18
+ var ts_diagnostic_civet_exports = {};
19
+ __export(ts_diagnostic_civet_exports, {
21
20
  flattenDiagnosticMessageText: () => flattenDiagnosticMessageText,
22
21
  remapPosition: () => remapPosition,
23
22
  remapRange: () => remapRange
24
23
  });
25
- module.exports = __toCommonJS(ts_diagnostic_exports);
24
+ module.exports = __toCommonJS(ts_diagnostic_civet_exports);
26
25
  function remapPosition(position, sourcemapLines) {
27
- if (!sourcemapLines)
28
- return position;
26
+ if (!sourcemapLines) return position;
29
27
  const { line, character } = position;
30
28
  const textLine = sourcemapLines[line];
31
- if (!textLine?.length)
32
- return position;
29
+ if (!textLine?.length) return position;
33
30
  let i = 0, p = 0, l = textLine.length, lastMapping, lastMappingPosition = 0;
34
31
  while (i < l) {
35
32
  const mapping = textLine[i];
@@ -1,10 +1,8 @@
1
1
  function remapPosition(position, sourcemapLines) {
2
- if (!sourcemapLines)
3
- return position;
2
+ if (!sourcemapLines) return position;
4
3
  const { line, character } = position;
5
4
  const textLine = sourcemapLines[line];
6
- if (!textLine?.length)
7
- return position;
5
+ if (!textLine?.length) return position;
8
6
  let i = 0, p = 0, l = textLine.length, lastMapping, lastMappingPosition = 0;
9
7
  while (i < l) {
10
8
  const mapping = textLine[i];
package/dist/types.d.ts CHANGED
@@ -49,6 +49,10 @@ declare module "@danielx/civet" {
49
49
  hits?: string
50
50
  trace?: string
51
51
  parseOptions?: ParseOptions
52
+ /** Specifying an empty array will prevent ParseErrors from being thrown */
53
+ errors?: ParseError[]
54
+ /** Number of parallel threads to compile with (Node only) */
55
+ threads?: number
52
56
  }
53
57
  export type GenerateOptions = Omit<CompileOptions, "sourceMap"> & {
54
58
  sourceMap?: undefined | SourceMap
@@ -12,6 +12,8 @@ export type PluginOptions = {
12
12
  js?: boolean;
13
13
  /** @deprecated Use "emitDeclaration" instead */
14
14
  dts?: boolean;
15
+ /** Number of parallel threads to compile with (Node only) */
16
+ threads?: number;
15
17
  /** Cache compilation results based on file mtime (useful for serve or watch mode) */
16
18
  cache?: boolean;
17
19
  /** config filename, or null to not look for default config file */
@@ -53,7 +53,7 @@ DiagnosticCategory[DiagnosticCategory["Error"] = 1] = "Error";
53
53
  DiagnosticCategory[DiagnosticCategory["Suggestion"] = 2] = "Suggestion";
54
54
  DiagnosticCategory[DiagnosticCategory["Message"] = 3] = "Message";
55
55
  var civetExtension = /\.civet$/;
56
- var isCivetTranspiled = /(\.civet)(\.[jt]sx)([?#].*)?$/;
56
+ var isCivetTranspiled = /(\.civet)(\.[jt]sx)?([?#].*)?$/;
57
57
  var postfixRE = /[?#].*$/s;
58
58
  var isWindows = import_os.default.platform() === "win32";
59
59
  var windowsSlashRE = /\\/g;
@@ -102,12 +102,10 @@ function implicitCivet(file) {
102
102
  return;
103
103
  }
104
104
  var rawPlugin = (options = {}, meta) => {
105
- if (options.dts)
106
- options.emitDeclaration = options.dts;
105
+ if (options.dts) options.emitDeclaration = options.dts;
107
106
  let compileOptions = {};
108
107
  let ts = options.ts;
109
- if (options.js)
110
- ts = "civet";
108
+ if (options.js) ts = "civet";
111
109
  if (!(ts != null)) {
112
110
  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.');
113
111
  ts = "civet";
@@ -142,7 +140,7 @@ var rawPlugin = (options = {}, meta) => {
142
140
  getCanonicalFileName: sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
143
141
  };
144
142
  };
145
- const cache = options.cache ? /* @__PURE__ */ new Map() : void 0;
143
+ const cache = !(options.cache === false) ? /* @__PURE__ */ new Map() : void 0;
146
144
  const plugin = {
147
145
  name: "unplugin-civet",
148
146
  enforce: "pre",
@@ -155,6 +153,9 @@ var rawPlugin = (options = {}, meta) => {
155
153
  ...compileOptions.parseOptions,
156
154
  ...options.parseOptions
157
155
  };
156
+ if (options.threads != null) {
157
+ compileOptions.threads = options.threads;
158
+ }
158
159
  if (transformTS || ts === "tsc") {
159
160
  let mogrify = function(key) {
160
161
  if (key in config && Array.isArray(config[key])) {
@@ -218,10 +219,8 @@ var rawPlugin = (options = {}, meta) => {
218
219
  readDirectory: systemReadDirectory
219
220
  } = system;
220
221
  system.fileExists = (filename) => {
221
- if (!filename.endsWith(".civet.tsx"))
222
- return systemFileExists(filename);
223
- if (fsMap.has(filename))
224
- return true;
222
+ if (!filename.endsWith(".civet.tsx")) return systemFileExists(filename);
223
+ if (fsMap.has(filename)) return true;
225
224
  return systemFileExists(filename.slice(0, -4));
226
225
  };
227
226
  system.readDirectory = (path2) => {
@@ -258,10 +257,8 @@ var rawPlugin = (options = {}, meta) => {
258
257
  recurse(parsed.imports);
259
258
  return modified ? JSON.stringify(parsed) : json;
260
259
  }
261
- if (!filename.endsWith(".civet.tsx"))
262
- return systemReadFile(filename, encoding);
263
- if (fsMap.has(filename))
264
- return fsMap.get(filename);
260
+ if (!filename.endsWith(".civet.tsx")) return systemReadFile(filename, encoding);
261
+ if (fsMap.has(filename)) return fsMap.get(filename);
265
262
  const civetFilename = filename.slice(0, -4);
266
263
  const rawCivetSource = fs.readFileSync(civetFilename, {
267
264
  encoding
@@ -290,11 +287,9 @@ var rawPlugin = (options = {}, meta) => {
290
287
  });
291
288
  const diagnostics = ts2.getPreEmitDiagnostics(program).map((diagnostic) => {
292
289
  const file = diagnostic.file;
293
- if (!file)
294
- return diagnostic;
290
+ if (!file) return diagnostic;
295
291
  const sourceMap = sourceMaps.get(file.fileName);
296
- if (!sourceMap)
297
- return diagnostic;
292
+ if (!sourceMap) return diagnostic;
298
293
  const sourcemapLines = sourceMap.data.lines;
299
294
  const range = (0, import_ts_diagnostic.remapRange)(
300
295
  {
@@ -323,14 +318,10 @@ var rawPlugin = (options = {}, meta) => {
323
318
  if (options.typecheck) {
324
319
  let failures = [];
325
320
  if (typeof options.typecheck === "string") {
326
- if (options.typecheck.includes("error"))
327
- failures.push(DiagnosticCategory.Error);
328
- if (options.typecheck.includes("warning"))
329
- failures.push(DiagnosticCategory.Warning);
330
- if (options.typecheck.includes("suggestion"))
331
- failures.push(DiagnosticCategory.Suggestion);
332
- if (options.typecheck.includes("message"))
333
- failures.push(DiagnosticCategory.Message);
321
+ if (options.typecheck.includes("error")) failures.push(DiagnosticCategory.Error);
322
+ if (options.typecheck.includes("warning")) failures.push(DiagnosticCategory.Warning);
323
+ if (options.typecheck.includes("suggestion")) failures.push(DiagnosticCategory.Suggestion);
324
+ if (options.typecheck.includes("message")) failures.push(DiagnosticCategory.Message);
334
325
  if (options.typecheck.includes("all")) {
335
326
  failures = { includes: () => true };
336
327
  }
@@ -410,8 +401,7 @@ var rawPlugin = (options = {}, meta) => {
410
401
  if (aliasResolver != null) {
411
402
  id = aliasResolver(id);
412
403
  }
413
- if (/\0/.test(id))
414
- return null;
404
+ if (/\0/.test(id)) return null;
415
405
  if (plugin.__virtualModulePrefix && importer?.startsWith(plugin.__virtualModulePrefix)) {
416
406
  importer = decodeURIComponent(importer.slice(plugin.__virtualModulePrefix.length));
417
407
  }
@@ -425,14 +415,11 @@ var rawPlugin = (options = {}, meta) => {
425
415
  }
426
416
  ;
427
417
  let resolvedId = ref2;
428
- if (!resolvedId)
429
- return null;
418
+ if (!resolvedId) return null;
430
419
  if (!resolvedId.endsWith(".civet")) {
431
- if (!implicitExtension)
432
- return null;
420
+ if (!implicitExtension) return null;
433
421
  const implicitId = implicitCivet(resolvedId);
434
- if (!implicitId)
435
- return null;
422
+ if (!implicitId) return null;
436
423
  resolvedId = implicitId;
437
424
  }
438
425
  if (options2.scan && meta.framework === "vite") {
@@ -450,114 +437,130 @@ var rawPlugin = (options = {}, meta) => {
450
437
  }
451
438
  const basename = id.slice(0, match.index + match[1].length);
452
439
  const filename = import_path.default.resolve(rootDir, basename);
453
- let mtime;
454
- if (cache) {
440
+ let mtime, cached, resolve;
441
+ if (cache != null) {
455
442
  mtime = (await fs.promises.stat(filename)).mtimeMs;
456
- const cached = cache?.get(filename);
443
+ cached = cache?.get(filename);
457
444
  if (cached && cached.mtime === mtime) {
458
- return cached.result;
459
- }
460
- }
461
- const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
462
- this.addWatchFile(filename);
463
- let compiled;
464
- let sourceMap;
465
- const civetOptions = {
466
- ...compileOptions,
467
- filename: id,
468
- errors: []
469
- };
470
- function checkErrors() {
471
- if (civetOptions.errors.length) {
472
- throw new import_civet.default.ParseErrors(civetOptions.errors);
445
+ if (cached.promise) {
446
+ await cached.promise;
447
+ }
448
+ if (cached.result) {
449
+ return cached.result;
450
+ }
473
451
  }
474
- ;
475
- return;
476
- }
477
- const ast = await import_civet.default.compile(rawCivetSource, {
478
- ...civetOptions,
479
- ast: true
480
- });
481
- const civetSourceMap = (0, import_civet.SourceMap)(rawCivetSource);
482
- if (ts === "civet") {
483
- compiled = await import_civet.default.generate(ast, {
484
- ...civetOptions,
485
- js: true,
486
- sourceMap: civetSourceMap
452
+ const promise = new Promise((r) => {
453
+ resolve = r;
487
454
  });
488
- sourceMap = civetSourceMap;
489
- checkErrors();
490
- } else {
491
- const compiledTS = await import_civet.default.generate(ast, {
455
+ cache.set(filename, cached = { mtime, promise });
456
+ }
457
+ try {
458
+ let checkErrors = function() {
459
+ if (civetOptions.errors.length) {
460
+ throw new import_civet.default.ParseErrors(civetOptions.errors);
461
+ }
462
+ ;
463
+ return;
464
+ };
465
+ this.addWatchFile(filename);
466
+ let compiled;
467
+ let sourceMap;
468
+ const civetOptions = {
469
+ ...compileOptions,
470
+ filename: id,
471
+ errors: []
472
+ };
473
+ const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
474
+ const ast = await import_civet.default.compile(rawCivetSource, {
492
475
  ...civetOptions,
493
- js: false,
494
- sourceMap: civetSourceMap
476
+ ast: true
495
477
  });
496
- checkErrors();
497
- switch (ts) {
498
- case "esbuild": {
499
- const esbuildTransform = (await import("esbuild")).transform;
500
- const result = await esbuildTransform(compiledTS, {
501
- jsx: "preserve",
502
- loader: "tsx",
503
- sourcefile: id,
504
- sourcemap: "external"
505
- });
506
- compiled = result.code;
507
- sourceMap = result.map;
508
- break;
478
+ const civetSourceMap = (0, import_civet.SourceMap)(rawCivetSource);
479
+ if (ts === "civet") {
480
+ compiled = await import_civet.default.generate(ast, {
481
+ ...civetOptions,
482
+ js: true,
483
+ sourceMap: civetSourceMap
484
+ });
485
+ sourceMap = civetSourceMap;
486
+ checkErrors();
487
+ } else {
488
+ const compiledTS = await import_civet.default.generate(ast, {
489
+ ...civetOptions,
490
+ js: false,
491
+ sourceMap: civetSourceMap
492
+ });
493
+ checkErrors();
494
+ switch (ts) {
495
+ case "esbuild": {
496
+ const esbuildTransform = (await import("esbuild")).transform;
497
+ const result = await esbuildTransform(compiledTS, {
498
+ jsx: "preserve",
499
+ loader: "tsx",
500
+ sourcefile: id,
501
+ sourcemap: "external"
502
+ });
503
+ compiled = result.code;
504
+ sourceMap = result.map;
505
+ break;
506
+ }
507
+ case "tsc": {
508
+ const tsTranspile = (await tsPromise).transpileModule;
509
+ const result = tsTranspile(compiledTS, {
510
+ compilerOptions: compilerOptionsWithSourceMap
511
+ });
512
+ compiled = result.outputText;
513
+ sourceMap = result.sourceMapText;
514
+ break;
515
+ }
516
+ case "preserve": {
517
+ compiled = compiledTS;
518
+ break;
519
+ }
509
520
  }
510
- case "tsc": {
511
- const tsTranspile = (await tsPromise).transpileModule;
512
- const result = tsTranspile(compiledTS, {
513
- compilerOptions: compilerOptionsWithSourceMap
514
- });
515
- compiled = result.outputText;
516
- sourceMap = result.sourceMapText;
517
- break;
521
+ }
522
+ if (transformTS) {
523
+ for (let ref3 = import_civet.lib.gatherRecursive(ast, ($) => $.type === "ModuleSpecifier"), i = 0, len = ref3.length; i < len; i++) {
524
+ const _spec = ref3[i];
525
+ const spec = _spec;
526
+ if (spec.module?.input) {
527
+ spec.module.token = spec.module.input.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
528
+ }
518
529
  }
519
- case "preserve": {
520
- compiled = compiledTS;
521
- break;
530
+ const compiledTS = await import_civet.default.generate(ast, {
531
+ ...civetOptions,
532
+ js: false,
533
+ sourceMap: civetSourceMap
534
+ });
535
+ checkErrors();
536
+ const tsx = filename + ".tsx";
537
+ fsMap.set(tsx, compiledTS);
538
+ sourceMaps.set(tsx, civetSourceMap);
539
+ const slashed = slash(tsx);
540
+ if (!(tsx === slashed)) {
541
+ fsMap.set(slashed, compiledTS);
542
+ sourceMaps.set(slashed, civetSourceMap);
522
543
  }
523
544
  }
524
- }
525
- if (transformTS) {
526
- for (let ref3 = import_civet.lib.gatherRecursive(ast, ($) => $.type === "ModuleSpecifier"), i = 0, len = ref3.length; i < len; i++) {
527
- const _spec = ref3[i];
528
- const spec = _spec;
529
- if (spec.module?.input) {
530
- spec.module.token = spec.module.input.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
531
- }
545
+ const jsonSourceMap = sourceMap && (typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap.json(
546
+ import_path.default.basename(id.replace(/\.[jt]sx$/, "")),
547
+ import_path.default.basename(id)
548
+ ));
549
+ let transformed = {
550
+ code: compiled,
551
+ map: jsonSourceMap
552
+ };
553
+ if (options.transformOutput) {
554
+ transformed = await options.transformOutput(transformed.code, id);
532
555
  }
533
- const compiledTS = await import_civet.default.generate(ast, {
534
- ...civetOptions,
535
- js: false,
536
- sourceMap: civetSourceMap
537
- });
538
- checkErrors();
539
- const tsx = filename + ".tsx";
540
- fsMap.set(tsx, compiledTS);
541
- sourceMaps.set(tsx, civetSourceMap);
542
- const slashed = slash(tsx);
543
- if (!(tsx === slashed)) {
544
- fsMap.set(slashed, compiledTS);
545
- sourceMaps.set(slashed, civetSourceMap);
556
+ if (cached != null) {
557
+ cached.result = transformed;
558
+ delete cached.promise;
546
559
  }
560
+ return transformed;
561
+ } finally {
562
+ resolve?.();
547
563
  }
548
- const jsonSourceMap = sourceMap && (typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap.json(
549
- import_path.default.basename(id.replace(/\.[jt]sx$/, "")),
550
- import_path.default.basename(id)
551
- ));
552
- let transformed = {
553
- code: compiled,
554
- map: jsonSourceMap
555
- };
556
- if (options.transformOutput) {
557
- transformed = await options.transformOutput(transformed.code, id);
558
- }
559
- cache?.set(filename, { mtime, result: transformed });
560
- return transformed;
561
564
  },
562
565
  esbuild: {
563
566
  config(options2) {