Skip to content
This repository has been 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
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -49,7 +49,6 @@
"dependencies": {
"loader-utils": "^2.0.0",
"mime-types": "^2.1.26",
"mini-svg-data-uri": "^1.1.3",
"schema-utils": "^2.6.5"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -47,7 +47,7 @@ export default function loader(src) {
typeof options.esModule !== 'undefined' ? options.esModule : true;

const encodedData = options.source
? options.source(src)
? options.source(src.toString())
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved
: `data:${mimetype || ''};${encoding},${src.toString(encoding)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look here https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

encoding is optinal, so

data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E is valid, but in our case we always insert ;

Rewrite it to "data:${mimetype || ''}${encoding? ;${encoding} : ''},${src.toString(encoding)}`;" + test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added boolean type for encoding, now we can disable it, if it's enabled it will use the default base64


return `${
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/source-option.test.js.snap

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions test/helpers/customSourceImplementation.js
@@ -0,0 +1,3 @@
export default (content) => {
return `data:image/svg+xml,${ content.trim().replace(/\s+/g, ' ')}`;
};
10 changes: 9 additions & 1 deletion test/helpers/index.js
Expand Up @@ -3,5 +3,13 @@ import execute from './execute';
import getCompiler from './getCompiler';
import normalizeErrors from './normalizeErrors';
import readAsset from './readAsset';
import customSourceImplementation from './customSourceImplementation';

export { compile, execute, getCompiler, normalizeErrors, readAsset };
export {
compile,
execute,
getCompiler,
normalizeErrors,
readAsset,
customSourceImplementation,
};
7 changes: 3 additions & 4 deletions test/source-option.test.js
Expand Up @@ -4,10 +4,9 @@ import {
getCompiler,
normalizeErrors,
readAsset,
customSourceImplementation,
} from './helpers';

const svgToMiniDataURI = require('mini-svg-data-uri');

describe('"source" option', () => {
it('should work with unspecified value with the default base64 encoding', async () => {
const compiler = getCompiler('simple-svg.js');
Expand All @@ -22,9 +21,9 @@ describe('"source" option', () => {
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});
it('should work with "Function" right mini-svg-data-uri encoding', async () => {
it('should work with "Function" right custom source implementation for encoding', async () => {
const compiler = getCompiler('simple-svg.js', {
source: (file) => svgToMiniDataURI(file.toString()),
source: (content) => customSourceImplementation(content),
});
const stats = await compile(compiler);

Expand Down