jq79 0.8.0 → 0.8.2

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/dev/vite.ts CHANGED
@@ -366,16 +366,61 @@ const declaredComponents = (source: string): string[] => {
366
366
  // content replaced. Attribute values are matched as quoted chunks so a ">"
367
367
  // inside one doesn't end the tag early
368
368
  const STYLE_BLOCK_RE = /<style((?:"[^"]*"|'[^']*'|[^>"'])*)>([\s\S]*?)<\/style\s*>/gi
369
- const LANG_ATTR_RE = /\blang\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i
370
- const TYPE_ATTR_RE = /\btype\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i
371
-
372
- // one attribute's value out of a tag's attribute string, whichever way it was
373
- // quoted - null when the attribute isn't there at all
374
- const attrValue = (attrs: string, re: RegExp): string | null => {
375
- const found = attrs.match(re)
376
- return found ? found[1] ?? found[2] ?? found[3] : null
369
+ // one attribute of a tag's attribute string: its name (lowercased, as the HTML
370
+ // parser reads it), its value (null for a bare `scoped`), and where it sits -
371
+ // `start` at the whitespace before it, so cutting [start, end) removes it cleanly
372
+ type AttrSpan = { name: string; value: string | null; start: number; end: number }
373
+
374
+ // a tag's attribute string read the way the HTML parser reads it: name, then an
375
+ // optional `=` and a double-quoted, single-quoted or bare value. A regex over the
376
+ // whole string can't do this - `\blang\s*=` also matches *inside* a value, and a
377
+ // prop with a default (`:setup="{ lang = 'en' }"`) then reads as the script's
378
+ // language, ahead of the real `lang="ts"`
379
+ const readAttrs = (attrs: string): AttrSpan[] => {
380
+ const spans: AttrSpan[] = []
381
+ const space = /[\s/]/
382
+ let i = 0
383
+ while (i < attrs.length) {
384
+ const start = i
385
+ while (i < attrs.length && space.test(attrs[i])) i++
386
+ const nameStart = i
387
+ while (i < attrs.length && !/[\s/=>]/.test(attrs[i])) i++
388
+ if (i === nameStart) {
389
+ if (i >= attrs.length) break
390
+ i++ // a stray `=` or `>`: skip it
391
+ continue
392
+ }
393
+ const name = attrs.slice(nameStart, i).toLowerCase()
394
+ let j = i
395
+ while (j < attrs.length && /\s/.test(attrs[j])) j++
396
+ let value: string | null = null
397
+ if (attrs[j] === "=") {
398
+ j++
399
+ while (j < attrs.length && /\s/.test(attrs[j])) j++
400
+ const quote = attrs[j]
401
+ if (quote === '"' || quote === "'") {
402
+ const close = attrs.indexOf(quote, j + 1)
403
+ const end = close === -1 ? attrs.length : close
404
+ value = attrs.slice(j + 1, end)
405
+ i = close === -1 ? end : end + 1
406
+ } else {
407
+ const valueStart = j
408
+ while (j < attrs.length && !/[\s>]/.test(attrs[j])) j++
409
+ value = attrs.slice(valueStart, j)
410
+ i = j
411
+ }
412
+ }
413
+ spans.push({ name, value, start, end: i })
414
+ }
415
+ return spans
377
416
  }
378
417
 
418
+ const findAttr = (attrs: string, name: string): AttrSpan | undefined =>
419
+ readAttrs(attrs).find(a => a.name === name)
420
+
421
+ // the attribute string without one attribute (and the whitespace before it)
422
+ const dropAttr = (attrs: string, span: AttrSpan): string => attrs.slice(0, span.start) + attrs.slice(span.end)
423
+
379
424
  // compiles <style lang="scss|less|styl|sass"> blocks to plain CSS with Vite's
380
425
  // own preprocessing (the same call @vitejs/plugin-vue makes), so the runtime
381
426
  // only ever sees CSS. The preprocessor picks its parser from the extension,
@@ -393,11 +438,11 @@ const compileStyleBlocks = async (
393
438
  const blocks = [...source.matchAll(STYLE_BLOCK_RE)]
394
439
  const compiled = await Promise.all(
395
440
  blocks.map(async ([, attrs, content]) => {
396
- const extension = attrValue(attrs, LANG_ATTR_RE)
397
- if (extension === null) return null
398
- const result = await preprocessCSS(content, `${file}.${extension}`, config)
441
+ const lang = findAttr(attrs, "lang")
442
+ if (!lang?.value) return null
443
+ const result = await preprocessCSS(content, `${file}.${lang.value}`, config)
399
444
  result.deps?.forEach(addWatchFile)
400
- return { attrs: attrs.replace(LANG_ATTR_RE, "").trimEnd(), css: result.code }
445
+ return { attrs: dropAttr(attrs, lang).trimEnd(), css: result.code }
401
446
  })
402
447
  )
403
448
 
@@ -429,11 +474,11 @@ const TS_TYPE_RE = /^(?:text|application)\/(?:x-)?typescript$/
429
474
  // types whichever one carried it. A `lang` answers on its own: `lang="coffee"`
430
475
  // is a language this plugin doesn't compile, and reading `type` past it would
431
476
  // compile a block the author said was something else
432
- const typescriptAttr = (attrs: string): RegExp | null => {
433
- const lang = attrValue(attrs, LANG_ATTR_RE)
434
- if (lang !== null) return TS_LANGS.has(lang.trim().toLowerCase()) ? LANG_ATTR_RE : null
435
- const type = attrValue(attrs, TYPE_ATTR_RE)
436
- return type !== null && TS_TYPE_RE.test(type.trim().toLowerCase()) ? TYPE_ATTR_RE : null
477
+ const typescriptAttr = (attrs: string): AttrSpan | null => {
478
+ const lang = findAttr(attrs, "lang")
479
+ if (lang?.value != null) return TS_LANGS.has(lang.value.trim().toLowerCase()) ? lang : null
480
+ const type = findAttr(attrs, "type")
481
+ return type?.value != null && TS_TYPE_RE.test(type.value.trim().toLowerCase()) ? type : null
437
482
  }
438
483
 
439
484
  type ViteTransform = (code: string, id: string, options?: unknown) => Promise<{ code: string }>
@@ -465,9 +510,6 @@ const stripTypes = async (ts: string, file: string): Promise<string> => {
465
510
  return code.replace(/^[ \t]*export\s*\{\s*\}\s*;?[ \t]*$/m, "")
466
511
  }
467
512
 
468
- // the `:setup` attribute, when it carries a value (a bare `:setup` is the
469
- // closed signature and can't be typed)
470
- const SETUP_ATTR_RE = /(:setup\s*=\s*)(?:"([^"]*)"|'([^']*)')/i
471
513
  // the arrow body the signature is wrapped in, so the parameter list can be
472
514
  // found again in the output without matching brackets
473
515
  const SIGNATURE_MARKER = "__jq79_signature__"
@@ -518,13 +560,14 @@ const compileScriptBlocks = async (source: string, file: string): Promise<string
518
560
  const marker = typescriptAttr(attrs)
519
561
  if (!marker) return null
520
562
 
521
- let rest = attrs.replace(marker, "").trimEnd()
522
- const setup = rest.match(SETUP_ATTR_RE)
523
- if (setup) {
524
- const signature = await stripSignatureTypes(setup[2] ?? setup[3], `${file}.signature.ts`)
525
- // a function replacement, not a string: `$&` and friends are live in a
526
- // replacement string and a default value is arbitrary source
527
- rest = rest.replace(SETUP_ATTR_RE, () => `${setup[1]}"${quoteAttrValue(signature)}"`)
563
+ let rest = dropAttr(attrs, marker).trimEnd()
564
+ // the `:setup` attribute, when it carries a value (a bare `:setup` is the
565
+ // closed signature and can't be typed)
566
+ const setup = findAttr(rest, ":setup")
567
+ if (setup?.value != null) {
568
+ const signature = await stripSignatureTypes(setup.value, `${file}.signature.ts`)
569
+ const before = rest.slice(setup.start, setup.end).match(/^\s*/)![0]
570
+ rest = rest.slice(0, setup.start) + `${before}:setup="${quoteAttrValue(signature)}"` + rest.slice(setup.end)
528
571
  }
529
572
 
530
573
  return { attrs: rest, js: await stripTypes(content, `${file}.ts`) }