- Rebuilt the entire project - now we can pass blockName in URL.

- Added "ViewMode"
This commit is contained in:
2024-01-14 00:36:14 +02:00
parent b76f83d4fa
commit f48b2a3274
26 changed files with 618 additions and 699 deletions
+109
View File
@@ -0,0 +1,109 @@
import gulp from "gulp";
import path from "path";
import sourcemaps from "gulp-sourcemaps";
import babel from "gulp-babel";
import PluginError from "plugin-error";
import rename from "gulp-rename";
import uglify from "gulp-uglify";
import dartSass from 'sass';
import gulpSass from "gulp-sass";
import {getBlockName} from "../helpers.js";
const sass = gulpSass(dartSass);
export function setupWatcher(viewSync) {
const watchSCSS = gulp.watch(['blocks/**/*.scss'], {delay: 400});
watchSCSS.on('change', async function (filepath, stats) {
const pathArray = filepath.split('/', 3);
pathArray.shift();
const block = getBlockName(pathArray.join('/'));
buildStyleFiles(path.join('blocks', '@' + block.project, block.name), () => {
viewSync.syncTemplate(block.blockName, 'styleUpdate');
})
});
const watchJS = gulp.watch(['blocks/**/*.js', '!blocks/**/*.min.js'], {delay: 400});
watchJS.on('change', async function (filepath) {
const pathArray = filepath.split('/', 3);
pathArray.shift();
const block = getBlockName(pathArray.join('/'));
buildScriptFiles(path.join('blocks', '@' + block.project, block.name), () => {
viewSync.syncTemplate(block.blockName, 'scriptUpdate');
})
});
const watchHBS = gulp.watch(['blocks/**/*.hbs'], {delay: 400});
watchHBS.on('change', async function (filepath) {
const pathArray = filepath.split('/', 3);
pathArray.shift();
const block = getBlockName(pathArray.join('/'));
await viewSync.syncTemplate(block.blockName);
});
}
export function buildAssetFiles(projectPath) {
// Register tasks.
gulp.task('build-script-files', (done) => buildScriptFiles(projectPath, done));
gulp.task('build-styling-files', (done) => buildStyleFiles(projectPath, done));
// Run first build.
return new Promise((resolve) => {
gulp.series('build-script-files', 'build-styling-files', function () {
resolve();
})();
});
}
function showError(errorMessage) {
console.log(errorMessage);
// TODO: Send this message to browser.
// So the developer can understand there is an error.
}
function buildScriptFiles(projectPath = '', done) {
const files = getJSBundleFiles(projectPath);
const stream = gulp.src(files, {base: path.posix.join(projectPath, 'src')})
.pipe(sourcemaps.init({}))
.pipe(babel()).on('error', function (error) {
showError(new PluginError('JavaScript', error).toString());
done();
})
.pipe(gulp.src(path.join(projectPath, 'vendor/*.js')))
// .pipe(gulp.dest('src/'))
.pipe(rename({extname: '.min.js'}))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(path.posix.join(projectPath, 'src')));
stream.on('end', done);
return stream;
}
function getJSBundleFiles(projectPath = '') {
return [path.posix.join(projectPath, "src/**/*.js"), path.posix.join(projectPath, "src/**/*.mjs"), "!" + path.posix.join(projectPath, "src/**/*.min.js")];
}
function buildStyleFiles(projectPath = '', done) {
const stream = gulp.src(path.join(projectPath, 'src/**/*.scss'), {base: path.posix.join(projectPath, 'src')})
.pipe(sourcemaps.init({}))
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', function (error) {
showError(new PluginError('SCSS', error.messageFormatted).toString());
// sass.logError(error);
done();
}))
// .pipe(gulp.dest('src/'))
.pipe(rename({extname: '.min.css'}))
.pipe(sourcemaps.write('.', {}))
.pipe(gulp.dest(path.posix.join(projectPath, 'src')));
stream.on('end', done);
return stream;
}
+53
View File
@@ -0,0 +1,53 @@
import express from "express";
import bodyParser from "body-parser";
import {createServer} from "http";
import {create} from "express-handlebars";
import {escape} from "lodash-es";
import {sanitizeUrl} from "@braintree/sanitize-url";
import path from "path";
export function init() {
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());
const hbs = getHandlebarsViewEngine();
app.engine('.hbs', hbs.engine);
app.set('view engine', '.hbs');
const modulesPath = '.';
app.set('views', path.join(modulesPath, 'layouts'));
// Static Files of blockDevTool.
app.use(express.static(path.join(modulesPath, 'layouts')));
return {
expressApp: app,
httpServer: createServer(app),
};
}
function getHandlebarsViewEngine() {
return create({
extname: '.hbs', defaultLayout: false, partialsDir: ['.'], helpers: handlebarsHelpers()
});
}
function handlebarsHelpers() {
return {
esc_attr(attr) {
return escape(attr);
},
esc_url(url) {
return sanitizeUrl(url);
},
esc_html(html) {
return html;
},
};
}
+50
View File
@@ -0,0 +1,50 @@
import {Server} from "socket.io";
import fs from "fs/promises";
import {getBlockName, handlebarLayoutsPath} from "../helpers.js";
import path from "path";
export class ViewSync {
constructor(httpServer) {
this.sessions = {};
this.init(httpServer);
}
addSession(session, blockName) {
if (!this.sessions[blockName]) {
this.sessions[blockName] = [];
}
this.sessions[blockName].push(session);
}
init(httpServer) {
const io = new Server(httpServer);
io.on('connection', async (socket) => {
const blockName = socket.handshake.query.block;
if (!blockName) {
return;
}
this.addSession(socket, blockName);
await this.syncTemplate(blockName);
});
// return httpServer;
}
async syncTemplate(blockName, updateType = 'templateUpdate') {
const block = getBlockName(blockName);
const projectPath = path.join('blocks', '@' + block.project, block.name);
const hbsTemplate = await fs.readFile(handlebarLayoutsPath(projectPath, 'src', `${block.name}.template.hbs`), 'utf8');
if (!this.sessions[blockName]) {
return;
}
this.sessions[blockName].forEach(session => {
session.emit(updateType, {block: blockName, template: hbsTemplate});
});
}
}