Skip to content

Commit 08653d7

Browse files
committed
Do not auto-minify JavaScript files #50
1 parent ea5cba5 commit 08653d7

14 files changed

+3
-389
lines changed

.github/workflows/build.yml

+1-23
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,10 @@ jobs:
8787
name: build-number
8888
path: work/BUILD_NUMBER
8989

90-
build-uglifyjs:
91-
runs-on: ubuntu-20.04
92-
steps:
93-
- uses: actions/checkout@main
94-
- run: uglifyjs/build-image
95-
- run: uglifyjs/run-nexe
96-
- uses: actions/upload-artifact@main
97-
with:
98-
name: artifact-uglifyjs
99-
path: work/uglifyjs
100-
10190
test:
102-
needs: build-uglifyjs
10391
runs-on: ubuntu-20.04
10492
steps:
10593
- uses: actions/checkout@main
106-
- uses: actions/download-artifact@main
107-
with:
108-
name: artifact-uglifyjs
109-
path: work
110-
- run: chmod +x work/uglifyjs
11194
- uses: actions/setup-go@main
11295
with:
11396
go-version: '~${{env.GO_VERSION}}'
@@ -138,17 +121,12 @@ jobs:
138121
bundle:
139122
needs:
140123
- build-go
141-
- build-uglifyjs
142124
runs-on: ubuntu-20.04
143125
steps:
144126
- uses: actions/download-artifact@main
145127
with:
146128
name: artifact-bootstrap
147-
- uses: actions/download-artifact@main
148-
with:
149-
name: artifact-uglifyjs
150-
- run: chmod +x bootstrap uglifyjs
151-
- run: zip -j imgconv.zip ./bootstrap ./uglifyjs
129+
- run: zip -j imgconv.zip ./bootstrap
152130
- uses: actions/upload-artifact@main
153131
with:
154132
name: artifact-imgconv

imgconv/convert.go

-162
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"image"
99
"io"
1010
"os"
11-
"os/exec"
1211
"path/filepath"
1312
"reflect"
1413
"strings"
@@ -24,7 +23,6 @@ import (
2423
"github.com/aws/smithy-go"
2524
"github.com/chai2010/webp"
2625
"go.uber.org/zap"
27-
"golang.org/x/sync/errgroup"
2826
)
2927

