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

Issues compiling when migrating from 0.11 to 0.12 #659

Closed
chopfitzroy opened this issue Jun 14, 2020 · 4 comments
Closed

Issues compiling when migrating from 0.11 to 0.12 #659

chopfitzroy opened this issue Jun 14, 2020 · 4 comments

Comments

@chopfitzroy
Copy link

Heya,

I have a project that is using Capacitor and while I can compile it in version 0.11.0 it falls over in 0.12.0, given that it does work in 0.11.0 and not 0.12.0 I am guessing this is a bundling issue but please let me know if I need to redirect my query to capacitor.

I have also tried using microbundle@next to bundle and I am running into the same issue.

A shimmed down version of my capacitor code is as follows:

import { Capacitor, Plugins } from "@capacitor/core";

const { Device } = Plugins;

const platform = Capacitor.getPlatform();

const resolveAndroidInformation = () =>
  new Promise((resolve) => {
    console.log("Attempting to connect to the Android device.");

    if (platform !== "android")
      return resolve({
        error: "Incorrect platform, Android resolver terminating.",
      });

    const deviceHandler = async () => {
      try {
        const info = await Device.getInfo();

        return resolve({
          info,
          // Additional info
        });
      } catch (error) {
        return resolve({
          error: `Failed to connect to Android device: ${error}.`,
        });
      }
    };

    deviceHandler();
  });

export { resolveAndroidInformation };

And when it successfully logs:

Attempting to connect to the Android device.

And then the promise it is meant to resolve, resolves as undefined before logging:

Incorrect platform, Android resolver terminating.

NOTE I am running in a web environment and expect this code to fail, platform does return "web" when logged.

The way this promise is called is as follows:

const populateDeviceInfomation = () =>
  new Promise(async resolve => {
    const control = [resolveAndroidInformation(), resolveChromeInformation()];

    for await (const result of control) {
      const { error } = result || { error: 'Result in device provider appears to be `undefined`, moving to next resolver' };

      if (error) {
        console.log(error);
      } else {
        return resolve(result);
      }
    }

    return resolve({ error: 'Unable to resolve any infromation: All resolvers failed' });
  });

And basically what happens is the 'Result in device provider appears to be ``undefined``, moving to next resolver message is logged infinitely (40,000+ then browser freezes).

Now I am aware this is a for await loop and looking at previous commits I can see tweaking has been made here, additionally Capacitor is written in Typescript which may or may not have an impact.

I tried to figure out what had changed between 0.11.0 and 0.12.0 but couldn't quite figure it out and was hoping it may be obvious to someone more involved with the project.

@developit
Copy link
Owner

developit commented Jun 14, 2020

Are you able to try the --format modern output? It doesn't transpile async/await.

My guess is the same as the yours, that this is related to the async plugin. I believe it is currently possible to work around that by adding a babel.config.js containing:

module.exports = {
  presets: [
    ['env', {
      loose: true
    }]
  ]
}

@chopfitzroy
Copy link
Author

chopfitzroy commented Jun 15, 2020

Hey,

My resolutions seemed to break using --format modern (I will look into this further eventually, it was to do with not explicitly using /index.js and the end of some of my imports....).

I tried the mentioned babel.config.js to no avail, I also tried:

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        exclude: ['transform-regenerator'],
      },
    ],
  ],
};

But no luck...

EDIT am I correct in understanding this is because 0.11.0 to 0.12.0 moves from buble to babel?

@developit
Copy link
Owner

developit commented Jun 15, 2020

Yes, the move from Bublé is the underlying cause here. Specifically, it is caused by this issue: rpetrich/babel-plugin-transform-async-to-promises#49.

--format modern shouldn't change anything to do with resolution FWIW, it just changes the output to use modern syntax.

Can you try a babel.config.js containing the following?

module.exports = {
  presets: [
    ['env', {
      loose: true,
      bugfixes: true,
      targets: {
        chrome: 72
      }
    }]
  ]
}

@chopfitzroy
Copy link
Author

That worked perfectly, thank you!

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

No branches or pull requests

2 participants