Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
fix: allow using limit as string when you use loader with query string (
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 25, 2019
1 parent c0341da commit 4842f93
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -118,7 +118,7 @@ module.exports = {

### `limit`

Type: `Number|Boolean`
Type: `Number|Boolean|String`
Default: `undefined`

The limit can be specified via loader options and defaults to no limit.
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -16,7 +16,7 @@ function shouldTransform(limit, size) {
return limit;
}

if (typeof limit === 'number') {
if (typeof limit === 'number' || typeof limit === 'string') {
return size <= parseInt(limit, 10);
}

Expand Down
2 changes: 1 addition & 1 deletion src/options.json
Expand Up @@ -2,7 +2,7 @@
"type": "object",
"properties": {
"limit": {
"type": ["boolean", "number"]
"type": ["boolean", "number", "string"]
},
"mimetype": {
"type": "string"
Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/limit-option.test.js.snap

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/__snapshots__/mimetype-option.test.js.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/__snapshots__/validate-options.test.js.snap
Expand Up @@ -3,7 +3,7 @@
exports[`validation 1`] = `
"URL Loader Invalid Options
options.limit should be boolean,number
options.limit should be boolean,number,string
"
`;

Expand Down
85 changes: 85 additions & 0 deletions test/limit-option.test.js
Expand Up @@ -163,4 +163,89 @@ describe('limit option', () => {

expect(source).toMatchSnapshot();
});

it('0 ({String})', async () => {
// Image size is 6777
const config = {
loader: {
test: /\.png$/,
options: {
limit: '0',
},
},
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;

expect(source).toMatchSnapshot();
});

it('0.1 ({String})', async () => {
// Image size is 6777
const config = {
loader: {
test: /\.png$/,
options: {
limit: '0.1',
},
},
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;

expect(source).toMatchSnapshot();
});

it('6776 ({String})', async () => {
// Image size is 6777
const config = {
loader: {
test: /\.png$/,
options: {
limit: '6776',
},
},
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;

expect(source).toMatchSnapshot();
});

it('6777 ({String})', async () => {
// Image size is 6777
const config = {
loader: {
test: /\.png$/,
options: {
limit: '6777',
},
},
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;

expect(source).toMatchSnapshot();
});

it('6778 ({String})', async () => {
// Image size is 6777
const config = {
loader: {
test: /\.png$/,
options: {
limit: '6778',
},
},
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;

expect(source).toMatchSnapshot();
});
});
26 changes: 24 additions & 2 deletions test/loader.test.js
Expand Up @@ -10,8 +10,30 @@ describe('Loader', () => {
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;
const { modules, errors, warnings } = stats.toJson();

expect(source).toMatchSnapshot();
expect(modules[0].source).toMatchSnapshot();
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
});

it('should works when limit as a query string', async () => {
const config = {
rules: [
{
test: /\.png$/,
use: {
loader: `${require.resolve('../src')}?limit=10000`,
},
},
],
};

const stats = await webpack('fixture.js', config);
const { modules, errors, warnings } = stats.toJson();

expect(modules[0].source).toMatchSnapshot();
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
});
});
16 changes: 16 additions & 0 deletions test/mimetype-option.test.js
Expand Up @@ -30,4 +30,20 @@ describe('mimetype option', () => {

expect(source).toMatchSnapshot();
});

it('unknown ({String})', async () => {
const config = {
loader: {
test: /\.png$/,
options: {
mimetype: 'unknown/unknown',
},
},
};

const stats = await webpack('fixture.js', config);
const [{ source }] = stats.toJson().modules;

expect(source).toMatchSnapshot();
});
});
3 changes: 2 additions & 1 deletion test/validate-options.test.js
Expand Up @@ -19,7 +19,8 @@ it('validation', async () => {

expect(() => validate({ limit: 8192 })).not.toThrow();
expect(() => validate({ limit: true })).not.toThrow();
expect(() => validate({ limit: '8192' })).toThrowErrorMatchingSnapshot();
expect(() => validate({ limit: '8192' })).not.toThrow();
expect(() => validate({ limit: [] })).toThrowErrorMatchingSnapshot();

expect(() => validate({ mimetype: 'image/png' })).not.toThrow();
expect(() => validate({ mimetype: true })).toThrowErrorMatchingSnapshot();
Expand Down

0 comments on commit 4842f93

Please sign in to comment.