diff --git a/lib/env-bundler.js b/lib/env-bundler.js index 7182b80..c5cf059 100644 --- a/lib/env-bundler.js +++ b/lib/env-bundler.js @@ -160,7 +160,7 @@ const componentCodeExtractors = { return c.name; }, asset: (c) => { - return c.correlationCode; + return c.correlationCode; }, group: (c) => { return c.name; @@ -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', @@ -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', @@ -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]}`) @@ -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); })); @@ -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; })); @@ -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;