forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspiler.js
More file actions
45 lines (42 loc) · 1.29 KB
/
transpiler.js
File metadata and controls
45 lines (42 loc) · 1.29 KB
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
import transform from '@svgr/core/lib/plugins/transform';
import { expandState } from '@svgr/core/lib/util';
import removeStylePlugin from '@svgr/core/lib/h2x/removeStyle';
import stripAttribute from '@svgr/core/lib/h2x/stripAttribute';
import removeComments from '@svgr/core/lib/h2x/removeComments';
import expandProps from '@svgr/core/lib/h2x/expandProps';
import { transform as h2xTransform } from 'h2x-core';
import h2xPluginJsx from 'h2x-plugin-jsx';
const DEFAULT_CONFIG = {
h2xConfig: null,
dimensions: true,
expandProps: true,
icon: false,
native: false,
prettier: true,
prettierConfig: null,
ref: false,
replaceAttrValues: null,
svgAttributes: null,
svgProps: null,
svgo: true,
svgoConfig: null,
template: null,
titleProp: false,
};
export async function svgrTransform(code: string, usedState) {
const config = { ...DEFAULT_CONFIG, ...{} };
const state = expandState(usedState);
let result = code;
const plugins = [
h2xPluginJsx,
stripAttribute('xmlns'),
removeComments(),
removeStylePlugin(),
expandProps('end'),
];
// Remove null-byte character (copy/paste from Illustrator)
result = String(result).replace('\0', '');
result = h2xTransform(code, { plugins, state });
result = await transform(result, config, state);
return result;
}