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

Update README.md #35

Closed
wants to merge 1 commit into from
Closed

Update README.md #35

wants to merge 1 commit into from

Conversation

dizel3d
Copy link

@dizel3d dizel3d commented Nov 29, 2016

Fixed typemoq.js URL.
Removed unnecessary tsconfig.json instruction.

Fixed typemoq.js URL.
Removed unnecessary tsconfig.json instruction.
@florinn
Copy link
Owner

florinn commented Nov 29, 2016

Thanks. About the "module": "umd" config requirement, it must be set that way to be able to use import * as TypeMoq from "typemoq" in the test script and then load the script in the browser without getting the error 'require' is undefined.

@dizel3d
Copy link
Author

dizel3d commented Nov 29, 2016

It means that smth is wrong with your build. In which case do you get the error?

@dizel3d
Copy link
Author

dizel3d commented Nov 29, 2016

As for me it looks good. https://jsfiddle.net/dizel3d/m113qxbp/
image

@florinn
Copy link
Owner

florinn commented Nov 29, 2016

Afaik the 'require' is undefined is due to the import form import * as TypeMoq from "typemoq" and not to the lodash dependency.

So for module None there's no loading error but a compile error due to the import form.
For AMD, CommonJS, System, ES2015, no compile error but a loading error.

Only UMD works both at compile and loading. I guess it makes some sense if you think about it.

@dizel3d
Copy link
Author

dizel3d commented Nov 29, 2016

Why do you do import if you use typemoq as global? It's incorrect. Just use global variable TypeMoq.

@dizel3d
Copy link
Author

dizel3d commented Nov 29, 2016

typemoq is UMD. It means you have 3 ways to use it:

  1. None (global)
<script src="lodash"></script>
<script src="typemoq"></script>
<script>
  TypeMoq.Mock.ofInstance(...)...
</script>
  1. AMD
<script src="requirejs"></script>
<script>
  requirejs(['typemoq'], TypeMoq => {
    TypeMoq.Mock.ofInstance(...)...
  });
  // or
  requirejs(['myTest']);
</script>

where module myTest is js-file built from ts-file

import * as TypeMoq from "typemoq"
TypeMoq.Mock.ofInstance(...)...
  1. CommonJS
import * as TypeMoq from "typemoq"
TypeMoq.Mock.ofInstance(...)...

@florinn
Copy link
Owner

florinn commented Nov 29, 2016

Yes, the right way to use it in a browser would be without the import form, but I don't think it's possible, at least for the moment.

Solving #25 would require TS to allow commonjs module concatenation and also to bundle up the type definitions.
Atm none of these is possible:
microsoft/TypeScript#5090
microsoft/TypeScript#4433

So I used rollup for module concatenation to umd and dts-bundle for type def concat.
The ouput from dts-bundle looks like this:

declare module 'typemoq' {
...

which is usable only with the import form

/// <reference path="./scripts/typemoq.d.ts" />
import * as TypeMoq from "typemoq";

To be able to use typemoq in the browser without the import form, bundle-dts had to be able to output smth like:

declare namespace TypeMoq {
... 

@dizel3d
Copy link
Author

dizel3d commented Nov 29, 2016

bundle-dts is not what you need, it solves CommonJS bundling problem. I offer you another trick, that I use today:

  1. build the library with "module": "commonjs"
  2. remove import statements from d.ts files, then concatenate them.
  3. add export as namespace TypeMoq at the end.

@florinn
Copy link
Owner

florinn commented Nov 29, 2016

Thanks for the tip, I'm going to try it when I have some time.
Btw, resorting to tricks for bundling type defs looks like a shoddy workaround.
I guess it would be a good idea to raise an issue about this limitation with TS.

In the meantime, a PR (this one or a new one) with the proposed changes to typemoq would be welcomed.

@dizel3d
Copy link
Author

dizel3d commented Nov 29, 2016

Yes, you're right. I think, global module is a rare use-case. Therefore don't waste your time to fix it. :-)

@florinn
Copy link
Owner

florinn commented Nov 29, 2016

It's not broken, just limited to UMD for the global module :)

I've fixed the broken url with this commit 730646c Closing this.

@florinn florinn closed this Nov 29, 2016
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.

2 participants