@danielx/civet 0.7.15 → 0.7.17

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.
Files changed (49) hide show
  1. package/dist/browser.js +14 -11
  2. package/dist/civet +141 -108
  3. package/dist/config.d.mts +1 -0
  4. package/dist/config.d.ts +1 -0
  5. package/dist/config.mjs +122 -0
  6. package/dist/esm.mjs +1 -1
  7. package/dist/main.js +14 -11
  8. package/dist/main.mjs +14 -11
  9. package/dist/unplugin/astro.d.ts +11 -0
  10. package/dist/unplugin/astro.js +49 -0
  11. package/dist/unplugin/astro.mjs +19 -0
  12. package/dist/unplugin/esbuild.d.ts +2 -0
  13. package/dist/unplugin/esbuild.js +36 -0
  14. package/dist/unplugin/esbuild.mjs +6 -0
  15. package/dist/unplugin/rollup.d.ts +2 -0
  16. package/dist/unplugin/rollup.js +36 -0
  17. package/dist/unplugin/rollup.mjs +6 -0
  18. package/dist/{unplugin.d.ts → unplugin/unplugin.d.ts} +7 -10
  19. package/dist/{unplugin.js → unplugin/unplugin.js} +81 -66
  20. package/dist/{unplugin-shared.mjs → unplugin/unplugin.mjs} +80 -65
  21. package/dist/unplugin/vite.d.ts +2 -0
  22. package/dist/unplugin/vite.js +36 -0
  23. package/dist/unplugin/vite.mjs +6 -0
  24. package/dist/unplugin/webpack.d.ts +2 -0
  25. package/dist/unplugin/webpack.js +36 -0
  26. package/dist/unplugin/webpack.mjs +6 -0
  27. package/package.json +27 -17
  28. package/dist/astro.d.mts +0 -16
  29. package/dist/astro.d.ts +0 -16
  30. package/dist/astro.js +0 -537
  31. package/dist/astro.mjs +0 -22
  32. package/dist/esbuild.d.mts +0 -8
  33. package/dist/esbuild.d.ts +0 -8
  34. package/dist/esbuild.js +0 -524
  35. package/dist/esbuild.mjs +0 -9
  36. package/dist/rollup.d.mts +0 -8
  37. package/dist/rollup.d.ts +0 -8
  38. package/dist/rollup.js +0 -524
  39. package/dist/rollup.mjs +0 -9
  40. package/dist/unplugin.d.mts +0 -26
  41. package/dist/unplugin.mjs +0 -10
  42. package/dist/vite.d.mts +0 -8
  43. package/dist/vite.d.ts +0 -8
  44. package/dist/vite.js +0 -524
  45. package/dist/vite.mjs +0 -9
  46. package/dist/webpack.d.mts +0 -7
  47. package/dist/webpack.d.ts +0 -7
  48. package/dist/webpack.js +0 -524
  49. package/dist/webpack.mjs +0 -9
