Skip to content

Commit a4b27f5

Browse files
committed
esbuildplugin: move to v0.8 plugin api
1 parent 5f23a05 commit a4b27f5

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

esbuildplugin/plugin.go

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,41 @@ func WithResolver(resolver cssc.Resolver) Option {
6868
}
6969

7070
// Plugin is an esbuild plugin for importing .css files.
71-
func Plugin(opts ...Option) func(api.Plugin) {
72-
return func(plugin api.Plugin) {
73-
plugin.SetName("cssc")
74-
plugin.AddLoader(
75-
api.LoaderOptions{Filter: `\.css$`},
76-
func(args api.LoaderArgs) (res api.LoaderResult, err error) {
77-
res.Loader = api.LoaderCSS
78-
79-
var errors csscReporter
80-
options := cssc.Options{
81-
Entry: []string{args.Path},
82-
Reporter: &errors,
83-
}
84-
for _, opt := range opts {
85-
options = opt(options)
86-
}
87-
88-
result := cssc.Compile(options)
89-
90-
if len(errors) > 0 {
91-
res.Errors = errors.toEsbuild()
92-
return
93-
}
94-
95-
f, ok := result.Files[args.Path]
96-
if !ok {
97-
err = oops.Errorf("cssc output did not contain %s", args.Path)
98-
return
99-
}
100-
101-
res.Contents = &f
102-
return res, nil
103-
},
104-
)
71+
func Plugin(opts ...Option) api.Plugin {
72+
return api.Plugin{
73+
Name: "cssc",
74+
Setup: func(build api.PluginBuild) {
75+
build.OnLoad(
76+
api.OnLoadOptions{Filter: `\.css$`},
77+
func(args api.OnLoadArgs) (res api.OnLoadResult, err error) {
78+
res.Loader = api.LoaderCSS
79+
80+
var errors csscReporter
81+
options := cssc.Options{
82+
Entry: []string{args.Path},
83+
Reporter: &errors,
84+
}
85+
for _, opt := range opts {
86+
options = opt(options)
87+
}
88+
89+
result := cssc.Compile(options)
90+
91+
if len(errors) > 0 {
92+
res.Errors = errors.toEsbuild()
93+
return
94+
}
95+
96+
f, ok := result.Files[args.Path]
97+
if !ok {
98+
err = oops.Errorf("cssc output did not contain %s", args.Path)
99+
return
100+
}
101+
102+
res.Contents = &f
103+
return res, nil
104+
},
105+
)
106+
},
105107
}
106108
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.14
44

55
require (
66
github.com/davecgh/go-spew v1.1.1
7-
github.com/evanw/esbuild v0.7.20-0.20201021193000-b8b5faabbd7d
7+
github.com/evanw/esbuild v0.8.6
88
github.com/kr/pretty v0.1.0 // indirect
99
github.com/samsarahq/go v0.0.0-20191220233105-8077c9fbaed5
1010
github.com/stretchr/testify v1.6.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/evanw/esbuild v0.7.20-0.20201021193000-b8b5faabbd7d h1:ql35c+LejbboC0mXuvEDy/V2MHrcN35QRd3FtwiBkyk=
5-
github.com/evanw/esbuild v0.7.20-0.20201021193000-b8b5faabbd7d/go.mod h1:mptxmSXIzBIKKCe4jo9A5SToEd1G+AKZ9JmY85dYRJ0=
4+
github.com/evanw/esbuild v0.8.6 h1:AgNzZzldj5GeIefqzzR3aPt/FHD10VP/KWRsTYMVxsM=
5+
github.com/evanw/esbuild v0.8.6/go.mod h1:mptxmSXIzBIKKCe4jo9A5SToEd1G+AKZ9JmY85dYRJ0=
66
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
77
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
88
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

0 commit comments

Comments
 (0)