Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
5.12.8, 6.0, 6.1
Description
Evaluating a script that contains an import statement with QJSEngine::evaluate() does not work.
import "file.js" as Test Test.someFunction('value')
results in "SyntaxError: Unexpected token `import'"
.import "file.js" as Test Test.someFunction('value')
results in "ReferenceError: Test is not defined"
With QQmlExpression, any import statement variant results in a syntax error. I have not loaded any QML document here, I just want to execute a plain JS script.
#include <QCoreApplication> #include <QDebug> #include <QTimer> #include <QQmlEngine> QString script = "\ .import 'file.js' as Test\n\ \n\ Test.someFunction('value')\n\ "; void test() { QQmlEngine *engine = new QQmlEngine(QCoreApplication::instance()); QJSValue value = engine->evaluate(script); if (value.isError()) { qDebug() << value.toString(); } qDebug() << value.toVariant(); QCoreApplication::quit(); } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QTimer::singleShot(0, &app, &test); return app.exec(); }
// file.js export var someVar = "blabla"; export function someFunction(someArg) { return someArg; }