@nuasite/cms-marker 0.0.46 → 0.0.48

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/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "directory": "packages/cms-marker"
15
15
  },
16
16
  "license": "Apache-2.0",
17
- "version": "0.0.46",
17
+ "version": "0.0.48",
18
18
  "module": "src/index.ts",
19
19
  "types": "src/index.ts",
20
20
  "type": "module",
@@ -5,6 +5,7 @@ import type { Plugin } from 'vite'
5
5
 
6
6
  export interface AstroTransformOptions {
7
7
  markComponents?: boolean
8
+ enabled?: boolean
8
9
  }
9
10
 
10
11
  /**
@@ -15,16 +16,27 @@ export interface AstroTransformOptions {
15
16
  * NOTE: Component marking is NOT done here because modifying component tags
16
17
  * in the raw .astro source breaks Astro's JSX-like parser. Component marking
17
18
  * is done at the HTML output level instead (in dev-middleware and build-processor).
19
+ *
20
+ * IMPORTANT: This plugin should only run in dev mode. During build, modifying
21
+ * .astro source files can cause Vite's build-import-analysis to fail with
22
+ * parsing errors. In build mode, source locations are extracted from Astro's
23
+ * compiler output in build-processor.ts instead.
18
24
  */
19
25
  export function createAstroTransformPlugin(options: AstroTransformOptions = {}): Plugin {
20
26
  // Component marking is intentionally disabled at the transform level
21
27
  // const { markComponents = true } = options;
28
+ const { enabled = true } = options
22
29
 
23
30
  return {
24
31
  name: 'astro-cms-source-injector',
25
32
  enforce: 'pre', // Run before Astro's own transforms
26
33
 
27
34
  async transform(code: string, id: string) {
35
+ // Skip transformation if disabled (e.g., during build mode)
36
+ if (!enabled) {
37
+ return null
38
+ }
39
+
28
40
  if (!id.endsWith('.astro')) {
29
41
  return null
30
42
  }
@@ -4,6 +4,7 @@ import path from 'node:path'
4
4
  import { fileURLToPath } from 'node:url'
5
5
  import { processHtml } from './html-processor'
6
6
  import type { ManifestWriter } from './manifest-writer'
7
+ import { findSourceLocation } from './source-finder'
7
8
  import type { CmsMarkerOptions } from './types'
8
9
 
9
10
  // Concurrency limit for parallel processing
@@ -66,8 +67,23 @@ async function processFile(
66
67
  idGenerator,
67
68
  )
68
69
 
69
- // Note: Source locations are now extracted from Astro's compiler attributes
70
- // in html-processor.ts, so we don't need the expensive findSourceLocation calls
70
+ // During build, source location attributes are not injected by astro-transform.ts
71
+ // (disabled to avoid Vite parse errors). Use findSourceLocation to look up source files.
72
+ for (const entry of Object.values(result.entries)) {
73
+ // Skip entries that already have source info from component detection
74
+ if (entry.sourcePath && !entry.sourcePath.endsWith('.html')) {
75
+ continue
76
+ }
77
+
78
+ const sourceLocation = await findSourceLocation(entry.text, entry.tag)
79
+ if (sourceLocation) {
80
+ entry.sourcePath = sourceLocation.file
81
+ entry.sourceLine = sourceLocation.line
82
+ entry.sourceSnippet = sourceLocation.snippet
83
+ entry.sourceType = sourceLocation.type
84
+ entry.variableName = sourceLocation.variableName
85
+ }
86
+ }
71
87
 
72
88
  // Add to manifest writer (handles per-page manifest writes)
73
89
  manifestWriter.addPage(pagePath, result.entries, result.components)
@@ -94,7 +94,7 @@ export class ComponentRegistry {
94
94
 
95
95
  while (i < content.length) {
96
96
  // Skip whitespace and newlines
97
- while (i < content.length && /\s/.test(content[i])) i++
97
+ while (i < content.length && /\s/.test(content[i] ?? '')) i++
98
98
  if (i >= content.length) break
99
99
 
100
100
  // Skip comments
@@ -113,27 +113,27 @@ export class ComponentRegistry {
113
113
 
114
114
  // Extract property name
115
115
  const nameStart = i
116
- while (i < content.length && /\w/.test(content[i])) i++
116
+ while (i < content.length && /\w/.test(content[i] ?? '')) i++
117
117
  const name = content.substring(nameStart, i)
118
118
 
119
119
  if (!name) break
120
120
 
121
121
  // Skip whitespace
122
- while (i < content.length && /\s/.test(content[i])) i++
122
+ while (i < content.length && /\s/.test(content[i] ?? '')) i++
123
123
 
124
124
  // Check for optional marker
125
125
  const optional = content[i] === '?'
126
126
  if (optional) i++
127
127
 
128
128
  // Skip whitespace
129
- while (i < content.length && /\s/.test(content[i])) i++
129
+ while (i < content.length && /\s/.test(content[i] ?? '')) i++
130
130
 
131
131
  // Expect colon
132
132
  if (content[i] !== ':') break
133
133
  i++
134
134
 
135
135
  // Skip whitespace
136
- while (i < content.length && /\s/.test(content[i])) i++
136
+ while (i < content.length && /\s/.test(content[i] ?? '')) i++
137
137
 
138
138
  // Extract type (up to semicolon, handling nested braces)
139
139
  const typeStart = i
@@ -154,7 +154,7 @@ export class ComponentRegistry {
154
154
  if (content[i] === ';') i++
155
155
 
156
156
  // Skip whitespace
157
- while (i < content.length && /[ \t]/.test(content[i])) i++
157
+ while (i < content.length && /[ \t]/.test(content[i] ?? '')) i++
158
158
 
159
159
  // Check for inline comment
160
160
  let description: string | undefined
@@ -215,7 +215,7 @@ export class ComponentRegistry {
215
215
 
216
216
  // Find the frontmatter section
217
217
  const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/)
218
- if (!frontmatterMatch) return props
218
+ if (!frontmatterMatch?.[1]) return props
219
219
 
220
220
  const frontmatter = frontmatterMatch[1]
221
221
 
package/src/index.ts CHANGED
@@ -64,6 +64,7 @@ export default function cmsMarker(options: CmsMarkerOptions = {}): AstroIntegrat
64
64
  componentDefinitions,
65
65
  config,
66
66
  idCounter,
67
+ command,
67
68
  }
68
69
 
69
70
  updateConfig({
@@ -8,10 +8,11 @@ export interface VitePluginContext {
8
8
  componentDefinitions: Record<string, ComponentDefinition>
9
9
  config: Required<CmsMarkerOptions>
10
10
  idCounter: { value: number }
11
+ command: 'dev' | 'build' | 'preview' | 'sync'
11
12
  }
12
13
 
13
14
  export function createVitePlugin(context: VitePluginContext): Plugin[] {
14
- const { manifestWriter, componentDefinitions, config } = context
15
+ const { manifestWriter, componentDefinitions, config, command } = context
15
16
 
16
17
  const virtualManifestPlugin: Plugin = {
17
18
  name: 'cms-marker-virtual-manifest',
@@ -34,8 +35,11 @@ export function createVitePlugin(context: VitePluginContext): Plugin[] {
34
35
  }
35
36
 
36
37
  // Create the Astro transform plugin to inject source location attributes
38
+ // Only enabled in dev mode - during build, source locations are handled
39
+ // in build-processor.ts after HTML is generated
37
40
  const astroTransformPlugin = createAstroTransformPlugin({
38
41
  markComponents: config.markComponents,
42
+ enabled: command === 'dev',
39
43
  })
40
44
 
41
45
  // Note: We cannot use transformIndexHtml for static Astro builds because