@danielx/civet 0.6.52 → 0.6.53

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
@@ -40,22 +40,15 @@ var import_civet = __toESM(require("@danielx/civet"));
40
40
  var import_ts_diagnostic = require("@danielx/civet/ts-diagnostic");
41
41
  var fs = __toESM(require("fs"));
42
42
  var import_path = __toESM(require("path"));
43
- var import_typescript = __toESM(require("typescript"));
44
43
  var tsvfs = __toESM(require("@typescript/vfs"));
45
44
  var import_os = __toESM(require("os"));
46
- var formatHost = {
47
- getCurrentDirectory: () => import_typescript.default.sys.getCurrentDirectory(),
48
- getNewLine: () => import_typescript.default.sys.newLine,
49
- getCanonicalFileName: import_typescript.default.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
50
- };
51
- var isCivet = (id) => /\.civet$/.test(id);
52
- var isCivetTranspiled = (id) => /\.civet\.[jt]sx(\?transform)?$/.test(id);
53
- var isCivetTranspiledTS = (id) => /\.civet\.tsx$/.test(id);
54
- var postfixRE = /(\.[jt]sx)?[?#].*$/s;
45
+ var isCivet = (id) => /\.civet([?#].*)?$/.test(id);
46
+ var isCivetTranspiled = (id) => /\.civet\.[jt]sx([?#].*)?$/.test(id);
47
+ var postfixRE = /[?#].*$/s;
55
48
  var isWindows = import_os.default.platform() === "win32";
56
49
  var windowsSlashRE = /\\/g;
57
50
  function cleanCivetId(id) {
58
- return id.replace(postfixRE, "");
51
+ return id.replace(postfixRE, "").replace(/\.[jt]sx$/, "");
59
52
  }
60
53
  function tryStatSync(file) {
61
54
  try {
@@ -83,61 +76,69 @@ function resolveAbsolutePath(rootDir, id) {
83
76
  return resolved;
84
77
  }
85
78
  var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
86
- if (options.dts && options.js) {
87
- throw new Error("Can't have both `dts` and `js` be set to `true`.");
88
- }
89
- if (options.typecheck && options.js) {
90
- throw new Error("Can't have both `typecheck` and `js` be set to `true`.");
91
- }
92
- const transpileToJS = options.js ?? false;
93
- const outExt = options.outputExtension ?? (transpileToJS ? ".jsx" : ".tsx");
79
+ if (options.dts)
80
+ options.emitDeclaration = options.dts;
81
+ if (options.js)
82
+ options.ts = "civet";
83
+ const transformTS = options.emitDeclaration || options.typecheck;
84
+ const outExt = options.outputExtension ?? (options.ts === "preserve" ? ".tsx" : ".jsx");
94
85
  let fsMap = /* @__PURE__ */ new Map();
95
86
  const sourceMaps = /* @__PURE__ */ new Map();
96
87
  let compilerOptions;
97
88
  let rootDir = process.cwd();
89
+ const tsPromise = transformTS || options.ts === "tsc" ? import("typescript").then((m) => m.default) : null;
90
+ const getFormatHost = (sys) => {
91
+ return {
92
+ getCurrentDirectory: () => sys.getCurrentDirectory(),
93
+ getNewLine: () => sys.newLine,
94
+ getCanonicalFileName: sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
95
+ };
96
+ };
98
97
  return {
99
98
  name: "unplugin-civet",
100
99
  enforce: "pre",
101
100
  async buildStart() {
102
- if (options.dts || options.typecheck) {
103
- const configPath = import_typescript.default.findConfigFile(process.cwd(), import_typescript.default.sys.fileExists);
101
+ if (transformTS || options.ts === "tsc") {
102
+ const ts = await tsPromise;
103
+ const configPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists);
104
104
  if (!configPath) {
105
105
  throw new Error("Could not find 'tsconfig.json'");
106
106
  }
107
- const { config, error } = import_typescript.default.readConfigFile(
107
+ const { config, error } = ts.readConfigFile(
108
108
  configPath,
109
- import_typescript.default.sys.readFile
109
+ ts.sys.readFile
110
110
  );
111
111
  if (error) {
112
- console.error(import_typescript.default.formatDiagnostic(error, formatHost));
112
+ console.error(ts.formatDiagnostic(error, getFormatHost(ts.sys)));
113
113
  throw error;
114
114
  }
115
- const configContents = import_typescript.default.parseJsonConfigFileContent(
115
+ const configContents = ts.parseJsonConfigFileContent(
116
116
  config,
117
- import_typescript.default.sys,
117
+ ts.sys,
118
118
  process.cwd()
119
119
  );
120
120
  compilerOptions = {
121
121
  ...configContents.options,
122
- target: import_typescript.default.ScriptTarget.ESNext
122
+ target: ts.ScriptTarget.ESNext
123
123
  };
124
124
  fsMap = /* @__PURE__ */ new Map();
125
125
  }
126
126
  },
127
- buildEnd() {
128
- if (options.dts || options.typecheck) {
129
- const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), import_typescript.default);
127
+ async buildEnd() {
128
+ if (transformTS) {
129
+ const ts = await tsPromise;
130
+ const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts);
130
131
  const host = tsvfs.createVirtualCompilerHost(
131
132
  system,
132
133
  compilerOptions,
133
- import_typescript.default
134
+ ts
134
135
  );
135
- const program = import_typescript.default.createProgram({
136
+ const program = ts.createProgram({
136
137
  rootNames: [...fsMap.keys()],
137
138
  options: compilerOptions,
138
139
  host: host.compilerHost
139
140
  });
140
- const diagnostics = import_typescript.default.getPreEmitDiagnostics(program).map((diagnostic) => {
141
+ const diagnostics = ts.getPreEmitDiagnostics(program).map((diagnostic) => {
141
142
  const file = diagnostic.file;
142
143
  if (!file)
143
144
  return diagnostic;
@@ -161,10 +162,13 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
161
162
  });
162
163
  if (diagnostics.length > 0) {
163
164
  console.error(
164
- import_typescript.default.formatDiagnosticsWithColorAndContext(diagnostics, formatHost)
165
+ ts.formatDiagnosticsWithColorAndContext(
166
+ diagnostics,
167
+ getFormatHost(ts.sys)
168
+ )
165
169
  );
166
170
  }
167
- if (options.dts) {
171
+ if (options.emitDeclaration) {
168
172
  for (const file of fsMap.keys()) {
169
173
  const sourceFile = program.getSourceFile(file);
170
174
  program.emit(
@@ -209,16 +213,74 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
209
213
  if (!isCivetTranspiled(id))
210
214
  return null;
211
215
  const filename = import_path.default.resolve(process.cwd(), id.slice(0, -outExt.length));
212
- const code = await fs.promises.readFile(filename, "utf-8");
216
+ const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
213
217
  this.addWatchFile(filename);
214
- const compiled = import_civet.default.compile(code, {
215
- // inlineMap: true,
216
- filename: id,
217
- js: transpileToJS,
218
- sourceMap: true
219
- });
220
- sourceMaps.set(import_path.default.resolve(process.cwd(), id), compiled.sourceMap);
221
- const jsonSourceMap = compiled.sourceMap.json(
218
+ let compiled = void 0;
219
+ if (options.ts === "civet" && !transformTS) {
220
+ compiled = import_civet.default.compile(rawCivetSource, {
221
+ filename: id,
222
+ js: true,
223
+ sourceMap: true
224
+ });
225
+ } else {
226
+ const compiledTS = import_civet.default.compile(rawCivetSource, {
227
+ filename: id,
228
+ js: false,
229
+ sourceMap: true
230
+ });
231
+ sourceMaps.set(
232
+ import_path.default.resolve(process.cwd(), id),
233
+ compiledTS.sourceMap
234
+ );
235
+ if (transformTS) {
236
+ const resolved = import_path.default.resolve(process.cwd(), id);
237
+ fsMap.set(resolved, compiledTS.code);
238
+ const slashed = slash(resolved);
239
+ if (resolved !== slashed)
240
+ fsMap.set(slashed, rawCivetSource);
241
+ }
242
+ switch (options.ts) {
243
+ case "esbuild": {
244
+ const esbuildTransform = (await import("esbuild")).transform;
245
+ const result = await esbuildTransform(compiledTS.code, {
246
+ jsx: "preserve",
247
+ loader: "tsx",
248
+ sourcefile: id,
249
+ sourcemap: "external"
250
+ });
251
+ compiled = { code: result.code, sourceMap: result.map };
252
+ break;
253
+ }
254
+ case "tsc": {
255
+ const tsTranspile = (await tsPromise).transpileModule;
256
+ const result = tsTranspile(compiledTS.code, { compilerOptions });
257
+ compiled = {
258
+ code: result.outputText,
259
+ sourceMap: result.sourceMapText ?? ""
260
+ };
261
+ break;
262
+ }
263
+ case "preserve": {
264
+ compiled = compiledTS;
265
+ break;
266
+ }
267
+ case "civet":
268
+ default: {
269
+ compiled = import_civet.default.compile(rawCivetSource, {
270
+ filename: id,
271
+ js: true,
272
+ sourceMap: true
273
+ });
274
+ if (options.ts == void 0) {
275
+ console.log(
276
+ '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.'
277
+ );
278
+ }
279
+ break;
280
+ }
281
+ }
282
+ }
283
+ const jsonSourceMap = typeof compiled.sourceMap == "string" ? compiled.sourceMap : compiled.sourceMap.json(
222
284
  import_path.default.basename(id.replace(/\.[jt]sx$/, "")),
223
285
  import_path.default.basename(id)
224
286
  );
@@ -230,33 +292,9 @@ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
230
292
  transformed = await options.transformOutput(transformed.code, id);
231
293
  return transformed;
232
294
  },
233
- transformInclude(id) {
234
- return isCivetTranspiledTS(id);
235
- },
236
- transform(code, id) {
237
- if (!isCivetTranspiledTS(id))
238
- return null;
239
- if (options.dts || options.typecheck) {
240
- const resolved = import_path.default.resolve(process.cwd(), id);
241
- fsMap.set(resolved, code);
242
- const slashed = slash(resolved);
243
- if (resolved !== slashed)
244
- fsMap.set(slashed, code);
245
- }
246
- return null;
247
- },
248
295
  vite: {
249
- config(config, { command }) {
296
+ config(config) {
250
297
  rootDir = import_path.default.resolve(process.cwd(), config.root ?? "");
251
- if (command === "build") {
252
- return {
253
- esbuild: {
254
- include: [/\.civet$/],
255
- loader: "tsx"
256
- }
257
- };
258
- }
259
- return null;
260
298
  },
261
299
  async transformIndexHtml(html) {
262
300
  return html.replace(
@@ -9,22 +9,15 @@ import {
9
9
  } from "@danielx/civet/ts-diagnostic";
10
10
  import * as fs from "fs";
11
11
  import path from "path";
12
- import ts from "typescript";
13
12
  import * as tsvfs from "@typescript/vfs";
14
13
  import os from "os";
15
- var formatHost = {
16
- getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
17
- getNewLine: () => ts.sys.newLine,
18
- getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
19
- };
20
- var isCivet = (id) => /\.civet$/.test(id);
21
- var isCivetTranspiled = (id) => /\.civet\.[jt]sx(\?transform)?$/.test(id);
22
- var isCivetTranspiledTS = (id) => /\.civet\.tsx$/.test(id);
23
- var postfixRE = /(\.[jt]sx)?[?#].*$/s;
14
+ var isCivet = (id) => /\.civet([?#].*)?$/.test(id);
15
+ var isCivetTranspiled = (id) => /\.civet\.[jt]sx([?#].*)?$/.test(id);
16
+ var postfixRE = /[?#].*$/s;
24
17
  var isWindows = os.platform() === "win32";
25
18
  var windowsSlashRE = /\\/g;
26
19
  function cleanCivetId(id) {
27
- return id.replace(postfixRE, "");
20
+ return id.replace(postfixRE, "").replace(/\.[jt]sx$/, "");
28
21
  }
29
22
  function tryStatSync(file) {
30
23
  try {
@@ -52,23 +45,30 @@ function resolveAbsolutePath(rootDir, id) {
52
45
  return resolved;
53
46
  }
54
47
  var civetUnplugin = createUnplugin((options = {}) => {
55
- if (options.dts && options.js) {
56
- throw new Error("Can't have both `dts` and `js` be set to `true`.");
57
- }
58
- if (options.typecheck && options.js) {
59
- throw new Error("Can't have both `typecheck` and `js` be set to `true`.");
60
- }
61
- const transpileToJS = options.js ?? false;
62
- const outExt = options.outputExtension ?? (transpileToJS ? ".jsx" : ".tsx");
48
+ if (options.dts)
49
+ options.emitDeclaration = options.dts;
50
+ if (options.js)
51
+ options.ts = "civet";
52
+ const transformTS = options.emitDeclaration || options.typecheck;
53
+ const outExt = options.outputExtension ?? (options.ts === "preserve" ? ".tsx" : ".jsx");
63
54
  let fsMap = /* @__PURE__ */ new Map();
64
55
  const sourceMaps = /* @__PURE__ */ new Map();
65
56
  let compilerOptions;
66
57
  let rootDir = process.cwd();
58
+ const tsPromise = transformTS || options.ts === "tsc" ? import("typescript").then((m) => m.default) : null;
59
+ const getFormatHost = (sys) => {
60
+ return {
61
+ getCurrentDirectory: () => sys.getCurrentDirectory(),
62
+ getNewLine: () => sys.newLine,
63
+ getCanonicalFileName: sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
64
+ };
65
+ };
67
66
  return {
68
67
  name: "unplugin-civet",
69
68
  enforce: "pre",
70
69
  async buildStart() {
71
- if (options.dts || options.typecheck) {
70
+ if (transformTS || options.ts === "tsc") {
71
+ const ts = await tsPromise;
72
72
  const configPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists);
73
73
  if (!configPath) {
74
74
  throw new Error("Could not find 'tsconfig.json'");
@@ -78,7 +78,7 @@ var civetUnplugin = createUnplugin((options = {}) => {
78
78
  ts.sys.readFile
79
79
  );
80
80
  if (error) {
81
- console.error(ts.formatDiagnostic(error, formatHost));
81
+ console.error(ts.formatDiagnostic(error, getFormatHost(ts.sys)));
82
82
  throw error;
83
83
  }
84
84
  const configContents = ts.parseJsonConfigFileContent(
@@ -93,8 +93,9 @@ var civetUnplugin = createUnplugin((options = {}) => {
93
93
  fsMap = /* @__PURE__ */ new Map();
94
94
  }
95
95
  },
96
- buildEnd() {
97
- if (options.dts || options.typecheck) {
96
+ async buildEnd() {
97
+ if (transformTS) {
98
+ const ts = await tsPromise;
98
99
  const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts);
99
100
  const host = tsvfs.createVirtualCompilerHost(
100
101
  system,
@@ -130,10 +131,13 @@ var civetUnplugin = createUnplugin((options = {}) => {
130
131
  });
131
132
  if (diagnostics.length > 0) {
132
133
  console.error(
133
- ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost)
134
+ ts.formatDiagnosticsWithColorAndContext(
135
+ diagnostics,
136
+ getFormatHost(ts.sys)
137
+ )
134
138
  );
135
139
  }
136
- if (options.dts) {
140
+ if (options.emitDeclaration) {
137
141
  for (const file of fsMap.keys()) {
138
142
  const sourceFile = program.getSourceFile(file);
139
143
  program.emit(
@@ -178,16 +182,74 @@ var civetUnplugin = createUnplugin((options = {}) => {
178
182
  if (!isCivetTranspiled(id))
179
183
  return null;
180
184
  const filename = path.resolve(process.cwd(), id.slice(0, -outExt.length));
181
- const code = await fs.promises.readFile(filename, "utf-8");
185
+ const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
182
186
  this.addWatchFile(filename);
183
- const compiled = civet.compile(code, {
184
- // inlineMap: true,
185
- filename: id,
186
- js: transpileToJS,
187
- sourceMap: true
188
- });
189
- sourceMaps.set(path.resolve(process.cwd(), id), compiled.sourceMap);
190
- const jsonSourceMap = compiled.sourceMap.json(
187
+ let compiled = void 0;
188
+ if (options.ts === "civet" && !transformTS) {
189
+ compiled = civet.compile(rawCivetSource, {
190
+ filename: id,
191
+ js: true,
192
+ sourceMap: true
193
+ });
194
+ } else {
195
+ const compiledTS = civet.compile(rawCivetSource, {
196
+ filename: id,
197
+ js: false,
198
+ sourceMap: true
199
+ });
200
+ sourceMaps.set(
201
+ path.resolve(process.cwd(), id),
202
+ compiledTS.sourceMap
203
+ );
204
+ if (transformTS) {
205
+ const resolved = path.resolve(process.cwd(), id);
206
+ fsMap.set(resolved, compiledTS.code);
207
+ const slashed = slash(resolved);
208
+ if (resolved !== slashed)
209
+ fsMap.set(slashed, rawCivetSource);
210
+ }
211
+ switch (options.ts) {
212
+ case "esbuild": {
213
+ const esbuildTransform = (await import("esbuild")).transform;
214
+ const result = await esbuildTransform(compiledTS.code, {
215
+ jsx: "preserve",
216
+ loader: "tsx",
217
+ sourcefile: id,
218
+ sourcemap: "external"
219
+ });
220
+ compiled = { code: result.code, sourceMap: result.map };
221
+ break;
222
+ }
223
+ case "tsc": {
224
+ const tsTranspile = (await tsPromise).transpileModule;
225
+ const result = tsTranspile(compiledTS.code, { compilerOptions });
226
+ compiled = {
227
+ code: result.outputText,
228
+ sourceMap: result.sourceMapText ?? ""
229
+ };
230
+ break;
231
+ }
232
+ case "preserve": {
233
+ compiled = compiledTS;
234
+ break;
235
+ }
236
+ case "civet":
237
+ default: {
238
+ compiled = civet.compile(rawCivetSource, {
239
+ filename: id,
240
+ js: true,
241
+ sourceMap: true
242
+ });
243
+ if (options.ts == void 0) {
244
+ console.log(
245
+ '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.'
246
+ );
247
+ }
248
+ break;
249
+ }
250
+ }
251
+ }
252
+ const jsonSourceMap = typeof compiled.sourceMap == "string" ? compiled.sourceMap : compiled.sourceMap.json(
191
253
  path.basename(id.replace(/\.[jt]sx$/, "")),
192
254
  path.basename(id)
193
255
  );
@@ -199,33 +261,9 @@ var civetUnplugin = createUnplugin((options = {}) => {
199
261
  transformed = await options.transformOutput(transformed.code, id);
200
262
  return transformed;
201
263
  },
202
- transformInclude(id) {
203
- return isCivetTranspiledTS(id);
204
- },
205
- transform(code, id) {
206
- if (!isCivetTranspiledTS(id))
207
- return null;
208
- if (options.dts || options.typecheck) {
209
- const resolved = path.resolve(process.cwd(), id);
210
- fsMap.set(resolved, code);
211
- const slashed = slash(resolved);
212
- if (resolved !== slashed)
213
- fsMap.set(slashed, code);
214
- }
215
- return null;
216
- },
217
264
  vite: {
218
- config(config, { command }) {
265
+ config(config) {
219
266
  rootDir = path.resolve(process.cwd(), config.root ?? "");
220
- if (command === "build") {
221
- return {
222
- esbuild: {
223
- include: [/\.civet$/],
224
- loader: "tsx"
225
- }
226
- };
227
- }
228
- return null;
229
267
  },
230
268
  async transformIndexHtml(html) {
231
269
  return html.replace(
@@ -4,15 +4,14 @@ import { TransformResult } from 'unplugin';
4
4
  type PluginOptions = {
5
5
  outputExtension?: string;
6
6
  transformOutput?: (code: string, id: string) => TransformResult | Promise<TransformResult>;
7
- } & ({
8
- dts?: false;
9
- typecheck?: false;
10
- js?: false | true;
11
- } | {
12
- dts?: true;
13
- typecheck?: true;
14
- js?: false;
15
- });
7
+ emitDeclaration?: boolean;
8
+ typecheck?: boolean;
9
+ ts?: 'civet' | 'esbuild' | 'tsc' | 'preserve';
10
+ /** @deprecated Use "ts" option instead */
11
+ js?: boolean;
12
+ /** @deprecated Use "emitDeclaration" instead */
13
+ dts?: boolean;
14
+ };
16
15
  declare function slash(p: string): string;
17
16
  declare const civetUnplugin: unplugin.UnpluginInstance<PluginOptions, boolean>;
18
17
 
@@ -4,15 +4,14 @@ import { TransformResult } from 'unplugin';
4
4
  type PluginOptions = {
5
5
  outputExtension?: string;
6
6
  transformOutput?: (code: string, id: string) => TransformResult | Promise<TransformResult>;
7
- } & ({
8
- dts?: false;
9
- typecheck?: false;
10
- js?: false | true;
11
- } | {
12
- dts?: true;
13
- typecheck?: true;
14
- js?: false;
15
- });
7
+ emitDeclaration?: boolean;
8
+ typecheck?: boolean;
9
+ ts?: 'civet' | 'esbuild' | 'tsc' | 'preserve';
10
+ /** @deprecated Use "ts" option instead */
11
+ js?: boolean;
12
+ /** @deprecated Use "emitDeclaration" instead */
13
+ dts?: boolean;
14
+ };
16
15
  declare function slash(p: string): string;
17
16
  declare const civetUnplugin: unplugin.UnpluginInstance<PluginOptions, boolean>;
18
17