@danielx/civet 0.9.1 → 0.9.3

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
  }
@@ -378,16 +369,6 @@ var rawPlugin = (options = {}, meta) => {
378
369
  compilerOptions.outDir ?? process.cwd(),
379
370
  filePath
380
371
  );
381
- if (meta.framework === "esbuild") {
382
- fs.mkdirSync(
383
- import_path.default.dirname(
384
- import_path.default.resolve(esbuildOptions.outdir, pathFromDistDir)
385
- ),
386
- {
387
- recursive: true
388
- }
389
- );
390
- }
391
372
  return this.emitFile({
392
373
  source: content,
393
374
  fileName: pathFromDistDir,
@@ -410,11 +391,7 @@ var rawPlugin = (options = {}, meta) => {
410
391
  if (aliasResolver != null) {
411
392
  id = aliasResolver(id);
412
393
  }
413
- if (/\0/.test(id))
414
- return null;
415
- if (plugin.__virtualModulePrefix && importer?.startsWith(plugin.__virtualModulePrefix)) {
416
- importer = decodeURIComponent(importer.slice(plugin.__virtualModulePrefix.length));
417
- }
394
+ if (/\0/.test(id)) return null;
418
395
  let postfix;
419
396
  ({ id, postfix } = cleanCivetId(id));
420
397
  let ref2;
@@ -425,14 +402,11 @@ var rawPlugin = (options = {}, meta) => {
425
402
  }
426
403
  ;
427
404
  let resolvedId = ref2;
428
- if (!resolvedId)
429
- return null;
405
+ if (!resolvedId) return null;
430
406
  if (!resolvedId.endsWith(".civet")) {
431
- if (!implicitExtension)
432
- return null;
407
+ if (!implicitExtension) return null;
433
408
  const implicitId = implicitCivet(resolvedId);
434
- if (!implicitId)
435
- return null;
409
+ if (!implicitId) return null;
436
410
  resolvedId = implicitId;
437
411
  }
438
412
  if (options2.scan && meta.framework === "vite") {
@@ -450,114 +424,130 @@ var rawPlugin = (options = {}, meta) => {
450
424
  }
451
425
  const basename = id.slice(0, match.index + match[1].length);
452
426
  const filename = import_path.default.resolve(rootDir, basename);
453
- let mtime;
454
- if (cache) {
427
+ let mtime, cached, resolve;
428
+ if (cache != null) {
455
429
  mtime = (await fs.promises.stat(filename)).mtimeMs;
456
- const cached = cache?.get(filename);
430
+ cached = cache?.get(filename);
457
431
  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);
432
+ if (cached.promise) {
433
+ await cached.promise;
434
+ }
435
+ if (cached.result) {
436
+ return cached.result;
437
+ }
473
438
  }
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
439
+ const promise = new Promise((r) => {
440
+ resolve = r;
487
441
  });
488
- sourceMap = civetSourceMap;
489
- checkErrors();
490
- } else {
491
- const compiledTS = await import_civet.default.generate(ast, {
442
+ cache.set(filename, cached = { mtime, promise });
443
+ }
444
+ try {
445
+ let checkErrors = function() {
446
+ if (civetOptions.errors.length) {
447
+ throw new import_civet.default.ParseErrors(civetOptions.errors);
448
+ }
449
+ ;
450
+ return;
451
+ };
452
+ this.addWatchFile(filename);
453
+ let compiled;
454
+ let sourceMap;
455
+ const civetOptions = {
456
+ ...compileOptions,
457
+ filename: id,
458
+ errors: []
459
+ };
460
+ const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
461
+ const ast = await import_civet.default.compile(rawCivetSource, {
492
462
  ...civetOptions,
493
- js: false,
494
- sourceMap: civetSourceMap
463
+ ast: true
495
464
  });
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;
465
+ const civetSourceMap = (0, import_civet.SourceMap)(rawCivetSource);
466
+ if (ts === "civet") {
467
+ compiled = await import_civet.default.generate(ast, {
468
+ ...civetOptions,
469
+ js: true,
470
+ sourceMap: civetSourceMap
471
+ });
472
+ sourceMap = civetSourceMap;
473
+ checkErrors();
474
+ } else {
475
+ const compiledTS = await import_civet.default.generate(ast, {
476
+ ...civetOptions,
477
+ js: false,
478
+ sourceMap: civetSourceMap
479
+ });
480
+ checkErrors();
481
+ switch (ts) {
482
+ case "esbuild": {
483
+ const esbuildTransform = (await import("esbuild")).transform;
484
+ const result = await esbuildTransform(compiledTS, {
485
+ jsx: "preserve",
486
+ loader: "tsx",
487
+ sourcefile: id,
488
+ sourcemap: "external"
489
+ });
490
+ compiled = result.code;
491
+ sourceMap = result.map;
492
+ break;
493
+ }
494
+ case "tsc": {
495
+ const tsTranspile = (await tsPromise).transpileModule;
496
+ const result = tsTranspile(compiledTS, {
497
+ compilerOptions: compilerOptionsWithSourceMap
498
+ });
499
+ compiled = result.outputText;
500
+ sourceMap = result.sourceMapText;
501
+ break;
502
+ }
503
+ case "preserve": {
504
+ compiled = compiledTS;
505
+ break;
506
+ }
509
507
  }
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;
508
+ }
509
+ if (transformTS) {
510
+ for (let ref3 = import_civet.lib.gatherRecursive(ast, ($) => $.type === "ModuleSpecifier"), i = 0, len = ref3.length; i < len; i++) {
511
+ const _spec = ref3[i];
512
+ const spec = _spec;
513
+ if (spec.module?.input) {
514
+ spec.module.token = spec.module.input.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
515
+ }
518
516
  }
519
- case "preserve": {
520
- compiled = compiledTS;
521
- break;
517
+ const compiledTS = await import_civet.default.generate(ast, {
518
+ ...civetOptions,
519
+ js: false,
520
+ sourceMap: civetSourceMap
521
+ });
522
+ checkErrors();
523
+ const tsx = filename + ".tsx";
524
+ fsMap.set(tsx, compiledTS);
525
+ sourceMaps.set(tsx, civetSourceMap);
526
+ const slashed = slash(tsx);
527
+ if (!(tsx === slashed)) {
528
+ fsMap.set(slashed, compiledTS);
529
+ sourceMaps.set(slashed, civetSourceMap);
522
530
  }
523
531
  }
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
- }
532
+ const jsonSourceMap = sourceMap && (typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap.json(
533
+ import_path.default.basename(id.replace(/\.[jt]sx$/, "")),
534
+ import_path.default.basename(id)
535
+ ));
536
+ let transformed = {
537
+ code: compiled,
538
+ map: jsonSourceMap
539
+ };
540
+ if (options.transformOutput) {
541
+ transformed = await options.transformOutput(transformed.code, id);
532
542
  }
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);
543
+ if (cached != null) {
544
+ cached.result = transformed;
545
+ delete cached.promise;
546
546
  }
547
+ return transformed;
548
+ } finally {
549
+ resolve?.();
547
550
  }
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
551
  },
562
552
  esbuild: {
563
553
  config(options2) {
@@ -605,6 +595,9 @@ var rawPlugin = (options = {}, meta) => {
605
595
  },
606
596
  webpack(compiler) {
607
597
  if (implicitExtension) {
598
+ compiler.options ??= {};
599
+ compiler.options.resolve ??= {};
600
+ compiler.options.resolve.extensions ??= ["", ".js", ".json", ".wasm"];
608
601
  compiler.options.resolve.extensions.unshift(".civet");
609
602
  }
610
603
  return aliasResolver = (id) => {