• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DanielXMoore / Civet / 23598229556

26 Mar 2026 01:57PM UTC coverage: 91.456% (-0.05%) from 91.506%
23598229556

push

github

web-flow
Merge pull request #1867 from DanielXMoore/unplugin-hash

3796 of 4143 branches covered (91.62%)

Branch coverage included in aggregate %.

2 of 18 new or added lines in 1 file covered. (11.11%)

1 existing line in 1 file now uncovered.

19388 of 21207 relevant lines covered (91.42%)

17685.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.36
/source/unplugin/unplugin.civet
1
import { type TransformResult, createUnplugin } from 'unplugin'
1✔
2
import civet, { decode, lib, SourceMap, type CompileOptions, type ParseOptions } from '@danielx/civet'
1✔
3
import { findInDir, loadConfig } from '@danielx/civet/config'
1✔
4
import {
1✔
5
  remapRange,
1✔
6
  flattenDiagnosticMessageText,
1✔
7
  // @ts-ignore
1✔
8
  // using ts-ignore because the version of @danielx/civet typescript is checking against
1✔
9
  // is the one published to npm, not the one in the repo
1✔
10
} from '@danielx/civet/ts-diagnostic'
1✔
11
import * as fs from 'fs'
1✔
12
import path from 'path'
1✔
13
import type { FormatDiagnosticsHost, Diagnostic, System } from 'typescript'
1✔
14
import { createFSBackedSystem, createVirtualCompilerHost } from '@typescript/vfs'
1✔
15
import type { UserConfig } from 'vite'
1✔
16
import type { BuildOptions } from 'esbuild'
1✔
17
import os from 'os'
1✔
18
import { DEFAULT_EXTENSIONS } from './constants.mjs'
1✔
19

1✔
20
// Copied from typescript to avoid importing the whole package
1✔
21
enum DiagnosticCategory
1✔
22
  Warning = 0
1✔
23
  Error = 1
1✔
24
  Suggestion = 2
1✔
25
  Message = 3
1✔
26

1✔
27
export type PluginOptions
1✔
28
  implicitExtension?: boolean
1✔
29
  outputExtension?: string
1✔
30
  transformOutput?: (
1✔
31
    code: string
1✔
32
    id: string
1✔
33
  ) => TransformResult | Promise<TransformResult>
1✔
34
  emitDeclaration?: boolean
1✔
35
  declarationExtension?: string
1✔
36
  typecheck?: boolean | string
1✔
37
  ts?: 'civet' | 'esbuild' | 'tsc' | 'preserve'
1✔
38
  /** @deprecated Use "ts" option instead */
1✔
39
  js?: boolean
1✔
40
  /** @deprecated Use "emitDeclaration" instead */
1✔
41
  dts?: boolean
1✔
42
  /** Number of parallel threads to compile with (Node only) */
1✔
43
  threads?: number
1✔
44
  /** Cache compilation results based on file mtime (useful for serve or watch mode) */
1✔
45
  cache?: boolean
1✔
46
  /** config filename, or false/null to not look for default config file */
1✔
47
  config?: string | false | null
1✔
48
  parseOptions?: ParseOptions
1✔
49

1✔
50
type CacheEntry
1✔
51
  mtime: number
1✔
52
  result?: TransformResult
1✔
53
  promise?: Promise<void>
1✔
54

