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 UMD module support Add Matrix property #565

Merged
merged 3 commits into from
Jan 12, 2017
Merged

Conversation

albohlabs
Copy link

Fix for #564

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 86.531% when pulling 1f2cafe on albohlabs:master into 4770c1e on svgdotjs:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 86.586% when pulling 888680c on albohlabs:master into 4770c1e on svgdotjs:master.

@Fuzzyma
Copy link
Member

Fuzzyma commented Jan 8, 2017

@rdfriedl can you review this please? :)

@hzrd149
Copy link
Contributor

hzrd149 commented Jan 8, 2017

while it dose not completely fix the bug, it dose highlights a big problem with they typings.
and that is

// while this works
var matrix: svgjs.Matrix = new SVG.Matrix();
// this dose not
var matrix: svgjs.Matrix = new SVG.Matrix(1,0,0,1,0,0);

i think the typings are going to need a little bit more work to fix and update them before they match.

also its partly my fault that this bug is here, when i was reviewing the code i found two typos here and here

export as namespace svgjs;

declare var svgjs: svgjs.Library;

Copy link
Contributor

Choose a reason for hiding this comment

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

could you explain what this code dose?
because when i test the typings with this code it just says that the library dose not have a default export.

Copy link
Author

@albohlabs albohlabs Jan 9, 2017

Choose a reason for hiding this comment

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

The reason for that is that svg.js does not contain a default export. As a result we need to import the entire module content as an object, which is the corresponding es6 syntax. [1]

could you explain what this code dose?

For clarification:

… anywhere the module keyword was used when declaring an internal module, the namespace keyword can and should be used instead. [2]

The export as namespace makes the svgjs namespace available globally if you use the /// <reference directive. [3]

The rest of the code is doing the same as before.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Description
[2] https://www.typescriptlang.org/docs/handbook/namespaces.html
[3] microsoft/TypeScript#7125

Copy link
Contributor

@hzrd149 hzrd149 Jan 9, 2017

Choose a reason for hiding this comment

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

i did some testing with importing the library and it looks like

import SVG from 'svg.js'

and

import * as SVG from 'svg.js'

returns the default SVG() function.
Its also possible to do

import {Element} from 'svg.js'

with that in mind I tested the old typings file and the one here and this is what i got.

The original
this worked fine

import SVG from 'svg.js'
var draw: svgjs.Doc = new SVG.Doc();

this dose not, because the original typings only exports svgjs.Library

import {Doc} from 'svg.js'
var draw: svgjs.Doc = new Doc();

this dose not work.
it makes SVG a object with the default property set to the default SVG() function

import * as SVG from 'svg.js'
var draw: svgjs.Doc = new SVG.Doc();

With your version
this dose not work, it says that there is no default export

import SVG from 'svg.js'
var draw: svgjs.Doc = new SVG.Doc();

but this works fine.
except for the svgjs.Doc, it says that Namespace 'svgjs' has no exported member 'Doc'

import {Doc} from 'svg.js'
var draw: svgjs.Doc = new Doc();

also this works.
but has the same problem with the svgjs namespace

import * as SVG from 'svg.js'
var draw: svgjs.Doc = new Doc();

Conclusion
it looks like your version works a little better then the original.
we would just have to fix the default export and the namespace error.

Copy link
Author

Choose a reason for hiding this comment

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

Great. 🍻

we would just have to fix the default export and the namespace error.

Like you already said in the comment bellow. I will try to do some work on this issue.

export interface Matrix {
(source: any): Matrix;
new(source: any): Matrix;
Copy link
Contributor

Choose a reason for hiding this comment

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

this makes sense though i think it highlights an even bigger problem with the typings.

when i wrote the typings i forgot to supply the arguments to the methods on the library
what i mean is, this works

var matrix: svgjs.Matrix = new SVG.Matrix();

but this dose not

var element: svgjs.Element = new SVG.Element();
var matrix: svgjs.Matrix = new SVG.Matrix(element);

this is a mistake on my end since i set the Matrix as a function on the Library and not a property that holds a reference to the Matrix class
a few examples, here and here
but i remembered to include in other places, like here

of course this means that we would have to run over the whole library and fix the parts like this

interface Library { Number(): void; }

and change then to something like this

interface Library { Number: Number; }

and then fix this

export interface Number{
    // change this
    (value: any, unit?: any): Number;
    // to this, that way SVG.Number can only be called with the "new"
    new (value: any, unit?: any): Number;
}

so while this dose fix it for the Matrix class i think we will end up having to go over the entire file and fix each class

Copy link
Author

Choose a reason for hiding this comment

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

This is a good starting point to fix the typing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I had a lot of free time on my hands so i decided to fix this bug.
If you want to check it out, its over here

Im not an expert in using git or github so im not sure how we would merge my code into this PR.
any ideas?

Copy link
Author

Choose a reason for hiding this comment

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

Awesome.

I could cherry pick your commits and include them into the PR, if this is ok for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

Go right ahead, it's better then me creating a whole new PR

Copy link
Member

Choose a reason for hiding this comment

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

You could also create a PR for his PR by forking his fork and merge it there then merge it here. However: Cherrypicking might be faster ^^

Copy link
Author

Choose a reason for hiding this comment

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

Nice try, @Fuzzyma 😝

The commits are now included into the PR. There are no errors when importing and using the Matrix property of the SVG object.
I think we are ready to merge this …

@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 86.586% when pulling 8559b15 on albohlabs:master into 422b84c on svgdotjs:master.

@hzrd149
Copy link
Contributor

hzrd149 commented Jan 11, 2017

there is still one problem left to fix.
this code works fine

import * as SVG from 'svg.js';
var draw: svgjs.Doc = new SVG.Doc();
document.body.appendChild(draw.node);

but this dose not work anymore

import SVG from 'svg.js';
var draw: svgjs.Doc = new SVG.Doc();
document.body.appendChild(draw.node);

it says that svg.js has no default export...
iv spent a lot of time on this and still can figure out to make it work, any ideas?

@albohlabs
Copy link
Author

This is a core design decision. You can see the discussion and conclusions in this issue.
To solve this we could define a default export in the library (and typings) itself or change the import syntax like recommended in this issue.

@albohlabs
Copy link
Author

albohlabs commented Jan 12, 2017

Here is a documentation change which also address our case.

@hzrd149
Copy link
Contributor

hzrd149 commented Jan 12, 2017

oh i see, i guess typescript modules dont work exactly like ES6 modules 😄
In that case everything looks good

@Fuzzyma can you merge this?

@Fuzzyma Fuzzyma merged commit a36c1f3 into svgdotjs:master Jan 12, 2017
@Fuzzyma
Copy link
Member

Fuzzyma commented Jan 12, 2017

sure thing. Thanks for your work!

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