NekR / offline-plugin

Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
MIT License
4.52k stars 295 forks source link

Build error when using a WebAssembly module #408

Closed mstange closed 5 years ago

mstange commented 6 years ago

Webpack 4 supports modules written in WebAssembly. Here's a commit that adds such a module to the offline-plugin-pwa example:

https://github.com/mstange/offline-plugin-pwa/commit/a4b2fdf232bc2e1711a2c1c8c5639615fe4fe638

However, when using offline-plugin on a project with WebAssembly modules, the offline-plugin build fails:

$ npm run build

> pwa-conf-offline-plugin@1.0.0 build /Users/mstange/code/offline-plugin-pwa
> webpack --log-level=debug

ℹ 「webpack」: Starting Build
➤ 「webpack」: Input File System Purged
✖ 「command」: A webpack error occured while building: use --log-level=debug for more error detail
✖ 「command」: The "data" argument must be one of type string, TypedArray, or DataView. Received type object
➤ 「command」: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be one of type string, TypedArray, or DataView. Received type object
    at Hash.update (internal/crypto/hash.js:58:11)
    at Object.getHashDigest (/Users/mstange/code/offline-plugin-pwa/node_modules/offline-plugin/node_modules/loader-utils/index.js:248:7)
    at /Users/mstange/code/offline-plugin-pwa/node_modules/offline-plugin/lib/index.js:465:45
    at Array.forEach (<anonymous>)
    at OfflinePlugin.setHashesMap (/Users/mstange/code/offline-plugin-pwa/node_modules/offline-plugin/lib/index.js:460:39)
    at emitFn (/Users/mstange/code/offline-plugin-pwa/node_modules/offline-plugin/lib/index.js:276:18)
    at _err1 (eval at create (/Users/mstange/code/offline-plugin-pwa/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:17:1)
    at callback (/Users/mstange/code/offline-plugin-pwa/node_modules/copy-webpack-plugin/dist/index.js:77:17)
    at /Users/mstange/code/offline-plugin-pwa/node_modules/copy-webpack-plugin/dist/index.js:118:24
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pwa-conf-offline-plugin@1.0.0 build: `webpack --log-level=debug`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the pwa-conf-offline-plugin@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mstange/.npm/_logs/2018-08-23T20_52_47_372Z-debug.log

You can reproduce this by checking out the add-wasm branch in my offline-plugin-pwa fork and running npm install && npm run build.

mstange commented 6 years ago

It looks like loader-utils getHashDigest is calling hash.update with an ArrayBuffer, which is not expected.

Here's the offending call to getHashDigest in offline-plugin: https://github.com/NekR/offline-plugin/blob/4553b5f5a6a02626b1985db2eb03f1f3622b859a/src/index.js#L480-L497

There doesn't seem to be documentation on the expected types of the buffer argument to getHashDigest, but I guess we now know that it accepts the same types as hash.update, which include TypedArray and Buffer but not ArrayBuffer.

So wrapping the buffer in setHashesMap should work:

let source = compilation.assets[key].source();
if (source instanceof ArrayBuffer) {
  // This is the case for WebAssembly modules.
  // getHashDigest doesn't accept ArrayBuffers, so we need to wrap the buffer into something
  // that it does support, e.g. a Buffer or a TypedArray.
  source = new Uint8Array(source);
}

 const hash = loaderUtils.getHashDigest(source, 'sha1');
mstange commented 6 years ago

I have a wip fix for this but had some trouble with the tests... my test was passing even without this fix and I didn't understand why. I can send a PR in a bit.

MaxBittker commented 5 years ago

also experiencing this!

NekR commented 5 years ago

Hey guys. I'll take a look, thank you for reporting this.