Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strf-6383 add check for template size #475

Merged
merged 1 commit into from
May 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;