@danielx/civet 0.6.37 → 0.6.39

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/vite.js CHANGED
@@ -37,6 +37,7 @@ module.exports = __toCommonJS(vite_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
  },
package/dist/webpack.js CHANGED
@@ -37,6 +37,7 @@ module.exports = __toCommonJS(webpack_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
  },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.6.37",
4
+ "version": "0.6.39",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",
@@ -36,6 +36,10 @@
36
36
  "require": "./dist/esbuild.js",
37
37
  "import": "./dist/esbuild.mjs"
38
38
  },
39
+ "./ts-diagnostic": {
40
+ "require": "./dist/ts-diagnostic.js",
41
+ "import": "./dist/ts-diagnostic.mjs"
42
+ },
39
43
  "./*": "./*",
40
44
  "./dist/*": "./dist/*"
41
45
  },
@@ -66,7 +70,7 @@
66
70
  "unplugin": "^1.4.0"
67
71
  },
68
72
  "devDependencies": {
69
- "@danielx/civet": "0.6.34",
73
+ "@danielx/civet": "0.6.38",
70
74
  "@danielx/hera": "^0.8.10",
71
75
  "@types/assert": "^1.5.6",
72
76
  "@types/mocha": "^9.1.1",
@@ -84,6 +88,8 @@
84
88
  "typescript": "^5.2.2",
85
89
  "vite": "^4.4.9",
86
90
  "vitepress": "^1.0.0-alpha.35",
91
+ "vscode-languageserver": "^8.1.0",
92
+ "vscode-languageserver-textdocument": "^1.0.8",
87
93
  "vue": "^3.2.45"
88
94
  },
89
95
  "peerDependencies": {