Skip to content

Commit

Permalink
refactors build to use import instead of require and switch to esbuil…
Browse files Browse the repository at this point in the history
…d for bundling scripts
  • Loading branch information
smohadjer committed Aug 11, 2023
1 parent 9604662 commit be2c93b
Show file tree
Hide file tree
Showing 14 changed files with 893 additions and 4,715 deletions.
38 changes: 38 additions & 0 deletions bin/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as esbuild from 'esbuild';

const args = process.argv.slice(2);

console.log('build type: ', args[0]);

if (args[0] === 'js') {
let ctx = await esbuild.context({
entryPoints: ['app/resources/js/main.ts'],
bundle: true,
outfile: 'public/resources/js/bundle.js',
});

if (args[1] === 'watch') {
await ctx.watch();
} else {
ctx.rebuild();
}
}

/*
if (args[0] === 'css') {
let ctxcss = await esbuild.context({
entryPoints: ['app/resources/css/styles.css'],
bundle: true,
loader: {
'.svg': 'dataurl',
'.ttf': 'copy'
},
outfile: 'public/resources/css/styles.css',
});
if (args[1] === 'watch') {
await ctxcss.watch();
console.log('watching css...');
}
}
*/
3 changes: 2 additions & 1 deletion bin/clean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
import * as fs from 'fs';

