Skip to content

Commit e8767bf

Browse files
committed
Move polyfills to a part of the esbuild step. In the future, additional polyfills should be able to be specified by the user
1 parent ff1aea4 commit e8767bf

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

internal/reactbuilder/build.go

+22-23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ import (
1010
"github.com/natewong1313/go-react-ssr/internal/utils"
1111
)
1212

13+
var loaders = map[string]esbuildApi.Loader{
14+
".png": esbuildApi.LoaderFile,
15+
".svg": esbuildApi.LoaderFile,
16+
".jpg": esbuildApi.LoaderFile,
17+
".jpeg": esbuildApi.LoaderFile,
18+
".gif": esbuildApi.LoaderFile,
19+
".bmp": esbuildApi.LoaderFile,
20+
".woff2": esbuildApi.LoaderFile,
21+
".woff": esbuildApi.LoaderFile,
22+
".ttf": esbuildApi.LoaderFile,
23+
".eot": esbuildApi.LoaderFile,
24+
}
25+
26+
var textEncoderPolyfill = `function TextEncoder(){}TextEncoder.prototype.encode=function(string){var octets=[];var length=string.length;var i=0;while(i<length){var codePoint=string.codePointAt(i);var c=0;var bits=0;if(codePoint<=0x0000007F){c=0;bits=0x00}else if(codePoint<=0x000007FF){c=6;bits=0xC0}else if(codePoint<=0x0000FFFF){c=12;bits=0xE0}else if(codePoint<=0x001FFFFF){c=18;bits=0xF0}octets.push(bits|(codePoint>>c));c-=6;while(c>=0){octets.push(0x80|((codePoint>>c)&0x3F));c-=6}i+=codePoint>=0x10000?2:1}return octets};function TextDecoder(){}TextDecoder.prototype.decode=function(octets){var string="";var i=0;while(i<octets.length){var octet=octets[i];var bytesNeeded=0;var codePoint=0;if(octet<=0x7F){bytesNeeded=0;codePoint=octet&0xFF}else if(octet<=0xDF){bytesNeeded=1;codePoint=octet&0x1F}else if(octet<=0xEF){bytesNeeded=2;codePoint=octet&0x0F}else if(octet<=0xF4){bytesNeeded=3;codePoint=octet&0x07}if(octets.length-i-bytesNeeded>0){var k=0;while(k<bytesNeeded){octet=octets[i+k+1];codePoint=(codePoint<<6)|(octet&0x3F);k+=1}}else{codePoint=0xFFFD;bytesNeeded=octets.length-i}string+=String.fromCodePoint(codePoint);i+=bytesNeeded+1}return string};`
27+
var processPolyfill = `var process = {env: {NODE_ENV: "production"}};`
28+
var consolePolyfill = `var console = {log: function(){}};`
29+
1330
type BuildResult struct {
1431
JS string
1532
CSS string
@@ -32,17 +49,10 @@ func BuildServer(buildContents, frontendDir, assetRoute string) (BuildResult, er
3249
MinifyWhitespace: true,
3350
MinifyIdentifiers: true,
3451
MinifySyntax: true,
35-
Loader: map[string]esbuildApi.Loader{ // for loading images properly
36-
".png": esbuildApi.LoaderFile,
37-
".svg": esbuildApi.LoaderFile,
38-
".jpg": esbuildApi.LoaderFile,
39-
".jpeg": esbuildApi.LoaderFile,
40-
".gif": esbuildApi.LoaderFile,
41-
".bmp": esbuildApi.LoaderFile,
42-
".woff2": esbuildApi.LoaderFile,
43-
".woff": esbuildApi.LoaderFile,
44-
".ttf": esbuildApi.LoaderFile,
45-
".eot": esbuildApi.LoaderFile,
52+
Loader: loaders,
53+
// We can inject the polyfills at the top of the generated js
54+
Banner: map[string]string{
55+
"js": textEncoderPolyfill + processPolyfill + consolePolyfill,
4656
},
4757
}
4858
return build(opts, false)
@@ -63,18 +73,7 @@ func BuildClient(buildContents, frontendDir, assetRoute string) (BuildResult, er
6373
MinifyWhitespace: os.Getenv("APP_ENV") == "production",
6474
MinifyIdentifiers: os.Getenv("APP_ENV") == "production",
6575
MinifySyntax: os.Getenv("APP_ENV") == "production",
66-
Loader: map[string]esbuildApi.Loader{ // for loading images properly
67-
".png": esbuildApi.LoaderFile,
68-
".svg": esbuildApi.LoaderFile,
69-
".jpg": esbuildApi.LoaderFile,
70-
".jpeg": esbuildApi.LoaderFile,
71-
".gif": esbuildApi.LoaderFile,
72-
".bmp": esbuildApi.LoaderFile,
73-
".woff2": esbuildApi.LoaderFile,
74-
".woff": esbuildApi.LoaderFile,
75-
".ttf": esbuildApi.LoaderFile,
76-
".eot": esbuildApi.LoaderFile,
77-
},
76+
Loader: loaders,
7877
}
7978
return build(opts, true)
8079
}

rendertask.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,13 @@ func injectProps(compiledJS, props string) string {
144144
return fmt.Sprintf(`var props = %s; %s`, props, compiledJS)
145145
}
146146

147-
var textEncoderPolyfill = `function TextEncoder(){}TextEncoder.prototype.encode=function(string){var octets=[];var length=string.length;var i=0;while(i<length){var codePoint=string.codePointAt(i);var c=0;var bits=0;if(codePoint<=0x0000007F){c=0;bits=0x00}else if(codePoint<=0x000007FF){c=6;bits=0xC0}else if(codePoint<=0x0000FFFF){c=12;bits=0xE0}else if(codePoint<=0x001FFFFF){c=18;bits=0xF0}octets.push(bits|(codePoint>>c));c-=6;while(c>=0){octets.push(0x80|((codePoint>>c)&0x3F));c-=6}i+=codePoint>=0x10000?2:1}return octets};function TextDecoder(){}TextDecoder.prototype.decode=function(octets){var string="";var i=0;while(i<octets.length){var octet=octets[i];var bytesNeeded=0;var codePoint=0;if(octet<=0x7F){bytesNeeded=0;codePoint=octet&0xFF}else if(octet<=0xDF){bytesNeeded=1;codePoint=octet&0x1F}else if(octet<=0xEF){bytesNeeded=2;codePoint=octet&0x0F}else if(octet<=0xF4){bytesNeeded=3;codePoint=octet&0x07}if(octets.length-i-bytesNeeded>0){var k=0;while(k<bytesNeeded){octet=octets[i+k+1];codePoint=(codePoint<<6)|(octet&0x3F);k+=1}}else{codePoint=0xFFFD;bytesNeeded=octets.length-i}string+=String.fromCodePoint(codePoint);i+=bytesNeeded+1}return string};`
148-
var processPolyfill = `var process = {env: {NODE_ENV: "production"}};`
149-
var consolePolyfill = `var console = {log: function(){}};`
150-
151147
// renderReactToHTML uses node to execute the server js file which outputs the rendered HTML
152148
func renderReactToHTMLNew(js string) (string, error) {
153149
rt := quickjs.NewRuntime()
154150
defer rt.Close()
155151
ctx := rt.NewContext()
156152
defer ctx.Close()
157-
res, err := ctx.Eval(textEncoderPolyfill + processPolyfill + consolePolyfill + js)
153+
res, err := ctx.Eval(js)
158154
if err != nil {
159155
return "", err
160156
}

0 commit comments

Comments
 (0)