Skip to content

Commit

Permalink
Merge pull request #475 from bc-williamkwon/addTempCheck
Browse files Browse the repository at this point in the history
fix: strf-6383 add check for template size
  • Loading branch information
mattolson authored May 29, 2019
2 parents 2d9d2b9 + f674c05 commit ce09c03
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/stencil-bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const MAX_SIZE_BUNDLE = 1048576 * 50; //50MB
const MEGABYTE = 1048576
const MAX_SIZE_BUNDLE = MEGABYTE * 50;
const PATHS_TO_ZIP = [
'assets/**/*',
'!assets/cdn/**',
Expand Down Expand Up @@ -308,12 +308,16 @@ function bundleTaskRunner(callback) {
bundleThemeFiles(archive, this.themePath, this.configuration);

// zip all generated files
bundleParsedFiles(archive, taskResults);
const failedTemplates = bundleParsedFiles(archive, taskResults);

fileStream.on('close', () => {
const stats = Fs.statSync(bundleZipPath);
const size = stats['size'];

if (failedTemplates.length) {
return console.error(`Error: Your bundle failed as templates generated from the files below are greater than or equal to 1 megabyte in size:\n${failedTemplates.join('\n')}`);
}

if (size > MAX_SIZE_BUNDLE) {
return console.error(`Error: Your bundle of size ${size} bytes is above the max size of ${MAX_SIZE_BUNDLE} bytes`);
}
Expand Down Expand Up @@ -354,12 +358,13 @@ function bundleThemeFiles(archive, themePath, configuration) {
* Archive all generated files (ex. parsed files)
* @param {Archive} archive
* @param {Object} taskResults
* @returns {Array}
*/
function bundleParsedFiles(archive, taskResults) {
const archiveJsonFile = (data, name) => {
archive.append(JSON.stringify(data), { name });
}

const failedTemplates = [];
for (let task in taskResults) {
let data = taskResults[task];
switch(task) {
Expand All @@ -373,8 +378,13 @@ function bundleParsedFiles(archive, taskResults) {
case 'templates':
// Create the parsed tree files
for (let filename in data) {
let hash = Crypto.createHash('md5').update(filename).digest('hex');
archiveJsonFile(data[filename], `parsed/templates/${hash}.json`);
const hash = Crypto.createHash('md5').update(filename).digest('hex');
const fileData = data[filename];
archiveJsonFile(fileData, `parsed/templates/${hash}.json`);
// if file size is greater than 1 megabyte push filename to failedTemplates
if (JSON.stringify(fileData).length >= MEGABYTE) {
failedTemplates.push(filename);
}
}
break;

Expand All @@ -394,6 +404,7 @@ function bundleParsedFiles(archive, taskResults) {
break;
}
}
return failedTemplates;
}

module.exports = Bundle;

0 comments on commit ce09c03

Please sign in to comment.