const removeDirectory = (dir) => {
console.log(fs.existsSync(dir));
if (fs.existsSync(dir)){
Expand Down
22 changes: 11 additions & 11 deletions bin/compile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs');
const path = require('path');
const config = require('./config.js');
const handlebars = require('handlebars');
const partials = require('./partials.js');
const utils = require('./utils.js');
import * as fs from 'fs';
import * as path from 'path';
import config from './config.js';
import Handlebars from "handlebars";
import partials from './partials.js';
import {traverseDir, registerHandlebarsHelpers} from './utils.js';

const compileFile = function(pathToPage, sourceFolder, targetFolder) {
const pathToLayout = sourceFolder + '/layout.hbs';
Expand All @@ -13,7 +13,7 @@ const compileFile = function(pathToPage, sourceFolder, targetFolder) {
const folder = path.dirname(pathToPage);
const source = fs.readFileSync(pathToLayout, 'utf8');

handlebars.registerPartial(
Handlebars.registerPartial(
'content',
fs.readFileSync(pathToPage, 'utf8')
)
Expand All @@ -31,7 +31,7 @@ const compileFile = function(pathToPage, sourceFolder, targetFolder) {
}
}

const template = handlebars.compile(source);
const template = Handlebars.compile(source);
/* using substring(1) to remove slash from id */
const subFolder = folder.replace(sourceFolder + '/pages', '');
const page_id = (subFolder + '/' + filename).substring(1);
Expand All @@ -55,13 +55,13 @@ const compileFile = function(pathToPage, sourceFolder, targetFolder) {
});
};

utils.registerHandlebarsHelpers();
registerHandlebarsHelpers();

config.pages.forEach(page => {
partials.registerPartials(page.source + '/partials');
utils.traverseDir(page.source + '/pages', function(path) {
traverseDir(page.source + '/pages', function(path) {
compileFile(path, page.source, page.target);
});
});

module.exports = compileFile;
export default compileFile;
6 changes: 3 additions & 3 deletions bin/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const fs = require('fs');
const content = fs.readFileSync("projectConfig.json");
module.exports = JSON.parse(content);
import * as fs from 'fs';
const content = fs.readFileSync('projectConfig.json');
export default JSON.parse(content);
14 changes: 7 additions & 7 deletions bin/copy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const config = require('./config.js');
const fse = require("fs-extra");
const path = require('path');
const utils = require('./utils.js');
const fs = require('fs');
import * as fs from 'fs';
import * as path from 'path';
import {traverseDir} from './utils.js';
import config from './config.js';
import * as fse from 'fs-extra/esm';

function copyDependencies(type) {
if (config.dependencies[type]) {
Expand All @@ -26,7 +26,7 @@ function copyResources(folder) {

if (folder === 'js') {
//compile typescript files
utils.traverseDir(srcDir, (filePath) => {
traverseDir(srcDir, (filePath) => {
console.log('js: ', filePath);
const extension = path.extname(filePath);
console.log(extension);
Expand Down Expand Up @@ -74,7 +74,7 @@ function copyFile(source, destination) {
}

function copyFolder(source, destination) {
if (fse.existsSync(source)) {
if (fs.existsSync(source)) {
fse.copy(source, destination, function (err) {
if (err){
console.log('An error occured while copying the folder.')
Expand Down
8 changes: 5 additions & 3 deletions bin/hbs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs');
const exec = require('child_process').exec;
import * as fs from 'fs';
import child_process from 'child_process';

const exec = child_process.exec;

const precompileHbsTemplates = () => {
const shell_command = 'handlebars --extension hbs --namespace myApp.templates app/resources/hbs -f public/resources/js/lib/handlebars.templates.js';
Expand All @@ -15,4 +17,4 @@ const precompileHbsTemplates = () => {

precompileHbsTemplates();

module.exports = precompileHbsTemplates;
export default precompileHbsTemplates;
12 changes: 6 additions & 6 deletions bin/partials.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs');
const path = require('path');
const handlebars = require('handlebars');
const utils = require('./utils.js');
import * as fs from 'fs';
import * as path from 'path';
import handlebars from 'handlebars';
import { traverseDir } from './utils.js';

module.exports = {
export default {
registerPartials: (pathToPartial) => {
const callback = (fullPath) => {
const extension = path.extname(fullPath);
Expand All @@ -12,6 +12,6 @@ module.exports = {
handlebars.registerPartial(partialName, fs.readFileSync(fullPath, 'utf8'));
};

utils.traverseDir(pathToPartial, callback);
traverseDir(pathToPartial, callback);
}
}
9 changes: 5 additions & 4 deletions bin/sassTocss.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const sass = require('sass');
import * as fs from 'fs';
import * as path from 'path';
import * as sass from 'sass';

const dir = 'app/resources/css';
const targetDir = dir.replace('app/', 'public/');
const convertToCSS = (fullPath, targetPath) => {
Expand Down Expand Up @@ -41,6 +42,6 @@ const readCSSDir = () => {

readCSSDir();

module.exports = { readCSSDir }
export default { readCSSDir }


3 changes: 2 additions & 1 deletion bin/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');
import express from 'express';

const app = express();
const args = process.argv.slice(2);
const folder = args[0];
Expand Down
72 changes: 37 additions & 35 deletions bin/utils.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
const fs = require('fs');
const path = require('path');
const handlebars = require('handlebars');
import * as fs from 'fs';
import * as path from 'path';
import Handlebars from "handlebars";

module.exports = {
// https://stackoverflow.com/questions/50121881/node-js-recursively-list-full-path-of-files
traverseDir: (dir, callback) => {
const myRecursiveFunction = function(dir, callback) {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
myRecursiveFunction(fullPath, callback);
} else {
callback(fullPath);
}
});
};

myRecursiveFunction(dir, callback);
},
registerHandlebarsHelpers: () => {
/* taken from: https://stackoverflow.com/a/31632215/884177 */
handlebars.registerHelper({
eq: (v1, v2) => v1 === v2,
ne: (v1, v2) => v1 !== v2,
lt: (v1, v2) => v1 < v2,
gt: (v1, v2) => v1 > v2,
lte: (v1, v2) => v1 <= v2,
gte: (v1, v2) => v1 >= v2,
and() {
return Array.prototype.every.call(arguments, Boolean);
},
or() {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);
// https://stackoverflow.com/questions/50121881/node-js-recursively-list-full-path-of-files
const traverseDir = (dir, callback) => {
const myRecursiveFunction = function(dir, callback) {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
myRecursiveFunction(fullPath, callback);
} else {
callback(fullPath);
}
});
}
}
};

myRecursiveFunction(dir, callback);
};

const registerHandlebarsHelpers = () => {
/* taken from: https://stackoverflow.com/a/31632215/884177 */
Handlebars.registerHelper({
eq: (v1, v2) => v1 === v2,
ne: (v1, v2) => v1 !== v2,
lt: (v1, v2) => v1 < v2,
gt: (v1, v2) => v1 > v2,
lte: (v1, v2) => v1 <= v2,
gte: (v1, v2) => v1 >= v2,
and() {
return Array.prototype.every.call(arguments, Boolean);
},
or() {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);
}
});
};

export {traverseDir, registerHandlebarsHelpers};

41 changes: 14 additions & 27 deletions bin/watch.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
const compileFile = require('./compile.js');
const fs = require('fs');
const fse = require("fs-extra");
const chokidar = require('chokidar');
const partials = require('./partials.js');
const utils = require('./utils.js');
const path = require('path');
const config = require('./config.js');
const sass = require('./sassToCss.js');
const precompileHbsTemplates = require('./hbs.js');

/* watching js files for bundling */
// https://rollupjs.org/javascript-api/
const rollup = require('rollup');
const inputOptions = {
input: 'public/resources/js/main.js'
};
const outputOptions = {
file: 'public/resources/js/bundle.js',
format: 'iife'
};
const watchOptions = {
...inputOptions,
output: [outputOptions]
};
rollup.watch(watchOptions);
import compileFile from './compile.js';
import * as fs from 'fs';
import * as fse from 'fs-extra/esm';
import chokidar from 'chokidar';
import partials from './partials.js';
import {traverseDir} from './utils.js';
import * as path from 'path';
import config from './config.js';
import precompileHbsTemplates from './hbs.js';
import sass from './sassToCss.js';

// using cwd option so instead of path we get filename
const watcher = chokidar.watch('.', {
Expand All @@ -33,9 +17,12 @@ const watcher = chokidar.watch('.', {
cwd: 'app'
});

console.log('Watching app folder...');

/* copies assets and resources to public folder */
const copyFile = (filepath) => {
if (filepath.indexOf('resources/css') >= 0) {
console.log('css changed....');
sass.readCSSDir();
} else if (filepath.indexOf('resources/hbs') >= 0) {
precompileHbsTemplates();
Expand Down Expand Up @@ -77,7 +64,7 @@ const compileHbs = (filepath) => {
config.pages.forEach(page => {
if (page.source + '/' === sourceFolder) {
partials.registerPartials(page.source + '/partials');
utils.traverseDir(page.source + '/pages', function(path) {
traverseDir(page.source + '/pages', function(path) {
compileFile(path, page.source, page.target);
});
}
Expand Down
Loading

0 comments on commit be2c93b

Please sign in to comment.