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

[Feature] Introducing image compression using imagemin #654

Merged
merged 9 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
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
31 changes: 29 additions & 2 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ 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 +409,26 @@ 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