Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add inline SVG files
  • Loading branch information
korywka committed Jan 11, 2020
commit 746c91fdaad057453f915d12c6cd4108345a3efb
24 changes: 23 additions & 1 deletion packages/image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

🍣 A Rollup plugin which imports JPG, PNG, GIF, SVG, and WebP files.

Images are encoded using base64, which means they will be 33% larger than the size on disk. You should therefore only use this for small images where the convenience of having them available on startup (e.g. rendering immediately to a canvas without co-ordinating asynchronous loading of several images) outweighs the cost.
In case you are not using inline SVG, images are encoded using base64, which means they will be 33% larger than the size on disk. You should therefore only use this for small images where the convenience of having them available on startup (e.g. rendering immediately to a canvas without co-ordinating asynchronous loading of several images) outweighs the cost.

## Requirements

Expand Down Expand Up @@ -70,6 +70,28 @@ import logo from './rollup.png';
document.body.appendChild(logo);
```

### `inline`

Type: `Boolean`<br>
Default: `false`

Depending on `dom` option, the plugin exports DOM `svg` node or svg content as string without encoding. Affects only SVG files.

If both `dom` and `inline` are `true`, the export can be used as such:

```js
import logo from './rollup.svg';
document.body.appendChild(logo);
logo.setAttribute('fill', 'red');
```

And string is exported using `inline` without `dom` option:

```js
import logo from './rollup.svg';
document.body.innerHTML += logo;
```

### `exclude`

Type: `String` | `Array[...String]`<br>
Expand Down
22 changes: 16 additions & 6 deletions packages/image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { createFilter } from '@rollup/pluginutils';
const defaults = {
dom: false,
exclude: null,
include: null
include: null,
inline: false
};

const mimeTypes = {
Expand Down Expand Up @@ -36,12 +37,21 @@ export default function image(opts = {}) {
return null;
}

const format = mime === mimeTypes['.svg'] ? 'utf-8' : 'base64';
const isSVG = mime === mimeTypes['.svg'];
const format = isSVG ? 'utf-8' : 'base64';
const source = readFileSync(id, format).replace(/[\r\n]+/gm, '');
const data = `'data:${mime};${format},${source}'`;
const code = options.dom
? `var img = new Image(); img.src = ${data}; export default img;`
: `const img = ${data}; export default img;`;
let code;
if (isSVG && options.inline) {
const svg = JSON.stringify(source);
code = options.dom
? `const svg = (new DOMParser().parseFromString(${svg}, 'image/svg+xml')).firstChild; export default svg;`
: `const svg = ${svg}; export default svg;`;
} else {
const data = `'data:${mime};${format},${source}'`;
code = options.dom
? `const img = new Image(); img.src = ${data}; export default img;`
: `const img = ${data}; export default img;`;
}

return code;
}
Expand Down
1 change: 1 addition & 0 deletions packages/image/test/fixtures/dom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
import logo from './rollup.jpg';

document.body.appendChild( logo );
32 changes: 31 additions & 1 deletion packages/image/test/snapshots/test.js.md

Large diffs are not rendered by default.

Binary file modified packages/image/test/snapshots/test.js.snap
Binary file not shown.
18 changes: 18 additions & 0 deletions packages/image/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ test('imports an svg without encoding', async (t) => {

t.snapshot(await getCode(bundle));
});

test('imports an svg and inline to string', async (t) => {
const bundle = await rollup.rollup({
input: 'fixtures/svg.js',
plugins: [image({ inline: true })]
});

t.snapshot(await getCode(bundle));
});

test('imports an svg and inline to dom', async (t) => {
const bundle = await rollup.rollup({
input: 'fixtures/svg.js',
plugins: [image({ dom: true, inline: true })]
});

t.snapshot(await getCode(bundle));
});