Skip to content

Commit ab6bab9

Browse files
ahmadalfyyangshun
authored andcommittedMay 9, 2018
[Feature] Introducing image compression using imagemin (#654)
* 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
1 parent ac718dd commit ab6bab9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3525
-686
lines changed
 

‎docs/api-commands.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ Docusaurus provides some default mappings to allow you to run commands following
5353

5454
## Reference
5555

56-
### `docusaurus-build`
56+
### `docusaurus-build [--skip-image-compression]`
5757

5858
Alias: `build`.
5959

60-
Generates the static website, applying translations if necessary. Useful for building the website prior to deployment.
60+
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.
6161

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

‎lib/server/generate.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ async function execute() {
2929
const sep = path.sep;
3030
const escapeStringRegexp = require('escape-string-regexp');
3131
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
32+
const commander = require('commander');
33+
const imagemin = require('imagemin');
34+
const imageminJpegtran = require('imagemin-jpegtran');
35+
const imageminOptipng = require('imagemin-optipng');
36+
const imageminSvgo = require('imagemin-svgo');
37+
const imageminGifsicle = require('imagemin-gifsicle');
38+
39+
commander.option('--skip-image-compression').parse(process.argv);
3240

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

401409
fs.writeFileSync(mainCss, cssContent);
410+
} else if (
411+
normalizedFile.match(/\.png$|.jpg$|.svg$|.gif$/) &&
412+
!commander.skipImageCompression
413+
) {
414+
const parts = normalizedFile.split(sep + 'static' + sep);
415+
const targetDirectory = join(
416+
buildDir,
417+
parts[1].substring(0, parts[1].lastIndexOf(sep))
418+
);
419+
mkdirp.sync(path.dirname(targetDirectory));
420+
imagemin([normalizedFile], targetDirectory, {
421+
use: [
422+
imageminOptipng(),
423+
imageminJpegtran(),
424+
imageminSvgo({
425+
plugins: [{removeViewBox: false}],
426+
}),
427+
imageminGifsicle(),
428+
],
429+
});
402430
} else if (!fs.lstatSync(normalizedFile).isDirectory()) {
403-
let parts = normalizedFile.split(sep + 'static' + sep);
404-
let targetFile = join(buildDir, parts[1]);
431+
const parts = normalizedFile.split(sep + 'static' + sep);
432+
const targetFile = join(buildDir, parts[1]);
405433
mkdirp.sync(path.dirname(targetFile));
406434
fs.copySync(normalizedFile, targetFile);
407435
}

0 commit comments

Comments
 (0)
Please sign in to comment.