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

Handle non-default exports with embroider #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mydea
Copy link

@mydea mydea commented Mar 8, 2021

For whatever reason, when running this with embroider & staticHelpers: true, helpers are compiled slightly different. They have no default export, but instead the module from require(name) contains the helper directly.

This fixes the resolver by adding the same check as exists in ember-resolver to just return the module directly in this case.

I have tested this in my app and it worked fine there.

Fixes #44

@rwjblue
Copy link
Contributor

rwjblue commented Mar 8, 2021

Can you share the "classic" vs Embroider transpilation output?

@mydea
Copy link
Author

mydea commented Mar 8, 2021

OK, here some steps:

Scenario 1: With ember-strict-resolver, embroider & staticHelpers: true

 resolve(fullName: string) {
    let moduleName = this.moduleNameForFullName(fullName);

    if (moduleName && this.has(moduleName)) {
      // eslint-disable-next-line
      let module = require(moduleName);

      if (!module?.default) {
        console.log('HAS NO DEFAULT!', fullName, module);
      }

      if (module && module.default) {
        module = module.default;
      }

      return module;
    }
    // miss
  }

Outputs for every helper, for example:

HAS NO DEFAULT?! helper:page-title ƒ (unknown)

Where the f (unknown) is really the default export itself from that helper.

Scenario 2: With ember-strict-resolver, embroider & staticHelpers: false

The same example outputs nothing without staticHelpers. To be honest I have no idea what exactly that does 😬

Scenario 3: With ember-resolver, embroider & staticHelpers: true

import Resolver from 'ember-resolver';
// @ts-ignore
import require from 'require';

export default class CustomResolver extends Resolver {
  _extractDefaultExport(normalizedModuleName: string) {
    let module = require(normalizedModuleName, null, null, true /* force sync */);

    if (!module?.default) {
      console.log('HAS NO DEFAULT?!', normalizedModuleName, module);
    }

    if (module && module['default']) {
      module = module['default'];
    }

    return module;
  }
}

Outputs the same as Scenario 1.

@mydea
Copy link
Author

mydea commented Mar 8, 2021

I have also tried to look at the generated output (for scenario 3, for what it's worth). And I noticed one small difference:

With staticHelpers: true

__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */
__webpack_require__.d(__webpack_exports__, 'default ', function () {
  return PageTitle;
});

With staticHelpers: false

__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */
__webpack_require__.d(__webpack_exports__, 'default', function () {
  return PageTitle;
});

Notice the space after default in the first example. No idea if that is "OK" or not... And also no idea why that would result in it just returning the default export directly instead of "wrapping" it.

@rwjblue
Copy link
Contributor

rwjblue commented Mar 8, 2021

yeah that seems like a bug

@rwjblue
Copy link
Contributor

rwjblue commented Mar 8, 2021

Can you open an issue on Embroider repo with that last snippet?

@mydea
Copy link
Author

mydea commented Mar 8, 2021

Cross reference: embroider-build/embroider#714

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

Successfully merging this pull request may close these issues.

Does not work with embroider & staticHelpers
2 participants