1- import {
2- collectTopLevelTransformMetadata ,
3- getFunctionLikeDeclarationNames ,
4- hasFunctionLikeDeclarationNamed ,
5- } from './jsx-top-level-declarations.js'
61import { canRenderPreview , resolvePreviewEntryTab } from './preview-entry-resolver.js'
72import { createWorkspaceIframePreviewBridge } from '../preview-runtime/iframe-preview-executor.js'
83import { planWorkspaceVirtualModules } from '../preview-runtime/virtual-workspace-modules.js'
@@ -14,7 +9,7 @@ export const createRenderRuntimeController = ({
149 cdnImports,
1510 importFromCdnWithFallback,
1611 renderMode,
17- isAutoRenderEnabled = ( ) => false ,
12+ isAutoRenderEnabled : _isAutoRenderEnabled = ( ) => false ,
1813 getJsxSource,
1914 getWorkspaceTabs,
2015 getPreviewHost,
@@ -47,11 +42,6 @@ export const createRenderRuntimeController = ({
4742 let iframeRuntimeBridge = null
4843 let lastRenderedEntryTabId = ''
4944 let lastRenderedDependencyTabIds = new Set ( )
50- let topLevelTransformMetadataCache = {
51- source : null ,
52- transformJsxSource : null ,
53- value : null ,
54- }
5545 let hasCompletedInitialRender = false
5646 const workspaceGraphCache = createPreviewWorkspaceGraphCache ( )
5747 const styleTabLanguages = new Set ( [ 'css' , 'less' , 'sass' , 'module' ] )
@@ -277,104 +267,6 @@ export const createRenderRuntimeController = ({
277267 return lines . join ( '\n' )
278268 }
279269
280- const hasAppDeclaration = declarations =>
281- hasFunctionLikeDeclarationNamed ( { declarations, name : 'App' } )
282-
283- const isComponentLikeName = name => typeof name === 'string' && / ^ [ A - Z ] / . test ( name )
284-
285- const getComponentNames = declarations =>
286- getFunctionLikeDeclarationNames ( { declarations, excludeNames : [ 'App' ] } ) . filter (
287- isComponentLikeName ,
288- )
289-
290- const isSourceRange = range =>
291- Array . isArray ( range ) &&
292- range . length === 2 &&
293- Number . isInteger ( range [ 0 ] ) &&
294- Number . isInteger ( range [ 1 ] )
295-
296- const sourceFromRange = ( { source, range } ) => {
297- if ( ! isSourceRange ( range ) ) {
298- return null
299- }
300-
301- const [ start , end ] = range
302- if ( start < 0 || end < start || end > source . length ) {
303- return null
304- }
305-
306- const expression = source . slice ( start , end ) . trim ( )
307- return expression || null
308- }
309-
310- const getTopLevelTransformMetadata = ( { source, transformJsxSource } ) => {
311- if (
312- topLevelTransformMetadataCache . source === source &&
313- topLevelTransformMetadataCache . transformJsxSource === transformJsxSource &&
314- topLevelTransformMetadataCache . value
315- ) {
316- return topLevelTransformMetadataCache . value
317- }
318-
319- const value = collectTopLevelTransformMetadata ( { source, transformJsxSource } )
320- topLevelTransformMetadataCache = {
321- source,
322- transformJsxSource,
323- value,
324- }
325-
326- return value
327- }
328-
329- const withImplicitAppWrapper = ( source , transformJsxSource ) => {
330- if ( ! source . trim ( ) ) {
331- return source
332- }
333-
334- if ( / ^ \s * e x p o r t \s + d e f a u l t \b / m. test ( source ) ) {
335- return source
336- }
337-
338- const {
339- declarations,
340- importCount,
341- hasTopLevelJsxExpression,
342- topLevelJsxExpressionRange,
343- } = getTopLevelTransformMetadata ( { source, transformJsxSource } )
344- if ( hasAppDeclaration ( declarations ) ) {
345- return source
346- }
347-
348- if ( hasTopLevelJsxExpression ) {
349- const expressionSource = sourceFromRange ( {
350- source,
351- range : topLevelJsxExpressionRange ,
352- } )
353-
354- if ( ! expressionSource ) {
355- throw new Error (
356- 'Unable to infer top-level JSX entry for implicit App. Define App explicitly.' ,
357- )
358- }
359-
360- if ( declarations . length > 0 || importCount > 0 ) {
361- throw new Error (
362- 'Top-level JSX with declarations or imports requires an explicit App component.' ,
363- )
364- }
365-
366- return `const App = () => (${ expressionSource } )`
367- }
368-
369- const componentNames = getComponentNames ( declarations )
370- if ( componentNames . length > 0 ) {
371- const children = componentNames . map ( name => ` <${ name } />` ) . join ( '\n' )
372- return `${ source } \n\nconst App = () => (\n <>\n${ children } \n </>\n)`
373- }
374-
375- return source
376- }
377-
378270 const isSassCompiler = candidate =>
379271 Boolean (
380272 candidate &&
@@ -765,28 +657,6 @@ export const createRenderRuntimeController = ({
765657 reactDomClient : getRuntimeSpecifier ( 'reactDomClient' ) ,
766658 } )
767659
768- const withPreparedEntrySource = ( { tabs, entryTab, transformJsxSource } ) => {
769- if ( ! isAutoRenderEnabled ( ) ) {
770- return tabs
771- }
772-
773- const entrySource = typeof entryTab ?. content === 'string' ? entryTab . content : ''
774- const wrappedEntrySource = withImplicitAppWrapper ( entrySource , transformJsxSource )
775-
776- if ( wrappedEntrySource === entrySource ) {
777- return tabs
778- }
779-
780- return tabs . map ( tab =>
781- tab ?. id === entryTab . id
782- ? {
783- ...tab ,
784- content : wrappedEntrySource ,
785- }
786- : tab ,
787- )
788- }
789-
790660 const renderWorkspaceInIframe = async ( { mode, cssText } ) => {
791661 const workspaceTabs = getWorkspaceTabsForPreview ( )
792662 const entryTab = resolveWorkspaceEntryTab ( workspaceTabs )
@@ -796,11 +666,7 @@ export const createRenderRuntimeController = ({
796666 }
797667
798668 const { transformJsxSource } = await ensureCoreRuntime ( )
799- const tabsForExecution = withPreparedEntrySource ( {
800- tabs : workspaceTabs ,
801- entryTab,
802- transformJsxSource,
803- } )
669+ const tabsForExecution = workspaceTabs
804670 const entryTabForExecution =
805671 resolvePreviewEntryTab ( tabsForExecution ) ??
806672 tabsForExecution . find ( tab => tab ?. id === entryTab . id ) ??
0 commit comments