metapensiero / metapensiero.pj

Javascript for refined palates: a Python 3 to ES6 Javascript translator
Other
901 stars 73 forks source link

--inline-map option incompatible with -s #35

Closed BrainBacon closed 6 years ago

BrainBacon commented 6 years ago

I am in the process of creating a Webpack loader for this library, but I am unable to retrieve the source map if I send a string into the CLI. The -s option is capable of receiving a string with new lines, shouldn't it be able to figure out the source map even if there isn't a file?

Also, a quick way to test this is the following:

$ pj --inline-map -s "$(<somefile.py)"
azazel75 commented 6 years ago

I've pushed a new release, the 0.8, so you can test this out more easily

BrainBacon commented 6 years ago

Awesome, thanks!

BrainBacon commented 6 years ago

Can confirm, it works beautifully! Thanks again!

BrainBacon commented 6 years ago

Well, after a journey I was finally able to produce a loader, but unfortunately the -s option didn't work out for me. For some reason it was producing significantly different source maps. I don't know if it's worth your time to pursue. In the end I resulted to outputting the transpiled JavaScript and map files to a cache directory. This worked out pretty great for me.

If you're interested the loader source is here: https://github.com/Beg-in/javascripthon-loader

azazel75 commented 6 years ago

uhumm...seems strange... it's the same sourcemap content, it's just base64 encoded and written in the source stream...

BrainBacon commented 6 years ago

Yeah I decoded them and they're pretty different. Here's a quick test:

test.py

class example:
    @classmethod
    def data(self):
        return {
            'foo': null,
        }

    @foo.setter
    def foo(self, value):
        self.foo = value

    @property
    def bar(self, value):
        return {
            'foo': self.foo,
        }

    def baz(self):
        print('this is a method')

With a file

Command: pj test.py --inline-map -o /dev/stdout

Result:

class example {
    static data() {
        return {"foo": null};
    }
    set foo(value) {
        this.foo = value;
    }
    get bar() {
        return {"foo": this.foo};
    }
    baz() {
        console.log("this is a method");
    }
}

//# sourceMappingURL=data:text/json;base64,eyJ2ZXJzaW9uIjogMywgIm1hcHBpbmdzIjogIkFBQUEsYUFBQTtJQUVJO1FBQ0ksT0FBTyxDQUNILEtBREcsRUFDSUEsSUFESjtJQURYO0lBTUE7UUFDSSxJQUFBQyxPQUFXQztJQURmO0lBSUE7UUFDSSxPQUFPLENBQ0gsS0FERyxFQUNJLElBQUFELElBREo7SUFEWDtJQUtBO21CQUNJLENBQU0sa0JBQU47SUFESjtBQWpCSiIsICJzb3VyY2VzIjogWyIuLi9ob21lL2JyaWFuL1Byb2plY3RzL2phdmFzY3JpcHRob24tbG9hZGVyL3Rlc3QucHkiXSwgIm5hbWVzIjogWyJudWxsIiwgInRoaXMuZm9vIiwgInZhbHVlIl0sICJzb3VyY2VzQ29udGVudCI6IFsiY2xhc3MgZXhhbXBsZTpcbiAgICBAY2xhc3NtZXRob2RcbiAgICBkZWYgZGF0YShzZWxmKTpcbiAgICAgICAgcmV0dXJuIHtcbiAgICAgICAgICAgICdmb28nOiBudWxsLFxuICAgICAgICB9XG5cbiAgICBAZm9vLnNldHRlclxuICAgIGRlZiBmb28oc2VsZiwgdmFsdWUpOlxuICAgICAgICBzZWxmLmZvbyA9IHZhbHVlXG5cbiAgICBAcHJvcGVydHlcbiAgICBkZWYgYmFyKHNlbGYsIHZhbHVlKTpcbiAgICAgICAgcmV0dXJuIHtcbiAgICAgICAgICAgICdmb28nOiBzZWxmLmZvbyxcbiAgICAgICAgfVxuXG4gICAgZGVmIGJheihzZWxmKTpcbiAgICAgICAgcHJpbnQoJ3RoaXMgaXMgYSBtZXRob2QnKVxuXG4iXX0=

