Skip to content

Commit 583f1dd

Browse files
committed
Pass svgo options from command line and loader query to svgo in loader
1 parent df83292 commit 583f1dd

File tree

5 files changed

+105
-10
lines changed

5 files changed

+105
-10
lines changed

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"babel-loader": "^6.2.2",
1313
"react": "^0.14.7",
1414
"react-dom": "^0.14.7",
15-
"webpack": "^1.10.1"
15+
"webpack": "2.1.0-beta.4"
1616
}
1717
}

example/webpack.config.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ module.exports = {
1010
loaders: [
1111
{
1212
test: /\.svg$/,
13-
loader: 'babel!' + svgLoader + '?es5=true'
13+
loaders: [
14+
'babel',
15+
{
16+
loader: svgLoader, // 'react-svg'
17+
query: {
18+
es5: false,
19+
svgo: {
20+
pretty: true
21+
}
22+
}
23+
}
24+
]
1425
},
1526
{
1627
test: /\.js$/,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"babel-plugin-syntax-jsx": "~6.5.0",
3939
"babel-preset-es2015-loose": "~7.0.0",
4040
"babel-preset-react": "~6.5.0",
41-
"loader-utils": "^0.2.13",
41+
"js-yaml": "~3.5.5",
42+
"loader-utils": "~0.2.13",
4243
"svgo": "~0.6.3",
4344
"yargs": "~3.18.0"
4445
},

src/cli.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,99 @@
33
import loader from './loader';
44
import fs from 'fs';
55
import yargs from 'yargs';
6+
import yaml from 'js-yaml';
7+
import path from 'path';
68

7-
let {argv} = yargs.boolean('es5').boolean('0');
9+
let {argv} = yargs
10+
.usage('Usage: $0 [files] [options]')
11+
.option('5', {
12+
alias: 'es5',
13+
describe: 'Use babel presets es2015 and react',
14+
boolean: true,
15+
default: false
16+
})
17+
.option('0', {
18+
alias: 'stdout',
19+
describe: 'Print output to stdout',
20+
boolean: true,
21+
default: 'false'
22+
})
23+
// svgo options
24+
.option('svgo', {
25+
describe: 'Path to YAML or JS or JSON config file for SVGO',
26+
nargs: 1
27+
})
28+
.array('svgo.plugins')
29+
.option('svgo.floatPrecision', {number: true, default: 3})
30+
.boolean('svgo.multipass')
31+
.boolean('svgo.full')
32+
.boolean('svgo.js2svg.pretty')
33+
.option('svgo.js2svg.useShortTags', {default: true, boolean: true})
34+
.demand(1)
35+
.version(require('../package.json').version)
36+
.help('h')
37+
.alias('h', 'help');
838

939
function makeFilename(filename) {
1040
return filename + '.react.js';
1141
}
1242

43+
function handlePath(configFile) {
44+
switch (path.extname(configFile)) {
45+
case '.yaml':
46+
return yaml.safeLoad(fs.readFileSync(configFile));
47+
case '.json':
48+
case '.js':
49+
return require(path.join(process.cwd(), configFile));
50+
default:
51+
throw new Error('Unsupported config file format.');
52+
}
53+
}
54+
55+
let svgoOpts;
56+
57+
switch (typeof argv.svgo) {
58+
case 'string':
59+
svgoOpts = handlePath(argv.svgo);
60+
break;
61+
case 'object':
62+
svgoOpts = argv.svgo;
63+
if (Array.isArray(svgoOpts.plugins)) break;
64+
65+
// when there is only one element
66+
if (typeof svgoOpts.plugins === 'string') {
67+
svgoOpts.plugins = [svgoOpts.plugins];
68+
break;
69+
}
70+
71+
// when there are many and is an object
72+
if (svgoOpts.plugins) {
73+
svgoOpts.plugins = Object
74+
.keys(svgoOpts.plugins)
75+
.map(key => {
76+
if (svgoOpts.plugins[key] === true) return key;
77+
return {[key]: svgoOpts.plugins[key]};
78+
});
79+
}
80+
break;
81+
}
82+
1383
argv._.map(file => {
1484
let source = fs.readFileSync(file);
85+
86+
let query;
87+
try {
88+
// serializable check
89+
query = '?' + JSON.stringify({
90+
es5: argv.es5,
91+
svgo: svgoOpts
92+
});
93+
} catch(e) {
94+
console.error('The options passed are not serializable.');
95+
process.exit(1);
96+
}
1597
let loaderContext = {
16-
query: `?es5=${argv.es5}`,
98+
query,
1799
cacheable() {},
18100
addDependency() {},
19101
async() {

src/loader.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import loaderUtils from 'loader-utils';
44

55
import plugin from './plugin';
66

7-
const svgo = new Svgo();
8-
9-
function optimize (content) {
10-
return new Promise(r => svgo.optimize(content, ({data}) => r(data)));
7+
function optimize (opts) {
8+
const svgo = new Svgo(opts);
9+
return function (content) {
10+
return new Promise(r => svgo.optimize(content, ({data}) => r(data)));
11+
};
1112
}
1213

1314
function transform (opts) {
@@ -38,7 +39,7 @@ export default function (content) {
3839
let cb = this.async();
3940

4041
Promise.resolve(String(content))
41-
.then(optimize)
42+
.then(optimize(query.svgo))
4243
.then(transform({
4344
es5: query.es5
4445
}))

0 commit comments

Comments
 (0)