Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
feat: broccoli image to thumbnail plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
josex2r committed Dec 17, 2018
1 parent 193be98 commit e7ef700
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-env node */
'use strict';

module.exports = {
extends: ['@commitlint/config-conventional']
};
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.{html,css,hbs,js,ts}]
indent_style = tab
indent_size = tab
tab_width = 4

[*.hbs]
insert_final_newline = false

[*.md]
trim_trailing_whitespace = false
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# dependencies
/node_modules/

# misc
/coverage/
!.*
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-env node */
'use strict';

module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2017
},
extends: 'eslint-config-bbva',
env: {
node: true
},
rules: {
"callback-return": 0
}
};
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# dependencies
/node_modules

# misc
/.sass-cache
/connect.lock
/coverage/*
/libpeerconnection.log
npm-debug.log*
yarn-error.log
testem.log
tags*
8 changes: 8 additions & 0 deletions .huskyrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
hooks: {
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
'pre-push': 'npm run lint:js && npm run lint:hbs && npm run test'
}
};
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# misc
/.editorconfig
/.eslintignore
/.eslintrc.js
/.gitignore
/.travis.yml
13 changes: 13 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"prepare": [
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git"
],
"verifyConditions": [
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git",
"@semantic-release/github"
]
}
53 changes: 53 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
language: node_js
sudo: false
dist: trusty
node_js: 'stable'

cache:
directories:
- $HOME/.npm

matrix:
fast_finish: true

before_install:
# Configuration.
- npm config set spin false
- npm install -g codecov
- npm install -g greenkeeper-lockfile@1
- npm install -g @commitlint/travis-cli
- npm --version

install:
- if [[ $TRAVIS_BRANCH =~ greenkeeper || $TRAVIS_PULL_REQUEST_BRANCH =~ greenkeeper ]]; then npm install; else npm ci; fi

before_script:
- greenkeeper-lockfile-update

script:
- commitlint-travis
- npm run lint:js
- COVERAGE=true node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO --skip-cleanup

after_script:
- greenkeeper-lockfile-upload

after_success:
- codecov --file=coverage/lcov.info

stages:
- test
- name: release
if: branch = master

jobs:
include:
- stage: release
script: skip
env: []
deploy:
provider: script
skip_cleanup: true
script:
- npx semantic-release
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# broccoli-thumbnail

[![Build Status](https://travis-ci.org/BBVAEngineering/broccoli-thumbnail.svg?branch=master)](https://travis-ci.org/BBVAEngineering/broccoli-thumbnail)
[![GitHub version](https://badge.fury.io/gh/BBVAEngineering%2Fbroccoli-thumbnail.svg)](https://badge.fury.io/gh/BBVAEngineering%2Fbroccoli-thumbnail)
[![NPM version](https://badge.fury.io/js/broccoli-thumbnail.svg)](https://badge.fury.io/js/broccoli-thumbnail)
[![Dependency Status](https://david-dm.org/BBVAEngineering/broccoli-thumbnail.svg)](https://david-dm.org/BBVAEngineering/broccoli-thumbnail)
[![codecov](https://codecov.io/gh/BBVAEngineering/broccoli-thumbnail/branch/master/graph/badge.svg)](https://codecov.io/gh/BBVAEngineering/broccoli-thumbnail)
[![Greenkeeper badge](https://badges.greenkeeper.io/BBVAEngineering/broccoli-thumbnail.svg)](https://greenkeeper.io/)

## Information

[![NPM](https://nodei.co/npm/broccoli-thumbnail.png?downloads=true&downloadRank=true)](https://nodei.co/npm/broccoli-thumbnail/)

Copy and resizes images given an input tree.

**Why?** For example, to generate a low quality copy of each image and improve the page speed when loading.

## Installation & usage

`npm install --save broccoli-thumbnail`


```javascript
// Raw
const Thumbnail = require('broccoli-thumbnail');

const myTree = new Funnel('assets/images');
const thumbnailTree = new Thumbnail(myTree, {
width: 128,
prefix: 'small-'
});
```

```javascript
// Ember addon style
const Thumbnail = require('broccoli-thumbnail');

module.exports = {
// ...

treeForPublic() {
return new Thumbnail('assets/images');
}
};
```

## Options

| Option | Type | Defaults | Description |
|--------|----------|-------------------------------|-------------------------------------------|
| prefix | `String` | `thumbnail_` | Prefix to be added on each thumbnail name |
| globs | `Array` | `['**/*.(jpg|jpeg|gif|png)']` | Files to be processed |
| width | `Number` | `64` | Thumbnail width |

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/BBVAEngineering/broccoli-thumbnail/tags).

