BBVAEngineering / ember-cli-workbox

Offline caching as a progressive enhancement using service workers
MIT License
20 stars 11 forks source link

Defining `importScriptsTransform` causes `DataCloneError` #105

Open emattias opened 4 years ago

emattias commented 4 years ago

After updating ember to 3.21.3 we start getting a DataCloneError when the importScriptsTransform function is defined in ember-cli-build

JSReloader watches js|ts|hbs
⠙ building... [Babel: ember-cli-workbox > applyPatches]error: --------------------------------------------------------------------------
error: An uncaught YUIDoc error has occurred, stack trace given below
error: --------------------------------------------------------------------------
error: DataCloneError: (importScripts) => {
      return importScripts.map((importScript) => {
        return ENV.EMBER_ASSETS_ROOT_PA...<omitted>... } could not be cloned.
    at Worker.postMessage (internal/worker.js:279:23)
    at Worker.worker.send (/Users/emattias/repos/ember-cli-workbox/node_modules/broccoli-babel-transpiler/node_modules/workerpool/lib/WorkerHandler.js:77:10)
    at Array.forEach (<anonymous>)
    at dispatchQueuedRequests (/Users/emattias/repos/ember-cli-workbox/node_modules/broccoli-babel-transpiler/node_modules/workerpool/lib/WorkerHandler.js:240:21)
    at Worker.<anonymous> (/Users/emattias/repos/ember-cli-workbox/node_modules/broccoli-babel-transpiler/node_modules/workerpool/lib/WorkerHandler.js:195:7)
    at Worker.emit (events.js:315:20)
    at Worker.EventEmitter.emit (domain.js:482:12)
    at MessagePort.<anonymous> (internal/worker.js:192:55)
    at MessagePort.emit (events.js:315:20)
    at MessagePort.EventEmitter.emit (domain.js:482:12)
error: --------------------------------------------------------------------------

If you cant have functions in the app config is there some other way to send the function to the addon? Here's a workaround used by trackJs for a similar problem.

rwjblue commented 4 years ago

This happens in the browser (not the build) right? What is the full stack trace? Why is it trying to postMessage the config?

rwjblue commented 4 years ago

Hmm, actually seems to be in node-land:

https://github.com/nodejs/node/blob/v14.13.0/lib/internal/worker.js#L298

IS that the full stack trace you provided above? I would expect at least a little more context there...

emattias commented 4 years ago

I updated it with more context and stack trace

rwjblue commented 4 years ago

Nice, that is really useful! I had originally gotten confused by the postMessage usage (within a project that is actually reasonably going to use postMessage anyways due to the SW aspect), but this is actually just an issue with babel transpilation. By default broccoli-babel-transpiler (which is what ember-cli-babel uses) uses workerpool to parallelize the babel build (either via threads if on new enough Node, or processes). It seems like the ember-cli-workbox options from ember-cli-build.js is being used as part of the configuration for that babel build (but it doesn't really seem like it ought to be) which is what is causing this error.

Can you whip up a reproduction that is largely a npx ember-cli new test-app-here that I could use to help debug?

adrigzr commented 4 years ago

Hi @emattias, we moved that option from config/environment to the ember-cli-build file for that very reason. I didn't test it on 3.21 but on 3.16 it is working fine.