3028
const (
@@ -292,164 +290,6 @@ func (e *Environment) Convert(ctx context.Context, path string, src, dest *Locat
292290
return nil
293291
}
294292

295-
saveOriginalJSToFile := func(srcPath string, srcBody io.Reader) error {
296-
file, err := os.Create(srcPath)
297-
if err != nil {
298-
e.log.Error("failed to create temporary file",
299-
zapBucketField,
300-
zapPathField,
301-
zap.Error(err))
302-
return err
303-
}
304-
defer func() {
305-
if err := file.Close(); err != nil {
306-
e.log.Error("failed to close copied JS file",
307-
zapBucketField,
308-
zapPathField,
309-
zap.Error(err))
310-
}
311-
}()
312-
313-
if _, err := io.Copy(file, srcBody); err != nil {
314-
e.log.Error("failed to save original JS file",
315-
zapBucketField,
316-
zapPathField,
317-
zap.Error(err))
318-
return err
319-
}
320-
return nil
321-
}
322-
323-
uglifyJSCommand := func(
324-
srcBody io.Reader,
325-
minifiedJSPath string,
326-
tempDir string,
327-
) error {
328-
srcPath := tempDir + "/src.js"
329-
if err := saveOriginalJSToFile(srcPath, srcBody); err != nil {
330-
return err
331-
}
332-
333-
var stderrBuf bytes.Buffer
334-
cmd := exec.CommandContext(
335-
ctx,
336-
e.Config.UglifyJSPath,
337-
srcPath,
338-
"--compress",
339-
"--mangle",
340-
"--keep-fnames",
341-
"--source-map",
342-
fmt.Sprintf("url='%s/%s.map',filename='%s',includeSources='%s'",
343-
e.Config.BaseURL,
344-
path,
345-
filepath.Base(minifiedJSPath),
346-
srcPath,
347-
),
348-
"--output",
349-
minifiedJSPath,
350-
)
351-
cmd.Stdout = io.Discard
352-
cmd.Stderr = &stderrBuf
353-
354-
if err := cmd.Run(); err != nil {
355-
e.log.Info("failed to run uglifyjs",
356-
zapBucketField,
357-
zapPathField,
358-
zap.Error(err),
359-
zap.String("stderr", stderrBuf.String()),
360-
)
361-
return err
362-
}
363-
364-
return nil
365-
}
366-
367-
updateMinifiedJSS3Object := func(
368-
ctx context.Context,
369-
srcObj *s3.GetObjectOutput,
370-
minifiedJSPath string,
371-
) error {
372-
jsFile, cleanup, err := openFile(minifiedJSPath)
373-
if err != nil {
374-
return err
375-
}
376-
defer cleanup()
377-
378-
key := dest.Prefix + path
379-
380-
size, err := updateS3Object(ctx, srcObj, jsFile, key, javaScriptContentType)
381-
if err != nil {
382-
return err
383-
}
384-
385-
if size != 0 {
386-
e.log.Info("JavaScript minified",
387-
zapBucketField,
388-
zapPathField,
389-
zap.String("dest-key", key),
390-
zap.Int64("before", srcObj.ContentLength),
391-
zap.Int64("after", size))
392-
}
393-
return nil
394-
}
395-
396-
updateSourceMapS3Object := func(
397-
ctx context.Context,
398-
srcObj *s3.GetObjectOutput,
399-
sourceMapPath string,
400-
) error {
401-
mapFile, cleanup, err := openFile(sourceMapPath)
402-
if err != nil {
403-
return err
404-
}
405-
defer cleanup()
406-
407-
key := dest.Prefix + path + ".map"
408-
409-
size, err := updateS3Object(ctx, srcObj, mapFile, key, sourceMapContentType)
410-
if err != nil {
411-
return err
412-
}
413-
414-
if size != 0 {
415-
e.log.Info("source map generated",
416-
zapBucketField,
417-
zapPathField,
418-
zap.String("dest-key", key),
419-
zap.Int64("size", size))
420-
}
421-
return nil
422-
}
423-
424-
minifyJavaScript := func(ctx context.Context, srcObj *s3.GetObjectOutput) error {
425-
minifiedJSPath := ""
426-
sourceMapPath := ""
427-
428-
if srcObj != nil {
429-
tempDir, cleanup, err := createTempDir()
430-
if err != nil {
431-
return err
432-
}
433-
defer cleanup()
434-
435-
minifiedJSPath = tempDir + "/out.js"
436-
sourceMapPath = tempDir + "/out.js.map"
437-
438-
if err := uglifyJSCommand(srcObj.Body, minifiedJSPath, tempDir); err != nil {
439-
return err
440-
}
441-
}
442-
443-
errGroup, ctx := errgroup.WithContext(ctx)
444-
errGroup.Go(func() error {
445-
return updateMinifiedJSS3Object(ctx, srcObj, minifiedJSPath)
446-
})
447-
errGroup.Go(func() error {
448-
return updateSourceMapS3Object(ctx, srcObj, sourceMapPath)
449-
})
450-
return errGroup.Wait()
451-
}
452-
453293
updateMinifiedCSSS3Object := func(
454294
ctx context.Context,
455295
srcObj *s3.GetObjectOutput,
@@ -546,8 +386,6 @@ func (e *Environment) Convert(ctx context.Context, path string, src, dest *Locat
546386
switch strings.ToLower(filepath.Ext(path)) {
547387
case ".jpg", ".jpeg", ".png", ".gif":
548388
return convertImage(ctx, srcObj)
549-
case ".js":
550-
return minifyJavaScript(ctx, srcObj)
551389
case ".css":
552390
return minifyCSS(ctx, srcObj)
553391
default:

imgconv/convert_test.go

-84
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"io"
66
"net/http"
7-
"strings"
87
"testing"
98
"time"
109

@@ -33,11 +32,6 @@ func (s *ConvertSuite) SetupTest() {
3332
copy(ctx, samplePNG, s.s3Src.Bucket, key, s.TestSuite)
3433
return nil
3534
})
36-
eg.Go(func() error {
37-
key := s.s3Src.Prefix + "dir/script.js"
38-
copy(ctx, sampleJS, s.s3Src.Bucket, key, s.TestSuite)
39-
return nil
40-
})
4135
eg.Go(func() error {
4236
key := s.s3Src.Prefix + "dir/style.css"
4337
copy(ctx, sampleCSS, s.s3Src.Bucket, key, s.TestSuite)
@@ -93,62 +87,6 @@ func (s *ConvertSuite) assertS3ImageObjectExists(path string) {
9387
s.Assert().Equal(s3SrcObjTime, s3DestObjTime)
9488
}
9589

96-
func (s *ConvertSuite) assertS3JSObjectExists(path string) {
97-
info, err := s.env.S3Client.HeadObject(s.ctx, &s3.HeadObjectInput{
98-
Bucket: &s.s3Src.Bucket,
99-
Key: aws.String(s.s3Src.Prefix + path),
100-
})
101-
s.Require().NoError(err)
102-
s3SrcObjTime, err := time.Parse(timestampLayout, info.Metadata[timestampMetadata])
103-
s.Require().NoError(err)
104-
105-
{
106-
res, err := s.env.S3Client.GetObject(s.ctx, &s3.GetObjectInput{
107-
Bucket: &s.s3Dest.Bucket,
108-
Key: aws.String(s.s3Dest.Prefix + path),
109-
})
110-
s.Assert().NoError(err)
111-
defer func() {
112-
s.Require().NoError(res.Body.Close())
113-
}()
114-
115-
var buf strings.Builder
116-
_, err = io.Copy(&buf, res.Body)
117-
s.Assert().NoError(err)
118-
jsStr := buf.String()
119-
120-
s3DestObjTime, err := time.Parse(timestampLayout, res.Metadata[timestampMetadata])
121-
s.Assert().NoError(err)
122-
s.Assert().Equal(s.s3Src.Bucket, res.Metadata[bucketMetadata])
123-
s.Assert().Equal(path, res.Metadata[pathMetadata])
124-
s.Assert().Equal(javaScriptContentType, *res.ContentType)
125-
s.Assert().Equal(s3SrcObjTime, s3DestObjTime)
126-
127-
s.Assert().Greater(info.ContentLength, res.ContentLength, "file size has been decreased")
128-
s.Assert().Greater(res.ContentLength, int64(100))
129-
s.Assert().Contains(jsStr, "\n//# sourceMappingURL=https://example.com/dir/script.js.map")
130-
}
131-
132-
{
133-
res, err := s.env.S3Client.GetObject(s.ctx, &s3.GetObjectInput{
134-
Bucket: &s.s3Dest.Bucket,
135-
Key: aws.String(s.s3Dest.Prefix + path + ".map"),
136-
})
137-
s.Assert().NoError(err)
138-
defer func() {
139-
s.Require().NoError(res.Body.Close())
140-
}()
141-
s3DestObjTime, err := time.Parse(timestampLayout, res.Metadata[timestampMetadata])
142-
s.Assert().NoError(err)
143-
s.Assert().Equal(s.s3Src.Bucket, res.Metadata[bucketMetadata])
144-
s.Assert().Equal(path, res.Metadata[pathMetadata])
145-
s.Assert().Equal(sourceMapContentType, *res.ContentType)
146-
s.Assert().Equal(s3SrcObjTime, s3DestObjTime)
147-
148-
s.Assert().Greater(res.ContentLength, int64(400))
149-
}
150-
}
151-
15290
func (s *ConvertSuite) assertS3CSSObjectExists(path string) {
15391
res, err := s.env.S3Client.GetObject(s.ctx, &s3.GetObjectInput{
15492
Bucket: &s.s3Dest.Bucket,
@@ -222,28 +160,6 @@ func (s *ConvertSuite) TestRemoveConvertedImage() {
222160
s.assertS3ObjectNotExists(path + ".webp")
223161
}
224162

225-
func (s *ConvertSuite) TestMinifyJS() {
226-
s.Assert().NoError(s.env.Convert(s.ctx, "dir/script.js", &s.s3Src, &s.s3Dest))
227-
s.assertS3JSObjectExists("dir/script.js")
228-
}
229-
230-
func (s *ConvertSuite) TestRemoveMinifiedJS() {
231-
path := "dir/script.js"
232-
s.Require().NoError(s.env.Convert(s.ctx, path, &s.s3Src, &s.s3Dest))
233-
234-
{
235-
_, err := s.env.S3Client.DeleteObject(s.ctx, &s3.DeleteObjectInput{
236-
Bucket: &s.s3Src.Bucket,
237-
Key: aws.String(s.s3Src.Prefix + path),
238-
})
239-
s.Require().NoError(err)
240-
}
241-
242-
s.Assert().NoError(s.env.Convert(s.ctx, path, &s.s3Src, &s.s3Dest))
243-
s.assertS3ObjectNotExists(path)
244-
s.assertS3ObjectNotExists(path + ".map")
245-
}
246-
247163
func (s *ConvertSuite) TestMinifyCSS() {
248164
s.Assert().NoError(s.env.Convert(s.ctx, "dir/style.css", &s.s3Src, &s.s3Dest))
249165
s.assertS3CSSObjectExists("dir/style.css")

imgconv/imgconv.go

-19
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"io"
7-
"os"
8-
"path/filepath"
97
"strconv"
108
"sync"
119
"time"
@@ -38,7 +36,6 @@ type Config struct {
3836
RetrieverCount uint8
3937
DeleterCount uint8
4038
OrderStop time.Duration
41-
UglifyJSPath string
4239
Log *zap.Logger
4340
}
4441

@@ -103,25 +100,9 @@ func createAWSConfig(ctx context.Context, cfg *Config) *aws.Config {
103100
return &awsCfg
104101
}
105102

106-
func uglifyJSPath(path string) string {
107-
if path == "" {
108-
ex, err := os.Executable()
109-
if err != nil {
110-
panic(err)
111-
}
112-
return filepath.Dir(ex) + "/uglifyjs"
113-
}
114-
p, err := filepath.Abs(path)
115-
if err != nil {
116-
panic(err)
117-
}
118-
return p
119-
}
120-
121103
// NewEnvironment initializes values needed for execution.
122104
func NewEnvironment(ctx context.Context, cfg *Config) *Environment {
123105
awsConfig := createAWSConfig(ctx, cfg)
124-
cfg.UglifyJSPath = uglifyJSPath(cfg.UglifyJSPath)
125106
minifier := minify.New()
126107
minifyCSS := func(w io.Writer, r io.Reader, params map[string]string) error {
127108
return (&css.Minifier{}).Minify(minifier, w, r, params)

0 commit comments

Comments
 (0)