github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/resource_transformers/js/options_test.go (about) 1 // Copyright 2020 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package js 15 16 import ( 17 "path/filepath" 18 "testing" 19 20 "github.com/gohugoio/hugo/hugofs" 21 "github.com/gohugoio/hugo/media" 22 23 "github.com/spf13/afero" 24 25 "github.com/evanw/esbuild/pkg/api" 26 27 qt "github.com/frankban/quicktest" 28 ) 29 30 // This test is added to test/warn against breaking the "stability" of the 31 // cache key. It's sometimes needed to break this, but should be avoided if possible. 32 func TestOptionKey(t *testing.T) { 33 c := qt.New(t) 34 35 opts := map[string]any{ 36 "TargetPath": "foo", 37 "Target": "es2018", 38 } 39 40 key := (&buildTransformation{optsm: opts}).Key() 41 42 c.Assert(key.Value(), qt.Equals, "jsbuild_7891849149754191852") 43 } 44 45 func TestToBuildOptions(t *testing.T) { 46 c := qt.New(t) 47 48 opts, err := toBuildOptions(Options{mediaType: media.Builtin.JavascriptType}) 49 50 c.Assert(err, qt.IsNil) 51 c.Assert(opts, qt.DeepEquals, api.BuildOptions{ 52 Bundle: true, 53 Target: api.ESNext, 54 Format: api.FormatIIFE, 55 Stdin: &api.StdinOptions{ 56 Loader: api.LoaderJS, 57 }, 58 }) 59 60 opts, err = toBuildOptions(Options{ 61 Target: "es2018", 62 Format: "cjs", 63 Minify: true, 64 mediaType: media.Builtin.JavascriptType, 65 AvoidTDZ: true, 66 }) 67 c.Assert(err, qt.IsNil) 68 c.Assert(opts, qt.DeepEquals, api.BuildOptions{ 69 Bundle: true, 70 Target: api.ES2018, 71 Format: api.FormatCommonJS, 72 MinifyIdentifiers: true, 73 MinifySyntax: true, 74 MinifyWhitespace: true, 75 Stdin: &api.StdinOptions{ 76 Loader: api.LoaderJS, 77 }, 78 }) 79 80 opts, err = toBuildOptions(Options{ 81 Target: "es2018", Format: "cjs", Minify: true, mediaType: media.Builtin.JavascriptType, 82 SourceMap: "inline", 83 }) 84 c.Assert(err, qt.IsNil) 85 c.Assert(opts, qt.DeepEquals, api.BuildOptions{ 86 Bundle: true, 87 Target: api.ES2018, 88 Format: api.FormatCommonJS, 89 MinifyIdentifiers: true, 90 MinifySyntax: true, 91 MinifyWhitespace: true, 92 Sourcemap: api.SourceMapInline, 93 Stdin: &api.StdinOptions{ 94 Loader: api.LoaderJS, 95 }, 96 }) 97 98 opts, err = toBuildOptions(Options{ 99 Target: "es2018", Format: "cjs", Minify: true, mediaType: media.Builtin.JavascriptType, 100 SourceMap: "inline", 101 }) 102 c.Assert(err, qt.IsNil) 103 c.Assert(opts, qt.DeepEquals, api.BuildOptions{ 104 Bundle: true, 105 Target: api.ES2018, 106 Format: api.FormatCommonJS, 107 MinifyIdentifiers: true, 108 MinifySyntax: true, 109 MinifyWhitespace: true, 110 Sourcemap: api.SourceMapInline, 111 Stdin: &api.StdinOptions{ 112 Loader: api.LoaderJS, 113 }, 114 }) 115 116 opts, err = toBuildOptions(Options{ 117 Target: "es2018", Format: "cjs", Minify: true, mediaType: media.Builtin.JavascriptType, 118 SourceMap: "external", 119 }) 120 c.Assert(err, qt.IsNil) 121 c.Assert(opts, qt.DeepEquals, api.BuildOptions{ 122 Bundle: true, 123 Target: api.ES2018, 124 Format: api.FormatCommonJS, 125 MinifyIdentifiers: true, 126 MinifySyntax: true, 127 MinifyWhitespace: true, 128 Sourcemap: api.SourceMapExternal, 129 Stdin: &api.StdinOptions{ 130 Loader: api.LoaderJS, 131 }, 132 }) 133 } 134 135 func TestResolveComponentInAssets(t *testing.T) { 136 c := qt.New(t) 137 138 for _, test := range []struct { 139 name string 140 files []string 141 impPath string 142 expect string 143 }{ 144 {"Basic, extension", []string{"foo.js", "bar.js"}, "foo.js", "foo.js"}, 145 {"Basic, no extension", []string{"foo.js", "bar.js"}, "foo", "foo.js"}, 146 {"Basic, no extension, typescript", []string{"foo.ts", "bar.js"}, "foo", "foo.ts"}, 147 {"Not found", []string{"foo.js", "bar.js"}, "moo.js", ""}, 148 {"Not found, double js extension", []string{"foo.js.js", "bar.js"}, "foo.js", ""}, 149 {"Index file, folder only", []string{"foo/index.js", "bar.js"}, "foo", "foo/index.js"}, 150 {"Index file, folder and index", []string{"foo/index.js", "bar.js"}, "foo/index", "foo/index.js"}, 151 {"Index file, folder and index and suffix", []string{"foo/index.js", "bar.js"}, "foo/index.js", "foo/index.js"}, 152 {"Index ESM file, folder only", []string{"foo/index.esm.js", "bar.js"}, "foo", "foo/index.esm.js"}, 153 {"Index ESM file, folder and index", []string{"foo/index.esm.js", "bar.js"}, "foo/index", "foo/index.esm.js"}, 154 {"Index ESM file, folder and index and suffix", []string{"foo/index.esm.js", "bar.js"}, "foo/index.esm.js", "foo/index.esm.js"}, 155 // We added these index.esm.js cases in v0.101.0. The case below is unlikely to happen in the wild, but add a test 156 // to document Hugo's behavior. We pick the file with the name index.js; anything else would be breaking. 157 {"Index and Index ESM file, folder only", []string{"foo/index.esm.js", "foo/index.js", "bar.js"}, "foo", "foo/index.js"}, 158 159 // Issue #8949 160 {"Check file before directory", []string{"foo.js", "foo/index.js"}, "foo", "foo.js"}, 161 } { 162 c.Run(test.name, func(c *qt.C) { 163 baseDir := "assets" 164 mfs := afero.NewMemMapFs() 165 166 for _, filename := range test.files { 167 c.Assert(afero.WriteFile(mfs, filepath.Join(baseDir, filename), []byte("let foo='bar';"), 0777), qt.IsNil) 168 } 169 170 bfs := hugofs.DecorateBasePathFs(afero.NewBasePathFs(mfs, baseDir).(*afero.BasePathFs)) 171 172 got := resolveComponentInAssets(bfs, test.impPath) 173 174 gotPath := "" 175 if got != nil { 176 gotPath = filepath.ToSlash(got.Path) 177 } 178 179 c.Assert(gotPath, qt.Equals, test.expect) 180 }) 181 182 } 183 }