/
SvgReactTransformer.js
62 lines (55 loc) 路 1.54 KB
/
SvgReactTransformer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// @flow
import {Transformer} from '@parcel/plugin';
import path from 'path';
import svgoPlugin from '@svgr/plugin-svgo';
import jsxPlugin from '@svgr/plugin-jsx';
import {transform} from '@svgr/core';
export default (new Transformer({
async loadConfig({config}) {
let svgrResult = await config.getConfig([
'.svgrrc',
'.svgrrc.json',
'.svgrrc.js',
'.svgrrc.cjs',
'svgr.config.json',
'svgr.config.js',
'svgr.config.cjs',
]);
let svgoResult = await config.getConfig([
'svgo.config.js',
'svgo.config.cjs',
'svgo.config.json',
]);
if (svgrResult) {
let isJavascript = path.extname(svgrResult.filePath).endsWith('js');
if (isJavascript) {
config.invalidateOnStartup();
}
}
if (svgoResult) {
let isJavascript = path.extname(svgoResult.filePath).endsWith('js');
if (isJavascript) {
config.invalidateOnStartup();
}
}
return {svgr: svgrResult?.contents, svgo: svgoResult?.contents};
},
async transform({asset, config}) {
let code = await asset.getCode();
const jsx = await transform(
code,
{svgoConfig: config.svgo, ...config.svgr, runtimeConfig: false},
{
caller: {
name: '@parcel/transformer-svg-react',
defaultPlugins: [svgoPlugin, jsxPlugin],
},
filePath: asset.filePath,
},
);
asset.type = config.svgr?.typescript ? 'tsx' : 'jsx';
asset.bundleBehavior = null;
asset.setCode(jsx);
return [asset];
},
}): Transformer);