Split out task generation independant from size computation.

This commit is contained in:
Jocelyn Badgley (Twipped) 2020-04-07 09:54:05 -07:00
parent eb2081ce9e
commit 21e8b86d89

View File

@ -1,13 +1,13 @@
const path = require('path'); const path = require('path');
const { pick } = require('lodash'); const { pick, each } = require('lodash');
const actions = require('./actions'); const actions = require('./actions');
const File = require('./file'); const File = require('./file');
const { TYPE } = require('./resolve'); const { TYPE } = require('./resolve');
const getImageDimensions = require('./lib/dimensions'); const getImageDimensions = require('./lib/dimensions');
const getVideoDimensions = require('get-video-dimensions'); const getVideoDimensions = require('get-video-dimensions');
const RESOLUTIONS = [ 2048, 1024, 768, 576, 300, 100 ]; const WIDTHS = [ 2048, 1024, 768, 576, 300, 100 ];
module.exports = exports = class Asset extends File { module.exports = exports = class Asset extends File {
@ -25,6 +25,7 @@ module.exports = exports = class Asset extends File {
case TYPE.VIDEO: return this.loadVideo(); case TYPE.VIDEO: return this.loadVideo();
case TYPE.IMAGE: return this.loadImage(); case TYPE.IMAGE: return this.loadImage();
default: default:
return this.loadOther();
} }
} }
@ -49,37 +50,54 @@ module.exports = exports = class Asset extends File {
orientation, orientation,
}; };
this.sizes = [ {
url: this.url,
width,
height,
} ];
if (this.preprocessed) { if (this.preprocessed) {
this.sizes = [ { this._tasks = [ {
output: this.out, output: this.out,
url: this.url, input: this.input,
action: actions.copy,
nocache: true,
} ];
return;
}
this._tasks = [
{
output: this.out,
input: this.input,
width, width,
height, height,
} ]; format: 'jpeg',
} else { action: actions.image,
this.sizes = [ },
{ ];
output: this.out,
url: this.url,
width,
height,
},
];
for (const w of RESOLUTIONS) { for (const w of WIDTHS) {
if (w > width) continue; if (w > width) continue;
const name = `${this.name}.${w}w${this.ext}`; const name = `${this.name}.${w}w${this.ext}`;
this.sizes.push({ this.sizes.push({
output: path.join(this.base, name), url: path.join(this.dir, name),
url: path.join(this.dir, name), width: w,
width: w, height: Math.ceil((w / width) * height),
height: Math.ceil((w / width) * height), });
}); this._tasks.push({
} output: path.join(this.base, name),
input: this.input,
this.sizes.reverse(); width: w,
format: 'jpeg',
fill: 'contain',
quality: 85,
action: actions.image,
});
} }
this.sizes.reverse();
return this; return this;
} }
@ -104,12 +122,20 @@ module.exports = exports = class Asset extends File {
}; };
this.sizes = [ { this.sizes = [ {
output: path.join(this.base, this.basename),
url: path.join(this.dir, this.basename), url: path.join(this.dir, this.basename),
width, width,
height, height,
} ]; } ];
this._tasks = [
{
output: this.out,
input: this.input,
action: actions.copy,
nocache: true,
},
];
return this; return this;
} }
@ -124,13 +150,7 @@ module.exports = exports = class Asset extends File {
} }
tasks () { tasks () {
return this.sizes.map(({ output, width }) => ({ return this._tasks;
input: this.input,
output,
format: this.preprocessed ? undefined : this.ext.slice(1),
width: this.preprocessed ? undefined : width,
action: this.preprocessed ? actions.copy : actions.image,
}));
} }
}; };