@fragments-sdk/cli 0.2.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/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +4783 -0
- package/dist/bin.js.map +1 -0
- package/dist/chunk-4FDQSGKX.js +786 -0
- package/dist/chunk-4FDQSGKX.js.map +1 -0
- package/dist/chunk-7H2MMGYG.js +369 -0
- package/dist/chunk-7H2MMGYG.js.map +1 -0
- package/dist/chunk-BSCG3IP7.js +619 -0
- package/dist/chunk-BSCG3IP7.js.map +1 -0
- package/dist/chunk-LY2CFFPY.js +898 -0
- package/dist/chunk-LY2CFFPY.js.map +1 -0
- package/dist/chunk-MUZ6CM66.js +6636 -0
- package/dist/chunk-MUZ6CM66.js.map +1 -0
- package/dist/chunk-OAENNG3G.js +1489 -0
- package/dist/chunk-OAENNG3G.js.map +1 -0
- package/dist/chunk-XHNKNI6J.js +235 -0
- package/dist/chunk-XHNKNI6J.js.map +1 -0
- package/dist/core-DWKLGY4N.js +68 -0
- package/dist/core-DWKLGY4N.js.map +1 -0
- package/dist/generate-4LQNJ7SX.js +249 -0
- package/dist/generate-4LQNJ7SX.js.map +1 -0
- package/dist/index.d.ts +775 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/init-EMVI47QG.js +416 -0
- package/dist/init-EMVI47QG.js.map +1 -0
- package/dist/mcp-bin.d.ts +1 -0
- package/dist/mcp-bin.js +1117 -0
- package/dist/mcp-bin.js.map +1 -0
- package/dist/scan-4YPRF7FV.js +12 -0
- package/dist/scan-4YPRF7FV.js.map +1 -0
- package/dist/service-QSZMZJBJ.js +208 -0
- package/dist/service-QSZMZJBJ.js.map +1 -0
- package/dist/static-viewer-MIPGZ4Z7.js +12 -0
- package/dist/static-viewer-MIPGZ4Z7.js.map +1 -0
- package/dist/test-SQ5ZHXWU.js +1067 -0
- package/dist/test-SQ5ZHXWU.js.map +1 -0
- package/dist/tokens-HSGMYK64.js +173 -0
- package/dist/tokens-HSGMYK64.js.map +1 -0
- package/dist/viewer-YRF4SQE4.js +11101 -0
- package/dist/viewer-YRF4SQE4.js.map +1 -0
- package/package.json +107 -0
- package/src/ai.ts +266 -0
- package/src/analyze.ts +265 -0
- package/src/bin.ts +916 -0
- package/src/build.ts +248 -0
- package/src/commands/a11y.ts +302 -0
- package/src/commands/add.ts +313 -0
- package/src/commands/audit.ts +195 -0
- package/src/commands/baseline.ts +221 -0
- package/src/commands/build.ts +144 -0
- package/src/commands/compare.ts +337 -0
- package/src/commands/context.ts +107 -0
- package/src/commands/dev.ts +107 -0
- package/src/commands/enhance.ts +858 -0
- package/src/commands/generate.ts +391 -0
- package/src/commands/init.ts +531 -0
- package/src/commands/link/figma.ts +645 -0
- package/src/commands/link/index.ts +10 -0
- package/src/commands/link/storybook.ts +267 -0
- package/src/commands/list.ts +49 -0
- package/src/commands/metrics.ts +114 -0
- package/src/commands/reset.ts +242 -0
- package/src/commands/scan.ts +537 -0
- package/src/commands/storygen.ts +207 -0
- package/src/commands/tokens.ts +251 -0
- package/src/commands/validate.ts +93 -0
- package/src/commands/verify.ts +215 -0
- package/src/core/composition.test.ts +262 -0
- package/src/core/composition.ts +255 -0
- package/src/core/config.ts +84 -0
- package/src/core/constants.ts +111 -0
- package/src/core/context.ts +380 -0
- package/src/core/defineSegment.ts +137 -0
- package/src/core/discovery.ts +337 -0
- package/src/core/figma.ts +263 -0
- package/src/core/fragment-types.ts +214 -0
- package/src/core/generators/context.ts +389 -0
- package/src/core/generators/index.ts +23 -0
- package/src/core/generators/registry.ts +364 -0
- package/src/core/generators/typescript-extractor.ts +374 -0
- package/src/core/importAnalyzer.ts +217 -0
- package/src/core/index.ts +149 -0
- package/src/core/loader.ts +155 -0
- package/src/core/node.ts +63 -0
- package/src/core/parser.ts +551 -0
- package/src/core/previewLoader.ts +172 -0
- package/src/core/schema/fragment.schema.json +189 -0
- package/src/core/schema/registry.schema.json +137 -0
- package/src/core/schema.ts +182 -0
- package/src/core/storyAdapter.test.ts +571 -0
- package/src/core/storyAdapter.ts +761 -0
- package/src/core/token-types.ts +287 -0
- package/src/core/types.ts +754 -0
- package/src/diff.ts +323 -0
- package/src/index.ts +43 -0
- package/src/mcp/__tests__/projectFields.test.ts +130 -0
- package/src/mcp/bin.ts +36 -0
- package/src/mcp/index.ts +8 -0
- package/src/mcp/server.ts +1310 -0
- package/src/mcp/utils.ts +54 -0
- package/src/mcp-bin.ts +36 -0
- package/src/migrate/__tests__/argTypes/argTypes.test.ts +189 -0
- package/src/migrate/__tests__/args/args.test.ts +452 -0
- package/src/migrate/__tests__/meta/meta.test.ts +198 -0
- package/src/migrate/__tests__/stories/stories.test.ts +278 -0
- package/src/migrate/__tests__/utils/utils.test.ts +371 -0
- package/src/migrate/__tests__/values/values.test.ts +303 -0
- package/src/migrate/bin.ts +108 -0
- package/src/migrate/converter.ts +658 -0
- package/src/migrate/detect.ts +196 -0
- package/src/migrate/index.ts +45 -0
- package/src/migrate/migrate.ts +163 -0
- package/src/migrate/parser.ts +1136 -0
- package/src/migrate/report.ts +624 -0
- package/src/migrate/types.ts +169 -0
- package/src/screenshot.ts +249 -0
- package/src/service/__tests__/ast-utils.test.ts +426 -0
- package/src/service/__tests__/enhance-scanner.test.ts +200 -0
- package/src/service/__tests__/figma/figma.test.ts +652 -0
- package/src/service/__tests__/metrics-store.test.ts +409 -0
- package/src/service/__tests__/patch-generator.test.ts +186 -0
- package/src/service/__tests__/props-extractor.test.ts +365 -0
- package/src/service/__tests__/token-registry.test.ts +267 -0
- package/src/service/analytics.ts +659 -0
- package/src/service/ast-utils.ts +444 -0
- package/src/service/browser-pool.ts +339 -0
- package/src/service/capture.ts +267 -0
- package/src/service/diff.ts +279 -0
- package/src/service/enhance/aggregator.ts +489 -0
- package/src/service/enhance/cache.ts +275 -0
- package/src/service/enhance/codebase-scanner.ts +357 -0
- package/src/service/enhance/context-generator.ts +529 -0
- package/src/service/enhance/doc-extractor.ts +523 -0
- package/src/service/enhance/index.ts +131 -0
- package/src/service/enhance/props-extractor.ts +665 -0
- package/src/service/enhance/scanner.ts +445 -0
- package/src/service/enhance/storybook-parser.ts +552 -0
- package/src/service/enhance/types.ts +346 -0
- package/src/service/enhance/variant-renderer.ts +479 -0
- package/src/service/figma.ts +1008 -0
- package/src/service/index.ts +249 -0
- package/src/service/metrics-store.ts +333 -0
- package/src/service/patch-generator.ts +349 -0
- package/src/service/report.ts +854 -0
- package/src/service/storage.ts +401 -0
- package/src/service/token-fixes.ts +281 -0
- package/src/service/token-parser.ts +504 -0
- package/src/service/token-registry.ts +721 -0
- package/src/service/utils.ts +172 -0
- package/src/setup.ts +241 -0
- package/src/shared/command-wrapper.ts +81 -0
- package/src/shared/dev-server-client.ts +199 -0
- package/src/shared/index.ts +8 -0
- package/src/shared/segment-loader.ts +59 -0
- package/src/shared/types.ts +147 -0
- package/src/static-viewer.ts +715 -0
- package/src/test/discovery.ts +172 -0
- package/src/test/index.ts +281 -0
- package/src/test/reporters/console.ts +194 -0
- package/src/test/reporters/json.ts +190 -0
- package/src/test/reporters/junit.ts +186 -0
- package/src/test/runner.ts +598 -0
- package/src/test/types.ts +245 -0
- package/src/test/watch.ts +200 -0
- package/src/validators.ts +152 -0
- package/src/viewer/__tests__/jsx-parser.test.ts +502 -0
- package/src/viewer/__tests__/render-utils.test.ts +232 -0
- package/src/viewer/__tests__/style-utils.test.ts +404 -0
- package/src/viewer/bin.ts +86 -0
- package/src/viewer/cli/health.ts +256 -0
- package/src/viewer/cli/index.ts +33 -0
- package/src/viewer/cli/scan.ts +124 -0
- package/src/viewer/cli/utils.ts +174 -0
- package/src/viewer/components/AccessibilityPanel.tsx +1404 -0
- package/src/viewer/components/ActionCapture.tsx +172 -0
- package/src/viewer/components/ActionsPanel.tsx +371 -0
- package/src/viewer/components/App.tsx +638 -0
- package/src/viewer/components/BottomPanel.tsx +224 -0
- package/src/viewer/components/CodePanel.tsx +589 -0
- package/src/viewer/components/CommandPalette.tsx +336 -0
- package/src/viewer/components/ComponentGraph.tsx +394 -0
- package/src/viewer/components/ComponentHeader.tsx +85 -0
- package/src/viewer/components/ContractPanel.tsx +234 -0
- package/src/viewer/components/ErrorBoundary.tsx +85 -0
- package/src/viewer/components/FigmaEmbed.tsx +231 -0
- package/src/viewer/components/FragmentEditor.tsx +485 -0
- package/src/viewer/components/HealthDashboard.tsx +452 -0
- package/src/viewer/components/HmrStatusIndicator.tsx +71 -0
- package/src/viewer/components/Icons.tsx +417 -0
- package/src/viewer/components/InteractionsPanel.tsx +720 -0
- package/src/viewer/components/IsolatedPreviewFrame.tsx +321 -0
- package/src/viewer/components/IsolatedRender.tsx +111 -0
- package/src/viewer/components/KeyboardShortcutsHelp.tsx +89 -0
- package/src/viewer/components/LandingPage.tsx +441 -0
- package/src/viewer/components/Layout.tsx +22 -0
- package/src/viewer/components/LeftSidebar.tsx +391 -0
- package/src/viewer/components/MultiViewportPreview.tsx +429 -0
- package/src/viewer/components/PreviewArea.tsx +404 -0
- package/src/viewer/components/PreviewFrameHost.tsx +310 -0
- package/src/viewer/components/PreviewPane.tsx +150 -0
- package/src/viewer/components/PreviewToolbar.tsx +176 -0
- package/src/viewer/components/PropsEditor.tsx +512 -0
- package/src/viewer/components/PropsTable.tsx +98 -0
- package/src/viewer/components/RelationsSection.tsx +57 -0
- package/src/viewer/components/ResizablePanel.tsx +328 -0
- package/src/viewer/components/RightSidebar.tsx +118 -0
- package/src/viewer/components/ScreenshotButton.tsx +90 -0
- package/src/viewer/components/Sidebar.tsx +169 -0
- package/src/viewer/components/SkeletonLoader.tsx +156 -0
- package/src/viewer/components/StoryRenderer.tsx +128 -0
- package/src/viewer/components/ThemeProvider.tsx +96 -0
- package/src/viewer/components/Toast.tsx +67 -0
- package/src/viewer/components/TokenStylePanel.tsx +708 -0
- package/src/viewer/components/UsageSection.tsx +95 -0
- package/src/viewer/components/VariantMatrix.tsx +350 -0
- package/src/viewer/components/VariantRenderer.tsx +131 -0
- package/src/viewer/components/VariantTabs.tsx +84 -0
- package/src/viewer/components/ViewportSelector.tsx +165 -0
- package/src/viewer/components/_future/CreatePage.tsx +836 -0
- package/src/viewer/composition-renderer.ts +381 -0
- package/src/viewer/constants/index.ts +1 -0
- package/src/viewer/constants/ui.ts +185 -0
- package/src/viewer/entry.tsx +299 -0
- package/src/viewer/hooks/index.ts +2 -0
- package/src/viewer/hooks/useA11yCache.ts +383 -0
- package/src/viewer/hooks/useA11yService.ts +498 -0
- package/src/viewer/hooks/useActions.ts +138 -0
- package/src/viewer/hooks/useAppState.ts +124 -0
- package/src/viewer/hooks/useFigmaIntegration.ts +132 -0
- package/src/viewer/hooks/useHmrStatus.ts +109 -0
- package/src/viewer/hooks/useKeyboardShortcuts.ts +222 -0
- package/src/viewer/hooks/usePreviewBridge.ts +347 -0
- package/src/viewer/hooks/useScrollSpy.ts +78 -0
- package/src/viewer/hooks/useUrlState.ts +330 -0
- package/src/viewer/hooks/useViewSettings.ts +125 -0
- package/src/viewer/index.html +28 -0
- package/src/viewer/index.ts +14 -0
- package/src/viewer/intelligence/healthReport.ts +505 -0
- package/src/viewer/intelligence/styleDrift.ts +340 -0
- package/src/viewer/intelligence/usageScanner.ts +309 -0
- package/src/viewer/jsx-parser.ts +485 -0
- package/src/viewer/postcss.config.js +6 -0
- package/src/viewer/preview-frame-entry.tsx +25 -0
- package/src/viewer/preview-frame.html +109 -0
- package/src/viewer/render-template.html +68 -0
- package/src/viewer/render-utils.ts +170 -0
- package/src/viewer/server.ts +276 -0
- package/src/viewer/style-utils.ts +414 -0
- package/src/viewer/styles/globals.css +355 -0
- package/src/viewer/tailwind.config.js +37 -0
- package/src/viewer/types/a11y.ts +197 -0
- package/src/viewer/utils/a11y-fixes.ts +471 -0
- package/src/viewer/utils/actionExport.ts +372 -0
- package/src/viewer/utils/colorSchemes.ts +201 -0
- package/src/viewer/utils/detectRelationships.ts +256 -0
- package/src/viewer/vite-plugin.ts +2143 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe exports for @fragments/core
|
|
3
|
+
* For Node.js-only APIs (config, discovery, loader), use '@fragments/core/node'
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Brand and default constants
|
|
7
|
+
export { BRAND, DEFAULTS } from "./constants.js";
|
|
8
|
+
export type { Brand, Defaults } from "./constants.js";
|
|
9
|
+
|
|
10
|
+
// Types
|
|
11
|
+
export type {
|
|
12
|
+
SegmentComponent,
|
|
13
|
+
SegmentMeta,
|
|
14
|
+
SegmentUsage,
|
|
15
|
+
PropType,
|
|
16
|
+
PropDefinition,
|
|
17
|
+
ControlType,
|
|
18
|
+
RelationshipType,
|
|
19
|
+
ComponentRelation,
|
|
20
|
+
VariantLoader,
|
|
21
|
+
PlayFunction,
|
|
22
|
+
PlayFunctionContext,
|
|
23
|
+
SegmentVariant,
|
|
24
|
+
SegmentDefinition,
|
|
25
|
+
FragmentsConfig,
|
|
26
|
+
SegmentsConfig, // @deprecated - use FragmentsConfig
|
|
27
|
+
RegistryOptions,
|
|
28
|
+
CompiledSegment,
|
|
29
|
+
CompiledSegmentsFile,
|
|
30
|
+
RecipeDefinition,
|
|
31
|
+
CompiledRecipe,
|
|
32
|
+
// Contract and provenance types
|
|
33
|
+
SegmentContract,
|
|
34
|
+
SegmentGenerated,
|
|
35
|
+
// Screenshot types
|
|
36
|
+
ScreenshotConfig,
|
|
37
|
+
ServiceConfig,
|
|
38
|
+
Viewport,
|
|
39
|
+
Theme,
|
|
40
|
+
Screenshot,
|
|
41
|
+
ScreenshotMetadata,
|
|
42
|
+
DiffResult,
|
|
43
|
+
BoundingBox,
|
|
44
|
+
BaselineInfo,
|
|
45
|
+
Manifest,
|
|
46
|
+
VerifyRequest,
|
|
47
|
+
VerifyResult,
|
|
48
|
+
// Figma property mapping types
|
|
49
|
+
FigmaPropMapping,
|
|
50
|
+
FigmaStringMapping,
|
|
51
|
+
FigmaBooleanMapping,
|
|
52
|
+
FigmaEnumMapping,
|
|
53
|
+
FigmaInstanceMapping,
|
|
54
|
+
FigmaChildrenMapping,
|
|
55
|
+
FigmaTextContentMapping,
|
|
56
|
+
// Token configuration
|
|
57
|
+
TokenConfig,
|
|
58
|
+
} from "./types.js";
|
|
59
|
+
|
|
60
|
+
// Token types
|
|
61
|
+
export type {
|
|
62
|
+
TokenCategory,
|
|
63
|
+
DesignToken,
|
|
64
|
+
TokenRegistry,
|
|
65
|
+
TokenRegistryMeta,
|
|
66
|
+
EnhancedStyleDiffItem,
|
|
67
|
+
TokenFix,
|
|
68
|
+
TokenParseResult,
|
|
69
|
+
TokenParseError,
|
|
70
|
+
TokenMatchRequest,
|
|
71
|
+
TokenMatchResult,
|
|
72
|
+
TokenUsageSummary,
|
|
73
|
+
} from "./token-types.js";
|
|
74
|
+
|
|
75
|
+
// Schema validation (Zod is browser-safe)
|
|
76
|
+
export {
|
|
77
|
+
segmentMetaSchema,
|
|
78
|
+
segmentUsageSchema,
|
|
79
|
+
propDefinitionSchema,
|
|
80
|
+
componentRelationSchema,
|
|
81
|
+
segmentVariantSchema,
|
|
82
|
+
segmentDefinitionSchema,
|
|
83
|
+
segmentsConfigSchema,
|
|
84
|
+
figmaPropMappingSchema,
|
|
85
|
+
// Contract and provenance schemas
|
|
86
|
+
segmentContractSchema,
|
|
87
|
+
segmentGeneratedSchema,
|
|
88
|
+
segmentBanSchema,
|
|
89
|
+
recipeDefinitionSchema,
|
|
90
|
+
} from "./schema.js";
|
|
91
|
+
|
|
92
|
+
// Main API
|
|
93
|
+
export { defineSegment, defineFragment, compileSegment, defineRecipe, compileRecipe } from "./defineSegment.js";
|
|
94
|
+
export type { InferProps } from "./defineSegment.js";
|
|
95
|
+
|
|
96
|
+
// Story adapter (runtime conversion of Storybook modules)
|
|
97
|
+
export {
|
|
98
|
+
storyModuleToSegment,
|
|
99
|
+
setPreviewConfig,
|
|
100
|
+
getPreviewConfig,
|
|
101
|
+
// Re-export @storybook/csf utilities
|
|
102
|
+
toId,
|
|
103
|
+
storyNameFromExport,
|
|
104
|
+
isExportStory,
|
|
105
|
+
} from "./storyAdapter.js";
|
|
106
|
+
export type {
|
|
107
|
+
StoryModule,
|
|
108
|
+
StoryMeta,
|
|
109
|
+
Story,
|
|
110
|
+
StoryArgType,
|
|
111
|
+
StoryContext,
|
|
112
|
+
Decorator,
|
|
113
|
+
Loader,
|
|
114
|
+
PreviewConfig,
|
|
115
|
+
CSF2Story,
|
|
116
|
+
} from "./storyAdapter.js";
|
|
117
|
+
|
|
118
|
+
// Context generation for AI agents
|
|
119
|
+
export { generateContext } from "./context.js";
|
|
120
|
+
export type { ContextOptions, ContextResult } from "./context.js";
|
|
121
|
+
|
|
122
|
+
// Figma property mapping DSL
|
|
123
|
+
export { figma, isFigmaPropMapping, resolveFigmaMapping } from "./figma.js";
|
|
124
|
+
|
|
125
|
+
// Fragment JSON types (for .fragment.json files)
|
|
126
|
+
export type {
|
|
127
|
+
Fragment,
|
|
128
|
+
FragmentFigma,
|
|
129
|
+
FragmentUsage,
|
|
130
|
+
FragmentDoNotItem,
|
|
131
|
+
FragmentPattern,
|
|
132
|
+
FragmentAccessibility,
|
|
133
|
+
FragmentRelated,
|
|
134
|
+
FragmentMeta,
|
|
135
|
+
FragmentIndex,
|
|
136
|
+
FragmentRegistry,
|
|
137
|
+
RegistryComponentEntry,
|
|
138
|
+
RegistryPropEntry,
|
|
139
|
+
FragmentContextOptions,
|
|
140
|
+
} from "./fragment-types.js";
|
|
141
|
+
|
|
142
|
+
// Composition analysis
|
|
143
|
+
export { analyzeComposition } from "./composition.js";
|
|
144
|
+
export type {
|
|
145
|
+
CompositionAnalysis,
|
|
146
|
+
CompositionWarning,
|
|
147
|
+
CompositionSuggestion,
|
|
148
|
+
CompositionGuideline,
|
|
149
|
+
} from "./composition.js";
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { writeFile, unlink } from 'node:fs/promises';
|
|
2
|
+
import { dirname, basename, join } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { build, type Plugin } from 'esbuild';
|
|
5
|
+
import type { SegmentDefinition } from './types.js';
|
|
6
|
+
import { BRAND } from './constants.js';
|
|
7
|
+
|
|
8
|
+
// Inline implementation of defineSegment that will be injected into bundled files
|
|
9
|
+
// This allows segment files to work without @fragments/core being installed
|
|
10
|
+
const DEFINE_SEGMENT_SHIM = `
|
|
11
|
+
export function defineSegment(def) {
|
|
12
|
+
return def;
|
|
13
|
+
}
|
|
14
|
+
export function defineFragment(def) {
|
|
15
|
+
return def;
|
|
16
|
+
}
|
|
17
|
+
export function defineRecipe(def) {
|
|
18
|
+
return def;
|
|
19
|
+
}
|
|
20
|
+
`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates an esbuild plugin that replaces @fragments/core imports with an inline shim.
|
|
24
|
+
* This allows segment files to be loaded without @fragments/core being installed in the target project.
|
|
25
|
+
*/
|
|
26
|
+
function createSegmentsCoreShimPlugin(): Plugin {
|
|
27
|
+
return {
|
|
28
|
+
name: BRAND.vitePluginNamespace,
|
|
29
|
+
setup(build) {
|
|
30
|
+
// Intercept @fragments/core imports
|
|
31
|
+
build.onResolve({ filter: /^@fragments\/core$/ }, (args) => {
|
|
32
|
+
return {
|
|
33
|
+
path: args.path,
|
|
34
|
+
namespace: BRAND.vitePluginNamespace,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Return the shim content
|
|
39
|
+
build.onLoad({ filter: /.*/, namespace: BRAND.vitePluginNamespace }, () => {
|
|
40
|
+
return {
|
|
41
|
+
contents: DEFINE_SEGMENT_SHIM,
|
|
42
|
+
loader: 'js',
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Load a segment file, handling TSX/TS transformation automatically.
|
|
51
|
+
* Uses esbuild to bundle and transform TypeScript/JSX before importing.
|
|
52
|
+
*/
|
|
53
|
+
export async function loadSegmentFile(
|
|
54
|
+
absolutePath: string
|
|
55
|
+
): Promise<SegmentDefinition | null> {
|
|
56
|
+
const ext = absolutePath.split('.').pop()?.toLowerCase();
|
|
57
|
+
const needsTransform = ext === 'tsx' || ext === 'ts' || ext === 'jsx';
|
|
58
|
+
|
|
59
|
+
if (!needsTransform) {
|
|
60
|
+
// Plain JS file, import directly
|
|
61
|
+
const fileUrl = pathToFileURL(absolutePath).href;
|
|
62
|
+
const module = await import(fileUrl);
|
|
63
|
+
return module.default ?? null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Bundle the file with all its local dependencies
|
|
67
|
+
const sourceDir = dirname(absolutePath);
|
|
68
|
+
const baseName = basename(absolutePath, `.${ext}`);
|
|
69
|
+
// Use .cjs extension to ensure CommonJS evaluation
|
|
70
|
+
const tempFile = join(sourceDir, `.${baseName}.segments-temp-${Date.now()}.cjs`);
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
// Use esbuild to bundle the segment file
|
|
74
|
+
// We inject a shim for @fragments/core so it doesn't need to be installed
|
|
75
|
+
// Using CommonJS format to avoid ESM/CJS interop issues with node_modules
|
|
76
|
+
await build({
|
|
77
|
+
entryPoints: [absolutePath],
|
|
78
|
+
outfile: tempFile,
|
|
79
|
+
bundle: true,
|
|
80
|
+
format: 'cjs', // Use CommonJS to avoid ESM issues with lodash, etc.
|
|
81
|
+
target: 'es2022',
|
|
82
|
+
jsx: 'automatic',
|
|
83
|
+
platform: 'node',
|
|
84
|
+
plugins: [createSegmentsCoreShimPlugin()],
|
|
85
|
+
// Externalize all node_modules - we only need segment metadata, not component code
|
|
86
|
+
packages: 'external',
|
|
87
|
+
// Also explicitly list patterns for nested imports
|
|
88
|
+
external: [
|
|
89
|
+
// React and its subpaths
|
|
90
|
+
'react',
|
|
91
|
+
'react-dom',
|
|
92
|
+
'react/*',
|
|
93
|
+
'react-dom/*',
|
|
94
|
+
],
|
|
95
|
+
// Don't emit sourcemaps for temp files
|
|
96
|
+
sourcemap: false,
|
|
97
|
+
// Suppress warnings
|
|
98
|
+
logLevel: 'silent',
|
|
99
|
+
// Use loader to ignore style imports
|
|
100
|
+
loader: {
|
|
101
|
+
'.scss': 'empty',
|
|
102
|
+
'.css': 'empty',
|
|
103
|
+
'.svg': 'empty',
|
|
104
|
+
'.png': 'empty',
|
|
105
|
+
'.jpg': 'empty',
|
|
106
|
+
'.jpeg': 'empty',
|
|
107
|
+
'.gif': 'empty',
|
|
108
|
+
'.woff': 'empty',
|
|
109
|
+
'.woff2': 'empty',
|
|
110
|
+
'.ttf': 'empty',
|
|
111
|
+
'.eot': 'empty',
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const fileUrl = pathToFileURL(tempFile).href;
|
|
116
|
+
const module = await import(fileUrl);
|
|
117
|
+
return module.default ?? null;
|
|
118
|
+
} finally {
|
|
119
|
+
// Clean up temp file
|
|
120
|
+
try {
|
|
121
|
+
await unlink(tempFile);
|
|
122
|
+
} catch {
|
|
123
|
+
// Ignore cleanup errors
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Load multiple segment files in parallel with error handling.
|
|
130
|
+
*/
|
|
131
|
+
export async function loadSegmentFiles(
|
|
132
|
+
absolutePaths: string[]
|
|
133
|
+
): Promise<Map<string, SegmentDefinition | Error>> {
|
|
134
|
+
const results = new Map<string, SegmentDefinition | Error>();
|
|
135
|
+
|
|
136
|
+
await Promise.all(
|
|
137
|
+
absolutePaths.map(async (path) => {
|
|
138
|
+
try {
|
|
139
|
+
const segment = await loadSegmentFile(path);
|
|
140
|
+
if (segment) {
|
|
141
|
+
results.set(path, segment);
|
|
142
|
+
} else {
|
|
143
|
+
results.set(path, new Error('No default export found'));
|
|
144
|
+
}
|
|
145
|
+
} catch (error) {
|
|
146
|
+
results.set(
|
|
147
|
+
path,
|
|
148
|
+
error instanceof Error ? error : new Error(String(error))
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
return results;
|
|
155
|
+
}
|
package/src/core/node.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-only exports for @fragments/core
|
|
3
|
+
* These use Node.js APIs and cannot be used in the browser.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Config
|
|
7
|
+
export { loadConfig, findConfigFile } from './config.js';
|
|
8
|
+
|
|
9
|
+
// Discovery
|
|
10
|
+
export {
|
|
11
|
+
discoverSegmentFiles,
|
|
12
|
+
discoverRecipeFiles,
|
|
13
|
+
discoverComponentFiles,
|
|
14
|
+
extractComponentName,
|
|
15
|
+
// New: Component discovery from source files (no segment files needed)
|
|
16
|
+
discoverComponentsFromSource,
|
|
17
|
+
discoverComponentsFromBarrel,
|
|
18
|
+
discoverAllComponents,
|
|
19
|
+
} from './discovery.js';
|
|
20
|
+
export type { DiscoveredFile, DiscoveredComponent } from './discovery.js';
|
|
21
|
+
|
|
22
|
+
// Loader (TSX/TS support)
|
|
23
|
+
export { loadSegmentFile, loadSegmentFiles } from './loader.js';
|
|
24
|
+
|
|
25
|
+
// AST Parser (no execution, static analysis only)
|
|
26
|
+
export { parseSegmentFile } from './parser.js';
|
|
27
|
+
export type { ParsedSegmentMetadata } from './parser.js';
|
|
28
|
+
|
|
29
|
+
// Preview loader (Storybook .storybook/preview.tsx support)
|
|
30
|
+
export {
|
|
31
|
+
findPreviewConfigPath,
|
|
32
|
+
findStorybookDir,
|
|
33
|
+
loadPreviewConfig,
|
|
34
|
+
autoLoadPreviewConfig,
|
|
35
|
+
generatePreviewModule,
|
|
36
|
+
} from './previewLoader.js';
|
|
37
|
+
|
|
38
|
+
// Generators (registry, context)
|
|
39
|
+
export {
|
|
40
|
+
extractPropsFromFile,
|
|
41
|
+
extractPropsFromSource,
|
|
42
|
+
generateRegistry,
|
|
43
|
+
resolveComponentPath,
|
|
44
|
+
getComponentsByCategory,
|
|
45
|
+
generateContextMd,
|
|
46
|
+
} from './generators/index.js';
|
|
47
|
+
export type {
|
|
48
|
+
ExtractedProps,
|
|
49
|
+
RegistryGeneratorOptions,
|
|
50
|
+
RegistryGeneratorResult,
|
|
51
|
+
ContextGeneratorResult,
|
|
52
|
+
} from './generators/index.js';
|
|
53
|
+
|
|
54
|
+
// Import analyzer (AST-based component relationship detection)
|
|
55
|
+
export {
|
|
56
|
+
analyzeFileImports,
|
|
57
|
+
buildImportGraph,
|
|
58
|
+
getImportedBy,
|
|
59
|
+
} from './importAnalyzer.js';
|
|
60
|
+
export type {
|
|
61
|
+
ComponentImport,
|
|
62
|
+
ImportAnalysisResult,
|
|
63
|
+
} from './importAnalyzer.js';
|