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

Configurable shim module format - AMD or ES2015 #99

Closed
wants to merge 2 commits into from

Conversation

timiyay
Copy link

@timiyay timiyay commented Jun 26, 2019

This proof-of-concept PR suggests a solution to #98. It allows downstream apps to choose the module format for the ember-query shim added in #33.

This solves an issue for AMD-dependent systems like one of ours, which uses ember-cli/loader to set up a module alias. This was breaking down when receiving the shim in ES2015 format.

@timiyay
Copy link
Author

timiyay commented Jun 26, 2019

I don't have a complete understanding of the underlying Ember build systems, so I'm uncertain whether this solution will have unwanted side effects.

I did some testing against one of our apps, where an Ember client wraps a HTML5 game framework. The game code that relies on the AMD jQuery alias started working again, and none of our Ember code was adversely affected, even the components that use import $ from 'jquery'.

@timiyay
Copy link
Author

timiyay commented Jun 26, 2019

The Travis CI run failed due to an unrelated issue. The release-it dependency is failing to install due to the Node version.

@simonihmig
Copy link
Contributor

simonihmig commented Jun 26, 2019

Although I did that shim change back then, I see myself unable to make a final call here, as I am not sure we want to support this here.

My understanding is:

I wondered why the AMD shim actually worked with Ember, but it seems there is some explicit compatibility support in loader.js: https://github.com/ember-cli/loader.js/blob/master/lib/loader/loader.js#L120

Can we eventually rely on that feature, and just use an AMD shim?

Any thoughts @rwjblue?

The Travis CI run failed due to an unrelated issue. The release-it dependency is failing to install due to the Node version.

Indeed, also failing for dependabot updates. Seems that package did not really follow semver! I dropped node 6 support here: #100

@timiyay
Copy link
Author

timiyay commented Jun 26, 2019

This is great background, thanks @simonihmig, especially the info on how ember-cli/loader's makeDefaultExport function enables AMD compatibility.

Thanks for patching the Travis failure, this PR is all green now 🍏

I'll wait to hear from others, before digging further into Ember's vendor-shim functionality (I've not worked with it before).

@rwjblue
Copy link
Member

rwjblue commented Jun 27, 2019

Can we eventually rely on that feature, and just use an AMD shim?

No, we are trying to deprecate/remove makeDefaultExport over in ember-cli/loader.js#128 (though I dropped the ball, and it needs a kick start to push into motion again).

@rwjblue
Copy link
Member

rwjblue commented Jun 27, 2019

@timiyay - Can you explain more about your usage of loader.js? Are you manually doing let jQuery = require('jquery')? In general, I would have assumed folks were already doing import jQuery from 'jquery' which roughly transpiles to reading require('jquery').default which does work (in 0.5 and 0.6).

@timiyay
Copy link
Author

timiyay commented Jun 27, 2019

@rwjblue I'll give my best explanation, with the caveat that I'm working on a legacy system I don't 100% understand just yet.

We have a HTML5 game framework which uses AMD modules. These games are embedded within our client Ember apps. The Ember apps are responsible for loading the game manifests, and executing the relevant game module.

At this point, the game module takes over, and loads its modules via AMD.

Since the games are always served within in Ember app, the game framework uses ember-cli/loader as its AMD module loader, instead of RequireJS. We also recycle the Ember-provided jQuery and use it within the games.

We recently upgraded ember-jquery to 0.6.1, because we needed jQuery 3.4.1 for an iOS 10 bugfix. This caused our game AMD loading to hit an issue, when it attempts to require jquery, as it expects an AMD module but receives an object { default: 'jquery-module', __esModule: true }

It sounds likely that our legacy module system using ember-cli/loader will need a rework in the near future, but it's no mean feat, so we're looking for strategies to maintain compatibility for as long as possible.

@rwjblue
Copy link
Member

rwjblue commented Jun 27, 2019

I think you could probably fix the issue by defining your own jquery module in your app and app.import it. 🤔

@timiyay
Copy link
Author

timiyay commented Jun 27, 2019

@rwjblue Hey yeah this crossed my mind, but I'm yet to properly consider it.

To be clear, you're suggesting we could skip ember-jquery altogether, and consume jquery in our preferred fashion straight from npm?

Would this nerf Ember.$?

Copy link
Member

rwjblue commented Jun 28, 2019

No, I was more thinking that you could use @ember/jquery, but in your app have a file like:

// vendor/jquery/shim.js
define('jquery', [], function() {
  self.jQuery.default = self.jQuery;
  return self.jQuery;
});

Then the app.import that @ember/jquery is doing already will get your file instead of the shim in this repo, but we should also add a config flag here to just avoid the app.import (since we really don't want to depend on the merging + clobbering behavior here).

@timiyay
Copy link
Author

timiyay commented Jun 28, 2019

Ah brilliant! I didn't connect the dots that files in the vendor dir can be overridden in this way, like with other dirs.

This fixes our issue, and seems like a reasonable solution for us.

@timiyay
Copy link
Author

timiyay commented Jul 2, 2019

We've tested out @rwjblue's solution, and it works for us, and is nice and explicit.

I'm satisfied.

@timiyay timiyay closed this Jul 2, 2019
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.

3 participants