github.com/evanw/esbuild@v0.21.4/internal/bundler_tests/bundler_glob_test.go (about) 1 package bundler_tests 2 3 import ( 4 "testing" 5 6 "github.com/evanw/esbuild/internal/config" 7 ) 8 9 var glob_suite = suite{ 10 name: "glob", 11 } 12 13 func TestGlobBasicNoBundle(t *testing.T) { 14 glob_suite.expectBundled(t, bundled{ 15 files: map[string]string{ 16 "/entry.js": ` 17 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 18 console.log({ 19 concat: { 20 require: require('./src/' + ab), 21 import: import('./src/' + ab), 22 }, 23 template: { 24 require: require(` + "`./src/${ab}`" + `), 25 import: import(` + "`./src/${ab}`" + `), 26 }, 27 }) 28 `, 29 }, 30 entryPaths: []string{"/entry.js"}, 31 options: config.Options{ 32 Mode: config.ModeConvertFormat, 33 OutputFormat: config.FormatCommonJS, 34 AbsOutputFile: "/out.js", 35 }, 36 }) 37 } 38 39 func TestGlobBasicNoSplitting(t *testing.T) { 40 glob_suite.expectBundled(t, bundled{ 41 files: map[string]string{ 42 "/entry.js": ` 43 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 44 console.log({ 45 concat: { 46 require: require('./src/' + ab), 47 import: import('./src/' + ab), 48 }, 49 template: { 50 require: require(` + "`./src/${ab}`" + `), 51 import: import(` + "`./src/${ab}`" + `), 52 }, 53 }) 54 `, 55 "/src/a.js": `module.exports = 'a'`, 56 "/src/b.js": `module.exports = 'b'`, 57 }, 58 entryPaths: []string{"/entry.js"}, 59 options: config.Options{ 60 Mode: config.ModeBundle, 61 AbsOutputFile: "/out.js", 62 }, 63 }) 64 } 65 66 func TestTSGlobBasicNoSplitting(t *testing.T) { 67 glob_suite.expectBundled(t, bundled{ 68 files: map[string]string{ 69 "/entry.ts": ` 70 const ab = Math.random() < 0.5 ? 'a.ts' : 'b.ts' 71 console.log({ 72 concat: { 73 require: require('./src/' + ab), 74 import: import('./src/' + ab), 75 }, 76 template: { 77 require: require(` + "`./src/${ab}`" + `), 78 import: import(` + "`./src/${ab}`" + `), 79 }, 80 }) 81 `, 82 "/src/a.ts": `module.exports = 'a'`, 83 "/src/b.ts": `module.exports = 'b'`, 84 }, 85 entryPaths: []string{"/entry.ts"}, 86 options: config.Options{ 87 Mode: config.ModeBundle, 88 AbsOutputFile: "/out.js", 89 }, 90 }) 91 } 92 93 func TestGlobBasicSplitting(t *testing.T) { 94 glob_suite.expectBundled(t, bundled{ 95 files: map[string]string{ 96 "/entry.js": ` 97 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 98 console.log({ 99 concat: { 100 require: require('./src/' + ab), 101 import: import('./src/' + ab), 102 }, 103 template: { 104 require: require(` + "`./src/${ab}`" + `), 105 import: import(` + "`./src/${ab}`" + `), 106 }, 107 }) 108 `, 109 "/src/a.js": `module.exports = 'a'`, 110 "/src/b.js": `module.exports = 'b'`, 111 }, 112 entryPaths: []string{"/entry.js"}, 113 options: config.Options{ 114 Mode: config.ModeBundle, 115 AbsOutputDir: "/out", 116 CodeSplitting: true, 117 }, 118 }) 119 } 120 121 func TestTSGlobBasicSplitting(t *testing.T) { 122 glob_suite.expectBundled(t, bundled{ 123 files: map[string]string{ 124 "/entry.ts": ` 125 const ab = Math.random() < 0.5 ? 'a.ts' : 'b.ts' 126 console.log({ 127 concat: { 128 require: require('./src/' + ab), 129 import: import('./src/' + ab), 130 }, 131 template: { 132 require: require(` + "`./src/${ab}`" + `), 133 import: import(` + "`./src/${ab}`" + `), 134 }, 135 }) 136 `, 137 "/src/a.ts": `module.exports = 'a'`, 138 "/src/b.ts": `module.exports = 'b'`, 139 }, 140 entryPaths: []string{"/entry.ts"}, 141 options: config.Options{ 142 Mode: config.ModeBundle, 143 AbsOutputDir: "/out", 144 CodeSplitting: true, 145 }, 146 }) 147 } 148 149 func TestGlobDirDoesNotExist(t *testing.T) { 150 glob_suite.expectBundled(t, bundled{ 151 files: map[string]string{ 152 "/entry.js": ` 153 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 154 console.log({ 155 concat: { 156 require: require('./src/' + ab), 157 import: import('./src/' + ab), 158 }, 159 template: { 160 require: require(` + "`./src/${ab}`" + `), 161 import: import(` + "`./src/${ab}`" + `), 162 }, 163 }) 164 `, 165 }, 166 entryPaths: []string{"/entry.js"}, 167 options: config.Options{ 168 Mode: config.ModeBundle, 169 AbsOutputDir: "/out", 170 CodeSplitting: true, 171 }, 172 expectedScanLog: `entry.js: ERROR: Could not resolve require("./src/**/*") 173 entry.js: ERROR: Could not resolve import("./src/**/*") 174 `, 175 }) 176 } 177 178 func TestGlobNoMatches(t *testing.T) { 179 glob_suite.expectBundled(t, bundled{ 180 files: map[string]string{ 181 "/entry.js": ` 182 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 183 console.log({ 184 concat: { 185 require: require('./src/' + ab + '.json'), 186 import: import('./src/' + ab + '.json'), 187 }, 188 template: { 189 require: require(` + "`./src/${ab}.json`" + `), 190 import: import(` + "`./src/${ab}.json`" + `), 191 }, 192 }) 193 `, 194 "/src/dummy.js": ``, 195 }, 196 entryPaths: []string{"/entry.js"}, 197 options: config.Options{ 198 Mode: config.ModeBundle, 199 AbsOutputDir: "/out", 200 CodeSplitting: true, 201 }, 202 expectedScanLog: `entry.js: WARNING: The glob pattern require("./src/**/*.json") did not match any files 203 entry.js: WARNING: The glob pattern import("./src/**/*.json") did not match any files 204 `, 205 }) 206 } 207 208 func TestGlobEntryPointAbsPath(t *testing.T) { 209 glob_suite.expectBundled(t, bundled{ 210 files: map[string]string{ 211 "/Users/user/project/src/entry.js": ` 212 works = true 213 `, 214 }, 215 entryPaths: []string{"/Users/user/project/**/*.js"}, 216 options: config.Options{ 217 Mode: config.ModeBundle, 218 AbsOutputDir: "/out", 219 }, 220 }) 221 } 222 223 func TestGlobWildcardSlash(t *testing.T) { 224 glob_suite.expectBundled(t, bundled{ 225 files: map[string]string{ 226 "/entry.js": ` 227 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 228 console.log({ 229 concat: { 230 require: require('./src/' + ab + '.js'), 231 import: import('./src/' + ab + '.js'), 232 }, 233 template: { 234 require: require(` + "`./src/${ab}.js`" + `), 235 import: import(` + "`./src/${ab}.js`" + `), 236 }, 237 }) 238 `, 239 240 "/src/file-a.js": `module.exports = 'a'`, 241 "/src/file-b.js": `module.exports = 'b'`, 242 "/src/file-a.js.map": `DO NOT BUNDLE`, 243 "/src/file-b.js.map": `DO NOT BUNDLE`, 244 245 "/src/nested/dir/file-a.js": `module.exports = 'a'`, 246 "/src/nested/dir/file-b.js": `module.exports = 'b'`, 247 "/src/nested/dir/file-a.js.map": `DO NOT BUNDLE`, 248 "/src/nested/dir/file-b.js.map": `DO NOT BUNDLE`, 249 }, 250 entryPaths: []string{"/entry.js"}, 251 options: config.Options{ 252 Mode: config.ModeBundle, 253 AbsOutputFile: "/out.js", 254 }, 255 }) 256 } 257 258 func TestGlobWildcardNoSlash(t *testing.T) { 259 glob_suite.expectBundled(t, bundled{ 260 files: map[string]string{ 261 "/entry.js": ` 262 const ab = Math.random() < 0.5 ? 'a.js' : 'b.js' 263 console.log({ 264 concat: { 265 require: require('./src/file-' + ab + '.js'), 266 import: import('./src/file-' + ab + '.js'), 267 }, 268 template: { 269 require: require(` + "`./src/file-${ab}.js`" + `), 270 import: import(` + "`./src/file-${ab}.js`" + `), 271 }, 272 }) 273 `, 274 275 "/src/file-a.js": `module.exports = 'a'`, 276 "/src/file-b.js": `module.exports = 'b'`, 277 "/src/file-a.js.map": `DO NOT BUNDLE`, 278 "/src/file-b.js.map": `DO NOT BUNDLE`, 279 280 "/src/nested/dir/file-a.js": `DO NOT BUNDLE`, 281 "/src/nested/dir/file-b.js": `DO NOT BUNDLE`, 282 "/src/nested/dir/file-a.js.map": `DO NOT BUNDLE`, 283 "/src/nested/dir/file-b.js.map": `DO NOT BUNDLE`, 284 }, 285 entryPaths: []string{"/entry.js"}, 286 options: config.Options{ 287 Mode: config.ModeBundle, 288 AbsOutputFile: "/out.js", 289 }, 290 }) 291 }