Esbuild generating strange output, including a SyntaxError
charliegroll opened this issue · 1 comments
BUG REPORT INFORMATION
While working on #625, I created a test that attempts to zip and use 3 functions; one with an import and export, one with just an import, and one with just an export.
tests/fixtures/local-require-esm
├── function
│ ├── file.js
│ └── function.js
├── function-export-only
│ └── index.js
└── function-import-only
└── index.js
The branch: https://github.com/netlify/zip-it-and-ship-it/tree/cg2/EsBuildBundlingError
The comparison: https://github.com/netlify/zip-it-and-ship-it/compare/cg2/EsBuildBundlingError?expand=1
There are 2 commits:
- e485a30 introduces a single test which passes
- 854f7f1 adds the export-/import-only scenarios, which cause a
SyntaxError
Isolated, the test has 2 failures. The first is expected, which is just saying Cannot use import statement outside a module
.
The second is more interesting:
Rejected promise returned by test. Reason:
SyntaxError {
message: 'Unexpected token \'}\'',
}
› SyntaxError: Unexpected token '}'
› Object.<anonymous> (/private/var/folders/1y/ncw647pd6vn79fg43s98wjrm0000gn/T/zip-it-test-bundler-esbuild-74878-s2RkNMGo46Ip/function-export-only.js:1:18)
because the main generated file contains all 3 functions and has an extra curly brace on line 11:
- What is the expected behavior?
esbuild should generate valid js output, and I would've assumed it would create 3 files for the 3 functions instead of clobbering them into 1 file that 3 functions merely import.
- Please mention your node.js, and operating system version.
Node 16.7.0
OS X 11.4
The reason this happens isn't a problem with esbuild! It's a problem with our test tooling.
every test makes use of
requireExtractedFiles
to open the generated zip (viaunzipFiles
) and require the files inside it.in that fixture, you have two functions whose entry file is named
index.js
, so there will be anindex.js
inside each of the corresponding zip archives.when the test suite unzips them, all the files will be placed in the temporary out directory, so it'll write the contents of both
index.js
files to the sameindex.js
destination, leaving you with a mashed upindex.js
in the endyou can fix the test by naming the files
function-export-only.js
andfunction-import-only.js
, instead ofindex.js
I've confirmed this works with 136daea in #625
Thanks @eduardoboucas for the investigation!