Skip to content

Commit

Permalink
fix(assemble-lite, pv-stylemark): use data in layout folder only when…
Browse files Browse the repository at this point in the history
… rendering the components

use same behaviour in assemble instance as the normal assemble, which uses data files in layout directory only for components
  • Loading branch information
mbehzad committed Jun 25, 2024
1 parent 55d7bdd commit 4312dc9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
42 changes: 34 additions & 8 deletions packages/assemble-lite/Assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = class Assemble {
this.layouts = {};
// data which will be used when handlebars templates are rendered
this.dataPool = {};
// data only used for the standalone components render when they are not in the lsg folder
// i.e. needed for pv-path helper
this.additionalComponentDataPool = {};
// list of current handlebar helpers (which is also the file name of these helpers).
this.helpers = {};
// paths of files which throw an error during parsing or executing,
Expand Down Expand Up @@ -82,6 +85,7 @@ module.exports = class Assemble {
components,
pages,
data,
additionalComponentData,
helpers,
layouts,
componentsTargetDirectory,
Expand All @@ -105,14 +109,21 @@ module.exports = class Assemble {

timer.start("GETTING-PATHS");
// get the list of paths for all relevant files for rendering the handlebars templates
let [helperPaths, layoutPaths, componentPaths, pagePaths, dataPaths] =
await Promise.all([
getPaths(helpers),
getPaths(layouts),
getPaths(components),
getPaths(pages),
getPaths(data),
]);
let [
helperPaths,
layoutPaths,
componentPaths,
pagePaths,
dataPaths,
additionalComponentDataPaths,
] = await Promise.all([
getPaths(helpers),
getPaths(layouts),
getPaths(components),
getPaths(pages),
getPaths(data),
getPaths(additionalComponentData),
]);
this.log("Getting paths took:", timer.measure("GETTING-PATHS", true), "s");

timer.start("PROCESSING-FILES");
Expand All @@ -129,6 +140,11 @@ module.exports = class Assemble {

// #region remove data from memory for deleted files
this._removeObsolete(this.dataPool, dataPaths, getName);
this._removeObsolete(
this.additionalComponentDataPool,
additionalComponentDataPaths,
getName
);
this._removeObsolete(this.layouts, layoutPaths, getName);
// remove obsolete helpers (strongly assuming helper name and file name are identical)
this._removeObsolete(this.helpers, helperPaths, getName, ({ name }) =>
Expand All @@ -150,6 +166,8 @@ module.exports = class Assemble {
componentPaths = componentPaths.filter(wasModified);
pagePaths = pagePaths.filter(wasModified);
dataPaths = dataPaths.filter(wasModified);
additionalComponentDataPaths =
additionalComponentDataPaths.filter(wasModified);
}

timer.start("READ-AND-PARSE-FILES");
Expand All @@ -170,8 +188,15 @@ module.exports = class Assemble {

// read data
const readData = await this._loadData(dataPaths);
const readAdditionalComponentData = await this._loadData(
additionalComponentDataPaths
);
// merge with (potentially) old data
Object.assign(this.dataPool, readData);
Object.assign(
this.additionalComponentDataPool,
readAdditionalComponentData
);

this.log(
"Reading and parsing took:",
Expand Down Expand Up @@ -405,6 +430,7 @@ module.exports = class Assemble {
);

if (tpl.type === "COMPONENT") {
Object.assign(curData, this.additionalComponentDataPool);
const body = tpl.render(curData);
const writingJobs = [];
const html = this.layouts[tpl.layout]
Expand Down
10 changes: 4 additions & 6 deletions packages/pv-stylemark/webpack-plugin/getFilesToWatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ const fileGlobes = {
resources: join(lsgAssetsSrc, "**"),
},
assembleFiles: {
data: [
// add .json,.yaml/.yml Component data files
join(componentsSrc, "**/*.{json,yaml,yml}"),
// add .json,.yaml/.yml Layout data files
join(cdTemplatesSrc, "**/*.{json,yaml,yml}"),
],
// add .json,.yaml/.yml Component data files
data: join(componentsSrc, "**/*.{json,yaml,yml}"),
// add .json,.yaml/.yml Layout data files
additionalComponentData: join(cdTemplatesSrc, "**/*.{json,yaml,yml}"),
// handlebars helpers
helpers: join(hbsHelperSrc, "*.js"),
// add .hbs Components files
Expand Down
3 changes: 2 additions & 1 deletion packages/pv-stylemark/webpack-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class PvStylemarkPlugin {
baseDir: resolveApp(componentsSrc),
components: resolveApp(fileGlobes.assembleFiles.components),
pages: resolveApp(fileGlobes.assembleFiles.pages),
data: fileGlobes.assembleFiles.data.map(resolveApp),
data: resolveApp(fileGlobes.assembleFiles.data),
additionalComponentData: resolveApp(fileGlobes.assembleFiles.additionalComponentData),
helpers: resolveApp(fileGlobes.assembleFiles.helpers),
layouts: resolveApp(fileGlobes.assembleFiles.layouts),
componentsTargetDirectory: resolveApp(join(destPath, "components")),
Expand Down

0 comments on commit 4312dc9

Please sign in to comment.