Skip to content

Commit

Permalink
Scripting: Added support for importing modules
Browse files Browse the repository at this point in the history
If you write your extension in an .mjs file, it will now not just be evaluated,
it will be imported as a module. As such, the script can also import other
modules, allowing to more easily organize the code of extensions into reusable
parts.

Closes #3261
Closes #2749
  • Loading branch information
bjorn committed Feb 3, 2022
1 parent 298bd7a commit b3ad516
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/tiled/scriptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,33 @@ void ScriptManager::loadExtension(const QString &path)
{
mWatcher.addPath(path);

const QStringList nameFilters = {
QLatin1String("*.js"),
QLatin1String("*.mjs")
};
const QDir dir(path);
const QStringList jsFiles = dir.entryList({ QLatin1String("*.js") },
const QStringList jsFiles = dir.entryList(nameFilters,
QDir::Files | QDir::Readable);

#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
bool hasWarned = false;
#endif

for (const QString &jsFile : jsFiles) {
const QString absolutePath = dir.filePath(jsFile);
evaluateFile(absolutePath);
if (absolutePath.endsWith(QLatin1String(".js"), Qt::CaseInsensitive)) {
evaluateFile(absolutePath);
} else {
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
if (!hasWarned) {
Tiled::WARNING(tr("Importing modules (%1) not supported by this version of Tiled").arg(absolutePath));
hasWarned = true;
}
#else
Tiled::INFO(tr("Importing module '%1'").arg(absolutePath));
checkError(mEngine->importModule(absolutePath));
#endif
}
mWatcher.addPath(absolutePath);
}
}
Expand Down

0 comments on commit b3ad516

Please sign in to comment.