Skip to content

Commit b290bdf

Browse files
committed
Enhance LivePreview for Modern JavaScript and Functional Components #7069
1 parent c91af47 commit b290bdf

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

src/code/LivePreview.mjs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import MonacoEditor from '../component/wrapper/MonacoEditor.mjs'
33
import TabContainer from '../tab/Container.mjs';
44

55
const
6-
classDeclarationRegex = /class\s+([a-zA-Z$_][a-zA-Z0-9$_]*)\s*(?:extends\s+[a-zA-Z$_][a-zA-Z0-9$_]*)?\s*{[\s\S]*?}/g,
7-
classNameRegex = /className\s*:\s*['"]([^'"]+)['"]/g,
8-
exportRegex = /export\s+(?:default\s+)?(?:const|let|var|class|function|async\s+function|generator\s+function|async\s+generator\s+function|(\{[\s\S]*?\}))/g,
9-
importRegex = /import\s+([\w-]+)\s+from\s+['"]([^'"]+)['"]/;
6+
classNameRegex = /className\s*:\s*['\"]([^'\"]+)['\"]/g,
7+
exportRegex = /export\s+(?:default\s+)?(?:const|let|var|class|function|async\s+function|generator\s+function|async\s+generator\s+function|(\{[\s\S]*?\}))/g,
8+
importRegex = /import\s+(?:([\w-]+)|\{([^}]+)\})\s+from\s+['\"]([^'\"]+)['\"]/,
9+
setupClassRegex = /(\w+)\s*=\s*Neo\.setupClass/g;
1010

1111
/**
1212
* @class Neo.code.LivePreview
@@ -229,7 +229,7 @@ class LivePreview extends Container {
229229
{environment} = Neo.config,
230230
container = me.getPreviewContainer(),
231231
source = me.editorValue || me.value,
232-
className = me.findLastClassName(source),
232+
className = me.findSetupClassName(source),
233233
cleanLines = [],
234234
moduleNameAndPath = [],
235235
params = [],
@@ -240,8 +240,9 @@ class LivePreview extends Container {
240240
let importMatch = line.match(importRegex);
241241

242242
if (importMatch) {
243-
let moduleName = importMatch[1],
244-
path = importMatch[2],
243+
let defaultImport = importMatch[1],
244+
namedImports = importMatch[2]?.split(',').map(name => name.trim()),
245+
path = importMatch[3],
245246
index;
246247

247248
// We want the non-minified version for code which can not get bundled.
@@ -266,7 +267,7 @@ class LivePreview extends Container {
266267
}
267268
}
268269

269-
moduleNameAndPath.push({moduleName, path})
270+
moduleNameAndPath.push({defaultImport, namedImports, path})
270271
} else if (line.match(exportRegex)) {
271272
// Skip export statements
272273
} else {
@@ -289,9 +290,15 @@ class LivePreview extends Container {
289290
// });
290291
// Making the promise part of the eval seems weird, but it made it easier to
291292
// set up the import vars.
292-
promises = moduleNameAndPath.map(item => {
293-
params.push(`${item.moduleName}Module`);
294-
vars.push(` const ${item.moduleName} = ${item.moduleName}Module.default;`);
293+
promises = moduleNameAndPath.map((item, i) => {
294+
let moduleAlias = `Module${i}`;
295+
params.push(moduleAlias);
296+
if (item.defaultImport) {
297+
vars.push(` const ${item.defaultImport} = ${moduleAlias}.default;`);
298+
}
299+
if (item.namedImports) {
300+
vars.push(` const {${item.namedImports.join(', ')}} = ${moduleAlias};`);
301+
}
295302
return `import('${item.path}')`
296303
});
297304

@@ -302,7 +309,10 @@ class LivePreview extends Container {
302309
`${vars.join('\n')}`,
303310
` ${cleanLines.join('\n')}`,
304311
'',
305-
` if (${className} && Neo.component.Base.isPrototypeOf(${className})) {`,
312+
` if (${className} && (`,
313+
` Neo.component.Base.isPrototypeOf(${className}) ||`,
314+
` Neo.functional.component.Base.isPrototypeOf(${className})`,
315+
` )) {`,
306316
` container.add({module:${className}})`,
307317
' }',
308318
'})',
@@ -352,11 +362,11 @@ class LivePreview extends Container {
352362
* @param {String} sourceCode
353363
* @returns {String|null}
354364
*/
355-
findLastClassName(sourceCode) {
365+
findSetupClassName(sourceCode) {
356366
let lastClassName = null,
357367
match;
358368

359-
while ((match = classDeclarationRegex.exec(sourceCode)) !== null) {
369+
while ((match = setupClassRegex.exec(sourceCode)) !== null) {
360370
// Update the last class name found
361371
lastClassName = match[1]
362372
}

0 commit comments

Comments
 (0)