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

feat: add encoding & generator options #210

Merged
merged 15 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,35 @@ module.exports = {
};
```

### `encoding`

Type: `String`
Default: `base64`

Specify the encoding which the file will be in-lined with. It supports [Node.js Buffers and Character Encodings](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) which are `["utf8","utf16le","latin1","base64","hex","ascii","binary","ucs2"]`.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
use: [
{
loader: 'url-loader',
options: {
mimetype: 'utf8',
},
},
],
},
],
},
};
```

### `esModule`

Type: `Boolean`
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function loader(src) {
if (shouldTransform(options.limit, src.length)) {
const file = this.resourcePath;
const mimetype = options.mimetype || mime.contentType(path.extname(file));
const encoding = options.encoding || 'base64';

if (typeof src === 'string') {
// eslint-disable-next-line no-param-reassign
Expand All @@ -48,7 +49,7 @@ export default function loader(src) {
return `${
esModule ? 'export default' : 'module.exports ='
} ${JSON.stringify(
`data:${mimetype || ''};base64,${src.toString('base64')}`
`data:${mimetype || ''};${encoding},${src.toString(encoding)}`
)}`;
}

Expand Down
14 changes: 14 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
"description": "Enables/Disables transformation target file into base64 URIs (https://github.com/webpack-contrib/url-loader#limit).",
"type": ["boolean", "number", "string"]
},
"encoding": {
"description": "Specify the encoding which the file will be in-lined with.",
"type": "string",
"enum": [
"utf8",
"utf16le",
"latin1",
"base64",
"hex",
"ascii",
"binary",
"ucs2"
]
},
"mimetype": {
"description": "The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype).",
"type": "string"
Expand Down
305 changes: 305 additions & 0 deletions test/__snapshots__/encoding-option.test.js.snap

Large diffs are not rendered by default.

165 changes: 165 additions & 0 deletions test/encoding-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import {
compile,
execute,
getCompiler,
normalizeErrors,
readAsset,
} from './helpers';

describe('"encoding" option', () => {
it('should work with unspecified value with the default base64 encoding', async () => {
const compiler = getCompiler('simple-svg.js');
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (utf8)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'utf8',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (hex)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'hex',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (utf16le)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'utf16le',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (latin1)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'latin1',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (base64)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'base64',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (ascii)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'ascii',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (binary)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'binary',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should work with "String" right encoding value (ucs2)', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'ucs2',
});
const stats = await compile(compiler);

expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(Object.keys(stats.compilation.assets)).toMatchSnapshot('assets');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

it('should throw an error with "String" wrong encoding value equal to xyz', async () => {
const compiler = getCompiler('simple-svg.js', {
encoding: 'xyz',
});
const stats = await compile(compiler);

try {
execute(readAsset('main.bundle.js', compiler, stats));
expect(true).toBe(false);
} catch (err) {
expect(err.message.indexOf('ValidationError')).not.toBe(-1);
}
});
});