1✔
55
queryPostfixRE := /\?.*$/s
1✔
56
escapedHashRE := /\0#/g
1✔
57
isWindows := os.platform() is 'win32'
1✔
58
windowsSlashRE := /\\/g
1✔
59
civetSuffix := '.civet'
1✔
60
workerRE := /(?:\?|&)(?:worker|sharedworker)(?:&|$|#)/
1✔
61

1✔
62
/**
1✔
63
Extract a possible Civet filename from an id, after removing a possible
1✔
64
outputExtension and/or a query/hash (?/#) postfix.
1✔
65
Returns {filename, postfix} in case you need to add the postfix back.
1✔
66
You should check whether the filename ends in .civet extension,
1✔
67
or needs an implicit .civet extension, or isn't Civet-related at all.
1✔
68
*/
1✔
69
function extractCivetFilename(id: string, outputExtension: string): {filename: string, postfix: string}
×
70
  postfix .= ''
×
NEW
71
  // Webpack escapes literal `#` in `loaderContext.resource` as `\0#` so it
×
NEW
72
  // can distinguish path characters from fragment suffixes. Undo that first.
×
NEW
73
  id = id.replace escapedHashRE, '#'
×
NEW
74
  // `?` is always treated as a suffix. Raw `?` is impossible in Windows paths,
×
NEW
75
  // and we rely on query suffixes across bundlers.
×
NEW
76
  filename .= id.replace queryPostfixRE, (match) =>
×
77
    postfix = match
×
78
    ''
×
79
  // Normally the outputExtension (.jsx/.tsx by default) should be present,
×
80
  // but sometimes (e.g. esbuild's alias feature) load directly without resolve
×
81
  if filename.endsWith outputExtension
×
82
    filename = filename[< -outputExtension#]
×
NEW
83
  // `#` may be either a literal path character or a fragment-like suffix.
×
NEW
84
  // Keep it when the file exists as written; otherwise strip one rightmost
×
NEW
85
  // `#...` suffix and let later resolution check whether file exists.
×
NEW
86
  hashIndex := filename.lastIndexOf '#'
×
NEW
87
  if hashIndex >= 0 and not tryStatSync filename
×
NEW
88
    postfix = filename[hashIndex..] + postfix
×
NEW
89
    filename = filename[..<hashIndex]
×
NEW
90
    if filename.endsWith outputExtension
×
NEW
91
      filename = filename[< -outputExtension#]
×
UNCOV
92
  {filename, postfix}
×
93

1✔
94
function tryStatSync(file: string): fs.Stats?
×
95
  try
×
96
    // The "throwIfNoEntry" is a performance optimization for cases where the file does not exist
×
NEW
97
    return fs.statSync file, throwIfNoEntry: false
×
98

1✔
99
export function slash(p: string): string
1✔
100
  p.replace windowsSlashRE, '/'
×
101

1✔
102
function normalizePath(id: string): string
×
103
  path.posix.normalize isWindows ? slash(id) : id
×
104

1✔
105
function tryFsResolve(file: string): string?
×
106
  fileStat := tryStatSync file
×
107
  if fileStat?.isFile()
×
108
    normalizePath file
×
109

1✔
110
function resolveAbsolutePath(rootDir: string, id: string, implicitExtension: boolean)
×
111
  file := path.join rootDir, id
×
112
  // Check for existence of resolved file and unresolved id,
×
113
  // without and with implicit .civet extension, and return first existing
×
114
  (or)
×
115
    tryFsResolve(file)
×
116
    implicitExtension and implicitCivet file
×
117
    tryFsResolve id
×
118
    implicitExtension and implicitCivet id
×
119

1✔
120
function implicitCivet(file: string): string?
×
121
  return if tryFsResolve file
×
122
  civet := file + '.civet'
×
123
  return civet if tryFsResolve civet
×
124

1✔
125
export const rawPlugin: Parameters<typeof createUnplugin<PluginOptions>>[0] =
1✔
126
(options: PluginOptions = {}, meta) =>
×
127
  if (options.dts) options.emitDeclaration = options.dts
×
128
  compileOptions: CompileOptions .= {}
×
129

×
130
  ts .= options.ts
×
131
  if (options.js) ts = 'civet'
×
132
  unless ts?
×
133
    console.log '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.'
×
134
    ts = "civet"
×
135
  unless ts is in ["civet", "esbuild", "tsc", "preserve"]
×
136
    console.log `WARNING: Invalid option ts: ${JSON.stringify ts}; switching to "civet"`
×
137
    ts = "civet"
×
138

×
139
  transformTS := options.emitDeclaration or options.typecheck
×
140
  outExt :=
×
141
    options.outputExtension ?? (ts is "preserve" ? ".tsx" : ".jsx")
×
142
  implicitExtension := options.implicitExtension ?? true
×
143
  let aliasResolver: (id: string) => string
×
144

×
145
  fsMap: Map<string, string> .= new Map
×
146
  sourceMaps := new Map<string, SourceMap>
×
147
  let compilerOptions: any, compilerOptionsWithSourceMap: any
×
148
  rootDir .= process.cwd()
×
149
  let esbuildOptions: BuildOptions
×
150
  let configErrors: Diagnostic[]?
×
151
  let configFileNames: string[]
×
152
  skipWorker .= false
×
153

×
154
  tsPromise := if transformTS or ts is "tsc"
×
155
    import('typescript').then .default
×
156
  getFormatHost := (sys: System): FormatDiagnosticsHost =>
×
157
    return {
×
158
      getCurrentDirectory: => sys.getCurrentDirectory()
×
159
      getNewLine: => sys.newLine
×
160
      getCanonicalFileName: sys.useCaseSensitiveFileNames
×
161
        ? (f) => f
×
162
        : (f) => f.toLowerCase()
×
163
    }
×
164

×
165
  cache := new Map<string, CacheEntry> unless options.cache is false
×
166

×
167
  plugin: ReturnType<typeof rawPlugin> := {
×
168
    name: 'unplugin-civet'
×
169
    enforce: 'pre'
×
170

×
171
    async buildStart(): Promise<void>
×
172
      civetConfigPath .= options.config
×
173
      if civetConfigPath is undefined
×
174
        civetConfigPath = await findInDir process.cwd()
×
175
      if civetConfigPath
×
176
        compileOptions = await loadConfig civetConfigPath
×
177
      // Merge parseOptions, with plugin options taking priority
×
178
      compileOptions.parseOptions = {
×
179
        ...compileOptions.parseOptions
×
180
        ...options.parseOptions
×
181
      }
×
182
      compileOptions.threads = options.threads if options.threads?
×
183

×
184
      if transformTS or ts is "tsc"
×
185
        ts := await tsPromise!
×
186

×
187
        tsConfigPath := ts.findConfigFile process.cwd(), ts.sys.fileExists
×
188

×
189
        unless tsConfigPath
×
190
          throw new Error "Could not find 'tsconfig.json'"
×
191

×
192
        { config, error } := ts.readConfigFile
×
193
          tsConfigPath
×
194
          ts.sys.readFile
×
195

×
196
        if error
×
197
          console.error ts.formatDiagnostic error, getFormatHost ts.sys
×
198
          throw error
×
199

×
200
        // Mogrify tsconfig.json "files" field to use .civet.tsx
×
201
        function mogrify(key: string)
×
202
          if key in config and Array.isArray config[key]
×
203
            config[key] = config[key].map (item: unknown) =>
×
204
              return item unless item <? "string"
×
205
              return item.replace(/\.civet\b(?!\.)/g, '.civet.tsx')
×
206
        mogrify "files"
×
207

×
208
        // Override readDirectory (used for include/exclude matching)
×
209
        // to include .civet files, as .civet.tsx files
×
210
        system := {...ts.sys}
×
211
        {readDirectory: systemReadDirectory} := system
×
212
        system.readDirectory = (path: string, extensions?: readonly string[], excludes?: readonly string[], includes?: readonly string[], depth?: number): string[] =>
×
213
          extensions = [ ...(extensions ?? []), ".civet" ]
×
214
          systemReadDirectory(path, extensions, excludes, includes, depth)
×
215
          .map &.endsWith(".civet") ? & + ".tsx" : &
×
216

×
217
        configContents := ts.parseJsonConfigFileContent
×
218
          config
×
219
          system
×
220
          process.cwd()
×
221
        configErrors = configContents.errors
×
222
        configFileNames = configContents.fileNames
×
223

×
224
        compilerOptions = {
×
225
          ...configContents.options
×
226
          target: ts.ScriptTarget.ESNext
×
227
          composite: false
×
228
        }
×
229
        // We use .tsx extensions when type checking, so need to enable
×
230
        // JSX mode even if the user doesn't request/use it.
×
231
        compilerOptions.jsx ??= ts.JsxEmit.Preserve
×
232
        compilerOptionsWithSourceMap = {
×
233
          ...compilerOptions
×
234
          sourceMap: true
×
235
        }
×
236
        fsMap = new Map()
×
237

×
238
    async buildEnd(useConfigFileNames = false): Promise<void>
×
239
      if transformTS
×
240
        const ts = await tsPromise!
×
241

×
242
        // Create a virtual file system with all source files processed so far,
×
243
        // but which further resolves any Civet dependencies that are needed
×
244
        // just for typechecking (e.g. `import type` which get removed in JS).
×
245
        system := createFSBackedSystem fsMap, process.cwd(), ts
×
246
        {
×
247
          fileExists: systemFileExists
×
248
          readFile: systemReadFile
×
249
          readDirectory: systemReadDirectory
×
250
        } := system
×
251

×
252
        system.fileExists = (filename: string): boolean =>
×
253
          if (!filename.endsWith('.civet.tsx')) return systemFileExists(filename)
×
254
          if (fsMap.has(filename)) return true
×
255
          return systemFileExists filename[...-4]
×
256

×
257
        system.readDirectory = (path: string): string[] =>
×
258
          systemReadDirectory(path)
×
259
          .map &.endsWith('.civet') ? & + '.tsx' : &
×
260

×
261
        tsCompileOptions := {
×
262
          ...compileOptions
×
263
          rewriteCivetImports: false
×
264
          rewriteTsImports: true
×
265
        }
×
266
        system.readFile = (filename: string, encoding: BufferEncoding = 'utf-8'): string? =>
×
267
          // Mogrify package.json imports field to use .civet.tsx
×
268
          if path.basename(filename) is "package.json"
×
269
            json := systemReadFile filename, encoding
×
270
            return json unless json
×
271
            parsed: Record<string, unknown> := JSON.parse(json)
×
272
            modified .= false
×
273
            function recurse(node: unknown): void
×
274
              if node? <? "object"
×
275
                for key in node
×
276
                  value := (node as Record<string, unknown>)[key]
×
277
                  if value <? "string"
×
278
                    if value.endsWith ".civet"
×
279
                      (node as Record<string, unknown>)[key] = value + '.tsx'
×
280
                      modified = true
×
281
                  else if value
×
282
                    recurse value
×
283
            recurse parsed.imports
×
284
            return modified ? JSON.stringify(parsed) : json
×
285

×
286
          // Generate .civet.tsx files on the fly
×
287
          if (!filename.endsWith('.civet.tsx')) return systemReadFile(filename, encoding)
×
288
          if (fsMap.has(filename)) return fsMap.get(filename)
×
289
          civetFilename := filename[...-4]
×
290
          rawCivetSource := fs.readFileSync civetFilename, {encoding}
×
291
          { code: compiledTS, sourceMap } := civet.compile rawCivetSource, {
×
292
            ...tsCompileOptions
×
293
            filename
×
294
            js: false
×
295
            sourceMap: true
×
296
            sync: true // TS readFile API seems to need to be synchronous
×
297
          }
×
298
          fsMap.set filename, compiledTS
×
299
          sourceMaps.set filename, sourceMap
×
300
          return compiledTS
×
301

×
302
        host := createVirtualCompilerHost
×
303
          system
×
304
          compilerOptions
×
305
          ts
×
306

×
307
        program := ts.createProgram
×
308
          rootNames: useConfigFileNames ? configFileNames : [...fsMap.keys()]
×
309
          options: compilerOptions
×
310
          host: host.compilerHost
×
311

×
312
        diagnostics: Diagnostic[] := ts
×
313
          .getPreEmitDiagnostics(program)
×
314
          .map (diagnostic) =>
×
315
            file := diagnostic.file
×
316
            if (!file) return diagnostic
×
317

×
318
            sourceMap := sourceMaps.get file.fileName
×
319
            if (!sourceMap) return diagnostic
×
320

×
321
            sourcemapLines := sourceMap.lines ?? sourceMap.data.lines
×
322
            range := remapRange(
×
323
              {
×
324
                start: diagnostic.start || 0,
×
325
                end: (diagnostic.start || 0) + (diagnostic.length || 1),
×
326
              },
×
327
              sourcemapLines
×
328
            )
×
329

×
330
            {
×
331
              ...diagnostic,
×
332
              messageText: flattenDiagnosticMessageText(diagnostic.messageText),
×
333
              length: diagnostic.length,
×
334
              start: range.start,
×
335
            }
×
336

×
337
        if configErrors?#
×
338
          diagnostics.unshift ...configErrors
×
339

×
340
        if diagnostics# > 0
×
341
          console.error
×
342
            ts.formatDiagnosticsWithColorAndContext
×
343
              diagnostics
×
344
              getFormatHost ts.sys
×
345
          if options.typecheck
×
346
            failures: DiagnosticCategory[] .= []
×
347
            if options.typecheck <? "string"
×
348
              if (options.typecheck.includes('error')) failures.push(DiagnosticCategory.Error)
×
349
              if (options.typecheck.includes('warning')) failures.push(DiagnosticCategory.Warning)
×
350
              if (options.typecheck.includes('suggestion')) failures.push(DiagnosticCategory.Suggestion)
×
351
              if (options.typecheck.includes('message')) failures.push(DiagnosticCategory.Message)
×
352
              if (options.typecheck.includes('all'))
×
353
                failures = { includes: () => true } as any as DiagnosticCategory[]
×
354
            else
×
355
              // Default behavior: fail on errors
×
356
              failures.push(DiagnosticCategory.Error)
×
357
            count := diagnostics.filter((d) => failures.includes(d.category)).length
×
358
            if count
×
359
              reason :=
×
360
                (count is diagnostics# ? count : `${count} out of ${diagnostics#}`)
×
361
              throw new Error `Aborting build because of ${reason} TypeScript diagnostic${diagnostics.length > 1 ? 's' : ''} above`
×
362

×
363
        if options.emitDeclaration
×
364
          if meta.framework is 'esbuild' and not esbuildOptions.outdir
×
365
            throw new Error "Civet unplugin's `emitDeclaration` requires esbuild's `outdir` option to be set;"
×
366

×
367
          // Removed duplicate slashed (`\`) versions of the same file for emit
×
368
          for file of fsMap.keys()
×
369
            slashed := slash file
×
370
            unless file is slashed
×
371
              fsMap.delete slashed
×
372

×
373
          for file of fsMap.keys()
×
374
            sourceFile := program.getSourceFile(file)!
×
375
            program.emit
×
376
              sourceFile
×
377
              (filePath, content) =>
×
378
                if options.declarationExtension?
×
379
                  if filePath.endsWith '.d.ts'
×
380
                    filePath = filePath[< -5]
×
381
                  else
×
382
                    console.log `WARNING: No .d.ts extension in ${filePath}`
×
383
                  if filePath.endsWith civetSuffix
×
384
                    filePath = filePath[< -civetSuffix#]
×
385
                  else
×
386
                    console.log `WARNING: No .civet extension in ${filePath}`
×
387
                  filePath += options.declarationExtension
×
388

×
389
                pathFromDistDir .= path.relative
×
390
                  compilerOptions.outDir ?? process.cwd()
×
391
                  filePath
×
392

×
393
                this.emitFile
×
394
                  source: content
×
395
                  fileName: pathFromDistDir
×
396
                  type: 'asset'
×
397
              undefined
×
398
              true // emitDtsOnly
×
399
              undefined
×
400
              // @ts-ignore @internal interface
×
401
              true // forceDtsEmit
×
402

×
403
    resolveId(id, importer, options)
×
404
      id = aliasResolver id if aliasResolver?
×
405
      if (/\0/.test(id)) return null
×
406

×
407
      // Remove query/hash postfix to get actual path
×
408
      {filename, postfix} := extractCivetFilename id, outExt
×
409

×
410
      resolved .=
×
411
        if path.isAbsolute filename
×
412
          resolveAbsolutePath rootDir, filename, implicitExtension
×
413
        else
×
414
          path.resolve path.dirname(importer ?? ''), filename
×
415
      if (!resolved) return null
×
416

×
417
      // Implicit .civet extension
×
418
      unless resolved.endsWith civetSuffix
×
419
        if (!implicitExtension) return null
×
420
        implicitId := implicitCivet resolved
×
421
        if (!implicitId) return null
×
422
        resolved = implicitId
×
423

×
424
      // Tell Vite that this is a virtual module during dependency scanning
×
425
      if (options as! {scan?: boolean}).scan and meta.framework is 'vite'
×
426
        resolved = `\0${resolved}`
×
427

×
428
      // Add back the original postfix at the end
×
429
      return resolved + outExt + postfix
×
430

×
431
    loadInclude(id)
×
432
      {filename, postfix} := extractCivetFilename id, outExt
×
433
      return false if skipWorker and workerRE.test postfix
×
434
      filename.endsWith civetSuffix
×
435

×
436
    async load(id)
×
437
      {filename} .= extractCivetFilename id, outExt
×
438
      // We're guaranteed to have .civet extension here by loadInclude
×
439

×
440
      filename = path.resolve rootDir, filename
×
441
      @addWatchFile filename
×
442

×
443
      let mtime: number?, cached: CacheEntry?, resolve: =>?
×
444
      if cache?
×
445
        try
×
446
          mtime = fs.promises.stat(filename) |> await |> .mtimeMs
×
447
        // If we fail to stat file, ignore cache
×
448
        if mtime?
×
449
          cached = cache.get filename
×
450
          if cached and cached.mtime is mtime
×
451
            // If the file is currently being compiled, wait for it to finish
×
452
            await cached.promise if cached.promise
×
453
            if result? := cached.result
×
454
              return result
×
455
          // We're the first to compile this file with this mtime
×
456
          promise := new Promise<void> (r): void => resolve = r
×
457
          cache.set filename, cached = {mtime, promise}
×
458
      finally resolve?()
×
459

×
460
      let compiled: string
×
461
      let sourceMap: SourceMap | string | undefined
×
462
      civetOptions := {
×
463
        ...compileOptions
×
464
        filename: id
×
465
        errors: []
×
466
      }
×
467
      function checkErrors
×
468
        if civetOptions.errors#
×
469
          throw new civet.ParseErrors civetOptions.errors
×
470

×
471
      rawCivetSource := decode await fs.promises.readFile filename
×
472
      ast := await civet.compile rawCivetSource, {
×
473
        ...civetOptions
×
474
        ast: true
×
475
      }
×
476
      civetSourceMap := new SourceMap rawCivetSource
×
477

×
478
      if ts is "civet"
×
479
        compiled = await civet.generate ast, {
×
480
          ...civetOptions
×
481
          js: true
×
482
          sourceMap: civetSourceMap
×
483
        }
×
484
        sourceMap = civetSourceMap
×
485
        checkErrors()
×
486
      else
×
487
        compiledTS := await civet.generate ast, {
×
488
          ...civetOptions
×
489
          js: false
×
490
          sourceMap: civetSourceMap
×
491
        }
×
492
        checkErrors()
×
493

×
494
        switch ts
×
495
          when "esbuild"
×
496
            esbuildTransform := import("esbuild") |> await |> .transform
×
497
            result := await esbuildTransform compiledTS,
×
498
              jsx: "preserve"
×
499
              loader: "tsx"
×
500
              sourcefile: id
×
501
              sourcemap: "external"
×
502

×
503
            compiled = result.code
×
504
            sourceMap = result.map
×
505
          when "tsc"
×
506
            tsTranspile := tsPromise! |> await |> .transpileModule
×
507
            result := tsTranspile compiledTS,
×
508
              compilerOptions: compilerOptionsWithSourceMap
×
509

×
510
            compiled = result.outputText
×
511
            sourceMap = result.sourceMapText
×
512
          when "preserve"
×
513
            compiled = compiledTS
×
514
            sourceMap = civetSourceMap
×
515

×
516
      if transformTS
×
517
        // When working with TypeScript, disable rewriteCivetImports and
×
518
        // force rewriteTsImports by rewriting imports again.
×
519
        // See `ModuleSpecifier` in parser.hera
×
520
        for each _spec of lib.gatherRecursive ast, (
×
521
          ($) => ($ as {type: string}).type is "ModuleSpecifier"
×
522
        )
×
523
          spec := _spec as { module?: { token: string, input?: string } }
×
524
          if spec.module?.input
×
525
            spec.module.token = spec.module.input
×
526
            .replace /\.([mc])?ts(['"])$/, ".$1js$2"
×
527

×
528
        compiledTS := await civet.generate ast, {
×
529
          ...civetOptions
×
530
          js: false
×
531
          sourceMap: civetSourceMap
×
532
        }
×
533
        checkErrors()
×
534

×
535
        // Force .tsx extension for type checking purposes.
×
536
        // Otherwise, TypeScript complains about types in .jsx files.
×
537
        tsx := filename + '.tsx'
×
538
        fsMap.set tsx, compiledTS
×
539
        sourceMaps.set tsx, civetSourceMap
×
540
        // Vite and Rollup normalize filenames to use `/` instead of `\`.
×
541
        // We give the TypeScript VFS both versions just in case.
×
542
        slashed := slash tsx
×
543
        unless tsx is slashed
×
544
          fsMap.set slashed, compiledTS
×
545
          sourceMaps.set slashed, civetSourceMap
×
546

×
547
      jsonSourceMap := sourceMap and
×
548
        if sourceMap <? "string"
×
549
          JSON.parse(sourceMap)
×
550
        else
×
551
          sourceMap.json
×
552
            path.relative rootDir, extractCivetFilename(id, outExt).filename
×
553
            path.relative rootDir, id
×
554

×
555
      transformed: TransformResult .=
×
556
        code: compiled
×
557
        map: jsonSourceMap
×
558

×
559
      if options.transformOutput
×
560
        transformed = await options.transformOutput transformed.code, id
×
561

×
562
      if cached?
×
563
        cached.result = transformed
×
564
        delete cached.promise
×
565

×
566
      return transformed
×
567

×
568
    esbuild: {
×
569
      config(options: BuildOptions): void
×
570
        esbuildOptions = options
×
571
    }
×
572
    vite: {
×
573
      config(config: UserConfig): void
×
574
        // Vite 7 requires us to skip processing ?worker
×
575
        // and instead process a followup request for ?worker_file
×
576
        // Luckily, Vite 7 introduced @meta.viteVersion for detection
×
577
        //@ts-ignore lacking type for this
×
578
        if @?meta?viteVersion and 7 <= Number @meta.viteVersion.split('.')[0]
×
579
          skipWorker = true
×
580

×
581
        rootDir = path.resolve process.cwd(), config.root ?? ''
×
582

×
583
        if implicitExtension
×
584
          config.resolve ??= {}
×
585
          config.resolve.extensions ??= DEFAULT_EXTENSIONS
×
586
          config.resolve.extensions.push '.civet'
×
587
      async transformIndexHtml(html)
×
588
        html.replace /<!--[^]*?-->|<[^<>]*>/g, (tag) =>
×
589
          tag.replace /<\s*script\b[^<>]*>/gi, (script) =>
×
590
            // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
×
591
            script.replace
×
592
              /([:_\p{ID_Start}][:\p{ID_Continue}]*)(\s*=\s*("[^"]*"|'[^']*'|[^\s"'=<>`]*))?/gu
×
593
              (attr, name, value) =>
×
594
                name.toLowerCase() === 'src' && value
×
595
                  ? attr.replace(
×
596
                      /(\.civet)(['"]?)$/,
×
597
                      (_, extension, endQuote) =>
×
598
                        `${extension}${outExt}?transform${endQuote}`
×
599
                    )
×
600
                  : attr
×
601
      handleHotUpdate({ file, server, modules })
×
602
        // `file` is an absolute path to the changed file on disk,
×
603
        // so for our case it should end with .civet extension
×
604
        return unless file.endsWith '.civet'
×
605
        // Convert into path as would be output by `resolveId`
×
606
        resolvedId := slash path.resolve(file) + outExt
×
607
        // Check for modules for this file
×
608
        if fileModules := server.moduleGraph.getModulesByFile resolvedId
×
609
          // Invalidate modules depending on this one
×
610
          server.moduleGraph.onFileChange resolvedId
×
611
          // Hot reload this module
×
612
          return [ ...modules, ...fileModules ]
×
613
        modules
×
614
    }
×
615

×
616
    rspack(compiler)
×
617
      if implicitExtension
×
618
        compiler.options ?= {}
×
619
        compiler.options.resolve ?= {}
×
620
        // Default from https://rspack.dev/config/resolve#resolveextensions
×
621
        compiler.options.resolve.extensions ?= ['', '.js', '.json', '.wasm']
×
622
        compiler.options.resolve.extensions.unshift ".civet"
×
623
    webpack(compiler)
×
624
      if implicitExtension
×
625
        compiler.options ?= {}
×
626
        compiler.options.resolve ?= {}
×
627
        // Default from https://webpack.js.org/configuration/resolve/#resolveextensions
×
628
        compiler.options.resolve.extensions ?= ['', '.js', '.json', '.wasm']
×
629
        compiler.options.resolve.extensions.unshift ".civet"
×
630
      aliasResolver = (id) =>
×
631
        // Based on normalizeAlias from
×
632
        // https://github.com/webpack/enhanced-resolve/blob/72999caf002f6f7bb4624e65fdeb7ba980b11e24/lib/ResolverFactory.js#L158
×
633
        // and AliasPlugin from
×
634
        // https://github.com/webpack/enhanced-resolve/blob/72999caf002f6f7bb4624e65fdeb7ba980b11e24/lib/AliasPlugin.js
×
635
        for key, value in compiler.options.resolve.alias
×
636
          if key.endsWith '$'
×
637
            if id is key[...-1]
×
638
              return value <? 'string' ? value : '\0'
×
639
          else
×
640
            if id is key or id.startsWith key + '/'
×
641
              return '\0' unless value <? 'string'
×
642
              return value + id[key.length..]
×
643
        id
×
644
  }
×
645

1✔
646
var unplugin = createUnplugin(rawPlugin)
1✔
647
export default unplugin
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc