Skip to content

Commit 13cf45f

Browse files
committed
Refactor code to disable auto prefix and fix tests
1 parent 919e7ed commit 13cf45f

23 files changed

+11093
-3510
lines changed

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": [
3+
"adidas-env/node",
4+
"adidas-es9",
5+
"adidas-babel"
6+
]
7+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
*.bck
2+
.eslintcache
3+
.vscode
14
node_modules
5+
test/coverage

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,33 @@ const NaughtyUsage = () => (
3939

4040
### 2. Namespaces `id`’s to prevent conflicts
4141

42-
If you inline a lot of SVGs you might get namespace conflicts, which could be really annoying if you're styling your SVG in CSS and whatnot. This plugin solves that by combining some [regex trickery](./optimise.js#L30) with SVGO's `cleanupIDs` plugin.
42+
If you inline a lot of SVGs you might get namespace conflicts, which could be really annoying if you're styling your SVG in CSS and whatnot. This plugin solves that by combining some [regex trickery](./optimize.js#L30) with SVGO's `prefixIds` plugin.
43+
44+
Configure it via `.babelrc` file:
45+
46+
```json
47+
{
48+
"plugins": [
49+
[
50+
"inline-svg",
51+
{
52+
"svgo": {
53+
"plugins": [
54+
{
55+
"prefixIds": {
56+
"delim": "-",
57+
"prefix": "customPrefix",
58+
"prefixIds": true,
59+
"prefixClassNames": false
60+
}
61+
}
62+
]
63+
}
64+
}
65+
]
66+
]
67+
}
68+
```
4369

4470
So given this simple `cheese.svg` file:
4571

@@ -56,11 +82,10 @@ import wheelOfCheese from 'cheese.svg';
5682
You get the following output:
5783

5884
```js
59-
var wheelOfCheese = '<svg><circle cx="10" cy="10" r="50" id="wheelOfCheese-someCircle"></circle></svg>';
85+
var wheelOfCheese = '<svg><circle cx="10" cy="10" r="50" id="customPrefix-someCircle"></circle></svg>';
6086
```
6187

62-
If you want to disable this feature, just pass an empty plugins list as a [plugin option](./__tests__/emptyOpts.test.js#L11) to SVGO in your babel settings (you could also pass `{ cleanupIDs: true }`, which is just the SVGO default).
63-
88+
If you want to disable this feature, just pass an empty plugins list as a [plugin option](./test/specs/empty-options.spec.js#L11) to SVGO in your babel settings.
6489

6590
## Installation
6691

__tests__/basic.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

__tests__/differentSVG.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

__tests__/emptyOpts.test.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

__tests__/withOpts.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

jest.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
roots: [
3+
'<rootDir>/lib',
4+
'<rootDir>/test'
5+
],
6+
collectCoverage: true,
7+
coverageDirectory: '<rootDir>/test/coverage',
8+
testMatch: [ '<rootDir>/test/specs/*.spec.js' ],
9+
testPathIgnorePatterns: [ '/node_modules/' ],
10+
verbose: true
11+
};

babel-plugin-inline-svg.js renamed to lib/babel-plugin-inline-svg.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,46 @@ const { extname, dirname } = require('path');
22
const { readFileSync } = require('fs');
33
const template = require('babel-template');
44
const resolveFrom = require('resolve-from');
5-
6-
const optimise = require('./optimise');
5+
const optimize = require('./optimize');
76

87
const buildOutput = template(`
98
var SVG_NAME = SVG_CODE;
10-
`
11-
);
9+
`);
1210

1311
let ignoreRegex;
1412

15-
module.exports = ({ types: t }) => ({
13+
module.exports = ({ types }) => ({
1614
visitor: {
1715
ImportDeclaration(path, state) {
1816
const { ignorePattern } = state.opts;
17+
1918
if (ignorePattern) {
2019
// Only set the ignoreRegex once:
2120
ignoreRegex = ignoreRegex || new RegExp(ignorePattern);
21+
2222
// Test if we should ignore this:
2323
if (ignoreRegex.test(path.node.source.value)) {
2424
return;
2525
}
2626
}
27+
2728
// This plugin only applies for SVGs:
2829
if (extname(path.node.source.value) === '.svg') {
2930
// We only support the import default specifier, so let's use that identifier:
3031
const importIdentifier = path.node.specifiers[0].local;
3132
const iconPath = state.file.opts.filename;
3233
const svgPath = resolveFrom(dirname(iconPath), path.node.source.value);
3334
const rawSource = readFileSync(svgPath, 'utf8');
34-
const varName = importIdentifier.name;
3535

36-
const optimisedSource = optimise(varName, rawSource, state.opts.svgo);
36+
const optimizedSource = optimize(rawSource, state.opts.svgo);
3737

3838
const svgReplacement = buildOutput({
3939
SVG_NAME: importIdentifier,
40-
SVG_CODE: t.stringLiteral(optimisedSource),
40+
SVG_CODE: types.stringLiteral(optimizedSource)
4141
});
4242

4343
path.replaceWith(svgReplacement);
4444
}
45-
},
46-
},
45+
}
46+
}
4747
});

0 commit comments

Comments
 (0)