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

Request: Support .mjs scripts #3261

Closed
user72356 opened this issue Feb 3, 2022 · 3 comments
Closed

Request: Support .mjs scripts #3261

user72356 opened this issue Feb 3, 2022 · 3 comments
Assignees
Labels
feature It's a feature, not a bug.

Comments

@user72356
Copy link

According to the Qt docs at https://doc.qt.io/qt-5/qtqml-javascript-imports.html, Qt supports importing .js and .mjs Javascript files. In the Qt world, .mjs files are basically the same as .js, but they can import/export modules, which .js files can't. I maintain an extension that is almost 2,000 lines and looking to break things down in several files/modules. The only way (other than using a JS bundler) is to save the modules in .mjs files. Unfortunately it seems like Tiled only looks for .js files and not .mjs files in the extension folder. I think this would be an easy change and would allow publishing modular extensions without messing with antiquated bundlers. Thank you for considering it!

@bjorn
Copy link
Member

bjorn commented Feb 3, 2022

I had tried loading modules as well in the past, and I hadn't gotten it to work. But now carefully reading this documentation again, I noticed that it says that importing modules only works from .mjs files. And since I was evaluating JavaScript using QJSEngine::evaluate, Qt would never have been aware that the script came from a module.

So, I've made a patch that uses the QJSEngine::importModule function, added in Qt 5.12, when encountering .mjs files. And indeed loading modules from there appears to work!

So, starting with Tiled 1.8, it will be possible to write and distribute extensions as .mjs files, and then you can also use the import and export mechanism to write modular code. :-)

@bjorn bjorn added the feature It's a feature, not a bug. label Feb 3, 2022
@bjorn bjorn closed this as completed in b3ad516 Feb 3, 2022
@bjorn bjorn self-assigned this Feb 4, 2022
@user72356
Copy link
Author

Thank you, it really helped my life.

@bjorn
Copy link
Member

bjorn commented Feb 7, 2022

@user72356 Yeah, I think it'll help many people!

Be aware that I had to make some additional changes, mainly to get loading error reporting to work also for modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature It's a feature, not a bug.
Projects
No open projects
Status: Done
Development

No branches or pull requests

2 participants