github.com/evanw/esbuild@v0.21.4/internal/bundler_tests/bundler_importstar_ts_test.go (about) 1 package bundler_tests 2 3 import ( 4 "testing" 5 6 "github.com/evanw/esbuild/internal/config" 7 ) 8 9 var importstar_ts_suite = suite{ 10 name: "importstar_ts", 11 } 12 13 func TestTSImportStarUnused(t *testing.T) { 14 importstar_ts_suite.expectBundled(t, bundled{ 15 files: map[string]string{ 16 "/entry.ts": ` 17 import * as ns from './foo' 18 let foo = 234 19 console.log(foo) 20 `, 21 "/foo.ts": ` 22 export const foo = 123 23 `, 24 }, 25 entryPaths: []string{"/entry.ts"}, 26 options: config.Options{ 27 Mode: config.ModeBundle, 28 AbsOutputFile: "/out.js", 29 }, 30 }) 31 } 32 33 func TestTSImportStarCapture(t *testing.T) { 34 importstar_ts_suite.expectBundled(t, bundled{ 35 files: map[string]string{ 36 "/entry.ts": ` 37 import * as ns from './foo' 38 let foo = 234 39 console.log(ns, ns.foo, foo) 40 `, 41 "/foo.ts": ` 42 export const foo = 123 43 `, 44 }, 45 entryPaths: []string{"/entry.ts"}, 46 options: config.Options{ 47 Mode: config.ModeBundle, 48 AbsOutputFile: "/out.js", 49 }, 50 }) 51 } 52 53 func TestTSImportStarNoCapture(t *testing.T) { 54 importstar_ts_suite.expectBundled(t, bundled{ 55 files: map[string]string{ 56 "/entry.ts": ` 57 import * as ns from './foo' 58 let foo = 234 59 console.log(ns.foo, ns.foo, foo) 60 `, 61 "/foo.ts": ` 62 export const foo = 123 63 `, 64 }, 65 entryPaths: []string{"/entry.ts"}, 66 options: config.Options{ 67 Mode: config.ModeBundle, 68 AbsOutputFile: "/out.js", 69 }, 70 }) 71 } 72 73 func TestTSImportStarExportImportStarUnused(t *testing.T) { 74 importstar_ts_suite.expectBundled(t, bundled{ 75 files: map[string]string{ 76 "/entry.ts": ` 77 import {ns} from './bar' 78 let foo = 234 79 console.log(foo) 80 `, 81 "/foo.ts": ` 82 export const foo = 123 83 `, 84 "/bar.ts": ` 85 import * as ns from './foo' 86 export {ns} 87 `, 88 }, 89 entryPaths: []string{"/entry.ts"}, 90 options: config.Options{ 91 Mode: config.ModeBundle, 92 AbsOutputFile: "/out.js", 93 }, 94 }) 95 } 96 97 func TestTSImportStarExportImportStarNoCapture(t *testing.T) { 98 importstar_ts_suite.expectBundled(t, bundled{ 99 files: map[string]string{ 100 "/entry.ts": ` 101 import {ns} from './bar' 102 let foo = 234 103 console.log(ns.foo, ns.foo, foo) 104 `, 105 "/foo.ts": ` 106 export const foo = 123 107 `, 108 "/bar.ts": ` 109 import * as ns from './foo' 110 export {ns} 111 `, 112 }, 113 entryPaths: []string{"/entry.ts"}, 114 options: config.Options{ 115 Mode: config.ModeBundle, 116 AbsOutputFile: "/out.js", 117 }, 118 }) 119 } 120 121 func TestTSImportStarExportImportStarCapture(t *testing.T) { 122 importstar_ts_suite.expectBundled(t, bundled{ 123 files: map[string]string{ 124 "/entry.ts": ` 125 import {ns} from './bar' 126 let foo = 234 127 console.log(ns, ns.foo, foo) 128 `, 129 "/foo.ts": ` 130 export const foo = 123 131 `, 132 "/bar.ts": ` 133 import * as ns from './foo' 134 export {ns} 135 `, 136 }, 137 entryPaths: []string{"/entry.ts"}, 138 options: config.Options{ 139 Mode: config.ModeBundle, 140 AbsOutputFile: "/out.js", 141 }, 142 }) 143 } 144 145 func TestTSImportStarExportStarAsUnused(t *testing.T) { 146 importstar_ts_suite.expectBundled(t, bundled{ 147 files: map[string]string{ 148 "/entry.ts": ` 149 import {ns} from './bar' 150 let foo = 234 151 console.log(foo) 152 `, 153 "/foo.ts": ` 154 export const foo = 123 155 `, 156 "/bar.ts": ` 157 export * as ns from './foo' 158 `, 159 }, 160 entryPaths: []string{"/entry.ts"}, 161 options: config.Options{ 162 Mode: config.ModeBundle, 163 AbsOutputFile: "/out.js", 164 }, 165 }) 166 } 167 168 func TestTSImportStarExportStarAsNoCapture(t *testing.T) { 169 importstar_ts_suite.expectBundled(t, bundled{ 170 files: map[string]string{ 171 "/entry.ts": ` 172 import {ns} from './bar' 173 let foo = 234 174 console.log(ns.foo, ns.foo, foo) 175 `, 176 "/foo.ts": ` 177 export const foo = 123 178 `, 179 "/bar.ts": ` 180 export * as ns from './foo' 181 `, 182 }, 183 entryPaths: []string{"/entry.ts"}, 184 options: config.Options{ 185 Mode: config.ModeBundle, 186 AbsOutputFile: "/out.js", 187 }, 188 }) 189 } 190 191 func TestTSImportStarExportStarAsCapture(t *testing.T) { 192 importstar_ts_suite.expectBundled(t, bundled{ 193 files: map[string]string{ 194 "/entry.ts": ` 195 import {ns} from './bar' 196 let foo = 234 197 console.log(ns, ns.foo, foo) 198 `, 199 "/foo.ts": ` 200 export const foo = 123 201 `, 202 "/bar.ts": ` 203 export * as ns from './foo' 204 `, 205 }, 206 entryPaths: []string{"/entry.ts"}, 207 options: config.Options{ 208 Mode: config.ModeBundle, 209 AbsOutputFile: "/out.js", 210 }, 211 }) 212 } 213 214 func TestTSImportStarExportStarUnused(t *testing.T) { 215 importstar_ts_suite.expectBundled(t, bundled{ 216 files: map[string]string{ 217 "/entry.ts": ` 218 import * as ns from './bar' 219 let foo = 234 220 console.log(foo) 221 `, 222 "/foo.ts": ` 223 export const foo = 123 224 `, 225 "/bar.ts": ` 226 export * from './foo' 227 `, 228 }, 229 entryPaths: []string{"/entry.ts"}, 230 options: config.Options{ 231 Mode: config.ModeBundle, 232 AbsOutputFile: "/out.js", 233 }, 234 }) 235 } 236 237 func TestTSImportStarExportStarNoCapture(t *testing.T) { 238 importstar_ts_suite.expectBundled(t, bundled{ 239 files: map[string]string{ 240 "/entry.ts": ` 241 import * as ns from './bar' 242 let foo = 234 243 console.log(ns.foo, ns.foo, foo) 244 `, 245 "/foo.ts": ` 246 export const foo = 123 247 `, 248 "/bar.ts": ` 249 export * from './foo' 250 `, 251 }, 252 entryPaths: []string{"/entry.ts"}, 253 options: config.Options{ 254 Mode: config.ModeBundle, 255 AbsOutputFile: "/out.js", 256 }, 257 }) 258 } 259 260 func TestTSImportStarExportStarCapture(t *testing.T) { 261 importstar_ts_suite.expectBundled(t, bundled{ 262 files: map[string]string{ 263 "/entry.ts": ` 264 import * as ns from './bar' 265 let foo = 234 266 console.log(ns, ns.foo, foo) 267 `, 268 "/foo.ts": ` 269 export const foo = 123 270 `, 271 "/bar.ts": ` 272 export * from './foo' 273 `, 274 }, 275 entryPaths: []string{"/entry.ts"}, 276 options: config.Options{ 277 Mode: config.ModeBundle, 278 AbsOutputFile: "/out.js", 279 }, 280 }) 281 } 282 283 func TestTSImportStarCommonJSUnused(t *testing.T) { 284 importstar_ts_suite.expectBundled(t, bundled{ 285 files: map[string]string{ 286 "/entry.ts": ` 287 import * as ns from './foo' 288 let foo = 234 289 console.log(foo) 290 `, 291 "/foo.ts": ` 292 exports.foo = 123 293 `, 294 }, 295 entryPaths: []string{"/entry.ts"}, 296 options: config.Options{ 297 Mode: config.ModeBundle, 298 AbsOutputFile: "/out.js", 299 }, 300 }) 301 } 302 303 func TestTSImportStarCommonJSCapture(t *testing.T) { 304 importstar_ts_suite.expectBundled(t, bundled{ 305 files: map[string]string{ 306 "/entry.ts": ` 307 import * as ns from './foo' 308 let foo = 234 309 console.log(ns, ns.foo, foo) 310 `, 311 "/foo.ts": ` 312 exports.foo = 123 313 `, 314 }, 315 entryPaths: []string{"/entry.ts"}, 316 options: config.Options{ 317 Mode: config.ModeBundle, 318 AbsOutputFile: "/out.js", 319 }, 320 }) 321 } 322 323 func TestTSImportStarCommonJSNoCapture(t *testing.T) { 324 importstar_ts_suite.expectBundled(t, bundled{ 325 files: map[string]string{ 326 "/entry.ts": ` 327 import * as ns from './foo' 328 let foo = 234 329 console.log(ns.foo, ns.foo, foo) 330 `, 331 "/foo.ts": ` 332 exports.foo = 123 333 `, 334 }, 335 entryPaths: []string{"/entry.ts"}, 336 options: config.Options{ 337 Mode: config.ModeBundle, 338 AbsOutputFile: "/out.js", 339 }, 340 }) 341 } 342 343 func TestTSImportStarAndCommonJS(t *testing.T) { 344 importstar_ts_suite.expectBundled(t, bundled{ 345 files: map[string]string{ 346 "/entry.js": ` 347 import * as ns from './foo' 348 const ns2 = require('./foo') 349 console.log(ns.foo, ns2.foo) 350 `, 351 "/foo.ts": ` 352 export const foo = 123 353 `, 354 }, 355 entryPaths: []string{"/entry.js"}, 356 options: config.Options{ 357 Mode: config.ModeBundle, 358 AbsOutputFile: "/out.js", 359 }, 360 }) 361 } 362 363 func TestTSImportStarNoBundleUnused(t *testing.T) { 364 importstar_ts_suite.expectBundled(t, bundled{ 365 files: map[string]string{ 366 "/entry.ts": ` 367 import * as ns from './foo' 368 let foo = 234 369 console.log(foo) 370 `, 371 }, 372 entryPaths: []string{"/entry.ts"}, 373 options: config.Options{ 374 AbsOutputFile: "/out.js", 375 }, 376 }) 377 } 378 379 func TestTSImportStarNoBundleCapture(t *testing.T) { 380 importstar_ts_suite.expectBundled(t, bundled{ 381 files: map[string]string{ 382 "/entry.ts": ` 383 import * as ns from './foo' 384 let foo = 234 385 console.log(ns, ns.foo, foo) 386 `, 387 }, 388 entryPaths: []string{"/entry.ts"}, 389 options: config.Options{ 390 AbsOutputFile: "/out.js", 391 }, 392 }) 393 } 394 395 func TestTSImportStarNoBundleNoCapture(t *testing.T) { 396 importstar_ts_suite.expectBundled(t, bundled{ 397 files: map[string]string{ 398 "/entry.ts": ` 399 import * as ns from './foo' 400 let foo = 234 401 console.log(ns.foo, ns.foo, foo) 402 `, 403 }, 404 entryPaths: []string{"/entry.ts"}, 405 options: config.Options{ 406 AbsOutputFile: "/out.js", 407 }, 408 }) 409 } 410 411 func TestTSImportStarMangleNoBundleUnused(t *testing.T) { 412 importstar_ts_suite.expectBundled(t, bundled{ 413 files: map[string]string{ 414 "/entry.ts": ` 415 import * as ns from './foo' 416 let foo = 234 417 console.log(foo) 418 `, 419 }, 420 entryPaths: []string{"/entry.ts"}, 421 options: config.Options{ 422 MinifySyntax: true, 423 AbsOutputFile: "/out.js", 424 }, 425 }) 426 } 427 428 func TestTSImportStarMangleNoBundleCapture(t *testing.T) { 429 importstar_ts_suite.expectBundled(t, bundled{ 430 files: map[string]string{ 431 "/entry.ts": ` 432 import * as ns from './foo' 433 let foo = 234 434 console.log(ns, ns.foo, foo) 435 `, 436 }, 437 entryPaths: []string{"/entry.ts"}, 438 options: config.Options{ 439 MinifySyntax: true, 440 AbsOutputFile: "/out.js", 441 }, 442 }) 443 } 444 445 func TestTSImportStarMangleNoBundleNoCapture(t *testing.T) { 446 importstar_ts_suite.expectBundled(t, bundled{ 447 files: map[string]string{ 448 "/entry.ts": ` 449 import * as ns from './foo' 450 let foo = 234 451 console.log(ns.foo, ns.foo, foo) 452 `, 453 }, 454 entryPaths: []string{"/entry.ts"}, 455 options: config.Options{ 456 MinifySyntax: true, 457 AbsOutputFile: "/out.js", 458 }, 459 }) 460 } 461 462 func TestTSReExportTypeOnlyFileES6(t *testing.T) { 463 importstar_ts_suite.expectBundled(t, bundled{ 464 files: map[string]string{ 465 "/entry.ts": ` 466 import * as ns from './re-export' 467 console.log(ns.foo) 468 `, 469 "/re-export.ts": ` 470 export * from './types1' 471 export * from './types2' 472 export * from './types3' 473 export * from './values' 474 `, 475 "/types1.ts": ` 476 export interface Foo {} 477 export type Bar = number 478 console.log('some code') 479 `, 480 "/types2.ts": ` 481 import {Foo} from "./type" 482 export {Foo} 483 console.log('some code') 484 `, 485 "/types3.ts": ` 486 export {Foo} from "./type" 487 console.log('some code') 488 `, 489 "/values.ts": ` 490 export let foo = 123 491 `, 492 "/type.ts": ` 493 export type Foo = number 494 `, 495 }, 496 entryPaths: []string{"/entry.ts"}, 497 options: config.Options{ 498 Mode: config.ModeBundle, 499 AbsOutputFile: "/out.js", 500 }, 501 }) 502 }