Decoded Source Map:

{
  "version": 3,
  "mappings": "AAAA,aAAA;IAEI;QACI,OAAO,CACH,KADG,EACIA,IADJ;IADX;IAMA;QACI,IAAAC,OAAWC;IADf;IAIA;QACI,OAAO,CACH,KADG,EACI,IAAAD,IADJ;IADX;IAKA;mBACI,CAAM,kBAAN;IADJ;AAjBJ",
  "sources": [
    "../home/brian/Projects/javascripthon-loader/test.py"
  ],
  "names": [
    "null",
    "this.foo",
    "value"
  ],
  "sourcesContent": [
    "class example:\n    @classmethod\n    def data(self):\n        return {\n            'foo': null,\n        }\n\n    @foo.setter\n    def foo(self, value):\n        self.foo = value\n\n    @property\n    def bar(self, value):\n        return {\n            'foo': self.foo,\n        }\n\n    def baz(self):\n        print('this is a method')\n\n"
  ]
}

With -s option

Command: pj -s "$(<test.py)" --inline-map

Result:

class example {
    static data() {
        return {"foo": null};
    }
    set foo(value) {
        this.foo = value;
    }
    get bar() {
        return {"foo": this.foo};
    }
    baz() {
        console.log("this is a method");
    }
}

//# sourceMappingURL=data:text/json;base64,eyJ2ZXJzaW9uIjogMywgIm1hcHBpbmdzIjogIkEsYTtJO1EsTyxDLEssRSxJO0k7STtRLEksTztJO0k7USxPLEMsSyxFLEksSTtJO0k7bUIsQyxrQjtJO0EiLCAic291cmNlcyI6IFtdLCAibmFtZXMiOiBbXSwgInNvdXJjZXNDb250ZW50IjogW119

Decoded Source Map:

{
  "version": 3,
  "mappings": "A,a;I;Q,O,C,K,E,I;I;I;Q,I,O;I;I;Q,O,C,K,E,I,I;I;I;mB,C,kB;I;A",
  "sources": [],
  "names": [],
  "sourcesContent": []
}

Side note, any interest in having my loader added to the README for this project? I would open a pull request, but I'm not sure if I should put it under Installation or Usage.

azazel75 commented 6 years ago

of course, maybe we can add a section "contrib", i think it's more appropriate. Anyway i'll look into that, thanks forr taking the time to dig into the issue.

For your invocation with -s, it can be simplyfied like:

$ pj -s - --inline-map < test.py

or

$ cat test.py | pj -s - --inline-map
BrainBacon commented 6 years ago

Yeah I could have used stdin to get the file. When I was working on the loader I was given a "content" string by webpack, so I was testing it without stdin and I used that in my example out of habit 😆. Yeah I think a contrib section would make sense. That would allow for future packages from others.

azazel75 commented 6 years ago

Hey @BrainBacon can you try the fix that I've just commited? You will have to install the code in master, because I haven't released it yet. You can do by using the following command:

$ pip uninstall metapensiero.pj
$ pip install https://github.com/azazel75/metapensiero.pj/archive/master.zip

For that to work you will have to change your command line by adding the --source-name option, like

$ cat test.py | pj -s - --inline-map --source-name test.py

Let me know if it works, and then send me the PR for the loader ;-) ciao

azazel75 commented 6 years ago

BTW, I add a look at the loader. A question, is it really a loader for Webpack 1 ? Isn't it a bit outdated?

BrainBacon commented 6 years ago

It's a loader for the current version of Webpack, but it should work with 1 in theory. There hasn't been much change to the loader API since 1. Just supporting whatever's out there.

Thanks, I'll take a look at the fix.

icarito commented 6 years ago

Hey this is very cool! I didn't know about. I recently added javascripthon to https://github.com/martim00/python-webpack-loader - it works well for me and just now I managed to use the inline sourcemap. Nice sync!

azazel75 commented 6 years ago

Thanks Sebastian, I'll add both to the docs. In the meantime, as you managed to use the inline sourcemap, I'm closing this