ianstormtaylor/slate-plugins

mime-db dependency is huge!

dmitrizzle opened this issue · 3 comments

Do you want to request a feature or report a bug?

bug, of sorts

What's the current behavior?

slate-drop-or-paste-images depends on mime-types to run this function:

  function onInsertFiles(event, change, editor, transfer) {
    const { target, files } = transfer

    for (const file of files) {
      if (extensions) {
        const ext = mime.extension(file.type)
        if (!extensions.includes(ext)) continue
      }

      if (target) {
        change.select(target)
      }

      asyncApplyChange(change, editor, file)
    }

    return true
  }

Which, as I understands check for allowed file types when the user attempts to insert one. That tool in turn depends on mime-db, which in production costs 144KB (before GZip).

What's the expected behavior?

I don't think filetype check function should cost as much as the entire Slate library from within a plugin. Perhaps there's a way to hardcode allowed types instead or use another dependency?

Damn, I agree with you. Open to ideas/PRs!

How about a simple file.type? https://developer.mozilla.org/en-US/docs/Web/API/File/type - According to docs the browser will look at extension, which isn't fool-proof, of course, but it'll reduce the bundle size by 90% and will work most of the time.

Alternatively we could use a byte reader to get the "magic numbers" which will be more accurate but will require a bit more work to get going: https://medium.com/the-everyday-developer/detect-file-mime-type-using-magic-numbers-and-javascript-16bc513d4e1e https://codepen.io/movii/pen/oBLNoJ

I'd be happy to make a PR for either, just would like to know what you'd think the appropriate scope of abilities for this plugin should be (focus on Slate logic and basic filetype detection or a bit more complex logic outside of Slate with a more solid file type detection).

Thanks @ianstormtaylor !

Nice, yeah I'm down with the file.type, that sounds easiest. Thank you!