Wireframing | Cell 8 | Cell 10 | Search

This code reads three JavaScript files synchronously from the file system using Node.js's fs module and stores their contents in variables. An HTML string is then created that includes the script elements, which decode and execute the base64-encoded JavaScript code from the three files using eval() and atob().

Cell 9

const fs = require('fs');
const js1 = fs.readFileSync('./sosmethod/dist/www/polyfills.59868df8094f160adda5.js');
const js2 = fs.readFileSync('./sosmethod/dist/www/vendor.59868df8094f160adda5.js');
const js3 = fs.readFileSync('./sosmethod/dist/www/app.59868df8094f160adda5.js');

$html$ = '<bc-app style="position:relative;display:block;min-height:600px;min-width:600px;">hello!</bc-app>'
    + '<script type="application/javascript">eval(atob("' + (new Buffer(js1)).toString('base64') + '"));</script>'
    + '<script type="application/javascript">eval(atob("' + (new Buffer(js2)).toString('base64') + '"));</script>'
    + '<script type="application/javascript">eval(atob("' + (new Buffer(js3)).toString('base64') + '"));</script>';

What the code could have been:

import * as fs from 'fs';
import * as path from 'path';

// Define constants for file paths
const polyfillsFilePath = './sosmethod/dist/www/polyfills.59868df8094f160adda5.js';
const vendorFilePath = './sosmethod/dist/www/vendor.59868df8094f160adda5.js';
const appFilePath = './sosmethod/dist/www/app.59868df8094f160adda5.js';

// Define a function to read and encode files
function readFileAndEncode(filePath: string): string {
  try {
    const fileBuffer = fs.readFileSync(filePath);
    return fileBuffer.toString('base64');
  } catch (err) {
    console.error(`Error reading file: ${err}`);
    return '';
  }
}

// Read and encode files
const polyfillsContent = readFileAndEncode(polyfillsFilePath);
const vendorContent = readFileAndEncode(vendorFilePath);
const appContent = readFileAndEncode(appFilePath);

// Define the HTML template
const htmlTemplate = `
  
    hello!
    
    
    
  
`;

// Generate the final HTML string
const finalHtml = htmlTemplate
 .replace('%s', polyfillsContent)
 .replace('%s', vendorContent)
 .replace('%s', appContent);

// TODO: Consider using a templating engine for easier maintenance and separation of concerns
// TODO: Add error handling for edge cases (e.g., file not found, invalid file format)

console.log(finalHtml);

Code Breakdown

Importing Modules

const fs = require('fs');

Reading Files

const js1 = fs.readFileSync('./sosmethod/dist/www/polyfills.59868df8094f160adda5.js');
const js2 = fs.readFileSync('./sosmethod/dist/www/vendor.59868df8094f160adda5.js');
const js3 = fs.readFileSync('./sosmethod/dist/www/app.59868df8094f160adda5.js');

Creating HTML String

$html$ = '<bc-app style="position:relative;display:block;min-height:600px;min-width:600px;">hello!</bc-app>'
    + '<script type="application/javascript">eval(atob("' + (new Buffer(js1)).toString('base64') + '"));</script>'
    + '<script type="application/javascript">eval(atob("' + (new Buffer(js2)).toString('base64') + '"));</script>'
    + '<script type="application/javascript">eval(atob("' + (new Buffer(js3)).toString('base64') + '"));</script>';