package/dist/webpack.js DELETED
@@ -1,524 +0,0 @@
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
-
30
- // src/webpack.ts
31
- var webpack_exports = {};
32
- __export(webpack_exports, {
33
- default: () => webpack_default
34
- });
35
- module.exports = __toCommonJS(webpack_exports);
36
-
37
- // src/index.ts
38
- var import_unplugin = require("unplugin");
39
- var import_civet = __toESM(require("@danielx/civet"));
40
- var import_config = require("@danielx/civet/config");
41
- var import_ts_diagnostic = require("@danielx/civet/ts-diagnostic");
42
- var fs = __toESM(require("fs"));
43
- var import_path = __toESM(require("path"));
44
- var tsvfs = __toESM(require("@typescript/vfs"));
45
- var import_os = __toESM(require("os"));
46
-
47
- // src/constants.ts
48
- var DEFAULT_EXTENSIONS = [".mjs", ".js", ".mts", ".ts", ".jsx", ".tsx", ".json"];
49
-
50
- // src/index.ts
51
- var isCivetTranspiled = /(\.civet)(\.[jt]sx)([?#].*)?$/;
52
- var postfixRE = /[?#].*$/s;
53
- var isWindows = import_os.default.platform() === "win32";
54
- var windowsSlashRE = /\\/g;
55
- function cleanCivetId(id) {
56
- return id.replace(postfixRE, "").replace(/\.[jt]sx$/, "");
57
- }
58
- function tryStatSync(file) {
59
- try {
60
- return fs.statSync(file, { throwIfNoEntry: false });
61
- } catch {
62
- return void 0;
63
- }
64
- }
65
- function slash(p) {
66
- return p.replace(windowsSlashRE, "/");
67
- }
68
- function normalizePath(id) {
69
- return import_path.default.posix.normalize(isWindows ? slash(id) : id);
70
- }
71
- function tryFsResolve(file) {
72
- const fileStat = tryStatSync(file);
73
- if (fileStat?.isFile())
74
- return normalizePath(file);
75
- return void 0;
76
- }
77
- function resolveAbsolutePath(rootDir, id, implicitExtension) {
78
- const file = import_path.default.join(rootDir, id);
79
- return tryFsResolve(file) || implicitExtension && implicitCivet(file) || tryFsResolve(id) || implicitExtension && implicitCivet(id);
80
- }
81
- function implicitCivet(file) {
82
- if (tryFsResolve(file))
83
- return;
84
- const civet2 = file + ".civet";
85
- if (tryFsResolve(civet2))
86
- return civet2;
87
- return;
88
- }
89
- var rawPlugin = (options = {}, meta) => {
90
- if (options.dts)
91
- options.emitDeclaration = options.dts;
92
- if (options.js)
93
- options.ts = "civet";
94
- let compileOptions = {};
95
- const transformTS = options.emitDeclaration || options.typecheck;
96
- const outExt = options.outputExtension ?? (options.ts === "preserve" ? ".tsx" : ".jsx");
97
- const implicitExtension = options.implicitExtension ?? true;
98
- let fsMap = /* @__PURE__ */ new Map();
99
- const sourceMaps = /* @__PURE__ */ new Map();
100
- let compilerOptions, compilerOptionsWithSourceMap;
101
- let rootDir = process.cwd();
102
- let esbuildOptions;
103
- let configErrors;
104
- let configFileNames;
105
- const tsPromise = transformTS || options.ts === "tsc" ? import("typescript").then((m) => m.default) : null;
106
- const getFormatHost = (sys) => {
107
- return {
108
- getCurrentDirectory: () => sys.getCurrentDirectory(),
109
- getNewLine: () => sys.newLine,
110
- getCanonicalFileName: sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
111
- };
112
- };
113
- const cache = options.cache ? /* @__PURE__ */ new Map() : void 0;
114
- return {
115
- name: "unplugin-civet",
116
- enforce: "pre",
117
- async buildStart() {
118
- if (transformTS || options.ts === "tsc") {
119
- let mogrify2 = function(key) {
120
- if (key in config && Array.isArray(config[key])) {
121
- config[key] = config[key].map((item) => {
122
- if (typeof item !== "string")
123
- return item;
124
- return item.replace(/\.civet\b(?!\.)/g, ".civet.tsx");
125
- });
126
- }
127
- };
128
- var mogrify = mogrify2;
129
- const ts = await tsPromise;
130
- const civetConfigPath = "config" in options ? options.config : await (0, import_config.findInDir)(process.cwd());
131
- if (civetConfigPath) {
132
- compileOptions = await (0, import_config.loadConfig)(civetConfigPath);
133
- }
134
- compileOptions.parseOptions = {
135
- ...compileOptions.parseOptions,
136
- ...options.parseOptions
137
- };
138
- const tsConfigPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists);
139
- if (!tsConfigPath) {
140
- throw new Error("Could not find 'tsconfig.json'");
141
- }
142
- const { config, error } = ts.readConfigFile(
143
- tsConfigPath,
144
- ts.sys.readFile
145
- );
146
- if (error) {
147
- console.error(ts.formatDiagnostic(error, getFormatHost(ts.sys)));
148
- throw error;
149
- }
150
- mogrify2("files");
151
- const system = { ...ts.sys };
152
- const { readDirectory: systemReadDirectory } = system;
153
- system.readDirectory = (path2, extensions, excludes, includes, depth) => {
154
- extensions = [...extensions ?? [], ".civet"];
155
- return systemReadDirectory(path2, extensions, excludes, includes, depth).map((name) => name.endsWith(".civet") ? name + ".tsx" : name);
156
- };
157
- const configContents = ts.parseJsonConfigFileContent(
158
- config,
159
- system,
160
- process.cwd()
161
- );
162
- configErrors = configContents.errors;
163
- configFileNames = configContents.fileNames;
164
- compilerOptions = {
165
- ...configContents.options,
166
- target: ts.ScriptTarget.ESNext,
167
- composite: false
168
- };
169
- compilerOptions.jsx ?? (compilerOptions.jsx = ts.JsxEmit.Preserve);
170
- compilerOptionsWithSourceMap = {
171
- ...compilerOptions,
172
- sourceMap: true
173
- };
174
- fsMap = /* @__PURE__ */ new Map();
175
- }
176
- },
177
- async buildEnd(useConfigFileNames = false) {
178
- if (transformTS) {
179
- const ts = await tsPromise;
180
- const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), ts);
181
- const {
182
- fileExists: systemFileExists,
183
- readFile: systemReadFile,
184
- readDirectory: systemReadDirectory
185
- } = system;
186
- system.fileExists = (filename) => {
187
- if (!filename.endsWith(".civet.tsx"))
188
- return systemFileExists(filename);
189
- if (fsMap.has(filename))
190
- return true;
191
- return systemFileExists(filename.slice(0, -4));
192
- };
193
- system.readDirectory = (path2) => systemReadDirectory(path2).map((name) => name.endsWith(".civet") ? name + ".tsx" : name);
194
- system.readFile = (filename, encoding = "utf-8") => {
195
- if (import_path.default.basename(filename) === "package.json") {
196
- let recurse2 = function(node) {
197
- if (node && typeof node === "object") {
198
- for (const key in node) {
199
- const value = node[key];
200
- if (typeof value === "string") {
201
- if (value.endsWith(".civet")) {
202
- node[key] = value + ".tsx";
203
- modified = true;
204
- }
205
- } else if (value) {
206
- recurse2(value);
207
- }
208
- }
209
- }
210
- };
211
- var recurse = recurse2;
212
- const json = systemReadFile(filename, encoding);
213
- if (!json)
214
- return json;
215
- const parsed = JSON.parse(json);
216
- let modified = false;
217
- recurse2(parsed.imports);
218
- return modified ? JSON.stringify(parsed) : json;
219
- }
220
- if (!filename.endsWith(".civet.tsx"))
221
- return systemReadFile(filename, encoding);
222
- if (fsMap.has(filename))
223
- return fsMap.get(filename);
224
- const civetFilename = filename.slice(0, -4);
225
- const rawCivetSource = fs.readFileSync(civetFilename, {
226
- encoding
227
- });
228
- const compiledTS = import_civet.default.compile(rawCivetSource, {
229
- ...compileOptions,
230
- filename,
231
- js: false,
232
- sync: true
233
- // TS readFile API seems to need to be synchronous
234
- });
235
- fsMap.set(filename, compiledTS);
236
- return compiledTS;
237
- };
238
- const host = tsvfs.createVirtualCompilerHost(
239
- system,
240
- compilerOptions,
241
- ts
242
- );
243
- host.compilerHost.getDirectories = system.getDirectories;
244
- const program = ts.createProgram({
245
- rootNames: useConfigFileNames ? configFileNames : [...fsMap.keys()],
246
- options: compilerOptions,
247
- host: host.compilerHost
248
- });
249
- const diagnostics = ts.getPreEmitDiagnostics(program).map((diagnostic) => {
250
- const file = diagnostic.file;
251
- if (!file)
252
- return diagnostic;
253
- const sourceMap = sourceMaps.get(file.fileName);
254
- if (!sourceMap)
255
- return diagnostic;
256
- const sourcemapLines = sourceMap.data.lines;
257
- const range = (0, import_ts_diagnostic.remapRange)(
258
- {
259
- start: diagnostic.start || 0,
260
- end: (diagnostic.start || 0) + (diagnostic.length || 1)
261
- },
262
- sourcemapLines
263
- );
264
- return {
265
- ...diagnostic,
266
- messageText: (0, import_ts_diagnostic.flattenDiagnosticMessageText)(diagnostic.messageText),
267
- length: diagnostic.length,
268
- start: range.start
269
- };
270
- });
271
- if (configErrors?.length) {
272
- diagnostics.unshift(...configErrors);
273
- }
274
- if (diagnostics.length > 0) {
275
- console.error(
276
- ts.formatDiagnosticsWithColorAndContext(
277
- diagnostics,
278
- getFormatHost(ts.sys)
279
- )
280
- );
281
- if (options.typecheck) {
282
- let failures = [];
283
- if (typeof options.typecheck === "string") {
284
- if (options.typecheck.includes("error"))
285
- failures.push(1 /* Error */);
286
- if (options.typecheck.includes("warning"))
287
- failures.push(0 /* Warning */);
288
- if (options.typecheck.includes("suggestion"))
289
- failures.push(2 /* Suggestion */);
290
- if (options.typecheck.includes("message"))
291
- failures.push(3 /* Message */);
292
- if (options.typecheck.includes("all")) {
293
- failures = { includes: () => true };
294
- }
295
- } else {
296
- failures.push(1 /* Error */);
297
- }
298
- const count = diagnostics.filter((d) => failures.includes(d.category)).length;
299
- if (count) {
300
- const reason = count === diagnostics.length ? count : `${count} out of ${diagnostics.length}`;
301
- throw new Error(`Aborting build because of ${reason} TypeScript diagnostic${diagnostics.length > 1 ? "s" : ""} above`);
302
- }
303
- }
304
- }
305
- if (options.emitDeclaration) {
306
- if (meta.framework === "esbuild" && !esbuildOptions.outdir) {
307
- console.log("WARNING: Civet unplugin's `emitDeclaration` requires esbuild's `outdir` option to be set;");
308
- }
309
- for (const file of fsMap.keys()) {
310
- const slashed = slash(file);
311
- if (file !== slashed) {
312
- fsMap.delete(slashed);
313
- }
314
- }
315
- for (const file of fsMap.keys()) {
316
- const sourceFile = program.getSourceFile(file);
317
- program.emit(
318
- sourceFile,
319
- (filePath, content) => {
320
- const pathFromDistDir = import_path.default.relative(
321
- compilerOptions.outDir ?? process.cwd(),
322
- filePath
323
- );
324
- if (meta.framework === "esbuild") {
325
- fs.mkdirSync(
326
- import_path.default.dirname(
327
- import_path.default.resolve(esbuildOptions.outdir, pathFromDistDir)
328
- ),
329
- { recursive: true }
330
- );
331
- }
332
- this.emitFile({
333
- source: content,
334
- fileName: pathFromDistDir,
335
- type: "asset"
336
- });
337
- },
338
- void 0,
339
- true,
340
- // emitDtsOnly
341
- void 0,
342
- // @ts-ignore @internal interface
343
- true
344
- // forceDtsEmit
345
- );
346
- }
347
- }
348
- }
349
- },
350
- resolveId(id, importer, options2) {
351
- if (/\0/.test(id))
352
- return null;
353
- id = cleanCivetId(id);
354
- let resolvedId = import_path.default.isAbsolute(id) ? resolveAbsolutePath(rootDir, id, implicitExtension) : import_path.default.resolve(import_path.default.dirname(importer ?? ""), id);
355
- if (!resolvedId)
356
- return null;
357
- if (!resolvedId.endsWith(".civet")) {
358
- if (!implicitExtension)
359
- return null;
360
- const implicitId = implicitCivet(resolvedId);
361
- if (!implicitId)
362
- return null;
363
- resolvedId = implicitId;
364
- }
365
- if (options2.scan && meta.framework === "vite") {
366
- resolvedId = `\0${resolvedId}`;
367
- }
368
- return resolvedId + outExt;
369
- },
370
- loadInclude(id) {
371
- return isCivetTranspiled.test(id);
372
- },
373
- async load(id) {
374
- const match = isCivetTranspiled.exec(id);
375
- if (!match)
376
- return null;
377
- const basename = id.slice(0, match.index + match[1].length);
378
- const filename = import_path.default.resolve(rootDir, basename);
379
- let mtime;
380
- if (cache) {
381
- mtime = (await fs.promises.stat(filename)).mtimeMs;
382
- const cached = cache?.get(filename);
383
- if (cached && cached.mtime === mtime) {
384
- return cached.result;
385
- }
386
- }
387
- const rawCivetSource = await fs.promises.readFile(filename, "utf-8");
388
- this.addWatchFile(filename);
389
- let compiled;
390
- const civetOptions = {
391
- ...compileOptions,
392
- filename: id,
393
- sourceMap: true
394
- };
395
- if (options.ts === "civet" && !transformTS) {
396
- compiled = await import_civet.default.compile(rawCivetSource, {
397
- ...civetOptions,
398
- js: true
399
- });
400
- } else {
401
- const compiledTS = await import_civet.default.compile(rawCivetSource, {
402
- ...civetOptions,
403
- js: false
404
- });
405
- const resolved = filename + outExt;
406
- sourceMaps.set(
407
- resolved,
408
- compiledTS.sourceMap
409
- );
410
- if (transformTS) {
411
- const tsx = filename + ".tsx";
412
- fsMap.set(tsx, compiledTS.code);
413
- const slashed = slash(tsx);
414
- if (tsx !== slashed)
415
- fsMap.set(slashed, compiledTS.code);
416
- }
417
- switch (options.ts) {
418
- case "esbuild": {
419
- const esbuildTransform = (await import("esbuild")).transform;
420
- const result = await esbuildTransform(compiledTS.code, {
421
- jsx: "preserve",
422
- loader: "tsx",
423
- sourcefile: id,
424
- sourcemap: "external"
425
- });
426
- compiled = { code: result.code, sourceMap: result.map };
427
- break;
428
- }
429
- case "tsc": {
430
- const tsTranspile = (await tsPromise).transpileModule;
431
- const result = tsTranspile(
432
- compiledTS.code,
433
- { compilerOptions: compilerOptionsWithSourceMap }
434
- );
435
- compiled = {
436
- code: result.outputText,
437
- sourceMap: result.sourceMapText
438
- };
439
- break;
440
- }
441
- case "preserve": {
442
- compiled = compiledTS;
443
- break;
444
- }
445
- case "civet":
446
- default: {
447
- compiled = await import_civet.default.compile(rawCivetSource, {
448
- ...civetOptions,
449
- js: true
450
- });
451
- if (options.ts == void 0) {
452
- console.log(
453
- '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.'
454
- );
455
- }
456
- break;
457
- }
458
- }
459
- }
460
- const jsonSourceMap = compiled.sourceMap && (typeof compiled.sourceMap === "string" ? JSON.parse(compiled.sourceMap) : compiled.sourceMap.json(
461
- import_path.default.basename(id.replace(/\.[jt]sx$/, "")),
462
- import_path.default.basename(id)
463
- ));
464
- let transformed = {
465
- code: compiled.code,
466
- map: jsonSourceMap
467
- };
468
- if (options.transformOutput)
469
- transformed = await options.transformOutput(transformed.code, id);
470
- cache?.set(filename, { mtime, result: transformed });
471
- return transformed;
472
- },
473
- esbuild: {
474
- config(options2) {
475
- esbuildOptions = options2;
476
- }
477
- },
478
- vite: {
479
- config(config) {
480
- var _a;
481
- rootDir = import_path.default.resolve(process.cwd(), config.root ?? "");
482
- if (implicitExtension) {
483
- config.resolve ?? (config.resolve = {});
484
- (_a = config.resolve).extensions ?? (_a.extensions = DEFAULT_EXTENSIONS);
485
- config.resolve.extensions.push(".civet");
486
- }
487
- },
488
- async transformIndexHtml(html) {
489
- return html.replace(
490
- /<!--[^]*?-->|<[^<>]*>/g,
491
- (tag) => tag.replace(
492
- /<\s*script\b[^<>]*>/gi,
493
- (script) => (
494
- // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
495
- script.replace(
496
- /([:_\p{ID_Start}][:\p{ID_Continue}]*)(\s*=\s*("[^"]*"|'[^']*'|[^\s"'=<>`]*))?/gu,
497
- (attr, name, value) => name.toLowerCase() === "src" && value ? attr.replace(
498
- /(\.civet)(['"]?)$/,
499
- (_, extension, endQuote) => `${extension}${outExt}?transform${endQuote}`
500
- ) : attr
501
- )
502
- )
503
- )
504
- );
505
- },
506
- handleHotUpdate({ file, server, modules }) {
507
- if (!file.endsWith(".civet"))
508
- return;
509
- const resolvedId = slash(import_path.default.resolve(file) + outExt);
510
- const module2 = server.moduleGraph.getModuleById(resolvedId);
511
- if (module2) {
512
- server.moduleGraph.onFileChange(resolvedId);
513
- return [...modules, module2];
514
- }
515
- return modules;
516
- }
517
- }
518
- };
519
- };
520
- var unplugin = (0, import_unplugin.createUnplugin)(rawPlugin);
521
- var src_default = unplugin;
522
-
523
- // src/webpack.ts
524
- var webpack_default = src_default.webpack;
package/dist/webpack.mjs DELETED
@@ -1,9 +0,0 @@
1
- import {
2
- src_default
3
- } from "./unplugin-shared.mjs";
4
-
5
- // src/webpack.ts
6
- var webpack_default = src_default.webpack;
7
- export {
8
- webpack_default as default
9
- };