A life beyond browserify (and potentially CommonJS)#46
Conversation
|
That makes a lot of sense. I'm glad we are opening up our doors to all bundlers.
Maybe it's just me. |
ec15b75 to
c00c8a0
Compare
|
@5punk Changes up integrating the detectOS changes you made (thanks again for this). I did refactor the regexes to match the previous syntax but agree that your code with With regards to your other comments:
Interesting, and good point we should probably do this. Would like to know what false positives people are hitting.
I have to stop myself from writing ES6 every time I touch this package as I expect it should probably still work with older versions of IE (there is a rule in there for detecting IE7) and I expect older versions of webkit (yes, pre blink) that exist on Android devices don't support newer JS either :(
Tell me more - you have my interest :) |
| function detectOS(userAgentString) { | ||
| var rules = getOperatingSystemRules(); | ||
| var detected = rules.filter(function (os) { | ||
| return os.rule && os.rule.test(userAgentString); |
There was a problem hiding this comment.
Slightly shorter form of what you had previously, but pretty much the same.
| return isNode ? { | ||
| name: 'node', | ||
| version: process.version.slice(1), | ||
| os: require('os').type().toLowerCase() |
There was a problem hiding this comment.
For the sake of consistency, I return something useful for node.
| return detected; | ||
| } | ||
|
|
||
| function getBrowserRules() { |
There was a problem hiding this comment.
A function for both getBrowserRules and getOperatingSystemRules mainly because I can see this translating well to a static method on a class at some point in the future (when we can have all the nice ES6 things).
|
What about updated TypeScript type definitions? I just updated to v2.0.0, but quickly realized that there is no corresponding |
|
@marcelgood Sure thing. I've tracked back the original authorship of the Just wondering if you folks might be able to work together to coordinate an update to that repo? I honestly didn't know definitions for |
|
@marcelgood @DamonOehlman Typings on @types/detect-browser have been updated to v2.0 API. |
|
@tiagoefmoraes Awesome - thanks heaps! |
When I first wrote
detect-browserit was really just to meet my own needs for browser detection in a simple app I wrote. That app usedbrowserifyand worked well. However, as more module loaders have hit the scene and we have ES6 modules available to us now I felt it was important to consider to updatedetect-browser(which has also been catalysed by others - see #23 /cc @ashubham).There are few things that the current implementation of
detect-browserdoesn't do quite right:It is implemented in a very
browserifyspecific way (using thebrowsertag in thepackage.jsonfile to specify where the browser specific logic lives). This really isn't necessary as we can make some determinations at runtime as to whether we are running in a node or browser environment through the existence of thenavigatorandprocessconstructors (though things likejsdommight have trouble).The detection logic occurs as the module is loaded. While this is quite convenient, it is generally bad practice and with ES6 modules have a different module resolution order (due to static analysis) to node modules we are likely to see this cause problems.
For these reasons I'm suggesting a refactoring of the files, and also of the API. The changes to the file structure can be seen here, and a simple code example of the API is shown below (equivalent to the example in the README):
While the example above uses some ES6 sure, the module itself is still written in pure ES6 so browserify can still be used to wrap the module up and have it run within an app that needs to target IE (and other older browsers).
I'd like to get this PR across the line before proceeding on implementing other features such as platform / os detection which have been discussed in issues #44 and #45.
@5punk @ashubham @tomekwi General thoughts?