-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
add an es-modules build (referenced with pkg.module) #1027
Conversation
this output file (mobx.module.js) is built with the ES2015 typescript target, so no ES2015 features get transpiled, and es module exports are preserved. this allows consumers of mobx to take advantage of tree-shaking bundlers such as rollupjs and webpack 2. I was considering using the `.mjs` extension, as that looks like it will be the most likely extension for es modules, but as it's not supported in node at all at the moment, I decided against it
module entry point should point to a ES2015 bundle. That's the standard practise if you look at library like redux for example. |
- this isn't actually necessary, it's an artifact from when I was putting the build script together in .ts before converting it back to .js
@rossipedia I tried it in node.js with babel and it failed with this:
This looks like a babel bug because doing class StubArray {
}
StubArray.prototype = []; works in a browser/node.js. Only babel fails on this. We'll need to resolve this before merging because most people are transpiling with Babel and all of them could get their builds broken IMHO. |
@rossipedia hmm assiging to a class prototype is actually illegal per ES6 spec in strict mode. I guess we'll have to fix it here. |
…ct.getOwnPropertyNames instead
what babel configuration are you using for this test? I couldn't repro using |
- babel doesn't like it when we set .prototype - es2015 spec says Array is extendable
Ahh ok, that's what the |
This reverts commit 99fd2fa.
- doesn't assign to StubArray.prototype anymore - polyfill from MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
I was using babel-preset-env. I think es2015 doesn't have the use-strict babel plugin and that is why it's working. |
I've tested a couple webpack+mobx apps against this branch (as well as with Not sure how much of a blocker that code coverage check is, but it's understandable since in most current environments |
@rossipedia Mobx is supposed to be usable from IE9 and up. Since IE10 and IE9 doesn't have |
@capaj yeah I think that's probably a good way to go. something like this: diff --git a/src/types/observablearray.ts b/src/types/observablearray.ts
index 9b53ea3..44d04bb 100644
--- a/src/types/observablearray.ts
+++ b/src/types/observablearray.ts
@@ -75,13 +75,19 @@ export interface IArrayWillSplice<T> {
let OBSERVABLE_ARRAY_BUFFER_SIZE = 0;
// Typescript workaround to make sure ObservableArray extends Array
-export class StubArray {
+export class StubArray { }
+function inherit(ctor, proto) {
+ if (typeof Object["setPrototypeOf"] !== "undefined") {
+ Object["setPrototypeOf"](ctor.prototype, proto);
+ } else if (typeof ctor.prototype.__proto__ !== "undefined") {
+ ctor.prototype.__proto__ = proto;
+ } else {
+ ctor["prototype"] = proto;
+ }
}
-const setProto: (o, p) => void = typeof Object["setPrototypeOf"] !== "undefined"
- ? Object["setPrototypeOf"]
- : (obj, proto) => obj.__proto__ = proto
- ;
-setProto(StubArray.prototype, Array.prototype);
+inherit(StubArray, Array.prototype);
+
+ I only tested this in the IE9 emulation mode in IE11 (which should be a fairly accurate emulation), and it seemed to work fine. |
@rossipedia yeah emulation is definitely good enough in IE. Fixed the problem I've had before. Great work. Merging. |
This output file (mobx.module.js) is built with the ES2015 typescript target, so no ES2015 features get transpiled, and es module exports are preserved. This allows consumers of mobx to take advantage of
tree-shaking bundlers such as rollupjs and webpack 2.
I was considering using the
.mjs
extension, as that looks like it will be the most likely extension for es modules, but as it's not supported in node at all at the moment, I decided against it.Point of discussion:
Should es2015+ features still be transpiled?
I'm not sure what the standard practice here is. Webpack usage typically avoids processing code from
node_modules
, so a consumer trying to include themobx
module version would have to be made aware that they need to include mobx in their transpilation process if they're targeting ES5. However, from what I've seen rollup configs usually include thenode-resolve
plugin before things like babel / typescript, so the module code would generally be included in that transpilation process as well.