## Authors

See the list of [contributors](https://github.com/BBVAEngineering/broccoli-thumbnail/graphs/contributors) who participated in this project.

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Thumbnail = require('./lib/thumbnail');

module.exports = Thumbnail;
61 changes: 61 additions & 0 deletions lib/thumbnail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const Plugin = require('broccoli-caching-writer');
const path = require('path');
const fs = require('fs-extra');
const walkSync = require('walk-sync');
const sharp = require('sharp');

async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}

class Thumbnail extends Plugin {
constructor(inputNode, options = {}) {
options = Object.assign({
prefix: 'thumbnail_',
globs: [
'**/*.{jpg,jpeg,gif,png}'
],
width: 64
}, options);

super([inputNode], options);
this.options = options;
}

async build() {
const { globs, prefix, width } = this.options;
const inputPath = this.inputPaths[0];
const allInputFiles = walkSync(inputPath, { directories: false });
const images = walkSync(inputPath, { directories: false, globs });

await asyncForEach(images, async(relativePath) => {
const absolutePath = path.join(inputPath, relativePath);
const basename = path.basename(relativePath);
const dirname = path.dirname(relativePath);
const thumbnailRelativePath = path.join(dirname, `${prefix}${basename}`);
const image = sharp(absolutePath);

await fs.ensureDir(path.join(this.outputPath, dirname));

const metadata = await image.metadata();
const height = parseInt(metadata.height * width / metadata.width, 10);

await image
.resize(width, height)
.toFile(path.join(this.outputPath, thumbnailRelativePath));
});

await asyncForEach(allInputFiles, async(relativePath) => {
const absolutePath = path.join(inputPath, relativePath);
const outputPath = path.join(this.outputPath, relativePath);

await fs.ensureLink(absolutePath, outputPath);
});
}
}

module.exports = Thumbnail;
73 changes: 73 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "broccoli-thumbnail",
"version": "1.0.0",
"description": "Copy and resizes images given an input tree",
"main": "index.js",
"scripts": {
"test": "mocha ./tests",
"lint:js": "eslint --ext .js --max-warnings 0 ./*.js lib tests",
"report-coverage": "istanbul report --include=coverage/coverage-final.json text",
"check-coverage": "istanbul check-coverage coverage/coverage-final.json --statements 95 --functions 95 --branches 95 --lines 95",
"commit": "git-cz",
"prepublishOnly": "ember ts:precompile",
"postpublish": "ember ts:clean",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/BBVAEngineering/broccoli-thumbnail.git"
},
"keywords": [
"broccoli",
"image",
"meta",
"tree",
"process"
],
"author": "BBVAEngineering",
"contributors": [
{
"name": "josex2r",
"url": "https://github.com/josex2r"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/BBVAEngineering/broccoli-thumbnail/issues"
},
"homepage": "https://github.com/BBVAEngineering/broccoli-thumbnail#readme",
"engines": {
"node": ">= 8.*"
},
"devDependencies": {
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.1",
"@semantic-release/changelog": "^3.0.0",
"@semantic-release/git": "^7.0.3",
"broccoli-funnel": "^2.0.1",
"broccoli-test-helper": "^2.0.0",
"chai": "^4.2.0",
"commitizen": "^3.0.5",
"cz-conventional-changelog": "^2.1.0",
"eslint": "^5.1.0",
"eslint-config-bbva": "^2.0.0",
"husky": "^1.0.0-rc.13",
"image-size": "^0.6.3",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"semantic-release": "^15.12.2",
"travis-deploy-once": "^5.0.9"
},
"dependencies": {
"broccoli-caching-writer": "^3.0.3",
"fs-extra": "^7.0.1",
"sharp": "^0.21.1",
"walk-sync": "^1.0.1"
}
}
Binary file added tests/assets/troll.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/troll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e7ef700

Please sign in to comment.