-
Notifications
You must be signed in to change notification settings - Fork 89
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
Use iife for browser and cjs for node #470
Comments
Sorry for hijacking your issue, but what a beautiful and extensive research you've done here @bidoubiwa, very detailed! One thing i want to add is that the Rollup setup you initially had, just when you added the axios depdency and AMD and Browser failed, is suitable as well. As Webpack writes on their website with an library example that uses the loadash dependency:
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> In my opinion, you can now start arguing when it makes sense to use IIFE which has all dependencies included, and when some other library is small enough, that it may only require 2-3 additional dependencies to run in the browser, to instruct the consumer to add these themself :) Again, big up for your work and thanks a lot! |
UMD bundle
In this issue I want to show you why the UMD is not working in its current state (466), why we will first go through a step where
iife
is used for browser andcjs
for node, and later, a step whereUMD
works.In the first step, we create a simple ES code that will be transpiled into UMD javascript. We follow the steps given here.
Base
First step, creation of ES script.
code
Then, we create a rollup configuration to transpile the script in UMD.
rollup
UMD output:
[explanation on how UMD works](https://riptutorial.com/javascript/example/16339/universal-module-definition--umd-
Explaination on
global
andfactory
:Tried in the following applications:
build: ✅
node: ✅
browser : ✅
AMD: ✅
Adding Axios
Now, we try to add
axios
to our ES script.code
This requires a more complete rollup configuration.
rollup:
UMD output:
)
build: ✅
node: ✅
browser : ❌
used in
axios.get
. Meaning get isundefined
.AMD: ❌
Cannot fetch
axios
. Try to import file that does not exist.Solutions 1
Step 1 : node resolve plugin
Add @rollup/plugin-node-resolve: A Rollup plugin which locates modules using the Node resolution algorithm, for using third party modules in node_modules.
Rollup config:
Build error ❌
Explaination for the above link:
Apparently
namedExports
is not necessary anymore.Step 2 : commonjs plugin
Add
@rollup/plugin-commonjs
: 🍣 A Rollup plugin to convert CommonJS modules to ES6, so they can be included in a Rollup bundle.Build error ❌
Issues mentionned:
Step 3 : json plugin
Add
@rollup/plugin-json
: A Rollup plugin which Converts .json files to ES6 modules.build: ✅
Warning:
output: huge file
node: ❌
browser: ❌
amd: ❌
Step 4: add resolve options
Following the first solution that was suggested here.
We add the options to the node-resolve plugin.
mainFields: ['jsnext', 'main']
browser
config:
build: ✅
output UMD: huge file
node: ❌
Browser: ✅
AMD: ✅
Following THIS stackoverflow post.
Possible solutions to this problem:
cjs
that will be main in the package.json and one that will beiife
that will be in the keybrowser
of the package.json.If we opt for this solution users will have to :
instead of
This file will be a little heavy as it is bundled with all its dependencies.
fetch
as it seems that it makes theumd
possible.Conclusion
Fast solution
Replace package.json
main
key withcjs
bundle and changebrowser
key withiife
bundle.Change
umd
rollup config with the following:Best solution:
Remove
axios
and replace withfetch-polyfill
androllup-plugin-node-polyfills
This will resolve the following issues:
Other related issues and stackoverflow posts:
Problem 2: meilisearch naming
To change the name it is done here:
the pascalCase transforms
meilisearch
intoMeilisearch
. The pascalCase does not work as intented as the pkg.name is retrieved frompackage.json
and ismeilisearch
. I suggest we change it like this:as this will impact this the
umd
or soon to beiife
bundle.See
name
key inoutput
key.The text was updated successfully, but these errors were encountered: