-
Notifications
You must be signed in to change notification settings - Fork 16
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
Output a UMD library instead of the default var library #155
Conversation
Actually, because this library functions similar, I found a extension to this pattern here in readthedocs/doc-diff#9 That PR creates two libraries for output, one that automatically executes (for browser usage) and one that doesn't (for usage as a Node library). I found that the logic here and in doc-diff to detect this doesn't actually work for Webpack and especially ES modules. The logic in both is This PR can be a standalone change, but there are more changes to make a library that doesn't automatically execute. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand this change, but I did test this manually in a browser context and there was no issues with it. There's no significant change in filesize either.
For a little more context here, the default for Webpack is It seems |
Is there anything blocking merging this PR here? |
There shouldn't be, other than a rebase. I wasn't sure exactly how to verify this doesn't break anything, so would probably want to leave merge and release up to @davidfischer. I'll rebase this at least for now. |
This is another change that will require some testing, but I'm mostly certain it does change anything about the current usage. The default library target for Webpack is "var", which defines a global variable, named `window.ethicalads` in this case. It, however, does not export anything as a normal Node library would, so is not usable from NPM directly. This changes the library type to UMD, which in the Webpack config: > "umd" ... exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable So, this seems to me to be only additive to the current configuration (global variable). It's very worth some testing though. This would also make it possible to package on NPM and install as a library, though we'll need to tune up the automatic execution code as we found this week on the RTD JS library work.
c91411d
to
0b04f6f
Compare
I reviewed it and it is good to merge. I'll aim to merge and release it this week. |
I'll be away next week, in case this leaks into next week, but users should be able to use the library through a |
I'll run a quick check but I want to limit the different ways we need to support things as much as possible so not sure we'll advertise this. |
Besides this change, in my PR I had to remove this chunk of code because it seems the auto-detection of being imported as a module does not work fine: https://github.com/readthedocs/ethical-ad-client/pull/148/files#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346L742 What's the best way to test/QA that code to find out how to make it work in a reliable way? |
I forget where I noted it, but that code will never work in a reliable way as there isn't really a good pattern anymore for detecting when a library is being imported vs requested via a If we want a library to both automatically initialize and never automatically initialize, depending on how it is used, I've been outputting two libraries. |
I found these two SO questions and I understand, as you said, it's not possible:
Can you expand a little more on this? What are the required changes? How the addons should import this module and how publishers should import this module (note that we cannot change anything on their side, I guess)? |
from our Slack conversation,
@agjohnson would that be possible? |
Install all the requirements and make required adjustments to be able to use `ethical-ad-client` as a js library. See readthedocs/ethical-ad-client#155
Install all the requirements and make required adjustments to be able to use `ethical-ad-client` as a js library. See readthedocs/ethical-ad-client#155
I was able to use this PR as-is. So, I think we can merge it and it will work as we expect. There are some notes that I want to mention here, tho:
I made this changes in readthedocs/addons#81 That said, I think we are 💯 to move forward and merge this PR. |
Webpack would need configured to output two bundles. One bundle has the code for automatic loading of placements, and is the bundle that EA would serve for pageviews outside Read the Docs. The other bundle looks similar, but does not make the same autoload method/function call on load. The reason for outputting two bundles is so that the EA client can perform all of the bundling operations, like compiling its SASS sources, and the addons client doesn't need to replicate this (or anything else the EA client webpack configuration is doing).
It would take some Webpack hacks on the addons library bundles, but a global might be possible in combination with Webpack We can still proceed with this PR though, all of these decisions still require the change in this PR either way. |
- remove dependency - don't require UDM library changes/complexity - load the dependency only if EthicalAd is enabled for the project - protect us from blocking `readthedocs-addons.js` by AdBlocker I decided to go this way because it simplifies things a lot and also it uses the standard/supported way of using EthicaAd client. * Closes #6 * Closes #81 * Closes readthedocs/ethical-ad-client#155
I feel like this should probably still move forward, it's a small change. Ultimately, we should be installing addons dependencies through packaging, to ensure backwards compatibility between addons and the EA client. This change is required for that, as is a change to avoid auto injection of placements. |
This is another change that will require some testing, but I'm mostly
certain it does change anything about the current usage.
The default library target for Webpack is "var", which defines a global
variable, named
window.ethicalads
in this case. It, however, does notexport anything as a normal Node library would, so is not usable from
NPM directly.
This changes the library type to UMD, which in the Webpack config:
So, this seems to me to be only additive to the current configuration (global
variable). It's very worth some testing though.
This would also make it possible to package on NPM and install as a
library, though we'll need to tune up the automatic execution code as we
found this week on the RTD JS library work.
Requires #154