-
-
Notifications
You must be signed in to change notification settings - Fork 967
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
JSPM + Riot.js + import statements in tags #1715
Comments
JSPM still uses Babel 5, so babel-preset-es2015 isn't going to help. I'm getting the same issue, by the way. I workaround it by importing all my components before mounting, but that's not really the same thing, especially when you've got stuff that are sub components of other components. should be the only thing that needs to import if you know what I mean. |
I'm not sure I understand what you mean - you import all your components in So is this a problem with with JSPM then? I wouldn't have thought so since it does its job fairly smoothly without riot. |
I would also maybe check with systemjs-riot. I don't know jspm very well but if we could do a precompile the tags then import the js it'd probably work fine. |
Yeah I really have no idea with this. I hoping this won't stop me from using riot, because it's been really fun so far. @guybedford please ignore this (and sorry for the ping) if you're busy: can you make any comment on riot from JSPM's point of view? It seems like a very promising framework and it'd be great for both JSPM and riot if they could get friendly with one another. |
Sure, I just had a brief look at this. So systemjs-riot is compiling assuming that tags are run as AMD modules - https://github.com/HuasoFoundries/systemjs-riot/blob/master/tag.js#L10. You can alter that to output ES modules with: var compiler = require('riot-compiler');
exports.translate = function(load) {
// @todo be able to pass the options in config.js
var options = {
//expr: true
//type: 'babel'
};
load.metadata.format = 'esm';
var compiledtag = compiler.compile(load.source, options);
// console.log(compiledtag);
var output = "import riot from 'riot';\n" + compiledtag;
return output;
}; But you then get a Babel error:
So this looks like something that isn't actually supported due to the nature of the riot compilation process itself. If the import statement could be hoisted during the compilation then it would all work out, but that would need to be some sort of ast transform which is not simple to do. You could get modules to work in tags by using CommonJS as the module format here instead. That would work out because CommonJs requires can appear anywhere in the code. Although it's not a very forward-thinking approach. Perhaps just use a dynamic |
Have you guys checked the |
@GianlucaGuarini awesome, I can confirm that the plugin adjustment works with manual hosting of that import statement against the example app. It would definitely be worth sending that adjustment as a PR to the systemjs-riot plugin then, as it also brings Rollup support with it as well. |
the problem here is that the es6 modules can work only if located in the main scope. <tag>
<script>
import baz from './baz'
this.foo = 'bar'
</script>
</tag> This gets translated by the riot compiler into: riot.tag2('tag','', function(opts) {
import baz from './baz' // import here is not allowed
this.foo = 'bar'
}) I think that using all the import outside of the tags is a good workaround for now |
Thanks for your investigations Guy and Gianluca! So just to confirm, the current approach to getting riot and JSPM working together is by
And editing this line to:
Is that correct? I'm not able to test this myself just now but will tomorrow. |
I figured I share my experiment as it is related: https://github.com/nikek/modern-riot It's small and works pretty well except for the case I mention under the known issues topic in the readme. Rollup complains about "duplication of imports in the same file", which I still haven't had time to debug yet. Hope it helps someone. |
I will close this issue because the es6 modules specs do not allow the import of scripts in nested functions. My workaround is enough to solve the issue |
Use esm instead of amd, see riot/riot#1715.
Add support for es6 `import` statements. Thanks to @kuashe! Related to [riot#1715](riot/riot#1715), [riot#1784](riot/riot#1784), and [riot#1864](riot/riot#1864).
Please test with the riot-compiler v2.5.4 Now the |
@aMarCruz @GianlucaGuarini I am also trying to load a tag dynamically and then mount it using jspm.
The tag is getting loaded in the DOM but its content is empty. It works If I put the import statement at the top of the module. But in this way I will have to load all the javascript file at once. I want to load these tags on demand. |
@shyamchandranmec , I don't know about jspm but from the specs |
Describe your issue:
I'm trying to get riot.js to work with JSPM and it was all going perfect until I put an
import
statement inside the script tag in one of my custom riot tags.I get this error:
Uncaught (in promise) Error: SyntaxError: Unexpected token import system.js:4
My question is: Is this a problem with riot, JSPM, me, or some combination?
JSPM has been working great for me so far so I don't think that's the problem. I've seen other examples where putting
import
statements inside script tags works fine, so I don't think it's a problem with riot.So maybe there's something simple I'm missing? Someone suggested to install babel-preset-es2015, but I think JSPM already handles all the babel stuff so that's not relevant to me.
Can you reproduce the issue?
I don't think so unless plunker supports JSPM. I made an example of what I'm trying to do in any case:
http://plnkr.co/edit/80FlhIABVnfTv7mops2I?p=preview
EDIT: I made a repo showing the problem: https://github.com/josephrocca/riot-jspm/tree/import_in_tag_test
Just need to call
npm install
and thenjspm install
and it's good to go.On which browser/OS does the issue appear?
Chrome, Windows
Which version of Riot does it affect?
2.3.17
How would you tag this issue?
The text was updated successfully, but these errors were encountered: