Skip to content

Commit

Permalink
Do not auto-minify JavaScript files #50
Browse files Browse the repository at this point in the history
  • Loading branch information
harai committed Oct 4, 2024
1 parent ea5cba5 commit 08653d7
Show file tree
Hide file tree
Showing 14 changed files with 3 additions and 389 deletions.
24 changes: 1 addition & 23 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,10 @@ jobs:
name: build-number
path: work/BUILD_NUMBER

build-uglifyjs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@main
- run: uglifyjs/build-image
- run: uglifyjs/run-nexe
- uses: actions/upload-artifact@main
with:
name: artifact-uglifyjs
path: work/uglifyjs

test:
needs: build-uglifyjs
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@main
- uses: actions/download-artifact@main
with:
name: artifact-uglifyjs
path: work
- run: chmod +x work/uglifyjs
- uses: actions/setup-go@main
with:
go-version: '~${{env.GO_VERSION}}'
Expand Down Expand Up @@ -138,17 +121,12 @@ jobs:
bundle:
needs:
- build-go
- build-uglifyjs
runs-on: ubuntu-20.04
steps:
- uses: actions/download-artifact@main
with:
name: artifact-bootstrap
- uses: actions/download-artifact@main
with:
name: artifact-uglifyjs
- run: chmod +x bootstrap uglifyjs
- run: zip -j imgconv.zip ./bootstrap ./uglifyjs
- run: zip -j imgconv.zip ./bootstrap
- uses: actions/upload-artifact@main
with:
name: artifact-imgconv
Expand Down
162 changes: 0 additions & 162 deletions imgconv/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"image"
"io"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
Expand All @@ -24,7 +23,6 @@ import (
"github.com/aws/smithy-go"
"github.com/chai2010/webp"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)

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

saveOriginalJSToFile := func(srcPath string, srcBody io.Reader) error {
file, err := os.Create(srcPath)
if err != nil {
e.log.Error("failed to create temporary file",
zapBucketField,
zapPathField,
zap.Error(err))
return err
}
defer func() {
if err := file.Close(); err != nil {
e.log.Error("failed to close copied JS file",
zapBucketField,
zapPathField,
zap.Error(err))
}
}()

if _, err := io.Copy(file, srcBody); err != nil {
e.log.Error("failed to save original JS file",
zapBucketField,
zapPathField,
zap.Error(err))
return err
}
return nil
}

uglifyJSCommand := func(
srcBody io.Reader,
minifiedJSPath string,
tempDir string,
) error {
srcPath := tempDir + "/src.js"
if err := saveOriginalJSToFile(srcPath, srcBody); err != nil {
return err
}

var stderrBuf bytes.Buffer
cmd := exec.CommandContext(
ctx,
e.Config.UglifyJSPath,
srcPath,
"--compress",
"--mangle",
"--keep-fnames",
"--source-map",
fmt.Sprintf("url='%s/%s.map',filename='%s',includeSources='%s'",
e.Config.BaseURL,
path,
filepath.Base(minifiedJSPath),
srcPath,
),
"--output",
minifiedJSPath,
)
cmd.Stdout = io.Discard
cmd.Stderr = &stderrBuf

if err := cmd.Run(); err != nil {
e.log.Info("failed to run uglifyjs",
zapBucketField,
zapPathField,
zap.Error(err),
zap.String("stderr", stderrBuf.String()),
)
return err
}

return nil
}

updateMinifiedJSS3Object := func(
ctx context.Context,
srcObj *s3.GetObjectOutput,
minifiedJSPath string,
) error {
jsFile, cleanup, err := openFile(minifiedJSPath)
if err != nil {
return err
}
defer cleanup()

key := dest.Prefix + path

size, err := updateS3Object(ctx, srcObj, jsFile, key, javaScriptContentType)
if err != nil {
return err
}

if size != 0 {
e.log.Info("JavaScript minified",
zapBucketField,
zapPathField,
zap.String("dest-key", key),
zap.Int64("before", srcObj.ContentLength),
zap.Int64("after", size))
}
return nil
}

updateSourceMapS3Object := func(
ctx context.Context,
srcObj *s3.GetObjectOutput,
sourceMapPath string,
) error {
mapFile, cleanup, err := openFile(sourceMapPath)
if err != nil {
return err
}
defer cleanup()

key := dest.Prefix + path + ".map"

size, err := updateS3Object(ctx, srcObj, mapFile, key, sourceMapContentType)
if err != nil {
return err
}

if size != 0 {
e.log.Info("source map generated",
zapBucketField,
zapPathField,
zap.String("dest-key", key),
zap.Int64("size", size))
}
return nil
}

minifyJavaScript := func(ctx context.Context, srcObj *s3.GetObjectOutput) error {
minifiedJSPath := ""
sourceMapPath := ""

if srcObj != nil {
tempDir, cleanup, err := createTempDir()
if err != nil {
return err
}
defer cleanup()

minifiedJSPath = tempDir + "/out.js"
sourceMapPath = tempDir + "/out.js.map"

if err := uglifyJSCommand(srcObj.Body, minifiedJSPath, tempDir); err != nil {
return err
}
}

errGroup, ctx := errgroup.WithContext(ctx)
errGroup.Go(func() error {
return updateMinifiedJSS3Object(ctx, srcObj, minifiedJSPath)
})
errGroup.Go(func() error {
return updateSourceMapS3Object(ctx, srcObj, sourceMapPath)
})
return errGroup.Wait()
}

updateMinifiedCSSS3Object := func(
ctx context.Context,
srcObj *s3.GetObjectOutput,
Expand Down Expand Up @@ -546,8 +386,6 @@ func (e *Environment) Convert(ctx context.Context, path string, src, dest *Locat
switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".gif":
return convertImage(ctx, srcObj)
case ".js":
return minifyJavaScript(ctx, srcObj)
case ".css":
return minifyCSS(ctx, srcObj)
default:
Expand Down
84 changes: 0 additions & 84 deletions imgconv/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"io"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -33,11 +32,6 @@ func (s *ConvertSuite) SetupTest() {
copy(ctx, samplePNG, s.s3Src.Bucket, key, s.TestSuite)
return nil
})
eg.Go(func() error {
key := s.s3Src.Prefix + "dir/script.js"
copy(ctx, sampleJS, s.s3Src.Bucket, key, s.TestSuite)
return nil
})
eg.Go(func() error {
key := s.s3Src.Prefix + "dir/style.css"
copy(ctx, sampleCSS, s.s3Src.Bucket, key, s.TestSuite)
Expand Down Expand Up @@ -93,62 +87,6 @@ func (s *ConvertSuite) assertS3ImageObjectExists(path string) {
s.Assert().Equal(s3SrcObjTime, s3DestObjTime)
}

func (s *ConvertSuite) assertS3JSObjectExists(path string) {
info, err := s.env.S3Client.HeadObject(s.ctx, &s3.HeadObjectInput{
Bucket: &s.s3Src.Bucket,
Key: aws.String(s.s3Src.Prefix + path),
})
s.Require().NoError(err)
s3SrcObjTime, err := time.Parse(timestampLayout, info.Metadata[timestampMetadata])
s.Require().NoError(err)

{
res, err := s.env.S3Client.GetObject(s.ctx, &s3.GetObjectInput{
Bucket: &s.s3Dest.Bucket,
Key: aws.String(s.s3Dest.Prefix + path),
})
s.Assert().NoError(err)
defer func() {
s.Require().NoError(res.Body.Close())
}()

var buf strings.Builder
_, err = io.Copy(&buf, res.Body)
s.Assert().NoError(err)
jsStr := buf.String()

s3DestObjTime, err := time.Parse(timestampLayout, res.Metadata[timestampMetadata])
s.Assert().NoError(err)
s.Assert().Equal(s.s3Src.Bucket, res.Metadata[bucketMetadata])
s.Assert().Equal(path, res.Metadata[pathMetadata])
s.Assert().Equal(javaScriptContentType, *res.ContentType)
s.Assert().Equal(s3SrcObjTime, s3DestObjTime)

s.Assert().Greater(info.ContentLength, res.ContentLength, "file size has been decreased")
s.Assert().Greater(res.ContentLength, int64(100))
s.Assert().Contains(jsStr, "\n//# sourceMappingURL=https://example.com/dir/script.js.map")
}

{
res, err := s.env.S3Client.GetObject(s.ctx, &s3.GetObjectInput{
Bucket: &s.s3Dest.Bucket,
Key: aws.String(s.s3Dest.Prefix + path + ".map"),
})
s.Assert().NoError(err)
defer func() {
s.Require().NoError(res.Body.Close())
}()
s3DestObjTime, err := time.Parse(timestampLayout, res.Metadata[timestampMetadata])
s.Assert().NoError(err)
s.Assert().Equal(s.s3Src.Bucket, res.Metadata[bucketMetadata])
s.Assert().Equal(path, res.Metadata[pathMetadata])
s.Assert().Equal(sourceMapContentType, *res.ContentType)
s.Assert().Equal(s3SrcObjTime, s3DestObjTime)

s.Assert().Greater(res.ContentLength, int64(400))
}
}

func (s *ConvertSuite) assertS3CSSObjectExists(path string) {
res, err := s.env.S3Client.GetObject(s.ctx, &s3.GetObjectInput{
Bucket: &s.s3Dest.Bucket,
Expand Down Expand Up @@ -222,28 +160,6 @@ func (s *ConvertSuite) TestRemoveConvertedImage() {
s.assertS3ObjectNotExists(path + ".webp")
}

func (s *ConvertSuite) TestMinifyJS() {
s.Assert().NoError(s.env.Convert(s.ctx, "dir/script.js", &s.s3Src, &s.s3Dest))
s.assertS3JSObjectExists("dir/script.js")
}

func (s *ConvertSuite) TestRemoveMinifiedJS() {
path := "dir/script.js"
s.Require().NoError(s.env.Convert(s.ctx, path, &s.s3Src, &s.s3Dest))

{
_, err := s.env.S3Client.DeleteObject(s.ctx, &s3.DeleteObjectInput{
Bucket: &s.s3Src.Bucket,
Key: aws.String(s.s3Src.Prefix + path),
})
s.Require().NoError(err)
}

s.Assert().NoError(s.env.Convert(s.ctx, path, &s.s3Src, &s.s3Dest))
s.assertS3ObjectNotExists(path)
s.assertS3ObjectNotExists(path + ".map")
}

func (s *ConvertSuite) TestMinifyCSS() {
s.Assert().NoError(s.env.Convert(s.ctx, "dir/style.css", &s.s3Src, &s.s3Dest))
s.assertS3CSSObjectExists("dir/style.css")
Expand Down
19 changes: 0 additions & 19 deletions imgconv/imgconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"encoding/json"
"io"
"os"
"path/filepath"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -38,7 +36,6 @@ type Config struct {
RetrieverCount uint8
DeleterCount uint8
OrderStop time.Duration
UglifyJSPath string
Log *zap.Logger
}

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

func uglifyJSPath(path string) string {
if path == "" {
ex, err := os.Executable()
if err != nil {
panic(err)
}
return filepath.Dir(ex) + "/uglifyjs"
}
p, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return p
}

// NewEnvironment initializes values needed for execution.
func NewEnvironment(ctx context.Context, cfg *Config) *Environment {
awsConfig := createAWSConfig(ctx, cfg)
cfg.UglifyJSPath = uglifyJSPath(cfg.UglifyJSPath)
minifier := minify.New()
minifyCSS := func(w io.Writer, r io.Reader, params map[string]string) error {
return (&css.Minifier{}).Minify(minifier, w, r, params)
Expand Down
Loading

0 comments on commit 08653d7

Please sign in to comment.