Skip to content

Commit

Permalink
Implement partial loader
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jul 7, 2024
1 parent 1d34ed0 commit 3563233
Show file tree
Hide file tree
Showing 11 changed files with 564 additions and 3 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
},
"homepage": "https://github.com/kekefreedog/CrazyPHP#readme",
"devDependencies": {
"@types/node": "^20.14.10",
"handlebars": "^4.7.8",
"localforage": "^1.10.0",
"object-hash": "^3.0.0",
"tslib": "^2.4.1"
}
}
}
62 changes: 62 additions & 0 deletions resources/Js/Webpack/Plugins/HandlebarsToJSPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const path = require('path');
const fs = require('fs');
const Handlebars = require('handlebars');
const CopyWebpackPlugin = require('copy-webpack-plugin');

class HandlebarsToJSPlugin {
constructor(options) {
this.options = options;
this.cleanDirectory = this.cleanDirectory.bind(this); // Bind cleanDirectory method
}

apply(compiler) {
compiler.hooks.afterEmit.tapAsync('HandlebarsToJSPlugin', (compilation, callback) => {
const partialsDir = this.options.partialDirs[0];
const outputDir = path.resolve(__dirname, 'public/dist/partials');

// Clean output directory before compilation
this.cleanDirectory(outputDir);

fs.readdir(partialsDir, (err, files) => {
if (err) throw err;

files.forEach(file => {
const filePath = path.join(partialsDir, file);
const fileContent = fs.readFileSync(filePath, 'utf-8');
const compiledTemplate = Handlebars.precompile(fileContent);

// Retrieve content hash from Webpack output filenames
const outputFiles = fs.readdirSync(outputDir+"/..");
const hashRegex = /index\.([a-f0-9]+)\.js/; // Adjust regex as per your filename format
let contentHash = '';

outputFiles.forEach(outputFile => {
const match = outputFile.match(hashRegex);
if (match) {
contentHash = match[1];
}
});

const jsFilePath = path.join(outputDir, `${file.replace(/\.hbs$/, '')}.${contentHash}.js`);
fs.writeFileSync(jsFilePath, `module.exports = ${compiledTemplate};`);
});

callback();
});
});
}

// Function to clean directory
cleanDirectory(directory) {
if (fs.existsSync(directory)) {
fs.readdirSync(directory).forEach(file => {
const filePath = path.join(directory, file);
fs.unlinkSync(filePath);
});
} else {
fs.mkdirSync(directory, { recursive: true });
}
}
}

module.exports = HandlebarsToJSPlugin;
16 changes: 16 additions & 0 deletions resources/Scss/style/dimensions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@

}

// Place element in the horizontal center only
&.screen-direction-y-asc {

// Position
flex-direction: column;

}

// Place element in the horizontal center only
&.screen-direction-y-desc {

// Position
flex-direction: column-reverse;

}

/* Children */

// Grow of flex child
Expand Down
6 changes: 6 additions & 0 deletions src/Front/Library/Crazypartial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export default abstract class Crazypartial {

public input:RegisterPartialScanned;

/**
* @param html:string
* Duplicate of the class name because build change name of class
*/
public static readonly html:string|null|CallableFunction = null;

/**
* Constructor
*
Expand Down
29 changes: 29 additions & 0 deletions src/Front/Library/Loader/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export default class Page {
.then(
// Load Post Action
Page.loadPostAction
)
.then(
// Set current page
Page.setCurrentPage
).catch(
Page.catchError
);
Expand All @@ -102,6 +106,10 @@ export default class Page {
.then(
// Load Post Action
Page.loadPostAction
)
.then(
// Set current page
Page.setCurrentPage
).catch(
Page.catchError
);
Expand Down Expand Up @@ -673,6 +681,27 @@ export default class Page {

}

/**
* set Current Page
*
* Set current page
*
* @param options:LoaderPageOptions Options with all page details
* @return Promise<LoaderPageOptions>
*/
public static setCurrentPage = async(options:LoaderPageOptions):Promise<LoaderPageOptions> => {

// Check status
if(options.status?.onReadyExecuted === true)

// Set curret page
window.Crazyobject.currentPage.set(options);

// Return options
return options;

}

/** Private methods
******************************************************
*/
Expand Down
Loading

0 comments on commit 3563233

Please sign in to comment.