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
57 changes: 57 additions & 0 deletions README.md
Expand Up @@ -236,6 +236,63 @@ module.exports = {
};
```

### `source`
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved

Type: `Function`
Default: `data:${mimetype || ''};${encoding},${src.toString(encoding)}`
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved

You can create you own custom implementation for encoding data. in the example we are compressing svg files using [mini-svg-data-uri](https://github.com/tigt/mini-svg-data-uri) implementation.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
use: [
{
loader: 'url-loader',
options: {
source: (svgContent) => {
const svgToMiniDataURI = require('mini-svg-data-uri');

return svgToMiniDataURI(svgContent);
},
},
},
],
},
],
},
};
```

By using your own implementation, `mimetype` and `encoding` won't have effect on the final output. until you specify them in the output manually for Example:

```js
module.exports = {
module: {
rules: [
{
test: /\.svg$/i,
use: [
{
loader: 'url-loader',
options: {
source: (svgContent) => {
return `data:image/svg;utf8,${svgContent}`;
},
},
},
],
},
],
},
};
```

### `esModule`

Type: `Boolean`
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"loader-utils": "^2.0.0",
"mime-types": "^2.1.26",
"mini-svg-data-uri": "^1.1.3",
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved
"schema-utils": "^2.6.5"
},
"devDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Expand Up @@ -46,11 +46,13 @@ export default function loader(src) {
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : true;

const encodedData = options.source
? options.source(src)
: `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 `${
esModule ? 'export default' : 'module.exports ='
} ${JSON.stringify(
`data:${mimetype || ''};${encoding},${src.toString(encoding)}`
)}`;
} ${JSON.stringify(encodedData)}`;
}

// Normalize the fallback.
Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Expand Up @@ -19,6 +19,10 @@
"ucs2"
]
},
"source": {
"description": "Adding custom implementation for encoding files.",
"instanceof": "Function"
},
"mimetype": {
"description": "The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype).",
"type": "string"
Expand Down
86 changes: 86 additions & 0 deletions test/__snapshots__/source-option.test.js.snap

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions test/source-option.test.js
@@ -0,0 +1,55 @@
import {
compile,
execute,
getCompiler,
normalizeErrors,
readAsset,
} 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');
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 "Function" right mini-svg-data-uri encoding', async () => {
const compiler = getCompiler('simple-svg.js', {
source: (file) => svgToMiniDataURI(file.toString()),
});
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');
});
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved
it('should work with "Function" that return the encoding manually', async () => {
const compiler = getCompiler('simple-svg.js', {
source: (content) => `data:image/svg+xml;utf8,${content}`,
});
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');
});
});
EslamHiko marked this conversation as resolved.
Show resolved Hide resolved