@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/dist/rollup.js CHANGED
@@ -37,6 +37,7 @@ module.exports = __toCommonJS(rollup_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
  },
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var ts_diagnostic_exports = {};
30
+ __export(ts_diagnostic_exports, {
31
+ convertDiagnostic: () => convertDiagnostic,
32
+ flattenDiagnosticMessageText: () => flattenDiagnosticMessageText,
33
+ rangeFromTextSpan: () => rangeFromTextSpan,
34
+ remapPosition: () => remapPosition,
35
+ remapRange: () => remapRange
36
+ });
37
+ module.exports = __toCommonJS(ts_diagnostic_exports);
38
+ var import_typescript = __toESM(require("typescript"));
39
+ var import_vscode_languageserver = __toESM(require("vscode-languageserver"));
40
+ var import_vscode_languageserver_textdocument = require("vscode-languageserver-textdocument");
41
+ const { DiagnosticCategory } = import_typescript.default;
42
+ function remapPosition(position, sourcemapLines) {
43
+ if (!sourcemapLines)
44
+ return position;
45
+ const { line, character } = position;
46
+ const textLine = sourcemapLines[line];
47
+ if (!textLine?.length)
48
+ return position;
49
+ let i = 0, p = 0, l = textLine.length, lastMapping, lastMappingPosition = 0;
50
+ while (i < l) {
51
+ const mapping = textLine[i];
52
+ p += mapping[0];
53
+ if (mapping.length === 4) {
54
+ lastMapping = mapping;
55
+ lastMappingPosition = p;
56
+ }
57
+ if (p >= character) {
58
+ break;
59
+ }
60
+ i++;
61
+ }
62
+ if (lastMapping) {
63
+ const srcLine = lastMapping[2];
64
+ const srcChar = lastMapping[3];
65
+ const newChar = srcChar + character - lastMappingPosition;
66
+ return {
67
+ line: srcLine,
68
+ character: newChar
69
+ };
70
+ } else {
71
+ return position;
72
+ }
73
+ }
74
+ function remapRange(range, sourcemapLines) {
75
+ return {
76
+ start: remapPosition(range.start, sourcemapLines),
77
+ end: remapPosition(range.end, sourcemapLines)
78
+ };
79
+ }
80
+ function flattenDiagnosticMessageText(diag, indent = 0) {
81
+ if (typeof diag === "string") {
82
+ return diag;
83
+ } else if (diag === void 0) {
84
+ return "";
85
+ }
86
+ let result = "";
87
+ if (indent) {
88
+ result += "\n";
89
+ for (let i = 0; i < indent; i++) {
90
+ result += " ";
91
+ }
92
+ }
93
+ result += diag.messageText;
94
+ indent++;
95
+ if (diag.next) {
96
+ for (const kid of diag.next) {
97
+ result += flattenDiagnosticMessageText(kid, indent);
98
+ }
99
+ }
100
+ return result;
101
+ }
102
+ function rangeFromTextSpan(span, document) {
103
+ return {
104
+ start: document.positionAt(span.start),
105
+ end: document.positionAt(span.start + span.length)
106
+ };
107
+ }
108
+ function convertDiagnostic(diagnostic, document, sourcemapLines) {
109
+ return {
110
+ message: flattenDiagnosticMessageText(diagnostic.messageText),
111
+ range: remapRange(
112
+ rangeFromTextSpan(
113
+ {
114
+ start: diagnostic.start || 0,
115
+ length: diagnostic.length ?? 1
116
+ },
117
+ document
118
+ ),
119
+ sourcemapLines
120
+ ),
121
+ severity: diagnosticCategoryToSeverity(diagnostic.category),
122
+ code: diagnostic.code,
123
+ source: diagnostic.source || "typescript"
124
+ };
125
+ }
126
+ function diagnosticCategoryToSeverity(category) {
127
+ switch (category) {
128
+ case DiagnosticCategory.Warning: {
129
+ return import_vscode_languageserver.DiagnosticSeverity.Warning;
130
+ }
131
+ case DiagnosticCategory.Error: {
132
+ return import_vscode_languageserver.DiagnosticSeverity.Error;
133
+ }
134
+ case DiagnosticCategory.Suggestion: {
135
+ return import_vscode_languageserver.DiagnosticSeverity.Hint;
136
+ }
137
+ case DiagnosticCategory.Message: {
138
+ return import_vscode_languageserver.DiagnosticSeverity.Information;
139
+ }
140
+ }
141
+ }
142
+ // Annotate the CommonJS export names for ESM import in node:
143
+ 0 && (module.exports = {
144
+ convertDiagnostic,
145
+ flattenDiagnosticMessageText,
146
+ rangeFromTextSpan,
147
+ remapPosition,
148
+ remapRange
149
+ });
@@ -0,0 +1,111 @@
1
+ import ts from "typescript";
2
+ import vs, { DiagnosticSeverity, Position, Range } from "vscode-languageserver";
3
+ import { TextDocument } from "vscode-languageserver-textdocument";
4
+ const { DiagnosticCategory } = ts;
5
+ function remapPosition(position, sourcemapLines) {
6
+ if (!sourcemapLines)
7
+ return position;
8
+ const { line, character } = position;
9
+ const textLine = sourcemapLines[line];
10
+ if (!textLine?.length)
11
+ return position;
12
+ let i = 0, p = 0, l = textLine.length, lastMapping, lastMappingPosition = 0;
13
+ while (i < l) {
14
+ const mapping = textLine[i];
15
+ p += mapping[0];
16
+ if (mapping.length === 4) {
17
+ lastMapping = mapping;
18
+ lastMappingPosition = p;
19
+ }
20
+ if (p >= character) {
21
+ break;
22
+ }
23
+ i++;
24
+ }
25
+ if (lastMapping) {
26
+ const srcLine = lastMapping[2];
27
+ const srcChar = lastMapping[3];
28
+ const newChar = srcChar + character - lastMappingPosition;
29
+ return {
30
+ line: srcLine,
31
+ character: newChar
32
+ };
33
+ } else {
34
+ return position;
35
+ }
36
+ }
37
+ function remapRange(range, sourcemapLines) {
38
+ return {
39
+ start: remapPosition(range.start, sourcemapLines),
40
+ end: remapPosition(range.end, sourcemapLines)
41
+ };
42
+ }
43
+ function flattenDiagnosticMessageText(diag, indent = 0) {
44
+ if (typeof diag === "string") {
45
+ return diag;
46
+ } else if (diag === void 0) {
47
+ return "";
48
+ }
49
+ let result = "";
50
+ if (indent) {
51
+ result += "\n";
52
+ for (let i = 0; i < indent; i++) {
53
+ result += " ";
54
+ }
55
+ }
56
+ result += diag.messageText;
57
+ indent++;
58
+ if (diag.next) {
59
+ for (const kid of diag.next) {
60
+ result += flattenDiagnosticMessageText(kid, indent);
61
+ }
62
+ }
63
+ return result;
64
+ }
65
+ function rangeFromTextSpan(span, document) {
66
+ return {
67
+ start: document.positionAt(span.start),
68
+ end: document.positionAt(span.start + span.length)
69
+ };
70
+ }
71
+ function convertDiagnostic(diagnostic, document, sourcemapLines) {
72
+ return {
73
+ message: flattenDiagnosticMessageText(diagnostic.messageText),
74
+ range: remapRange(
75
+ rangeFromTextSpan(
76
+ {
77
+ start: diagnostic.start || 0,
78
+ length: diagnostic.length ?? 1
79
+ },
80
+ document
81
+ ),
82
+ sourcemapLines
83
+ ),
84
+ severity: diagnosticCategoryToSeverity(diagnostic.category),
85
+ code: diagnostic.code,
86
+ source: diagnostic.source || "typescript"
87
+ };
88
+ }
89
+ function diagnosticCategoryToSeverity(category) {
90
+ switch (category) {
91
+ case DiagnosticCategory.Warning: {
92
+ return DiagnosticSeverity.Warning;
93
+ }
94
+ case DiagnosticCategory.Error: {
95
+ return DiagnosticSeverity.Error;
96
+ }
97
+ case DiagnosticCategory.Suggestion: {
98
+ return DiagnosticSeverity.Hint;
99
+ }
100
+ case DiagnosticCategory.Message: {
101
+ return DiagnosticSeverity.Information;
102
+ }
103
+ }
104
+ }
105
+ export {
106
+ convertDiagnostic,
107
+ flattenDiagnosticMessageText,
108
+ rangeFromTextSpan,
109
+ remapPosition,
110
+ remapRange
111
+ };
@@ -1,6 +1,12 @@
1
1
  // src/index.ts
