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

build: use Ably as UMD lib name #1558

Merged
merged 3 commits into from
Feb 2, 2024
Merged

build: use Ably as UMD lib name #1558

merged 3 commits into from
Feb 2, 2024

Conversation

owenpearson
Copy link
Member

Fixes a regression in which, when using UMD without commonjs/amd, the esbuild UMD wrapper would add each named export of ably to the global namespace rather than adding it in an object called Ably (as it did in v1).

@github-actions github-actions bot temporarily deployed to staging/pull/1558/features December 19, 2023 16:10 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1558/bundle-report December 19, 2023 16:11 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1558/typedoc December 19, 2023 16:11 Inactive
@VeskeR VeskeR self-requested a review January 26, 2024 13:28
Copy link
Contributor

@VeskeR VeskeR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esbuild-plugin-umd-wrapper outputs different UMD wrapper code for AMD resolution when libraryName option is provided compared to what webpack does on our v1 branch. That causes all our browser tests to fail with errors like: "TypeError: Cannot read properties of undefined (reading 'Ably')"

webpack from v1 branch (build/ably.js):

...
else if(typeof define === 'function' && define.amd)
	define([], factory);
...

esbuild-plugin-umd-wrapper with libraryName: 'Ably' from this PR (build/ably.js):

...
} else if ("function" == typeof define && define.amd) {
	define("Ably", [], f);
}
...

Note the difference for AMD resolution (define function call):
Webpack defines "anonymous" module, just define([], factory);
esbuild-plugin-umd-wrapper plugin defines a named module define("Ably", [], f);

This causes all browser tests to fail.
I'm not sure how exactly named modules in AMD behave, and I couldn't find any meaningful explanation of this feature. Seems like it's not recommended to use named modules anyway, and we should stick to how it was done in v1 to avoid problems.

There doesn't seem to be an option to change this behavior in esbuild-plugin-umd-wrapper.

@owenpearson WDYT?

@VeskeR
Copy link
Contributor

VeskeR commented Feb 1, 2024

I've created a fork of an esbuild-plugin-umd-wrapper plugin that would allow us to not name our AMD module, thus preserving the behavior we had on v1 branch using webpack.

To use it, we need to change dependency line in package.json to:

"esbuild-plugin-umd-wrapper": "ably-forks/esbuild-plugin-umd-wrapper#1.0.7-optional-amd-named-module",

and this line to:

plugins: [umdWrapper.default({ libraryName: 'Ably', amdNamedModule: false })],

@owenpearson let me know if you want me to push those changes to this PR (and also resolve conflicts with integration/v2 branch, since it got updated a few times since this PR was opened)

@owenpearson
Copy link
Member Author

@VeskeR, nice one, thanks for not only identifying the issue but also creating the fork to fix it! If you're happy to update the PR and resolve conflicts too that would be amazing 🙂

@github-actions github-actions bot temporarily deployed to staging/pull/1558/features February 1, 2024 23:42 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1558/typedoc February 1, 2024 23:43 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1558/bundle-report February 1, 2024 23:43 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Fix importing the library via script tag adds each named export of ably to the global namespace on v2 branch
3 participants