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