-
Notifications
You must be signed in to change notification settings - Fork 2k
ESM build for the browser #2277
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
40aa07b
Export es build that enforces extensions on imports
lukejacksonn 8e7cc02
Checks typeof process in instanceOf before using it
lukejacksonn d27e528
Vendors iterall to make the code dependency free
lukejacksonn 316a69e
Implements export statement rewriting
lukejacksonn d7210ec
Update index import paths to be more explicit
lukejacksonn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// @flow strict | ||
|
||
const SYMBOL = typeof Symbol === 'function' ? Symbol : undefined; | ||
const SYMBOL_ITERATOR = SYMBOL && SYMBOL.iterator; | ||
const SYMBOL_ASYNC_ITERATOR = SYMBOL && SYMBOL.asyncIterator; | ||
|
||
function AsyncFromSyncIterator(iterator) { | ||
this._i = iterator; | ||
} | ||
|
||
AsyncFromSyncIterator.prototype[$$asyncIterator] = function() { | ||
return this; | ||
}; | ||
|
||
AsyncFromSyncIterator.prototype.next = function() { | ||
const step = this._i.next(); | ||
return Promise.resolve(step.value).then(value => ({ | ||
value, | ||
done: step.done, | ||
})); | ||
}; | ||
|
||
function ArrayLikeIterator(obj) { | ||
this._o = obj; | ||
this._i = 0; | ||
} | ||
|
||
ArrayLikeIterator.prototype[$$iterator] = function() { | ||
return this; | ||
}; | ||
|
||
ArrayLikeIterator.prototype.next = function() { | ||
if (this._o === undefined || this._i >= this._o.length) { | ||
this._o = undefined; | ||
return { value: undefined, done: true }; | ||
} | ||
return { value: this._o[this._i++], done: false }; | ||
}; | ||
|
||
export const $$iterator = SYMBOL_ITERATOR || '@@iterator'; | ||
|
||
export function isIterable(obj) { | ||
return Boolean(getIteratorMethod(obj)); | ||
} | ||
|
||
export function isArrayLike(obj) { | ||
const length = obj != null && obj.length; | ||
return typeof length === 'number' && length >= 0 && length % 1 === 0; | ||
} | ||
|
||
export function isCollection(obj) { | ||
return Object(obj) === obj && (isArrayLike(obj) || isIterable(obj)); | ||
} | ||
|
||
export function getIterator(iterable) { | ||
const method = getIteratorMethod(iterable); | ||
if (method) { | ||
return method.call(iterable); | ||
} | ||
} | ||
|
||
export function getIteratorMethod(iterable) { | ||
if (iterable != null) { | ||
const method = | ||
(SYMBOL_ITERATOR && iterable[SYMBOL_ITERATOR]) || iterable['@@iterator']; | ||
if (typeof method === 'function') { | ||
return method; | ||
} | ||
} | ||
} | ||
|
||
export function createIterator(collection) { | ||
if (collection != null) { | ||
const iterator = getIterator(collection); | ||
if (iterator) { | ||
return iterator; | ||
} | ||
if (isArrayLike(collection)) { | ||
return new ArrayLikeIterator(collection); | ||
} | ||
} | ||
} | ||
|
||
export function forEach(collection, callback, thisArg) { | ||
if (collection != null) { | ||
if (typeof collection.forEach === 'function') { | ||
return collection.forEach(callback, thisArg); | ||
} | ||
let i = 0; | ||
const iterator = getIterator(collection); | ||
if (iterator) { | ||
let step; | ||
while (!(step = iterator.next()).done) { | ||
callback.call(thisArg, step.value, i++, collection); | ||
if (i > 9999999) { | ||
throw new TypeError('Near-infinite iteration.'); | ||
} | ||
} | ||
} else if (isArrayLike(collection)) { | ||
for (; i < collection.length; i++) { | ||
if (Object.prototype.hasOwnProperty.call(collection, i)) { | ||
callback.call(thisArg, collection[i], i, collection); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const $$asyncIterator = SYMBOL_ASYNC_ITERATOR || '@@asyncIterator'; | ||
|
||
export const isAsyncIterable = obj => Boolean(getAsyncIteratorMethod(obj)); | ||
|
||
export const getAsyncIterator = asyncIterable => { | ||
const method = getAsyncIteratorMethod(asyncIterable); | ||
if (method) { | ||
return method.call(asyncIterable); | ||
} | ||
}; | ||
|
||
export const getAsyncIteratorMethod = asyncIterable => { | ||
if (asyncIterable != null) { | ||
const method = | ||
(SYMBOL_ASYNC_ITERATOR && asyncIterable[SYMBOL_ASYNC_ITERATOR]) || | ||
asyncIterable['@@asyncIterator']; | ||
if (typeof method === 'function') { | ||
return method; | ||
} | ||
} | ||
}; | ||
|
||
export const createAsyncIterator = source => { | ||
if (source != null) { | ||
const asyncIterator = getAsyncIterator(source); | ||
if (asyncIterator) { | ||
return asyncIterator; | ||
} | ||
const iterator = createIterator(source); | ||
if (iterator) { | ||
return new AsyncFromSyncIterator(iterator); | ||
} | ||
} | ||
}; | ||
|
||
export const forAwaitEach = (source, callback, thisArg) => { | ||
const asyncIterator = createAsyncIterator(source); | ||
if (asyncIterator) { | ||
let i = 0; | ||
return new Promise((resolve, reject) => { | ||
function next() { | ||
asyncIterator | ||
.next() | ||
.then(step => { | ||
if (!step.done) { | ||
Promise.resolve(callback.call(thisArg, step.value, i++, source)) | ||
.then(next) | ||
.catch(reject); | ||
} else { | ||
resolve(); | ||
} | ||
return null; | ||
}) | ||
.catch(reject); | ||
return null; | ||
} | ||
next(); | ||
}); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@lukejacksonn Can you please extract it into separate PR?
Also, please test that Webpack is able to recognize this pattern and switch to the production code.
Is it the last change we need to support ESM?
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.
Yeh I can do that.. this is the last source modification required for ESM support (assuming that the mjs build now adds extensions to imports paths).
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.
Great π Thanks a lot!
You can see preview build here:
https://github.com/graphql/graphql-js/blob/npm/graphql.mjs
If you have time to try it here is the instruction:
https://github.com/graphql/graphql-js#want-to-ride-the-bleeding-edge
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.
Hi @lukejacksonn,
Did you have a time to test this change with webpack?
I planning to release new RC so it would be great to also include 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.
Hey @IvanGoncharov sorry this task completely slipped my mind. I have made a PR #2409 but am not so sure about what webpack specific config needs to be checked