Skip to content
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 build step and TS support #243

Merged
merged 5 commits into from
Jan 4, 2019
Merged

Conversation

givanse
Copy link
Contributor

@givanse givanse commented Jan 2, 2019

README.md Outdated Show resolved Hide resolved
"description": "Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.",
"license": "MIT",
"engines": {
"node": ">=4"
},
"scripts": {
"prepublishOnly": "npm run build && npm run tests-only",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that the build step is ran before publishing a new version, in case a PR bypassed a red CI somehow.

"test": "npm run lint && npm run jscs && npm run tests-only",
"test-ci": "npm run pretest && npm run lint && npm run jscs && npm run tests-only-ci",
"test-ci": "npm run pretest && npm run build && npm run lint && npm run jscs && npm run tests-only-ci",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that the build step is ran before running the tests, in case there is a PR that didn't include the build outputs.

@mike-north

This comment has been minimized.

@givanse givanse force-pushed the add-build-step branch 2 times, most recently from 74fb3dd to 10cf35a Compare January 2, 2019 18:39
package.json Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/index.ts Outdated
@@ -144,7 +131,7 @@ function Pretender(/* routeMap1, routeMap2, ..., options*/) {
self.XMLHttpRequest = interceptor(ctx);

// polyfill fetch when xhr is ready
this._fetchProps = FakeFetch ? ['fetch', 'Headers', 'Request', 'Response'] : [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just added in a recently merged PR #235, so I'm surprised to see you changing it back. Have you studied the other PR, and verified that your change is correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial impression was that it was just a fallback put in place to avoid an error state. (had seem to be a reaction to the issue linked to in the original PR)

Now I see that I was wrong, it is about restoring the global space to its original state if needed. I would have used different syntax, but that is out of scope for this PR :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, I still don't see how would FakeFetch ever be undefined? Originally, the conversation around it started because of issue #221, but that one is easily explained. The html wasn't loading fetch.umd.js.

I don't think we need the fallback, but I'm going to leave that in place since it isn't immediately pertinent to the objective of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xg-wang can you comment here as to the correct behavior? I don't want anyone to roll back a legitimate bugfix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When appearsBrowserified is false, if the whatwg-fetch lib is not installed self.WHATWGFetch will be undefined. This always happens for non ember-cli-pretender users since they have to manual bundle the fetch lib.

In this pr the build output will be:

var FakeFetch = self.WHATWGFetch;
var Pretender = (function (self, RouteRecognizer, FakeXMLHttpRequest, FakeFetch) {
}(self, RouteRecognizer, FakeXMLHttpRequest, FakeFetch));

The self.WHATWGFetch is pulled in by https:/pretenderjs/pretender/blob/v2.1.1/karma.conf.js#L24

tsconfig.json Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
src/pretender.es.js Outdated Show resolved Hide resolved
@givanse
Copy link
Contributor Author

givanse commented Jan 2, 2019

@mike-north thanks for the feedback! I'll be addressing each comment.

rollup.config.js Outdated Show resolved Hide resolved
dist/pretender.js Outdated Show resolved Hide resolved
@mike-north mike-north self-assigned this Jan 3, 2019
@mike-north
Copy link
Member

mike-north commented Jan 3, 2019

We have to think about how to deal with the type information consumers see, and how this PR may affect/break things. Typically, after TS conversion is complete, hand-written declaration files (*.d.ts) are not necessary anymore (and are in fact a pain to maintain, since they can drift away from the real types in .ts code)

Ultimately (i.e., before we publish a new release) we want type information to come from the .ts source (via a generated .d.ts file) and not from my handwritten definitions (#223). Your TS configuration here is not strict enough to provide real type safety, so here's what I suggest

  • Leave the index.d.ts in place
  • Ensure that rollup generates no declaration files (if it does, we can simply remove them in a prepublish step)
  • Try your branch in a real project, and ensure that type information (via index.d.ts is available to consumers, exactly the same as it is in the latest stable release)

return module.default || module;
}

var appearsBrowserified = typeof self !== 'undefined' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't break anyones app. The build step produces two files with unique consumption mechanisms that immediately inform whether you are browserifying or not depending on which one you choose to use. So, Pretender doesn't need to figure that out at runtime anymore.

@givanse
Copy link
Contributor Author

givanse commented Jan 4, 2019

I linked it to ember-cli-mirage and ran all the tests (npm run test:all), all passed.

Adding the types blew out the diffs. If you prefer, I'm okay to go back to plan B (not generate the declaration file) and postpone the addition of the types for a subsequent PR.

@mike-north
Copy link
Member

@givanse Yes, let's ensure rollup does not generate declaration files, and stick with the handwritten types until we can get the src/index.ts code up to a level of type safety such that the emitted types will be suitable for consumer use

tsconfig.json Show resolved Hide resolved
Copy link
Member

@mike-north mike-north left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good to me now. @givanse we ready to merge?

@givanse
Copy link
Contributor Author

givanse commented Jan 4, 2019

Yeah, it is ready. Thanks for your help.

@mike-north mike-north merged commit 8a023ca into pretenderjs:master Jan 4, 2019
@givanse givanse deleted the add-build-step branch January 4, 2019 23:51
@cowboyd
Copy link

cowboyd commented Feb 5, 2019

This is excellent! Can we get a release based on this version? We're using Pretender in an Angular app and installing off of master fixes the manual module resolution problems that we were encountering 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants