Skip to content

Commit

Permalink
Merge pull request #44 from entando/ENG-1680-filter-widget-fragments
Browse files Browse the repository at this point in the history
ENG-1680 filtering widget fragments during extraction
  • Loading branch information
ffleandro authored Dec 25, 2020
2 parents 2402e3d + 617672c commit 172d7a2
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/env-bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const componentCodeExtractors = {
return c.name;
},
asset: (c) => {
return c.correlationCode;
return c.correlationCode;
},
group: (c) => {
return c.name;
Expand Down Expand Up @@ -370,7 +370,7 @@ const componentResponseProcessor = {
'id': [{
key: 'correlationCode',
transform: function(id, source) {
return source.correlationCode !== undefined ? source.correlationCode : id;
return source.correlationCode !== null ? source.correlationCode : id;
}
}],
'type': 'type',
Expand Down Expand Up @@ -577,7 +577,7 @@ const processContentAttribute = function(attribute) {
'id': [{
key: 'correlationCode',
transform: function(id, source) {
return source.correlationCode !== undefined ? source.correlationCode : id;
return source.correlationCode !== null ? source.correlationCode : id;
}
}],
'name': 'name',
Expand Down Expand Up @@ -743,6 +743,10 @@ function canonizeCode (code) {
return code.trim().toLowerCase().replace(new RegExp(' ', 'g'), '-').replace(new RegExp('-+', 'g'), '-');
}

function canonizeCodeUnderline (code) {
return code.trim().toLowerCase().replace(new RegExp('[\\s\\-_]+', 'g'), '_');
}

const urlEncoder = function (payload) {
return Object.keys(payload)
.map((k) => `${k}=${payload[k]}`)
Expand Down Expand Up @@ -922,16 +926,18 @@ async function generateBundle (options, components) {
console.log('Generating bundle...');

const { code, description, location } = options;

const path = location.charAt(location.length - 1) === '/' ? location.slice(0, -1) : location;

// Filter widget fragments
const filteredComponents = await filterWidgetFragments(components);

// Extract used resources
const resources = await Promise.all(ALL_TYPES.map(async (type) => {
if (components[type] === undefined) return [];
if (filteredComponents[type] === undefined) return [];

const idExtractor = componentIdExtractors[type];
const codeExtractor = componentCodeExtractors[type];
const cachedComponents = components[type].map(c => componentCache[type].find(cached => idExtractor(cached) === c));
const cachedComponents = filteredComponents[type].map(c => componentCache[type].find(cached => idExtractor(cached) === c));

return resourceExtractorByType[type](cachedComponents);
}));
Expand All @@ -942,7 +948,7 @@ async function generateBundle (options, components) {

// Save components descriptors by type
const results = await Promise.all(ALL_TYPES.map(async (type) => {
const descriptors = await saveComponentsDescriptor(type, path, code, components[type]);
const descriptors = await saveComponentsDescriptor(type, path, code, filteredComponents[type]);
return descriptors;
}));

Expand All @@ -955,6 +961,12 @@ async function generateBundle (options, components) {
await saveBundleDescriptor(code, description, path, descriptors);
}

async function filterWidgetFragments(components) {
const widgets = components['widget'].map(w => canonizeCodeUnderline(w));
components['fragment'] = components['fragment'].filter(f => !widgets.includes(canonizeCodeUnderline(f)));
return components;
}

async function saveComponentsDescriptor(type, location, bundle_code, componentIds) {
if (undefined === componentIds) return;

Expand Down

0 comments on commit 172d7a2

Please sign in to comment.