Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when returning js filex contents from onLoad #914

Closed
Arcath opened this issue Mar 4, 2021 · 7 comments
Closed

Panic when returning js filex contents from onLoad #914

Arcath opened this issue Mar 4, 2021 · 7 comments

Comments

@Arcath
Copy link

Arcath commented Mar 4, 2021

I have written a plugin to get file contents from absoluteFiles. The aim here is to remove the need for the passed source code to have to be written to disk before I can bundle it.

When ever I return the files contents from the onLoad callback I just get an error thrown from Go.

Plugin Code:

const inMemoryPlugin: Plugin = {
    name: 'inMemory',
    setup(build){
      build.onResolve({filter: /__mdx_bundler_fake_dir__/}, (args) => {
        if(absoluteFiles[args.path]){
          return {
            path: args.path,
            namespace: 'mdx-bundler',
            pluginData: {
              contents: absoluteFiles[args.path]
            }
          }
        }

        throw new Error(`Could not resolve ${args.path}`)
      })

      build.onLoad({filter: /.*/, namespace: 'mdx-bundler'}, (args) => {
        return {
          contents: args.pluginData.contents,
          loader: 'js'
        }
      })
    }
  }

Error:

panic: interface conversion: interface {} is map[string]interface {}, not []uint8

goroutine 5 [running]:
, 0xb, 0xe363a0, 0x11011e0, 0x0, 0x0, 0x0, 0x0, ...)460, 0x50, 0xc000013ce0
        /Users/evan/dev/esbuild/cmd/esbuild/service.go:733 +0xe27
github.com/evanw/esbuild/pkg/api.(*pluginImpl).OnLoad.func1(0xc000010460, 0x50, 0xc000013ce0, 0xb, 0x0, 0x0, 0x0, 0xe363a0, 0x11011e0, 0x0, ...)
        /Users/evan/dev/esbuild/pkg/api/api_impl.go:1244 +0x142
github.com/evanw/esbuild/internal/bundler.runOnLoadPlugins(0xc0001bc040, 0x1, 0x1, 0xf45258, 0xc0001c0000, 0xf48140, 0xc0001bc080, 0xc00019a0d8, 0xc0001b6000, 0xc00018a0d8, ...)
        /Users/evan/dev/esbuild/internal/bundler/bundler.go:763 +0x85e
github.com/evanw/esbuild/internal/bundler.parseFile(0xf48140, 0xc0001bc080, 0xc0001b6000, 0xc00018a0d8, 0xc0001b6080, 0xf45258, 0xc0001c0000, 0xc00019a0c0, 0xc000010460, 0x50, ...)
        /Users/evan/dev/esbuild/internal/bundler/bundler.go:176 +0x4b14
created by github.com/evanw/esbuild/internal/bundler.(*scanner).maybeParseFile
        /Users/evan/dev/esbuild/internal/bundler/bundler.go:979 +0x578

My build code is:

const bundle = await build({
    entryPoints: [entryPath],
    write: false,
    outfile: 'fakePath',
    plugins: [inMemoryPlugin]
  })

I'm not sure why its failing. I've confirmed that it gets to the onLoad function and that the string I'm returning is the correct source.

@evanw
Copy link
Owner

evanw commented Mar 4, 2021

I can't reproduce this myself. Can you provide a more complete way to reproduce this? For example, your build code presumably doesn't work without bundle: true and your plugin code is missing the absoluteFiles variable. It looks like the problem is caused by whatever value is being provided to contents in onLoad, so alternatively maybe you can just say what that is? It seems like maybe it's somehow an object that isn't a Uint8Array or a string? And just in case: what environment are you running this in (node/browser version)?

@Arcath
Copy link
Author

Arcath commented Mar 4, 2021

I'm working on swapping https://github.com/kentcdodds/mdx-bundler/blob/main/src/index.ts over to esbuild.

absoluteFiles is an object which stores the content of the files as a string. e.g. absoluteFile['/path/to/file.js']

The aim is to take mdx and any supplied modules and return the mdx bundle.

I've confirmed that what my onLoad is returning is {content: 'some-string'} so I'm not sure why its crashing out.

@evanw
Copy link
Owner

evanw commented Mar 4, 2021

Would it be possible to push your work-in-progress code to a branch so I can use it to reproduce the issue?

@Arcath
Copy link
Author

Arcath commented Mar 4, 2021

I've pushed my WIP code to here: https://github.com/Arcath/mdx-bundler/tree/pr/esbuild

@Arcath
Copy link
Author

Arcath commented Mar 4, 2021

My environment:

  • Windows 10
  • Node 14.15.5

@evanw
Copy link
Owner

evanw commented Mar 4, 2021

This is caused by your test setup which is breaking node's Buffer API. I think this might be jestjs/jest#4422. I might be able to work around this broken Buffer implementation in esbuild's code.

@evanw evanw closed this as completed in ea3242a Mar 4, 2021
@Arcath
Copy link
Author

Arcath commented Mar 5, 2021

Thanks for your help!

For those having this issue in the future my solution was to add this to my jest.config.js

testEnvironment: 'node'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants