将 FileAPI 库与 browserify 一起使用

Using the FileAPI library with browserify

文件API库(https://github.com/mailru/FileAPI/issues/202) does not officially support CommonJS modules. I've tried using browserify-shim but I'm not able to make it work. After requireing fileapi I just get an empty object back. I've created a repo for reproduction here https://github.com/Prinzhorn/browserify-fileapi

相关package.json部分

{
  "dependencies": {
    "fileapi": "2.0.15"
  },
  "devDependencies": {
    "browserify": "11.1.0",
    "browserify-shim": "3.8.10"
  },
  "browser": {
    "fileapi": "./node_modules/fileapi/dist/FileAPI.html5.js"
  },
  "browserify-shim": {
    "fileapi": "FileAPI"
  }
}

如果您想在本地尝试:

git clone git@github.com:Prinzhorn/browserify-fileapi.git
npm install
npm run build
chromium-browser index.html

查看 Chromium 中的控制台,您会看到来自 运行 console.log(Object.keys(require('fileapi'))) 的空数组。请注意,有一个全局 window.FileAPI 和正确的 API.

有谁知道 browserify-shim 是否能够填充 FileAPI?因为我相信它会做一些奇特的事情来管理它的依赖性(连接的文件需要某些全局变量)。

您需要告诉 browserify 使用 browserify-shim 作为 package.json 中的转换,如 this example

中所述

主要是你缺少:

"browserify": {
   "transform": [ "browserify-shim" ]
}