Skip to content

Commit

Permalink
[Feature] Introducing image compression using imagemin (#654)
Browse files Browse the repository at this point in the history
* Introduce imagemin for compressing images

* Replace original images with the optimized ones

* Add imagemin-svgo to dependencies

* Remove console statement, replace let with const

* Replace let with const

* Add --skip-image-compression

* Run Prettier
  • Loading branch information
ahmadalfy authored and yangshun committed May 9, 2018
1 parent ac718dd commit ab6bab9
Show file tree
Hide file tree
Showing 45 changed files with 3,525 additions and 686 deletions.
4 changes: 2 additions & 2 deletions docs/api-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ Docusaurus provides some default mappings to allow you to run commands following

## Reference

### `docusaurus-build`
### `docusaurus-build [--skip-image-compression]`

Alias: `build`.

Generates the static website, applying translations if necessary. Useful for building the website prior to deployment.
Generates the static website, applying translations if necessary. Useful for building the website prior to deployment. By default Docusaurus will attempt to compress your images unless you provided the `--skip-image-compression` flag.

See also [`docusaurus-start`](api-commands.md#docusaurus-start-port-number).

Expand Down
32 changes: 30 additions & 2 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ async function execute() {
const sep = path.sep;
const escapeStringRegexp = require('escape-string-regexp');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
const commander = require('commander');
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
const imageminOptipng = require('imagemin-optipng');
const imageminSvgo = require('imagemin-svgo');
const imageminGifsicle = require('imagemin-gifsicle');

commander.option('--skip-image-compression').parse(process.argv);

// create the folder path for a file if it does not exist, then write the file
function writeFileAndCreateFolder(file, content) {
Expand Down Expand Up @@ -399,9 +407,29 @@ async function execute() {
}

fs.writeFileSync(mainCss, cssContent);
} else if (
normalizedFile.match(/\.png$|.jpg$|.svg$|.gif$/) &&
!commander.skipImageCompression
) {
const parts = normalizedFile.split(sep + 'static' + sep);
const targetDirectory = join(
buildDir,
parts[1].substring(0, parts[1].lastIndexOf(sep))
);
mkdirp.sync(path.dirname(targetDirectory));
imagemin([normalizedFile], targetDirectory, {
use: [
imageminOptipng(),
imageminJpegtran(),
imageminSvgo({
plugins: [{removeViewBox: false}],
}),
imageminGifsicle(),
],
});
} else if (!fs.lstatSync(normalizedFile).isDirectory()) {
let parts = normalizedFile.split(sep + 'static' + sep);
let targetFile = join(buildDir, parts[1]);
const parts = normalizedFile.split(sep + 'static' + sep);
const targetFile = join(buildDir, parts[1]);
mkdirp.sync(path.dirname(targetFile));
fs.copySync(normalizedFile, targetFile);
}
Expand Down
Loading

0 comments on commit ab6bab9

Please sign in to comment.