Skip to content

Commit

Permalink
update package to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
korywka committed Feb 6, 2023
1 parent c68ca41 commit 64c5c64
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 2,317 deletions.
8 changes: 4 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": [
"eslint-config-for-javascript"
],
"ignorePatterns": ["test"]
"extends": [
"eslint-config-ecmascript"
],
"ignorePatterns": ["test"]
}
21 changes: 0 additions & 21 deletions .github/workflows/main.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.idea
.idea
.DS_Store
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ Import `.svg` images in javascript files

## Installation

```shell script
```sh
npm install --save-dev rollup-plugin-svg-import
```

## Usage

Import SVG image as a `<svg>` DOM Node:

```javascript
```js
import icon from './icon.svg';

document.body.appendChild(icon());
```

For SSR support set `stringify` option to `true`, so SVG output is just a string:

```javascript
```js
import icon from './icon.svg';

document.body.innerHTML += icon;
Expand All @@ -46,7 +46,3 @@ export default {
],
};
```

## License

MIT
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const extname = require('path').extname;
const createFilter = require('@rollup/pluginutils').createFilter;
import { extname } from 'node:path';
import { createFilter } from '@rollup/pluginutils';

const injectNode = (svg) => (`
export default function() {
return (new DOMParser().parseFromString(${svg}, 'image/svg+xml')).firstChild;
return (new DOMParser().parseFromString(${svg}, 'image/svg+xml')).firstChild;
};
`);

Expand All @@ -15,19 +15,19 @@ const injectString = (svg) => `export default ${svg};`;
* @param options.exclude
* @param options.stringify - if true returns String, otherwise returns DOM Node
*/
module.exports = function (options = {}) {
const filter = createFilter(options.include, options.exclude);
export default function svgImportPlugin(options = {}) {
const filter = createFilter(options.include, options.exclude);

return {
name: 'svg-import',
transform: (code, id) => {
if (!filter(id) || extname(id) !== '.svg') return null;
const content = JSON.stringify(code);
return {
name: 'svg-import',
transform: (code, id) => {
if (!filter(id) || extname(id) !== '.svg') return null;
const content = JSON.stringify(code);

return {
code: options.stringify ? injectString(content) : injectNode(content),
map: { mappings: '' },
};
},
};
};
return {
code: options.stringify ? injectString(content) : injectNode(content),
map: { mappings: '' },
};
},
};
}
Loading

0 comments on commit 64c5c64

Please sign in to comment.