Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

[code] [code] [code] [code] Task - Update our page.js compiler, to transform the HTML…

  1. User
    project-root/
    ├── cat/
    │   ├── bin/
    │   │   └── cat.js
    │   └── src/
    │       └── compiler.js
    |       └── component.js
    |       └── page.js
    ├── index.html
    ├── package.json
    └── cat.config.json
    
    // cat/src/compiler.js
    
    const fs = require('fs-extra');
    const path = require('path');
    const glob = require('glob');
    const { parseComponent, parseScript, generateClassCode, transformMethodBody, generateDOMCode } = require('./component');
    const { parsePage, detectLoadRoutes } = require('./page');
    const { parseDocument } = require('htmlparser2');
    
    /**
     * Detect used components in the provided HTML contents by parsing the HTML tags.
     * @param {Array<string>} htmlContents - Array of HTML file contents.
     * @param {Set<string>} availableComponents - Set of all available component tag names.
     * @returns {Set<string>} - Set of used component tag names.
     */
    function detectUsedComponents(htmlContents, availableComponents) {
      const usedComponents = new Set();
    
      // Regex to match all tags in the HTML
      const tagRegex = /<\s*([a-zA-Z][a-zA-Z0-9-]*)\b[^>]*>/g;
    
      htmlContents.forEach(content => {
        let match;
        while ((match = tagRegex.exec(content)) !== null) {
          let tag = match[1];
          if (availableComponents.has(tag)) {
            usedComponents.add(tag);
          }
        }
      });
    
      return usedComponents;
    }
    
    /**
     * Generate the registration script for the used components.
     * @param {Map<string, string>} components - Map of tagName to compiled class code.
     * @returns {string} - JavaScript code for registering components.
     */
    function generateRegistrationScript(components) {
      let script = '';
    
      components.forEach((classCode) => {
        script += `
    ${classCode}
      `;
      });
    
      return script;
    }
    
    /**
     * Compiler Function
     * @param {string} indexPath - Path to the index.html file.
     * @returns {Promise<string>} - Compiled HTML content with component registrations and page injections.
     */
    async function compile(indexPath) {
      // Read the index.html content
      let indexContent = await fs.readFile(indexPath, 'utf-8');
    
      // Define project root
      const projectRoot = path.dirname(indexPath);
    
      // -----------------------
      // Step 1: Process Components
      // -----------------------
    
      // Find all .comp.html files in the project directory
      const compFiles = glob.sync('**/*.comp.html', {
        cwd: projectRoot,
        absolute: true,
        ignore: ['node_modules/**', 'cat/**'] // Ignore node_modules and cat directory to prevent recursion
      });
    
      const componentMap = new Map(); // Map of tagName to compiled class code
      const parsedComponents = new Set(); // To track parsed components
    
      // Parse and compile each component file
      for (const file of compFiles) {
        try {
          const { tagName, html, script, style } = await parseComponent(file);
    
          if (!parsedComponents.has(tagName)) {
            // Parse script to extract properties and methods
            const { properties, methods } = parseScript(script);
    
            // Generate class code
            const classCode = generateClassCode({
              tagName,
              html,
              style,
              properties,
              methods,
            });
    
            componentMap.set(tagName, classCode);
            parsedComponents.add(tagName);
    
            console.log(`🔍 Parsed and compiled component: <${tagName}> from ${path.relative(projectRoot, file)}`);
          } else {
            console.log(`ℹ️  Component <${tagName}> from ${path.relative(projectRoot, file)} has already been parsed.`);
          }
        } catch (error) {
          console.error(`❌ Failed to parse ${file}: ${error.message}`);
        }
      }
    
      if (componentMap.size === 0) {
        console.warn('⚠️  No components found to compile.');
      }
    
      // -----------------------
      // Step 2: Process Pages
      // -----------------------
    
      // Find all .page.html files in the project directory
      const pageFiles = glob.sync('**/*.page.html', {
        cwd: projectRoot,
        absolute: true,
        ignore: ['node_modules/**', 'cat/**']
      });
    
      const routeMap = new Map(); // Map of route to page content
    
      for (const file of pageFiles) {
        try {
          const { route, content } = await parsePage(file);
          if (routeMap.has(route)) {
            console.warn(`⚠️  Duplicate route "${route}" found in ${path.relative(projectRoot, file)}. Overwriting previous route.`);
          }
          routeMap.set(route, content);
          console.log(`📄 Mapped route "${route}" to ${path.relative(projectRoot, file)}`);
        } catch (error) {
          console.error(`❌ Failed to parse page ${file}: ${error.message}`);
        }
      }
    
      if (routeMap.size === 0) {
        console.warn('⚠️  No pages found to map routes.');
      }
    
      // -----------------------
      // Step 3: Detect Used Components
      // -----------------------
    
      // Find all .html files in the project directory for usage detection
      const htmlFiles = glob.sync('**/*.html', {
        cwd: projectRoot,
        absolute: true,
        ignore: ['node_modules/**', 'cat/**']
      });
    
      const htmlContents = [];
      for (const file of htmlFiles) {
        try {
          const content = await fs.readFile(file, 'utf-8');
          htmlContents.push(content);
        } catch (error) {
          console.error(`❌ Failed to read HTML file ${file}: ${error.message}`);
        }
      }
    
      // Detect which components are used across all HTML files
      const availableComponents = new Set(componentMap.keys());
      const usedComponents = detectUsedComponents(htmlContents, availableComponents);
    
      if (usedComponents.size === 0) {
        console.warn('⚠️  No used components found in HTML files.');
      } else {
        console.log(`📦 Used components: ${[...usedComponents].join(', ')}`);
      }
    
      // -----------------------
      // Step 4: Handle <load> Tags and Inject Page Content
      // -----------------------
    
      // Detect all <load> routes in all HTML files
      const loadRoutes = detectLoadRoutes(htmlContents);
    
      // Prepare a map of route to page content
      // Verify that each load route exists in the routeMap
      loadRoutes.forEach(route => {
        if (!routeMap.has(route)) {
          console.error(`❌ No page found for route "${route}". Please ensure a .page.html file exists with route="${route}".`);
        }
      });
    
      // Now, replace <load route="..."> tags in indexContent with the corresponding page content
      const loadTagRegex = /<load\s+[^>]*route=["']([^"']+)["'][^>]*>/gi;
    
      indexContent = indexContent.replace(loadTagRegex, (match, route) => {
        if (routeMap.has(route)) {
          console.log(`🔄 Injecting content for route "${route}"`);
          return routeMap.get(route);
        } else {
          console.warn(`⚠️  No content found for route "${route}". Keeping <load> tag.`);
          return match; // Keep the <load> tag if no content is found
        }
      });
    
      // -----------------------
      // Step 5: Generate Registration Script
      // -----------------------
    
      const registrationScript = generateRegistrationScript(
        Array.from(usedComponents).reduce((map, tag) => map.set(tag, componentMap.get(tag)), new Map())
      );
    
      // -----------------------
      // Step 6: Inject Registration Script
      // -----------------------
    
      if (registrationScript.trim()) {
        // Inject the registration script before the closing </body> tag
        const scriptTag = `<script>
    ${registrationScript}
    </script>
    `;
    
        if (indexContent.includes('</body>')) {
          indexContent = indexContent.replace('</body>', `${scriptTag}</body>`);
        } else {
          // If no </body> tag, append the script at the end
          indexContent += `\n${scriptTag}`;
        }
        console.log('📝 Injected component registration scripts.');
      }
    
      // -----------------------
      // Step 7: Return Compiled Content
      // -----------------------
    
      return indexContent;
    }
    
    module.exports = { compile };
    
    // cat/src/component.js
    
    const fs = require('fs-extra');
    const path = require('path');
    const acorn = require('acorn'); // Import Acorn for JavaScript parsing
    const { parseDocument } = require('htmlparser2');
    
    /**
     * Parse a component file to extract tagName, HTML, script, and style sections.
     * @param {string} filePath - Path to the .comp.html file.
     * @returns {Promise<{ tagName: string, html: string, script: string, style: string }>} - Component details.
     */
    async function parseComponent(filePath) {
      const content = await fs.readFile(filePath, 'utf-8');
    
      // Extract the outermost tag name and inner HTML
      const componentRegex = /<([\w-]+)>([\s\S]*?)<\/\1>/;
      const componentMatch = content.match(componentRegex);
    
      if (!componentMatch) {
        throw new Error(`Invalid component structure in ${path.basename(filePath)}`);
      }
    
      const tagName = componentMatch[1];
      let innerContent = componentMatch[2];
    
      // Extract all <script> sections
      const scriptRegex = /<script>([\s\S]*?)<\/script>/g;
      let scriptContent = '';
      let scriptMatch;
      while ((scriptMatch = scriptRegex.exec(content)) !== null) {
        scriptContent += scriptMatch[1].trim() + '\n';
      }
    
      // Extract all <style> sections
      const styleRegex = /<style>([\s\S]*?)<\/style>/g;
      let styleContent = '';
      let styleMatch;
      while ((styleMatch = styleRegex.exec(content)) !== null) {
        styleContent += styleMatch[1].trim() + '\n';
      }
    
      // Remove all <script> and <style> tags from innerContent
      innerContent = innerContent
        .replace(scriptRegex, '')
        .replace(styleRegex, '')
        .trim();
    
      return {
        tagName,
        html: innerContent,
        script: scriptContent,
        style: styleContent,
      };
    }
    
    /**
     * Parse the script content to extract properties and methods.
     * @param {string} script - The script content.
     * @returns {{ properties: Array, methods: Array }} - Parsed properties and methods.
     */
    function parseScript(script) {
      const properties = [];
      const methods = [];
    
      try {
        // Parse the script content into an AST
        const ast = acorn.parse(script, { ecmaVersion: 2020, locations: true });
    
        ast.body.forEach(node => {
          if (node.type === 'VariableDeclaration') {
            node.declarations.forEach(declarator => {
              if (declarator.id.type === 'Identifier') {
                const varName = declarator.id.name;
                const varValue = script.substring(declarator.init.start, declarator.init.end);
                properties.push({ name: varName, value: varValue });
              }
            });
          } else if (node.type === 'FunctionDeclaration') {
            const funcName = node.id.name;
            const params = node.params.map(param => script.substring(param.start, param.end)).join(', ');
            const body = script.substring(node.body.start, node.body.end);
            methods.push({ name: funcName, params, body });
          }
        });
      } catch (error) {
        console.error(`Error parsing script: ${error.message}`);
      }
    
      return { properties, methods };
    }
    
    /**
     * Convert a kebab-case or other tag name to PascalCase for class naming.
     * @param {string} tagName - The custom element tag name.
     * @returns {string} - PascalCase string.
     */
    function toPascalCase(tagName) {
      return tagName
        .split('-')
        .map(word => word.charAt(0).toUpperCase() + word.slice(1))
        .join('');
    }
    
    /**
     * Escape backticks in templates to avoid breaking template literals.
     * @param {string} str - The template string.
     * @returns {string} - Escaped string.
     */
    function escapeBackticks(str) {
      return str.replace(/`/g, '\\`');
    }
    
    /**
     * Generate the class code for the component.
     * @param {object} component - Component details.
     * @returns {string} - The compiled class code.
     */
    function generateClassCode({ tagName, html, style, properties, methods }) {
      const className = toPascalCase(tagName);
      let classCode = `class ${className} extends HTMLElement {
      constructor() {
        super();
    `;
    
      // Initialize properties
      properties.forEach(prop => {
        classCode += `    this._${prop.name} = ${prop.value};
    `;
      });
    
      classCode += `
        this.attachShadow({ mode: 'open' });
        this._buildDOM();
      }
    
      _buildDOM() {
        const fragment = document.createDocumentFragment();
    `;
    
      // Add styles
      if (style) {
        classCode += `    const style = document.createElement('style');
        style.textContent = \`
    ${style}
        \`;
        fragment.appendChild(style);
    
    `;
      }
    
      // Generate DOM creation code
      const { code: domCode, cachedNodes, eventListeners } = generateDOMCode(html);
      classCode += domCode;
    
      classCode += `
        this.shadowRoot.appendChild(fragment);
      }
    `;
    
      // connectedCallback
      classCode += `
      connectedCallback() {
    `;
      
      // Attach event listeners
      if (eventListeners && eventListeners.length > 0) {
        eventListeners.forEach(listener => {
          classCode += `    this.${listener.elementVarName}.addEventListener('${listener.event}', this.${listener.handlerName}.bind(this));
    `;
        });
      }
    
      classCode += `  }
    
      disconnectedCallback() {
    `;
    
      // Remove event listeners
      if (eventListeners && eventListeners.length > 0) {
        eventListeners.forEach(listener => {
          classCode += `    this.${listener.elementVarName}.removeEventListener('${listener.event}', this.${listener.handlerName}.bind(this));
    `;
        });
      }
    
      classCode += `  }
    `;
    
      // Generate getters and setters
      Object.keys(cachedNodes).forEach(propName => {
        const nodes = cachedNodes[propName];
    
        classCode += `
      get ${propName}() { return this._${propName}; }
      set ${propName}(value) {
        if (this._${propName} !== value) {
          this._${propName} = value;
    `;
        
        nodes.forEach(node => {
          if (node.type === 'text') {
            classCode += `      this.${node.varName}.textContent = ${node.expression};
    `;
          } else if (node.type === 'attribute') {
            classCode += `      this.${node.elementVarName}.setAttribute('${node.attrName}', ${node.expression});
    `;
          }
        });
    
        classCode += `    }
      }
    `;
      });
    
      // Add methods
      methods.forEach(method => {
        const transformedBody = transformMethodBody(method.body, properties.map(p => p.name));
        classCode += `
      ${method.name}(${method.params}) {
    ${transformedBody}
      }
    `;
      });
    
      classCode += `}
      
    customElements.define('${tagName}', ${className});
    `;
    
      return classCode;
    }
    
    /**
     * Transform method body to replace variable references with 'this.' prefix.
     * @param {string} body - The method body.
     * @param {Array<string>} propNames - The property names to replace.
     * @returns {string} - The transformed method body.
     */
    function transformMethodBody(body, propNames) {
      let transformedBody = body;
    
      propNames.forEach(propName => {
        // Replace assignments like 'propName = value;' with 'this.propName = value;'
        const assignRegex = new RegExp(`([^\\.\\w$])${propName}\\s*=`, 'g');
        transformedBody = transformedBody.replace(assignRegex, `$1this.${propName} =`);
    
        // Replace variable references with 'this.propName'
        const varRegex = new RegExp(`([^\\.\\w$])${propName}([^\\w$])`, 'g');
        transformedBody = transformedBody.replace(varRegex, `$1this.${propName}$2`);
      });
    
      // Ensure braces are correctly balanced and formatted
      transformedBody = transformedBody.trim();
    
      // Remove extra outer braces if they exist
      if (transformedBody.startsWith('{') && transformedBody.endsWith('}')) {
        transformedBody = transformedBody.slice(1, -1).trim();
      }
    
      // Properly indent the method body
      transformedBody = transformedBody
        .split('\n')
        .map(line => '    ' + line.trim())
        .join('\n');
    
      return transformedBody;
    }
    
    /**
     * Generate DOM creation code from HTML using htmlparser2.
     * @param {string} html - The inner HTML string of the component.
     * @returns {object} - Object containing generated code, cached nodes, and event listeners.
     */
    function generateDOMCode(html) {
      const dom = parseDocument(html).children; // Array of DOM nodes
    
      let code = '';
      let varCounter = 0;
      const cachedNodes = {}; // For properties to nodes
      const eventListeners = [];
    
      function traverse(node, parentVarName) {
        if (node.type === 'tag') {
          const varName = node.name + ++varCounter;
    
          // Decide whether to assign to 'this.' or 'const'
          let declaration;
          if (needsReference(node)) {
            declaration = `this.${varName}`;
          } else {
            declaration = `const ${varName}`;
          }
    
          const varReference = declaration.replace(/^const /, '');
          code += `    ${declaration} = document.createElement('${node.name}');
    `;
    
          // Handle attributes
          const attribs = node.attribs || {};
          for (let [attrName, attrValue] of Object.entries(attribs)) {
            if (attrName === 'class' || attrName === 'style' || attrName.startsWith('data-') || attrName === 'id' || attrName === 'src' || attrName === 'alt') {
              // Handle attributes with embedded expressions
              const { expression, propNames } = generateAttributeExpression(attrValue);
              code += `    ${varReference}.setAttribute('${attrName}', ${expression});
    `;
    
              // Cache nodes for reactive updates
              propNames.forEach(propName => {
                if (!cachedNodes[propName]) cachedNodes[propName] = [];
                cachedNodes[propName].push({
                  type: 'attribute',
                  elementVarName: varReference.replace(/^this\./, ''),
                  attrName: attrName,
                  expression: generateAttributeExpression(attrValue).expression,
                });
              });
            } else if (attrName.startsWith('on')) {
              // Event listener
              const event = attrName.substring(2);
              const handlerName = attrValue.replace(/\(\)$/, '').trim();
              eventListeners.push({ elementVarName: varReference.replace(/^this\./, ''), event, handlerName });
            } else {
              // For other attributes
              code += `    ${varReference}.setAttribute('${attrName}', '${attrValue}');
    `;
            }
          }
    
          // Append to parent
          const parentRef = parentVarName ? parentVarName : 'fragment';
          code += `    ${parentRef}.appendChild(${varReference});
    `;
    
          // Traverse children
          if (node.children && node.children.length) {
            for (let child of node.children) {
              traverse(child, varReference);
            }
          }
        } else if (node.type === 'text') {
          const textContent = node.data;
    
          if (textContent.trim()) {
            // Handle text nodes with expressions
            const { expression, propNames } = generateTextExpression(textContent);
    
            const varName = 'textNode' + ++varCounter;
    
            // Decide whether to assign to 'this.' or 'const'
            let declaration;
            if (expression.includes('this._')) {
              declaration = `this.${varName}`;
            } else {
              declaration = `const ${varName}`;
            }
    
            const varReference = declaration.replace(/^const /, '');
            code += `    ${declaration} = document.createTextNode(${expression});
    `;
            code += `    ${parentVarName}.appendChild(${varReference});
    `;
    
            // Cache nodes for reactive updates
            propNames.forEach(propName => {
              if (!cachedNodes[propName]) cachedNodes[propName] = [];
              cachedNodes[propName].push({
                type: 'text',
                varName: varReference.replace(/^this\./, ''),
                expression: generateTextExpression(textContent).expression,
              });
            });
          }
        }
      }
    
      function needsReference(node) {
        // Determines if the element needs to be assigned to 'this.'
        // Elements that are:
        // - Targets of event listeners
        // - Contain reactive expressions
        // Need to be accessible outside of _buildDOM
        if (node.attribs) {
          const hasEventListener = Object.keys(node.attribs).some(attr => attr.startsWith('on'));
          const hasReactiveAttr = Object.values(node.attribs).some(value => /\{([\w$]+)\}/.test(value));
          if (hasEventListener || hasReactiveAttr) {
            return true;
          }
        }
    
        if (node.children && node.children.some(child => child.type === 'text' && /\{([\w$]+)\}/.test(child.data))) {
          return true;
        }
    
        return false;
      }
    
      function generateTextExpression(textContent) {
        const exprRegex = /\{([\w$]+)\}/g;
        let segments = [];
        let propNames = [];
        let lastIndex = 0;
        let match;
    
        while ((match = exprRegex.exec(textContent)) !== null) {
          if (match.index > lastIndex) {
            segments.push(JSON.stringify(textContent.slice(lastIndex, match.index)));
          }
          segments.push(`this._${match[1]}`);
          propNames.push(match[1]);
          lastIndex = exprRegex.lastIndex;
        }
    
        if (lastIndex < textContent.length) {
          segments.push(JSON.stringify(textContent.slice(lastIndex)));
        }
    
        return {
          expression: segments.join(' + '),
          propNames,
        };
      }
    
      function generateAttributeExpression(attrValue) {
        const exprRegex = /\{([\w$]+)\}/g;
        let segments = [];
        let propNames = [];
        let lastIndex = 0;
        let match;
    
        while ((match = exprRegex.exec(attrValue)) !== null) {
          if (match.index > lastIndex) {
            segments.push(JSON.stringify(attrValue.slice(lastIndex, match.index)));
          }
          segments.push(`this._${match[1]}`);
          propNames.push(match[1]);
          lastIndex = exprRegex.lastIndex;
        }
    
        if (lastIndex < attrValue.length) {
          segments.push(JSON.stringify(attrValue.slice(lastIndex)));
        }
    
        return {
          expression: segments.join(' + '),
          propNames,
        };
      }
    
      dom.forEach(node => traverse(node, null));
    
      return { code, cachedNodes, eventListeners };
    }
    
    module.exports = {
      parseComponent,
      parseScript,
      toPascalCase,
      escapeBackticks,
      generateClassCode,
      transformMethodBody,
      generateDOMCode
    };
    
    // cat/src/page.js
    
    const fs = require('fs-extra');
    const path = require('path');
    const glob = require('glob');
    
    /**
     * Parse a page file to extract the route and its content.
     * @param {string} filePath - Path to the .page.html file.
     * @returns {Promise<{ route: string, content: string }>} - Page details.
     */
    async function parsePage(filePath) {
      const content = await fs.readFile(filePath, 'utf-8');
    
      // Regex to match the <page> tag and extract the route attribute
      const pageTagRegex = /<page\s+[^>]*route=["']([^"']+)["'][^>]*>/i;
      const match = content.match(pageTagRegex);
    
      if (!match) {
        throw new Error(`No valid <page> tag with 'route' attribute found in ${path.basename(filePath)}`);
      }
    
      const route = match[1];
    
      // Extract the inner HTML of the <page> tag
      const innerContentRegex = new RegExp(`<page[^>]*>([\\s\\S]*?)<\\/page>`, 'i');
      const innerMatch = content.match(innerContentRegex);
    
      const pageContent = innerMatch ? innerMatch[1].trim() : '';
    
      return { route, content: pageContent };
    }
    
    /**
     * Detect all <load> tags in the provided HTML contents and extract their routes.
     * @param {Array<string>} htmlContents - Array of HTML file contents.
     * @returns {Array<string>} - Array of routes found in <load> tags.
     */
    function detectLoadRoutes(htmlContents) {
      const loadRoutes = [];
    
      // Regex to match <load route="..."> tags
      const loadTagRegex = /<load\s+[^>]*route=["']([^"']+)["'][^>]*>/gi;
    
      htmlContents.forEach(content => {
        let match;
        while ((match = loadTagRegex.exec(content)) !== null) {
          const route = match[1];
          loadRoutes.push(route);
        }
      });
    
      return loadRoutes;
    }
    
    module.exports = {
      parsePage,
      detectLoadRoutes
    };
    

    Task

    • Update our page.js compiler, to transform the HTML into the JS, following the same logic used on component.js.
    • But instead of registering them as custom elements, you must find the most efficient and powerful way to register them, considering they are going to be structured in the same way components are after compiled, with their getter and setter by {expression}, custom methods, etc...
  2. Assistant

    To enhance your page.js compiler and align it with the functionality of component.js, we can transform each page into a JavaScript class with properties, getters, setters, and custom methods. Instead of registering these classes as custom elements, we'll register them with a centralized Router or PageManager. This approach ensures efficient management of page lifecycles and seamless integration with your application's routing system.

    Below is the updated page.js with detailed explanations and comments to guide you through the changes:

    // cat/src/page.js
    
    const fs = require('fs-extra');
    const path = require('path');
    const acorn = require('acorn'); // JavaScript parser
    const { parseDocument } = require('htmlparser2');
    const { generateClassCode, parseScript, toPascalCase, transformMethodBody, generateDOMCode } = require('./component');
    
    /**
     * Parse a page file to extract route, HTML template, script, and style sections.
     * @param {string} filePath - Path to the .page.html file.
     * @returns {Promise<{ route: string, html: string, script: string, style: string }>} - Page details.
     */
    async function parsePage(filePath) {
      const content = await fs.readFile(filePath, 'utf-8');
    
      // Extract the <page> tag with route attribute
      const pageTagRegex = /<page\s+[^>]*route=["']([^"']+)["'][^>]*>/i;
      const pageMatch = content.match(pageTagRegex);
    
      if (!pageMatch) {
        throw new Error(`No valid <page> tag with 'route' attribute found in ${path.basename(filePath)}`);
      }
    
      const route = pageMatch[1];
    
      // Extract inner HTML, <script>, and <style> sections
      const innerContentRegex = new RegExp(`<page[^>]*>([\\s\\S]*?)<\\/page>`, 'i');
      const innerMatch = content.match(innerContentRegex);
      const innerContent = innerMatch ? innerMatch[1].trim() : '';
    
      // Extract all <script> sections
      const scriptRegex = /<script>([\s\S]*?)<\/script>/g;
      let scriptContent = '';
      let scriptMatch;
      while ((scriptMatch = scriptRegex.exec(content)) !== null) {
        scriptContent += scriptMatch[1].trim() + '\n';
      }
    
      // Extract all <style> sections
      const styleRegex = /<style>([\s\S]*?)<\/style>/g;
      let styleContent = '';
      let styleMatch;
      while ((styleMatch = styleRegex.exec(content)) !== null) {
        styleContent += styleMatch[1].trim() + '\n';
      }
    
      return {
        route,
        html: innerContent,
        script: scriptContent,
        style: styleContent,
      };
    }
    
    /**
     * Parse the script content to extract properties and methods.
     * @param {string} script - The script content.
     * @returns {{ properties: Array, methods: Array }} - Parsed properties and methods.
     */
    // Reuse parseScript from component.js
    // const { parseScript } = require('./component'); // Already imported
    
    /**
     * Generate the class code for the page.
     * @param {object} page - Page details.
     * @returns {string} - The compiled class code.
     */
    function generatePageClassCode({ route, html, script, style }) {
      const className = `${toPascalCase(route)}Page`;
      const { properties, methods } = parseScript(script);
    
      let classCode = `class ${className} {
      constructor() {
        this._initializeProperties();
        this._buildDOM();
        this._attachEventListeners();
      }
    
      _initializeProperties() {
    `;
    
      // Initialize properties
      properties.forEach(prop => {
        classCode += `    this._${prop.name} = ${prop.value};
    `;
      });
    
      classCode += `  }
    
      _buildDOM() {
        this.element = document.createElement('div');
    `;
    
      // Add styles if any
      if (style) {
        classCode += `    const style = document.createElement('style');
        style.textContent = \`
    ${style}
        \`;
        this.element.appendChild(style);
    `;
      }
    
      // Generate DOM creation code from HTML
      const { code: domCode, cachedNodes, eventListeners } = generateDOMCode(html);
      classCode += `${domCode}
        document.body.appendChild(this.element);
      }
    
      _attachEventListeners() {
    `;
    
      // Attach event listeners as defined in the HTML
      if (eventListeners && eventListeners.length > 0) {
        eventListeners.forEach(listener => {
          classCode += `    this.${listener.elementVarName}.addEventListener('${listener.event}', this.${listener.handlerName}.bind(this));
    `;
        });
      }
    
      classCode += `  }
    
      _detachEventListeners() {
    `;
    
      // Detach event listeners for cleanup
      if (eventListeners && eventListeners.length > 0) {
        eventListeners.forEach(listener => {
          classCode += `    this.${listener.elementVarName}.removeEventListener('${listener.event}', this.${listener.handlerName}.bind(this));
    `;
        });
      }
    
      classCode += `  }
    
    `;
    
      // Generate getters and setters for reactive properties
      Object.keys(cachedNodes).forEach(propName => {
        const nodes = cachedNodes[propName];
    
        classCode += `
      get ${propName}() { return this._${propName}; }
      set ${propName}(value) {
        if (this._${propName} !== value) {
          this._${propName} = value;
    `;
    
        nodes.forEach(node => {
          if (node.type === 'text') {
            classCode += `      this.${node.varName}.textContent = ${node.expression};
    `;
          } else if (node.type === 'attribute') {
            classCode += `      this.${node.elementVarName}.setAttribute('${node.attrName}', ${node.expression});
    `;
          }
        });
    
        classCode += `    }
      }
    `;
      });
    
      // Add custom methods
      methods.forEach(method => {
        const transformedBody = transformMethodBody(method.body, properties.map(p => p.name));
        classCode += `
      ${method.name}(${method.params}) {
    ${transformedBody}
      }
    `;
      });
    
      // Add lifecycle methods for the Router
      classCode += `
      // Lifecycle method called when the page is displayed
      onShow() {
        // Custom logic when the page is shown
      }
    
      // Lifecycle method called when the page is hidden
      onHide() {
        this._detachEventListeners();
        // Custom logic when the page is hidden
      }
    }
    `;
    
      // Export the class
      classCode += `
    export default ${className};
    `;
    
      return classCode;
    }
    
    /**
     * Compiler Function for Pages
     * @param {string} projectRoot - Root directory of the project.
     * @returns {Promise<Map<string, string>>} - Map of route to compiled class code.
     */
    async function compilePages(projectRoot) {
      // Find all .page.html files in the project directory
      const pageFiles = glob.sync('**/*.page.html', {
        cwd: projectRoot,
        absolute: true,
        ignore: ['node_modules/**', 'cat/**']
      });
    
      const compiledPages = new Map(); // Map of route to compiled class code
    
      for (const file of pageFiles) {
        try {
          const { route, html, script, style } = await parsePage(file);
    
          // Generate class code for the page
          const classCode = generatePageClassCode({ route, html, script, style });
    
          compiledPages.set(route, classCode);
    
          console.log(`🔍 Parsed and compiled page: [${route}] from ${path.relative(projectRoot, file)}`);
        } catch (error) {
          console.error(`❌ Failed to parse page ${file}: ${error.message}`);
        }
      }
    
      if (compiledPages.size === 0) {
        console.warn('⚠️  No pages found to compile.');
      }
    
      return compiledPages;
    }
    
    /**
     * Generate the Router Registration Script for the compiled pages.
     * @param {Map<string, string>} compiledPages - Map of route to compiled class code.
     * @returns {string} - JavaScript code for registering pages with the router.
     */
    function generateRouterRegistrationScript(compiledPages) {
      let script = '';
    
      compiledPages.forEach((classCode, route) => {
        // Each class code should be saved to a separate module/file.
        // Here, we assume that the compiler will handle file creation, and pages are imported accordingly.
        // For simplicity, we'll inline the classes in this example.
    
        script += `
    ${classCode}
    
    Router.register('${route}', ${toPascalCase(route)}Page);
    `;
      });
    
      return script;
    }
    
    module.exports = {
      parsePage,
      compilePages,
      generateRouterRegistrationScript
    };
    

    Detailed Breakdown of Changes

    1. Parsing Enhanced to Extract Script and Style:

      • parsePage Function:
        • Now extracts not only the route and inner HTML but also <script> and <style> sections from the .page.html files.
        • This ensures that any JavaScript logic or CSS styles defined within the page are captured for compilation.
    2. Generating Page Classes:

      • generatePageClassCode Function:
        • Mirrors the structure of component.js by creating a JavaScript class for each page.
        • Initializes properties, builds the DOM, attaches event listeners, and defines lifecycle methods (onShow and onHide).
        • Handles reactive properties by generating getters and setters that update the DOM when properties change.
        • Compiles methods, transforming their bodies to ensure proper this context and reactivity.
    3. Compiling All Pages:

      • compilePages Function:
        • Iterates through all .page.html files, parses each one, and generates the corresponding JavaScript class.
        • Stores the compiled classes in a Map with the route as the key, facilitating easy registration with the router.
    4. Registering Pages with the Router:

      • generateRouterRegistrationScript Function:
        • Creates a script that imports each compiled page class and registers it with a centralized Router.
        • Assumes the existence of a Router class with a register method that maps routes to page classes.
        • This centralized registration allows the router to instantiate and manage pages based on the current route.
    5. Assumed Changes in Compiler Integration:

      • While the user specifically asked for updates to page.js, integrating the compiled pages requires corresponding changes in compiler.js.
      • After compiling pages, you should invoke generateRouterRegistrationScript and inject the resulting script into your index.html, ensuring that the router is aware of all pages.

    Example Integration in compiler.js

    To provide a complete picture, here's how you might integrate the updated page.js into your compiler.js:

    // cat/src/compiler.js
    
    const path = require('path');
    const { compile } = require('./compiler');
    const { compilePages, generateRouterRegistrationScript } = require('./page');
    
    async function compileProject(projectRoot) {
      const indexPath = path.join(projectRoot, 'index.html');
      
      // Compile components as before
      const compiledContent = await compile(indexPath);
      
      // Compile pages
      const compiledPages = await compilePages(projectRoot);
      
      // Generate router registration script
      const routerScript = generateRouterRegistrationScript(compiledPages);
      
      // Inject the router script before the closing </body> tag
      const scriptTag = `<script>
    ${routerScript}
    </script>
    </body>`;
    
      const finalContent = compiledContent.replace('</body>', scriptTag);
      
      // Write the final compiled HTML
      await fs.writeFile(path.join(projectRoot, 'dist', 'index.html'), finalContent, 'utf-8');
      
      console.log('✅ Compilation completed successfully.');
    }
    
    module.exports = { compileProject };
    

    Centralized Router Implementation

    For the above approach to work, you need a centralized Router that can manage page instances based on the current route. Here's a simple implementation:

    // cat/src/router.js
    
    class Router {
      constructor() {
        this.routes = new Map();
        window.addEventListener('popstate', this.handleRouteChange.bind(this));
      }
    
      static register(route, PageClass) {
        if (!Router.instance) {
          Router.instance = new Router();
        }
        Router.instance.routes.set(route, PageClass);
      }
    
      static navigate(route) {
        history.pushState({}, '', route);
        if (Router.instance) {
          Router.instance.handleRouteChange();
        }
      }
    
      handleRouteChange() {
        const path = window.location.pathname;
        const PageClass = this.routes.get(path);
        
        if (PageClass) {
          if (this.currentPage) {
            this.currentPage.onHide();
            document.body.removeChild(this.currentPage.element);
          }
          this.currentPage = new PageClass();
          this.currentPage.onShow();
        } else {
          console.error(`❌ No page registered for route: ${path}`);
        }
      }
    }
    
    // Initialize Router on page load
    window.addEventListener('DOMContentLoaded', () => {
      if (Router.instance) {
        Router.instance.handleRouteChange();
      }
    });
    
    export default Router;
    

    Explanation of the Router:

    • Registration:

      • Pages register themselves with the router using Router.register(route, PageClass).
    • Navigation:

      • Use Router.navigate(route) to programmatically navigate to a different route.
      • This updates the browser's history and triggers the route change handler.
    • Handling Route Changes:

      • On route change (either via navigation or browser controls), the router instantiates the corresponding page class.
      • It calls lifecycle methods onShow and onHide to manage page visibility and event listeners.

    Benefits of This Approach

    • Separation of Concerns:

      • Pages are managed separately from components, allowing for cleaner organization and easier maintenance.
    • Reactive Properties:

      • Similar to components, pages have reactive properties that automatically update the DOM when their values change.
    • Lifecycle Management:

      • Lifecycle methods (onShow and onHide) provide hooks for initializing and cleaning up resources, ensuring optimal performance.
    • Centralized Routing:

      • A single Router manages all page instances, simplifying navigation and state management across the application.

    Final Notes

    • Modularization:

      • Ensure that each compiled page class is either inlined (as shown) or properly modularized and imported where necessary.
    • Error Handling:

      • The compiler and router include basic error handling, but you may want to enhance it based on your application's requirements.
    • Extensibility:

      • This setup allows you to easily extend functionality, such as adding middleware, authentication checks, or dynamic imports for code-splitting.

    By implementing these changes, your page.js compiler will effectively transform page HTML files into robust JavaScript classes, seamlessly integrating them with your application's routing system and ensuring a maintainable and scalable architecture.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.