-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 AMD module #90
Build AMD module #90
Conversation
vp2177
commented
Dec 22, 2015
- wider compatibility
@@ -51,7 +51,7 @@ | |||
"underscore": "^1.8.3" | |||
}, | |||
"devDependencies": { | |||
"babel": "^5.8.21", | |||
"babel": "^5.8", |
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.
These are functionally equivalent, so this change is not necessary. To quote a popular Stack Overflow answer:
The caret, on the other hand, is more relaxed. It will update you to the most recent major version (the first number). ^1.2.3 will match any 1.x.x release including 1.3.0, but will hold off on 2.0.0.
Nitty gritty details at https://docs.npmjs.com/misc/semver#caret-ranges-1-2-3-0-2-5-0-0-4
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.
Also, it's actually preferred to include the patch version, since it indicates what the minimum patch version we require is.
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.
Doing an npm install
does result in different versions getting installed in these two scenarios, maybe it depends on the version of npm
used
Anyway, removed this change
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.
if another package requires a lower 5.8
than 5.8.21
, you should have two versions, since there's a version conflict. If both packages use ^
then it should auto-dedupe.
Can you elaborate on the purpose of this? There's no need for enzyme to produce a UMD module, since consumers can (and should) use their own pipeline to wrap it if they want AMD, or a browser global, or SystemJS - that's basically what browserify and webpack do. |
The purpose of this is, being able to use Enzyme in our tests, but I'm open to other suggestions. Our tests are run in a browser of choice using Karma, and libraries are loaded using RequireJS, which unfortunately only understands AMD (and UMD). |
Can you not use browserify or webpack to transform your test entry point (and all the things it requires) into AMD? |
Well I would prefer webpack as browserify needs a plugin to understand our existing modules (AMD), Making Enzyme build UMD instead of CommonJS modules seemed much easier. What is the downside of building in UMD format? |
It encourages legacy use of browser globals and/or AMD, for one. It also makes the resulting package larger, and can complicate things for those who are using a module pipeline. |
We are using a module pipeline (r.js). A horrible one but still. And both Browserify and Webpack are fine consuming UMD modules. We are stuck with AMD, and while I don't want to defend it, it is not any more legacy or reliant on browser globals ( |
What about just including a |
That would work just fine, but I have a strong philosophical objection to UMD. At the very least, if we do use a UMD, it should be one that does not expose a browser global. |
If it's a separate build file, we aren't encouraging it, and it doesn't increase our normally-required package size. It would, however, increase our compatibility with people still using AMD or script-based imports? |
That is very true, as a gitignored prepublish-initiated dist file, it would achieve those things without impacting other uses. |
Yes, sounds like a better idea than making everything UMD, depending on our sprint I can look into it on Monday. |
Not UMD as the build output is already in CommonJS Sinon is not straightforward to Webpack though, so this is not finished 🌱
"lint": "eslint src/**", | ||
"test": "mocha --compilers js:babel/register --recursive src/**/__tests__/*.js", | ||
"check": "npm run lint && npm run test:all", | ||
"build": "babel src --out-dir build", | ||
"build:amd": "webpack", |
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.
why do we need all of webpack in order to produce a umd build? we should be able to do that with browserify pretty trivially
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.
webpack and browserify seem somewhat comparable in my mind. Not sure it should matter much since its a dev dep...
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.
Either one is fine. If browserify is already used elsewhere in enzyme or is preferred, I can switch to it
Closing until further work is done in next sprint to actually get the build to succeed |
Any news about adding AMD/UMD support any time soon? |
@kuakman Unfortunately the code has non-top-level |
@vp2177 Thanks for the quick response. The other alternative instead of going full UMD, considering @ljharb concerns about exposing enzyme with global scope and not have to worry about top level require()'s, is going only AMD by using I could pull your fork and see if we can work together to get it working. Need to understand a little bit the code base which I'm kinda new and see how the modules are organized. Other than that, Distro folder looks good along with the clean task you did. |
Not sure I follow; would it be possible to use in order to use Enzyme with RequireJS? |
@soroushhakami Unfortunately, the build doesn't actually work, because |
@vp2177 Oh ok, I see. So using Enzyme in a RequireJS project now seems almost impossible, or is there some alternative route? |
@soroushhakami I am out of ideas, looks like it's not possible without some major changes to |
@vp2177 okay, thanks for letting me know! Hope the Enzyme team considers adding support for this. Alot of us are stuck in legacy projects, trying to migrate to React step by step, and being able to use Enzyme would help out alot. |
@vp2177 @soroushhakami I was able to get Enzyme to work with our legacy RequireJS project by using Browserify with the |
@jonmbake awesome 👍 Yeah a documentation PR would be fantastic, thanks! |
Do you think I should modify: https://github.com/airbnb/enzyme/blob/master/docs/guides/browserify.md? Or create a new a top level document possibly titled something like "Using Enzyme with Legacy Module Loaders"? |
Admittedly I haven't tried |
I created a gist describing how to build Enzyme for AMD or other legacy module loaders: https://gist.github.com/jonmbake/6362a9f54f57a4506057166aa5ec7027 |
The problem is not "conditional imports." A CommonJS
gets transpiled to:
The problem is with the Babel's
|
@jonmbake but in case of When I tried, the codebase also contained code that checks whether a |
Anybody recently tried latest enzyme for AMD, apparently with new Enzyme we need to configure adapter as well, I tried couple of options but none work for me :( If anyone has used recent version of Enzyme for AMD then can you please share steps to do this? @jonmbake any idea? Appreciate any help here. |
@nikhilexe This should work.
Might need to use the 15.0 adapter if still using react 15.X.
|
Thanks @jonmbake, this worked for me 👍, however I am facing another issue. With previous version of enzyme 2.9.1 I was getting following error whenever I used mount API
The error was pointing that I might be loading multiple react copies which could be the case as it looks like browserified has bundled react as well. I tried to exclude react using -x react but still the same issue. I thought that this could be issue with enzyme version as mentioned here and so I was upgrading the version but still facing the same issue with newer version. Am i missing something here, maybe I need to pass some different parameter to exclude react? |
@nikhilexe Right. I think you can get past that error by including React in the UMD bundle:
Usage:
Or you can "define"
and then "require" them independently:
|
Thanks @jonmbake, If I understand it correctly then I don't need to do any react import in my test and use UMD react as you mentioned. I guess this could be problematic for me as we are using typescript for writing test and it needs d.ts to understand this, I am using |
Why would you need TypeScript after bundling? The bundle would have all the type information stripped, usually. |
Ok, this i was not aware of but after following steps which @jonmbake has shared it is still not working for me, I see react is still loaded, maybe product is doing so, let me debug this. Interestingly my shallow rendering is working with enzyme, its only mount API which is giving this error |
Thanks @jonmbake and @ljharb, I can confirm that with above workaround things are working fine now except I need to add one more exclude -x react-dom but I am little confused with the behavior and need your help to understand why this is happening. Can you please help me to answer the following question:
|
@nikhilexe I created a project to build the Enzyme UMD here: https://github.com/jonmbake/enzyme-umd.... Didn't realize the module built (https://github.com/jonmbake/enzyme-umd/blob/master/build/enzyme-react.min.js) was so large (> 1MB)... It is basically concatenating all the Enzyme/React dependencies so I guess that makes sense. Anyway, there are unit tests here: https://github.com/jonmbake/enzyme-umd/blob/master/test/amd-test.js so feel free to add a few if something with |
Thanks @jonmbake, with your enzyme-react UMD I am not facing any more issue with mount API, but my question was that why without your UMD change shallow rendering is working fine but full DOM rendering is failing. Interestingly I have one case also where mount API is working even without your change. |
@nikhilexe According to the Enzyme Readme:
The UMD module needs to include |
(enzyme-adapter-react-16 now includes react-test-renderer as a normal dep, not a dev dep, fwiw) |
@ljharb Thanks for the info. I updated the fix: jonmbake/enzyme-umd@412963f |