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

DanielXMoore / Civet / 21772072879

07 Feb 2026 01:50AM UTC coverage: 91.42% (-0.2%) from 91.596%
21772072879

Pull #1779

github

web-flow
Merge 817af4c6a into 3f8c07ee4
Pull Request #1779: LSP: fix(diagnostics), ensure immediate updates for opened dependent files on change

3777 of 4118 branches covered (91.72%)

Branch coverage included in aggregate %.

19185 of 20999 relevant lines covered (91.36%)

17409.21 hits per line

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

12.5
/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
postfixRE := /[?#].*$/s
1✔
56
isWindows := os.platform() is 'win32'
1✔
57
windowsSlashRE := /\\/g
1✔
58
civetSuffix := '.civet'
1✔
59
workerRE := /(?:\?|&)(?:worker|sharedworker)(?:&|$|#)/
1✔
60

1✔
61
/**
1✔
62
Extract a possible Civet filename from an id, after removing a possible
1✔
63
outputExtension and/or a query/hash (?/#) postfix.
1✔
64
Returns {filename, postfix} in case you need to add the postfix back.
1✔
65
You should check whether the filename ends in .civet extension,
1✔
66
or needs an implicit .civet extension, or isn't Civet-related at all.
1✔
67
*/
1✔
68
function extractCivetFilename(id: string, outputExtension: string): {filename: string, postfix: string}
×
69
  postfix .= ''
×
70
  filename .= id.replace postfixRE, (match) =>
×
71
    postfix = match
×
72
    ''
×
73
  // Normally the outputExtension (.jsx/.tsx by default) should be present,
×
74
  // but sometimes (e.g. esbuild's alias feature) load directly without resolve
×
75
  if filename.endsWith outputExtension
×
76
    filename = filename[< -outputExtension#]
×
77
  {filename, postfix}
×
78

1✔
79
function tryStatSync(file: string): fs.Stats?
×
80
  try
×
81
    // The "throwIfNoEntry" is a performance optimization for cases where the file does not exist
×
82
    return fs.statSync(file, { throwIfNoEntry: false });
×
83

1✔
84
export function slash(p: string): string
1✔
85
  p.replace windowsSlashRE, '/'
×
86

1✔
87
function normalizePath(id: string): string
×
88
  path.posix.normalize isWindows ? slash(id) : id
×
89

1✔
90
function tryFsResolve(file: string): string?
×
91
  fileStat := tryStatSync file
×
92
  if fileStat?.isFile()
×
93
    normalizePath file
×
94

1✔
95
function resolveAbsolutePath(rootDir: string, id: string, implicitExtension: boolean)
×
96
  file := path.join rootDir, id
×
97
  // Check for existence of resolved file and unresolved id,
×
98
  // without and with implicit .civet extension, and return first existing
×
99
  (or)
×
100
    tryFsResolve(file)
×
101
    implicitExtension and implicitCivet file
×
102
    tryFsResolve id
×
103
    implicitExtension and implicitCivet id
×
104

1✔
105
function implicitCivet(file: string): string?
×
106
  return if tryFsResolve file
×
107
  civet := file + '.civet'
×
108
  return civet if tryFsResolve civet
×
109

1✔
110
export const rawPlugin: Parameters<typeof createUnplugin<PluginOptions>>[0] =
1✔
111
(options: PluginOptions = {}, meta) =>
×
112
  if (options.dts) options.emitDeclaration = options.dts
×
113
  compileOptions: CompileOptions .= {}
×
114

×
115
  ts .= options.ts
×
116
  if (options.js) ts = 'civet'
×
117
  unless ts?
×
118
    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.'
×
119
    ts = "civet"
×
120
  unless ts is in ["civet", "esbuild", "tsc", "preserve"]
×
121
    console.log `WARNING: Invalid option ts: ${JSON.stringify ts}; switching to "civet"`
×
122
    ts = "civet"
×
123

×
124
  transformTS := options.emitDeclaration or options.typecheck
×
125
  outExt :=
×
126
    options.outputExtension ?? (ts is "preserve" ? ".tsx" : ".jsx")
×
127
  implicitExtension := options.implicitExtension ?? true
×
128
  let aliasResolver: (id: string) => string
×
129

×
130
  fsMap: Map<string, string> .= new Map
×
131
  sourceMaps := new Map<string, SourceMap>
×
132
  let compilerOptions: any, compilerOptionsWithSourceMap: any
×
133
  rootDir .= process.cwd()
×
134
  let esbuildOptions: BuildOptions
×
135
  let configErrors: Diagnostic[]?
×
136
  let configFileNames: string[]
×
137
  skipWorker .= false
×
138

×
139
  tsPromise := if transformTS or ts is "tsc"
×
140
    import('typescript').then .default
×
141
  getFormatHost := (sys: System): FormatDiagnosticsHost =>
×
142
    return {
×
143
      getCurrentDirectory: => sys.getCurrentDirectory()
×
144
      getNewLine: => sys.newLine
×
145
      getCanonicalFileName: sys.useCaseSensitiveFileNames
×
146
        ? (f) => f
×
147
        : (f) => f.toLowerCase()
×
148
    }
×
149

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

×
152
  plugin: ReturnType<typeof rawPlugin> := {
×
153
    name: 'unplugin-civet'
×
154
    enforce: 'pre'
×
155

×
156
    async buildStart(): Promise<void>
×
157
      civetConfigPath .= options.config
×
158
      if civetConfigPath is undefined
×
159
        civetConfigPath = await findInDir process.cwd()
×
160
      if civetConfigPath
×
161
        compileOptions = await loadConfig civetConfigPath
×
162
      // Merge parseOptions, with plugin options taking priority
×
163
      compileOptions.parseOptions = {
×
164
        ...compileOptions.parseOptions
×
165
        ...options.parseOptions
×
166
      }
×
167
      compileOptions.threads = options.threads if options.threads?
×
168

×
169
      if transformTS or ts is "tsc"
×
170
        ts := await tsPromise!
×
171

×
172
        tsConfigPath := ts.findConfigFile process.cwd(), ts.sys.fileExists
×
173

×
174
        unless tsConfigPath
×
175
          throw new Error "Could not find 'tsconfig.json'"
×
176

×
177
        { config, error } := ts.readConfigFile
×
178
          tsConfigPath
×
179
          ts.sys.readFile
×
180

×
181
        if error
×
182
          console.error ts.formatDiagnostic error, getFormatHost ts.sys
×
183
          throw error
×
184

×
185
        // Mogrify tsconfig.json "files" field to use .civet.tsx
×
186
        function mogrify(key: string)
×
187
          if key in config and Array.isArray config[key]
×
188
            config[key] = config[key].map (item: unknown) =>
×
189
              return item unless item <? "string"
×
190
              return item.replace(/\.civet\b(?!\.)/g, '.civet.tsx')
×
191
        mogrify "files"
×
192

×
193
        // Override readDirectory (used for include/exclude matching)
×
194
        // to include .civet files, as .civet.tsx files
×
195
        system := {...ts.sys}
×
196
        {readDirectory: systemReadDirectory} := system
×
197
        system.readDirectory = (path: string, extensions?: readonly string[], excludes?: readonly string[], includes?: readonly string[], depth?: number): string[] =>
×
198
          extensions = [ ...(extensions ?? []), ".civet" ]
×
199
          systemReadDirectory(path, extensions, excludes, includes, depth)
×
200
          .map &.endsWith(".civet") ? & + ".tsx" : &
×
201

×
202
        configContents := ts.parseJsonConfigFileContent
×
203
          config
×
204
          system
×
205
          process.cwd()
×
206
        configErrors = configContents.errors
×
207
        configFileNames = configContents.fileNames
×
208

×
209
        compilerOptions = {
×
210
          ...configContents.options
×
211
          target: ts.ScriptTarget.ESNext
×
212
          composite: false
×
213
        }
×
214
        // We use .tsx extensions when type checking, so need to enable
×
215
        // JSX mode even if the user doesn't request/use it.
×
216
        compilerOptions.jsx ??= ts.JsxEmit.Preserve
×
217
        compilerOptionsWithSourceMap = {
×
218
          ...compilerOptions
×
219
          sourceMap: true
×
220
        }
×
221
        fsMap = new Map()
×
222

×
223
    async buildEnd(useConfigFileNames = false): Promise<void>
×
224
      if transformTS
×
225
        const ts = await tsPromise!
×
226

×
227
        // Create a virtual file system with all source files processed so far,
×
228
        // but which further resolves any Civet dependencies that are needed
×
229
        // just for typechecking (e.g. `import type` which get removed in JS).
×
230
        system := createFSBackedSystem fsMap, process.cwd(), ts
×
231
        {
×
232
          fileExists: systemFileExists
×
233
          readFile: systemReadFile
×
234
          readDirectory: systemReadDirectory
×
235
        } := system
×
236

×
237
        system.fileExists = (filename: string): boolean =>
×
238
          if (!filename.endsWith('.civet.tsx')) return systemFileExists(filename)
×
239
          if (fsMap.has(filename)) return true
×
240
          return systemFileExists filename[...-4]
×
241

×
242
        system.readDirectory = (path: string): string[] =>
×
243
          systemReadDirectory(path)
×
244
          .map &.endsWith('.civet') ? & + '.tsx' : &
×
245

×
246
        tsCompileOptions := {
×
247
          ...compileOptions
×
248
          rewriteCivetImports: false
×
249
          rewriteTsImports: true
×
250
        }
×
251
        system.readFile = (filename: string, encoding: BufferEncoding = 'utf-8'): string? =>
×
252
          // Mogrify package.json imports field to use .civet.tsx
×
253
          if path.basename(filename) is "package.json"
×
254
            json := systemReadFile filename, encoding
×
255
            return json unless json
×
256
            parsed: Record<string, unknown> := JSON.parse(json)
×
257
            modified .= false
×
258
            function recurse(node: unknown): void
×
259
              if node? <? "object"
×
260
                for key in node
×
261
                  value := (node as Record<string, unknown>)[key]
×
262
                  if value <? "string"
×
263
                    if value.endsWith ".civet"
×
264
                      (node as Record<string, unknown>)[key] = value + '.tsx'
×
265
                      modified = true
×
266
                  else if value
×
267
                    recurse value
×
268
            recurse parsed.imports
×
269
            return modified ? JSON.stringify(parsed) : json
×
270

×
271
          // Generate .civet.tsx files on the fly
×
272
          if (!filename.endsWith('.civet.tsx')) return systemReadFile(filename, encoding)
×
273
          if (fsMap.has(filename)) return fsMap.get(filename)
×
274
          civetFilename := filename[...-4]
×
275
          rawCivetSource := fs.readFileSync civetFilename, {encoding}
×
276
          { code: compiledTS, sourceMap } := civet.compile rawCivetSource, {
×
277
            ...tsCompileOptions
×
278
            filename
×
279
            js: false
×
280
            sourceMap: true
×
281
            sync: true // TS readFile API seems to need to be synchronous
×
282
          }
×
283
          fsMap.set filename, compiledTS
×
284
          sourceMaps.set filename, sourceMap
×
285
          return compiledTS
×
286

×
287
        host := createVirtualCompilerHost
×
288
          system
×
289
          compilerOptions
×
290
          ts
×
291

×
292
        program := ts.createProgram
×
293
          rootNames: useConfigFileNames ? configFileNames : [...fsMap.keys()]
×
294
          options: compilerOptions
×
295
          host: host.compilerHost
×
296

×
297
        diagnostics: Diagnostic[] := ts
×
298
          .getPreEmitDiagnostics(program)
×
299
          .map (diagnostic) =>
×
300
            file := diagnostic.file
×
301
            if (!file) return diagnostic
×
302

×
303
            sourceMap := sourceMaps.get file.fileName
×
304
            if (!sourceMap) return diagnostic
×
305

×
306
            sourcemapLines := sourceMap.lines ?? sourceMap.data.lines
×
307
            range := remapRange(
×
308
              {
×
309
                start: diagnostic.start || 0,
×
310
                end: (diagnostic.start || 0) + (diagnostic.length || 1),
×
311
              },
×
312
              sourcemapLines
×
313
            )
×
314

×
315
            {
×
316
              ...diagnostic,
×
317
              messageText: flattenDiagnosticMessageText(diagnostic.messageText),
×
318
              length: diagnostic.length,
×
319
              start: range.start,
×
320
            }
×
321

×
322
        if configErrors?#
×
323
          diagnostics.unshift ...configErrors
×
324

×
325
        if diagnostics# > 0
×
326
          console.error
×
327
            ts.formatDiagnosticsWithColorAndContext
×
328
              diagnostics
×
329
              getFormatHost ts.sys
×
330
          if options.typecheck
×
331
            failures: DiagnosticCategory[] .= []
×
332
            if options.typecheck <? "string"
×
333
              if (options.typecheck.includes('error')) failures.push(DiagnosticCategory.Error)
×
334
              if (options.typecheck.includes('warning')) failures.push(DiagnosticCategory.Warning)
×
335
              if (options.typecheck.includes('suggestion')) failures.push(DiagnosticCategory.Suggestion)
×
336
              if (options.typecheck.includes('message')) failures.push(DiagnosticCategory.Message)
×
337
              if (options.typecheck.includes('all'))
×
338
                failures = { includes: () => true } as any as DiagnosticCategory[]
×
339
            else
×
340
              // Default behavior: fail on errors
×
341
              failures.push(DiagnosticCategory.Error)
×
342
            count := diagnostics.filter((d) => failures.includes(d.category)).length
×
343
            if count
×
344
              reason :=
×
345
                (count is diagnostics# ? count : `${count} out of ${diagnostics#}`)
×
346
              throw new Error `Aborting build because of ${reason} TypeScript diagnostic${diagnostics.length > 1 ? 's' : ''} above`
×
347

×
348
        if options.emitDeclaration
×
349
          if meta.framework is 'esbuild' and not esbuildOptions.outdir
×
350
            throw new Error "Civet unplugin's `emitDeclaration` requires esbuild's `outdir` option to be set;"
×
351

×
352
          // Removed duplicate slashed (`\`) versions of the same file for emit
×
353
          for file of fsMap.keys()
×
354
            slashed := slash file
×
355
            unless file is slashed
×
356
              fsMap.delete slashed
×
357

×
358
          for file of fsMap.keys()
×
359
            sourceFile := program.getSourceFile(file)!
×
360
            program.emit
×
361
              sourceFile
×
362
              (filePath, content) =>
×
363
                if options.declarationExtension?
×
364
                  if filePath.endsWith '.d.ts'
×
365
                    filePath = filePath[< -5]
×
366
                  else
×
367
                    console.log `WARNING: No .d.ts extension in ${filePath}`
×
368
                  if filePath.endsWith civetSuffix
×
369
                    filePath = filePath[< -civetSuffix#]
×
370
                  else
×
371
                    console.log `WARNING: No .civet extension in ${filePath}`
×
372
                  filePath += options.declarationExtension
×
373

×
374
                pathFromDistDir .= path.relative
×
375
                  compilerOptions.outDir ?? process.cwd()
×
376
                  filePath
×
377

×
378
                this.emitFile
×
379
                  source: content
×
380
                  fileName: pathFromDistDir
×
381
                  type: 'asset'
×
382
              undefined
×
383
              true // emitDtsOnly
×
384
              undefined
×
385
              // @ts-ignore @internal interface
×
386
              true // forceDtsEmit
×
387

×
388
    resolveId(id, importer, options)
×
389
      id = aliasResolver id if aliasResolver?
×
390
      if (/\0/.test(id)) return null
×
391

×
392
      // Remove query/hash postfix to get actual path
×
393
      {filename, postfix} := extractCivetFilename id, outExt
×
394

×
395
      resolved .=
×
396
        if path.isAbsolute filename
×
397
          resolveAbsolutePath rootDir, filename, implicitExtension
×
398
        else
×
399
          path.resolve path.dirname(importer ?? ''), filename
×
400
      if (!resolved) return null
×
401

×
402
      // Implicit .civet extension
×
403
      unless resolved.endsWith civetSuffix
×
404
        if (!implicitExtension) return null
×
405
        implicitId := implicitCivet resolved
×
406
        if (!implicitId) return null
×
407
        resolved = implicitId
×
408

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

×
413
      // Add back the original postfix at the end
×
414
      return resolved + outExt + postfix
×
415

×
416
    loadInclude(id)
×
417
      {filename, postfix} := extractCivetFilename id, outExt
×
418
      return false if skipWorker and workerRE.test postfix
×
419
      filename.endsWith civetSuffix
×
420

×
421
    async load(id)
×
422
      {filename} .= extractCivetFilename id, outExt
×
423
      // We're guaranteed to have .civet extension here by loadInclude
×
424

×
425
      filename = path.resolve rootDir, filename
×
426
      @addWatchFile filename
×
427

×
428
      let mtime: number?, cached: CacheEntry?, resolve: =>?
×
429
      if cache?
×
430
        try
×
431
          mtime = fs.promises.stat(filename) |> await |> .mtimeMs
×
432
        // If we fail to stat file, ignore cache
×
433
        if mtime?
×
434
          cached = cache.get filename
×
435
          if cached and cached.mtime is mtime
×
436
            // If the file is currently being compiled, wait for it to finish
×
437
            await cached.promise if cached.promise
×
438
            if result? := cached.result
×
439
              return result
×
440
          // We're the first to compile this file with this mtime
×
441
          promise := new Promise<void> (r): void => resolve = r
×
442
          cache.set filename, cached = {mtime, promise}
×
443
      finally resolve?()
×
444

×
445
      let compiled: string
×
446
      let sourceMap: SourceMap | string | undefined
×
447
      civetOptions := {
×
448
        ...compileOptions
×
449
        filename: id
×
450
        errors: []
×
451
      }
×
452
      function checkErrors
×
453
        if civetOptions.errors#
×
454
          throw new civet.ParseErrors civetOptions.errors
×
455

×
456
      rawCivetSource := decode await fs.promises.readFile filename
×
457
      ast := await civet.compile rawCivetSource, {
×
458
        ...civetOptions
×
459
        ast: true
×
460
      }
×
461
      civetSourceMap := new SourceMap rawCivetSource
×
462

×
463
      if ts is "civet"
×
464
        compiled = await civet.generate ast, {
×
465
          ...civetOptions
×
466
          js: true
×
467
          sourceMap: civetSourceMap
×
468
        }
×
469
        sourceMap = civetSourceMap
×
470
        checkErrors()
×
471
      else
×
472
        compiledTS := await civet.generate ast, {
×
473
          ...civetOptions
×
474
          js: false
×
475
          sourceMap: civetSourceMap
×
476
        }
×
477
        checkErrors()
×
478

×
479
        switch ts
×
480
          when "esbuild"
×
481
            esbuildTransform := import("esbuild") |> await |> .transform
×
482
            result := await esbuildTransform compiledTS,
×
483
              jsx: "preserve"
×
484
              loader: "tsx"
×
485
              sourcefile: id
×
486
              sourcemap: "external"
×
487

×
488
            compiled = result.code
×
489
            sourceMap = result.map
×
490
          when "tsc"
×
491
            tsTranspile := tsPromise! |> await |> .transpileModule
×
492
            result := tsTranspile compiledTS,
×
493
              compilerOptions: compilerOptionsWithSourceMap
×
494

×
495
            compiled = result.outputText
×
496
            sourceMap = result.sourceMapText
×
497
          when "preserve"
×
498
            compiled = compiledTS
×
499
            sourceMap = civetSourceMap
×
500

×
501
      if transformTS
×
502
        // When working with TypeScript, disable rewriteCivetImports and
×
503
        // force rewriteTsImports by rewriting imports again.
×
504
        // See `ModuleSpecifier` in parser.hera
×
505
        for each _spec of lib.gatherRecursive ast, (
×
506
          ($) => ($ as {type: string}).type is "ModuleSpecifier"
×
507
        )
×
508
          spec := _spec as { module?: { token: string, input?: string } }
×
509
          if spec.module?.input
×
510
            spec.module.token = spec.module.input
×
511
            .replace /\.([mc])?ts(['"])$/, ".$1js$2"
×
512

×
513
        compiledTS := await civet.generate ast, {
×
514
          ...civetOptions
×
515
          js: false
×
516
          sourceMap: civetSourceMap
×
517
        }
×
518
        checkErrors()
×
519

×
520
        // Force .tsx extension for type checking purposes.
×
521
        // Otherwise, TypeScript complains about types in .jsx files.
×
522
        tsx := filename + '.tsx'
×
523
        fsMap.set tsx, compiledTS
×
524
        sourceMaps.set tsx, civetSourceMap
×
525
        // Vite and Rollup normalize filenames to use `/` instead of `\`.
×
526
        // We give the TypeScript VFS both versions just in case.
×
527
        slashed := slash tsx
×
528
        unless tsx is slashed
×
529
          fsMap.set slashed, compiledTS
×
530
          sourceMaps.set slashed, civetSourceMap
×
531

×
532
      jsonSourceMap := sourceMap and
×
533
        if sourceMap <? "string"
×
534
          JSON.parse(sourceMap)
×
535
        else
×
536
          sourceMap.json
×
537
            path.relative rootDir, extractCivetFilename(id, outExt).filename
×
538
            path.relative rootDir, id
×
539

×
540
      transformed: TransformResult .=
×
541
        code: compiled
×
542
        map: jsonSourceMap
×
543

×
544
      if options.transformOutput
×
545
        transformed = await options.transformOutput transformed.code, id
×
546

×
547
      if cached?
×
548
        cached.result = transformed
×
549
        delete cached.promise
×
550

×
551
      return transformed
×
552

×
553
    esbuild: {
×
554
      config(options: BuildOptions): void
×
555
        esbuildOptions = options
×
556
    }
×
557
    vite: {
×
558
      config(config: UserConfig): void
×
559
        // Vite 7 requires us to skip processing ?worker
×
560
        // and instead process a followup request for ?worker_file
×
561
        // Luckily, Vite 7 introduced @meta.viteVersion for detection
×
562
        //@ts-ignore lacking type for this
×
563
        if @?meta?viteVersion and 7 <= Number @meta.viteVersion.split('.')[0]
×
564
          skipWorker = true
×
565

×
566
        rootDir = path.resolve process.cwd(), config.root ?? ''
×
567

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

×
601
    rspack(compiler)
×
602
      if implicitExtension
×
603
        compiler.options ?= {}
×
604
        compiler.options.resolve ?= {}
×
605
        // Default from https://rspack.dev/config/resolve#resolveextensions
×
606
        compiler.options.resolve.extensions ?= ['', '.js', '.json', '.wasm']
×
607
        compiler.options.resolve.extensions.unshift ".civet"
×
608
    webpack(compiler)
×
609
      if implicitExtension
×
610
        compiler.options ?= {}
×
611
        compiler.options.resolve ?= {}
×
612
        // Default from https://webpack.js.org/configuration/resolve/#resolveextensions
×
613
        compiler.options.resolve.extensions ?= ['', '.js', '.json', '.wasm']
×
614
        compiler.options.resolve.extensions.unshift ".civet"
×
615
      aliasResolver = (id) =>
×
616
        // Based on normalizeAlias from
×
617
        // https://github.com/webpack/enhanced-resolve/blob/72999caf002f6f7bb4624e65fdeb7ba980b11e24/lib/ResolverFactory.js#L158
×
618
        // and AliasPlugin from
×
619
        // https://github.com/webpack/enhanced-resolve/blob/72999caf002f6f7bb4624e65fdeb7ba980b11e24/lib/AliasPlugin.js
×
620
        for key, value in compiler.options.resolve.alias
×
621
          if key.endsWith '$'
×
622
            if id is key[...-1]
×
623
              return value <? 'string' ? value : '\0'
×
624
          else
×
625
            if id is key or id.startsWith key + '/'
×
626
              return '\0' unless value <? 'string'
×
627
              return value + id[key.length..]
×
628
        id
×
629
  }
×
630

1✔
631
var unplugin = createUnplugin(rawPlugin)
1✔
632
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