-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.rsdk.go
231 lines (199 loc) · 6.37 KB
/
esbuild.rsdk.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"log"
"os"
"path/filepath"
"github.com/davecgh/go-spew/spew"
"github.com/evanw/esbuild/pkg/api"
)
type ioPaths struct {
outPath string
configTransformerPath string
}
func commonAliasResolver() api.Plugin {
aliasResolvers := api.Plugin{
Name: "importAliases",
Setup: func(pb api.PluginBuild) {
pb.OnResolve(api.OnResolveOptions{Filter: "react-native$"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
path, err := filepath.Abs("./node_modules/react-native-web/dist/cjs/index.js")
if err != nil {
log.Fatalln(err)
}
return api.OnResolveResult{Path: path}, nil
},
)
// Using rtm bridge to translate React Native RTM SDK calls to web SDK calls
pb.OnResolve(api.OnResolveOptions{Filter: "agora-react-native-rtm$"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
path, err := filepath.Abs("./bridge/rtm/web/index.ts")
if err != nil {
log.Fatalln(err)
}
return api.OnResolveResult{Path: path}, nil
},
)
// Using rtc bridge to translate React Native RTC SDK calls to web SDK calls for web and linux
// Using rtc bridge to translate React Native RTC SDK calls to electron SDK calls for windows and mac
pb.OnResolve(api.OnResolveOptions{Filter: "react-native-agora$"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
path, err := filepath.Abs("./bridge/rtc/webNg/index.ts")
if err != nil {
log.Fatalln(err)
}
return api.OnResolveResult{Path: path}, nil
},
)
pb.OnResolve(api.OnResolveOptions{Filter: "customization-api"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
path, err := filepath.Abs("./customization-api/index.ts")
if err != nil {
log.Fatalln(err)
}
return api.OnResolveResult{Path: path}, nil
},
)
pb.OnResolve(api.OnResolveOptions{Filter: "customization-implementation"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
path, err := filepath.Abs("./customization-implementation/index.ts")
if err != nil {
log.Fatalln(err)
}
return api.OnResolveResult{Path: path}, nil
},
)
pb.OnResolve(api.OnResolveOptions{Filter: "customization"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
fpePath, err := filepath.Abs("./customization/index.ts")
if err != nil {
log.Fatalln(err)
}
fpeDummyPath, err := filepath.Abs("./customization-implementation/dummyConfig.ts")
if err != nil {
log.Fatalln(err)
}
_, err = os.Stat(fpePath)
if err != nil {
return api.OnResolveResult{Path: fpeDummyPath}, nil
}
return api.OnResolveResult{Path: fpePath}, nil
},
)
pb.OnResolve(api.OnResolveOptions{Filter: "agora-react-native-rtm/lib/typescript/src"},
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
path, err := filepath.Abs("./bridge/rtm/web/index.ts")
if err != nil {
log.Fatalln(err)
}
return api.OnResolveResult{Path: path}, nil
},
)
},
}
return aliasResolvers
}
func commonResolveExtensions() []string {
// Adds platform specific extensions and OS specific extensions
// .web.tsx works for web specific code
resolveExtensions := []string{
".rsdk.tsx",
".rsdk.ts",
".sdk.ts",
".sdk.tsx",
".web.ts",
".web.tsx",
".tsx",
".ts",
".jsx",
".js",
".node",
}
return resolveExtensions
}
func commonLoader() map[string]api.Loader {
loader := map[string]api.Loader{
// since esbuild targets latest js by default we dont need plugins that
// convert optional chaining and class properties (node 16.11.0+ has both features)
// use sx loaders for js|ts files as well
".js": api.LoaderTSX,
".jsx": api.LoaderTSX,
".ts": api.LoaderTSX,
".tsx": api.LoaderTSX,
// todo: might need to deal with non-lowercase extensions for these media files
".png": api.LoaderDataURL,
".jpg": api.LoaderDataURL,
".jpeg": api.LoaderDataURL,
".gif": api.LoaderDataURL,
}
return loader
}
func common(iopath *ioPaths) api.BuildOptions {
commonBuildOpts := api.BuildOptions{
// we can safely ignore (webpack) plugins for now because they seem to be used only for not reactsdk
Plugins: []api.Plugin{commonAliasResolver()},
ResolveExtensions: commonResolveExtensions(),
Loader: commonLoader(),
// embed config variables values into the code
Define: map[string]string{
"$config": "config",
},
Inject: []string{iopath.configTransformerPath},
}
return commonBuildOpts
}
func rsdk(iopath *ioPaths) api.BuildResult {
commonBuildOpts := common(iopath)
rsdkBuildOpts := api.BuildOptions{
// build options common to rsdk and other components
Plugins: commonBuildOpts.Plugins,
ResolveExtensions: commonBuildOpts.ResolveExtensions,
Loader: commonBuildOpts.Loader,
// rsdk specific build options
EntryPoints: []string{"./index.rsdk.tsx"},
External: []string{
"react",
"react-dom",
"react-router",
"react-router-dom",
"@apollo/client",
},
Define: commonBuildOpts.Define,
Inject: commonBuildOpts.Inject,
// target es6 because other host applications typically do not transpile node_modules
// i.e. they will not transpile agora-app-builder-sdk
Target: api.Target(api.ES2015),
// bundle in cjs format because this index.js is meant to be used by other host applications
// like webpack which runs on node
Format: api.FormatCommonJS,
Outfile: iopath.outPath,
// other esbuild options
Write: true,
Bundle: true,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
Sourcemap: api.SourceMapExternal,
// debug options. does not affect bundle size
// Metafile: true,
}
rsdk := api.Build(rsdkBuildOpts)
return rsdk
}
func main() {
outPath := flag.String("outpath", "../Builds/react-sdk/index.js", "path to write bundled js file")
configTransformerPath := flag.String("configtransformerpath", "./esbuildConfigTransform.js", "path to inject file")
flag.Parse()
iopath := &ioPaths{
outPath: *outPath,
configTransformerPath: *configTransformerPath,
}
log.Println("esbuild args = ", iopath)
rsdkRes := rsdk(iopath)
if len(rsdkRes.Errors) > 0 {
spew.Dump(rsdkRes)
log.Fatalln("build failed")
}
// text := api.AnalyzeMetafile(rsdkRes.Metafile, api.AnalyzeMetafileOptions{Verbose: false})
// log.Printf("%s", text)
}