esbuild-plugin-babel icon indicating copy to clipboard operation
esbuild-plugin-babel copied to clipboard

issue with loading babel() in esbuild.plugins[]

Open noahehall opened this issue 4 years ago • 2 comments

having issue using this plugin in my esbuild.config.json.

I just copy pasted your example, throws error immediately.


 > node_modules/statuses/index.js:13:20: error: [plugin: babel] /[appdir]/node_modules/statuses/codes.json: Missing semicolon. (2:7)

  1 | {
> 2 |   "100": "Continue",
    |        ^
  3 |   "101": "Switching Protocols",
  4 |   "102": "Processing",
  5 |   "103": "Early Hints",
     13 │ var codes = require('./codes.json');


    ╵                          ~~~~~~~~~~~
   node_modules/esbuild/lib/main.js:756:22: note: This error came from the "onLoad" callback registered here
    756 │         let promise = setup({
        ╵                       ^
    at setup (file:///[appdir]/node_modules/esbuild-plugin-babel/src/index.js:36:9)

you can see the error in action by uncommenting this lline

and running npm start

noahehall avatar May 27 '21 20:05 noahehall

The issue seems to be that the plugin by default uses an ESBuild filter of /.*/. This will result in it trying to Babel transform all files that ESBuild processes, including any imported JSON files (e.g. codes.json in your example), and Babel does not support transforming JSON directly.

ESBuild filters are parsed using Go's RegExp library, which doesn't support lookbehind assertions, so I hacked around this in my project by passing a custom filter to the plugin in the ESBuild config:

      babel({
        filter: /.*[^j][^s][^o][^n]$/,
      }),

gavinsharp avatar Oct 20 '21 12:10 gavinsharp

Hi @gavinsharp , thanks for this plugin, question: how could we filter many extensions with this regex?

michelson avatar Jan 21 '22 06:01 michelson