Skip to content

Commit

Permalink
largeFileCallback support
Browse files Browse the repository at this point in the history
  • Loading branch information
Juice10 committed Jan 21, 2021
1 parent 47263bb commit 42e78df
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ You can define local directories (globs supported) or URLs.

Sets a max file size (in bytes) for inlined images. Set to `0` to disable size checking.

#### largeFileCallback

* Type: `function`
* Default: undefined
* Example: `function (file) { console.log('big file found:', file.path); return file.path }`
* Required: `false`

Allows you to act on large files and change the url if you'd like. Make sure you have `strict` set to `false` when using this.

#### b64Svg

* Type: `bool`
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DEFAULTS = {
maxFileSize: 10240,
b64Svg: false,
strict: false,
largeFileCallback: undefined,
};

const loop = (cb) => {
Expand Down
5 changes: 5 additions & 0 deletions lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function getDataUriMapping(urls = [], options = {}) {
const file = await resolve(url, options);
if (file && file.mime && /image/.test(file.mime)) {
result[url] = await getDataUri(file, options);
} else if (options.largeFileCallback) {
const largeFile = await resolve(url, {...options, maxFileSize: 0});
if (largeFile && largeFile.mime && /image/.test(largeFile.mime)) {
result[url] = await options.largeFileCallback(largeFile);
}
}

return result;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/styles/big.modified.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.big {
background-image: url('https://cdn.example.com/images/blank.gif');
}
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe('postcss-image-inliner', () => {
test('big.css', 'big.css', {maxFileSize: 1}, done);
});

it('should trigger callback on too big images', (done) => {
const largeFileCallback = (file) => {
return file.path.replace('http://localhost:3000/', 'https://cdn.example.com/');
};

test('big.css', 'big.modified.css', {maxFileSize: 1, largeFileCallback}, done);
});

it('should handle multiple background images', (done) => {
test('multi.in.css', 'multi.out.css', {}, done);
});
Expand Down

0 comments on commit 42e78df

Please sign in to comment.