-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyledjsx.go
77 lines (68 loc) · 1.63 KB
/
styledjsx.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package styledjsx
import (
"fmt"
"strconv"
esbuild "github.com/evanw/esbuild/pkg/api"
"github.com/matthewmueller/jsx"
"github.com/matthewmueller/jsx/ast"
"github.com/matthewmueller/styledjsx/internal/style"
)
func New() *Rewriter {
return &Rewriter{
Import: Import{
Name: "Style",
Path: "styled-jsx",
IsDefault: true,
},
Minify: false,
}
}
func Rewrite(path, code string) (string, error) {
return New().Rewrite(path, code)
}
type Import struct {
Name string
Path string
IsDefault bool
}
func (i *Import) String() string {
if i.IsDefault {
return "import " + i.Name + " from " + strconv.Quote(i.Path) + ";\n"
}
return "import {" + i.Name + "} from " + strconv.Quote(i.Path) + ";\n"
}
type Rewriter struct {
Import Import
// Used by ESBuild
Minify bool
Engines []esbuild.Engine
}
func (r *Rewriter) Rewrite(path, code string) (string, error) {
ast, err := jsx.Parse(path, code)
if err != nil {
return "", fmt.Errorf("styledjsx: unable to parse %q: %w", path, err)
}
if err := r.RewriteAST(path, ast); err != nil {
return "", fmt.Errorf("styledjsx: unable to rewrite %q: %w", path, err)
}
return ast.String(), nil
}
func (r *Rewriter) RewriteAST(path string, script *ast.Script) error {
visitor := &style.Visitor{
Path: path,
Prefix: "jsx-",
Attr: "class",
ImportName: r.Import.Name,
Minify: r.Minify,
Engines: r.Engines,
}
if err := visitor.Rewrite(script); err != nil {
return err
}
// Add the import statement to the top of the file
script.Body = append([]ast.Fragment{
&ast.Text{Value: r.Import.String()}},
script.Body...,
)
return nil
}