2
- import { createUnplugin } from "unplugin";
2
+ import {
3
+ createUnplugin
4
+ } from "unplugin";
3
5
  import civet from "@danielx/civet";
6
+ import {
7
+ remapRange,
8
+ flattenDiagnosticMessageText
9
+ } from "@danielx/civet/ts-diagnostic";
4
10
  import * as fs from "fs";
5
11
  import path from "path";
6
12
  import ts from "typescript";
@@ -16,15 +22,19 @@ var civetUnplugin = createUnplugin((options = {}) => {
16
22
  if (options.dts && options.js) {
17
23
  throw new Error("Can't have both `dts` and `js` be set to `true`.");
18
24
  }
19
- const transpileToJS = options.js ?? !options.dts;
25
+ if (options.typecheck && options.js) {
26
+ throw new Error("Can't have both `typecheck` and `js` be set to `true`.");
27
+ }
28
+ const transpileToJS = options.js ?? !(options.dts || options.typecheck);
20
29
  const outExt = options.outputExtension ?? (transpileToJS ? ".jsx" : ".tsx");
21
30
  let fsMap = /* @__PURE__ */ new Map();
31
+ const sourceMaps = /* @__PURE__ */ new Map();
22
32
  let compilerOptions;
23
33
  return {
24
34
  name: "unplugin-civet",
25
35
  enforce: "pre",
26
36
  async buildStart() {
27
- if (options.dts) {
37
+ if (options.dts || options.typecheck) {
28
38
  const configPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists);
29
39
  if (!configPath) {
30
40
  throw new Error("Could not find 'tsconfig.json'");
@@ -50,7 +60,7 @@ var civetUnplugin = createUnplugin((options = {}) => {
50
60
  }
51
61
  },
52
62
  buildEnd() {
53
- if (options.dts) {
63
+ if (options.dts || options.typecheck) {
54
64
  const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts);
55
65
  const host = tsvfs.createVirtualCompilerHost(
56
66
  system,
@@ -62,20 +72,55 @@ var civetUnplugin = createUnplugin((options = {}) => {
62
72
  options: compilerOptions,
63
73
  host: host.compilerHost
64
74
  });
65
- for (const file of fsMap.keys()) {
66
- const sourceFile = program.getSourceFile(file);
67
- program.emit(
68
- sourceFile,
69
- (filePath, content) => {
70
- this.emitFile({
71
- source: content,
72
- fileName: path.relative(process.cwd(), filePath),
73
- type: "asset"
74
- });
75
+ const diagnostics = ts.getPreEmitDiagnostics(program).map((diagnostic) => {
76
+ const file = diagnostic.file;
77
+ if (!file)
78
+ return diagnostic;
79
+ const sourceMap = sourceMaps.get(file.fileName);
80
+ if (!sourceMap)
81
+ return diagnostic;
82
+ const sourcemapLines = sourceMap.data.lines;
83
+ const range = remapRange(
84
+ {
85
+ start: diagnostic.start || 0,
86
+ end: (diagnostic.start || 0) + (diagnostic.length || 1)
75
87
  },
76
- void 0,
77
- true
88
+ sourcemapLines
78
89
  );
90
+ return {
91
+ ...diagnostic,
92
+ messageText: flattenDiagnosticMessageText(diagnostic.messageText),
93
+ length: diagnostic.length,
94
+ start: range.start
95
+ };
96
+ });
97
+ if (diagnostics.length > 0) {
98
+ console.error(
99
+ ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost)
100
+ );
101
+ }
102
+ if (options.dts) {
103
+ for (const file of fsMap.keys()) {
104
+ const sourceFile = program.getSourceFile(file);
105
+ program.emit(
106
+ sourceFile,
107
+ async (filePath, content) => {
108
+ const dir = path.dirname(filePath);
109
+ await fs.promises.mkdir(dir, { recursive: true });
110
+ const pathFromDistDir = path.relative(
111
+ compilerOptions.outDir ?? process.cwd(),
112
+ filePath
113
+ );
114
+ this.emitFile({
115
+ source: content,
116
+ fileName: pathFromDistDir,
117
+ type: "asset"
118
+ });
119
+ },
120
+ void 0,
121
+ true
122
+ );
123
+ }
79
124
  }
80
125
  }
81
126
  },
@@ -99,13 +144,20 @@ var civetUnplugin = createUnplugin((options = {}) => {
99
144
  return null;
100
145
  const filename = path.resolve(process.cwd(), id.slice(0, -outExt.length));
101
146
  const code = await fs.promises.readFile(filename, "utf-8");
147
+ const compiled = civet.compile(code, {
148
+ // inlineMap: true,
149
+ filename: id,
150
+ js: transpileToJS,
151
+ sourceMap: true
152
+ });
153
+ sourceMaps.set(path.resolve(process.cwd(), id), compiled.sourceMap);
154
+ const jsonSourceMap = compiled.sourceMap.json(
155
+ path.basename(id.replace(/\.[jt]sx$/, "")),
156
+ path.basename(id)
157
+ );
102
158
  let transformed = {
103
- code: civet.compile(code, {
104
- inlineMap: true,
105
- filename: id,
106
- js: transpileToJS
107
- }),
108
- map: null
159
+ code: compiled.code,
160
+ map: jsonSourceMap
109
161
  };
110
162
  if (options.transformOutput)
111
163
  transformed = await options.transformOutput(transformed.code, id);
@@ -114,8 +166,12 @@ var civetUnplugin = createUnplugin((options = {}) => {
114
166
  transform(code, id) {
115
167
  if (!/\.civet\.tsx?$/.test(id))
116
168
  return null;
117
- if (options.dts) {
118
- fsMap.set(path.resolve(process.cwd(), id), code);
169
+ if (options.dts || options.typecheck) {
170
+ const resolved = path.resolve(process.cwd(), id);
171
+ fsMap.set(resolved, code);
172
+ const slash = resolved.replace(/\\/g, "/");
173
+ if (resolved !== slash)
174
+ fsMap.set(slash, code);
119
175
  }
120
176
  return null;
121
177
  },
@@ -6,9 +6,11 @@ type PluginOptions = {
6
6
  transformOutput?: (code: string, id: string) => TransformResult | Promise<TransformResult>;
7
7
  } & ({
8
8
  dts?: false;
9
+ typecheck?: false;
9
10
  js?: false | true;
10
11
  } | {
11
12
  dts?: true;
13
+ typecheck?: true;
12
14
  js?: false;
13
15
  });
14
16
  declare const civetUnplugin: unplugin.UnpluginInstance<PluginOptions, boolean>;
@@ -6,9 +6,11 @@ type PluginOptions = {
6
6
  transformOutput?: (code: string, id: string) => TransformResult | Promise<TransformResult>;
7
7
  } & ({
8
8
  dts?: false;
9
+ typecheck?: false;
9
10
  js?: false | true;
10
11
  } | {
11
12
  dts?: true;
13
+ typecheck?: true;
12
14
  js?: false;
13
15
  });
14
16
  declare const civetUnplugin: unplugin.UnpluginInstance<PluginOptions, boolean>;