Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. const fs = require('fs');
  2. const path = require('path');
  3. const chalk = require('chalk');
  4.  
  5. const COMPONENT_DIR = path.join(__dirname, '../src/components/');
  6. const IMPORT_TEXT = `import { html } from 'lit-html';`;
  7.  
  8. /**
  9. * Given a string of the templated, finds and extracts variables in the
  10. * template so they can be passed to a function during render.
  11. * @param {string} templateText The text which form the template.
  12. * @returns {string[]} An array of variable names which the template uses.
  13. */
  14. function getVariablesFromTemplate(templateText) {
  15. // TODO: This don't work for nested things - see Strutts awful template. :struttspin
  16. // Get the contents of any '${}' brackets
  17. let variables = templateText.split('${')
  18. variables = variables.map((variable) => variable.split('}')[0]);
  19. // Remove anything after the variable name e.g. example.something or example[]
  20. variables = variables.map((variable) => {
  21. return variable.split('.')[0].split('[')[0].split('(')[0].split(' ')[0]
  22. });
  23. // Remove the first 'variable'as it will just be a tag or some string.
  24. variables = variables.slice(1, variables.length);
  25. // Remove any duplicates
  26. variables = new Set(variables);
  27. variables = [...variables];
  28. return variables;
  29. }
  30.  
  31. /**
  32. * Takes a template name and text and forms a export statement for the template,
  33. * either a constant or a function depending on if there is any variables used
  34. * in the templated.
  35. * @param {string} templateName The name of the template to be used in the file to export
  36. * a constant or function to represent the component.
  37. * @param {string} templateText The text which form the template.
  38. * @returns {string} The export statement for the JS component file.
  39. */
  40. function TEMPLATE_DEFINITION(templateName, templateText) {
  41. if (templateText.includes('${')) {
  42. const variablesList = getVariablesFromTemplate(templateText).join(', ');
  43. return `export const ${templateName} = (${variablesList}) => html\`${templateText}\`;`
  44. }
  45. return `export const ${templateName} = html\`${templateText}\`;`;
  46. }
  47.  
  48. // Function to form the JS template file
  49. /**
  50. * Takes a template name and content and forms a lit-HTML JS component from it.
  51. * @param {string} templateName Name of the template to name component.
  52. * @param {string} templateText Contents of the template to convert to lit-HTML.
  53. * @returns {string} String content of the JS component file.
  54. */
  55. const TEMPLATE = (templateName, templateText) => `${IMPORT_TEXT}
  56. ${TEMPLATE_DEFINITION(templateName, templateText)}`;
  57.  
  58. // TODO: Make this take in a directory?
  59. // TODO: Make this search child directories and have paths in full e.g. /sub/component.html.
  60. module.exports.findComponents = function () {
  61. // Find all the HTML component files in the components folder.
  62. const componentsPath = path.join(__dirname, '../src/components');
  63. return fs.readdirSync(componentsPath).filter((component) => component.endsWith('.html'));
  64. }
  65.  
  66. /**
  67. * Compiles a specified component into a lit-HTML JS component.
  68. * @param {string} component The relative path to the component to compile.
  69. */
  70. module.exports.compileComponent = function (component) {
  71. // Get the name of the template and its path
  72. let templateName = `${component.split('.')[0]}`;
  73. let templatePath = path.join(COMPONENT_DIR, templateName);
  74. // Check the template exists.
  75. if (!fs.existsSync(`${templatePath}.html`)) {
  76. console.error(`File ${templatePath}.html not found so can't be compiled!`);
  77. return;
  78. }
  79. // (Re)Compile the template
  80. let templateContent = fs.readFileSync(`${templatePath}.html`, {
  81. encoding: 'utf8'
  82. });
  83. getVariablesFromTemplate(templateContent);
  84. fs.writeFileSync(templatePath + '.js', TEMPLATE(templateName, templateContent));
  85. }
  86.  
  87. /**
  88. * Compiles all the HTML components in the project into lit-HTML JS components.
  89. */
  90. module.exports.compileComponents = function () {
  91. const components = this.findComponents();
  92. components.forEach((component) => {
  93. this.compileComponent(component);
  94. console.log(chalk.green.bold(` > Compiled ${component}`));
  95. });
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement