How to convert Dart to JavaScript? - javascript

The Dart app runs fine in Dartium, but I would like to convert it to JS.
C:\dart-sdk\bin\dart2js --out=test.js main.dart generates JS from:
import 'dart:html';
void main() {
querySelector('#myid').text = 'Wake up, sleepy head!';
}
Then I add it to the <head>:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<p id="myid">hello</p>
</body>
</html>
But when I run it in Firefox, it doesn't work. It shows only "Hello" on the page. What am I doing wrong?

dart2js is usually not the straight-forward way to generated JS from Dart.
Normally you just run pub build in your project directory.
Ensure you have your pubspec.yaml configured properly csp: true (for Chrome apps Using Polymer in a Dart Chrome App, Chrome App CSP violation after Dart/Polymer transform/compile to JavaScript)

Related

Trigger A Python Script By Using JavaScript

I am doing a little project where i have prepared the back end in python and the graphic user interface woth HTML, CSS & JS.
The python script doesn't require any external librariesand the only thing that it does is opening a JSON file with the data processed, since it's a game the results are casually generated, and because of this I don't have to pass any parameters to the script.
The problem is that I don't know how to trigger the script so it generates the json that i can access trough JabaScript.
the scheme of the project is this:
index.html
pages (folder)
newGame.html
loadGame.html
rules.html
python (folder)
python_script1.py
python_script2.py
python_script3.py
main.py
specifically i have to trigger the script once the user has loaded the newGame page or the loadGame one.
(obiuvsly the js isn't node.js is actual client-side JavaScript)
I obiuvsly did some research and i found the pyodyde open source project does what i want, the fact is that i can't figure out how to connect the interface file with the back-end one with this resource.
solution
Ok, with some other research i found out how to solve my problem.
But firstable I want to thank you guys for recommending me the PyScript library.
The problem I was having is that i needed to connect a python file within the pyscript tag.
At first i tought that I had to import it with the import keyword and than executing the file.
But than I discovered that the py-script tag has an src attribute and you can link all the external files that you want.
So, in the end, iall i had to do was to connect my file is this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>newGame</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<py-script src= "./python scripts/test_file.py"></py-script>
<script src="script.js"></script>
</body>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</html>
There is a new library called PyScript (it can help you run python in your HTML), all you need to do to install the cli is doing this command:
pip install pyscript-cli (This command will install the cli not the library).
then you should do this command:
pyscript wrap python_file.py (NOTE: this will convert the python file to an html file so the python file name would be the name of the html file)
And it should be done.
The HTML code would be this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Homepage</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css"/>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<py-script>
Code here
</py-script>
</body>
</html>
But than, once i typed in the command and created the html file from the python script, how do I connect it to the "newLevel" page?

Qt resources .js files can not load in release mode

I want to load an external JavaScript file index.js in index.html within QWebEngineView.
qrc file:
<RCC>
<qresource prefix="/">
<file>index.html</file>
<file>index.js</file>
</qresource>
</RCC>
index.js and index.html file:
console.log('test');
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello world!</h1>
<script src="index.js"></script>
</body>
</html>
I use ui->widget->load(QUrl("qrc:/index.html")); to load the html file, and run in debug mode. It works fine, I can see "Hello world!" and console message "test". When I run in release mode, I got a Resource :/index.js not found or is empty.
I can see some difference files in release build folder, like index_js.cpp which not generated in debug mode, it seems that qml engine load the js file. How to disable it and load js file in release mode properly?
My Qt Version: 5.12.7 MSVC2017 64bit.

Invalid regular expression:: Range out of order in character class

I'm trying to use some protocol buffer code generated with protoc for javascript. I got some javascript files that i can import (after choosing the commonjs style) into other javascript files.
I've installed an npm dependency called google-protobuf and am able to bundle all javascript with webpack. When i try to run the page in firefox i get no errors related to this, chrome however gives me:
Invalid regular expression: /[Ö‘-Û¯Ûº-ࣿâ€�-��-�ï¬-ï·¿ï¹°-ﻼ]/: Range out of order in character class
I've tried adding some additional tags to <meta> and <script> and they don't work. I've never seen an error like this and don't know where to start looking.
The end goal is, i'd like all my npm i and all my import { Foo } from 'foo.js' to work on a web page without errors.
What webpack injected using HTMLWebpackPlugin is:
<script type="text/javascript" src="frontseat.bundle.js"></script>
Originally there are no <meta> tags, i just added the one under head so it looks:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>...
Try to update your regex to this regex below:
[Ö‘\-Û¯Ûº-ࣿâ€�-��-�ï¬-ï·¿ï¹°-ﻼ]
And your meta tag is like this? <meta charset="utf-8"> with this charset
I got the same issue as you have.I cannot upgrade google-protobuf over than 3.6.1. Only version 3.6.1 works for me before. All versions later 3.6.1 raise the same errors in my project.
However, the <meta charset="utf-8"> helps now on my side. I create a minimum sample and hope it can give you some idea.
google-protobuf version: 3.11.2
index.js
import * as pb from 'google-protobuf';
console.log(pb);
build command
npx webpack index.js -o bundle.js
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="bundle.js"></script>
</head>
</html>
Then, you should be able to see something like this on chrome devtools console without the error.
{Map: ƒ, Message: ƒ, BinaryReader: ƒ, BinaryWriter: ƒ, ExtensionFieldInfo: ƒ, …}

WebStorm : debugging typescript

Issue updated. Please check the edit
I want to use the debugger included in Webstorm to run my project. I'm
using Angular2 and TypeScript. I need to place breakpoints into my typescript files.
Basically, my index.html just imports some .js files and starts my
app by calling a function named bootstrap defined in another .js file (transpiled from a .ts file !).
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/">
<meta charset="UTF-8">
<title></title>
<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="../node_modules/es6-shim/es6-shim.min.js"></script>
<script src="../node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="../node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/rxjs/bundles/Rx.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="build/core/system.config.js"></script>
<script src="build/core/moduleLoader.js"></script>
<script src="build/core/bootstrap.js"></script>
<script src="../node_modules/angular2/bundles/router.dev.js"></script>
<script>
bootstrap();
</script>
</head>
<body>
<app-view>Loading...</app-view>
</body>
</html>
bootstrap is defined as follow :
const bootstrap:()=>Promise<void>= async function() {
await ModuleLoader.loadAll();
System.import('core/app').then(null, console.error.bind(console));
};
window.bootstrap = bootstrap;
When I run my index.html in debug mode, an error is thrown,
bootstrap() is not defined.
When I run the app inside a browser, it works well. Have I missed some configuration ?
I've installed the jetbrains plugin (I'm using chrome) and configured the port. I can see the "Loading..." on my browser.
EDIT
Maybe a usefull information :
I'm using node. Into my main server-side script, I set up express and do some configuration. When I'm trying to debugging, my server is listening but on another port (8080). The debugger uses the port 63343.
You can start a JavaScript debug session for any URL - you just need to specify it in the JavaScript run configuration. If you app is running on localhost:8080, then use that URL in the JavaScript debug configuration.
Another important point: make sure you generate source maps when compiling your TypeScript code.

How to work with src file and keep the directory clean (from compiled files)?

The reason to have src and dist folders is pretty clear: we change source code commit it to repository and use compiled dist files for production.
But what could you recommend for development? We still want to keep src clean from compiled file and make it very fast to review the changes.
Before we just transpiled SAAS into CSS and put it into source and keep JS files as they are(without compiling in one file or coping them to dist). And now we decided to rethink the concept.
we are doing web development, but I think question is more broad.
If I well understood, you are looking for a development architecture/workflow to directly serve you sources to the browser (and use DevTools edition features btw). In fact, there are 2 things to do to achieve this.
1) For compiled/transpiled languages (SAAS, LESS, TypeScript, ...) and unsupported languages (ES6, ES7), you will need to compile/transpile them on the client side. Tools like LESS.js, typescript.js, BabelJS are great to do that. I don't know any SAAS compiler implementation in Javascript.
2) Then, you need 2 different html indexes. For example index.hml for production and dev.html for development.
index.html with bundled (dist) files:
<!DOCTYPE html>
<html>
<head>
<title>App Title</title>
<link rel="stylesheet" type="text/css" href="dist/app.bundle.css">
<script type="text/javascript" src="dist/app.bundle.js"></script>
</head>
<body>
...
</body>
</html>
dev.html with all sources (src) files:
<!DOCTYPE html>
<html>
<head>
<title>App Title (Dev)</title>
<link rel="stylesheet/less" type="text/css" href="src/file1.less" />
<script type="text/javascript" src="src/file1.js"></script>
<script type="text/javascript" src="src/file2.js"></script>
<script type="text/javascript" src="src/file3.js"></script>
<script type="text/javascript" src="lib/less.js"></script>
</head>
<body>
...
</body>
</html>
This way, developers access the web app using, let's say http://localhost/dev.html and can enjoy all the goodness to have sources right in the browser. Without taking care of the compilation.
To apply such an architecture you may need to adapt client and/or server code and also build tools to work in both mode (prod and dev).
Hope I helped.

Categories