@danielx/civet 0.6.51 → 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/esbuild.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(