Here is the task:
I want to run .js file which was created by Emscripten from .cpp file, from another .js file.
i.e.: I have ping.cpp file, which simply displays text "ping". I use Emscripten to create ping.js To do it, I type em++ ping.cpp and here it is - ping.js.
Now I can run it using node ping.js, but I want it to run from my second .js file which is called init.js and I can't understand how should I do it. Because ping.js doesn't have main functions which display "ping" and which I can call from another .js file or for example .html file, instead of this it has 68500 lines of code.
So, is there any chance for me to run ping.js from init.js?
You should probably load your ping.js file with a script tag in your HTML file, just like any other script. The trick is to make sure that you make any functions you want to use accessible to OTHER scripts that you load. To do so, you need to set an EXPORTED_FUNCTIONS compilation flag to indicate which function names you want to preserve. Specifically
em++ -s EXPORTED_FUNCTIONS="['_ping']" ping.cpp -o ping.js
Note the underscore ('_') that needed to be added in front of the function name to compensate for name mangling.
In the other JS file where you want to use ping, you need to set it up. Make sure the compiled script loads first, and then do:
ping = Module.cwrap('ping', 'null', ['string']);
This will then allow you to use ping, assuming it has a void return type (hence the null) and a single c-style string argument (hence the ['string']).
If you really do want to load it from another JavaScript file, see this answer:
How do I include a JavaScript file in another JavaScript file?
Related
I added a "scripts" folder as a passthrough copy to my Eleventy site and installed some npm dependencies inside it for scripts to be run on the page load.
So my .eleventy.js has some lines like this:
eleventyConfig.addPassthroughCopy("img");
eleventyConfig.addPassthroughCopy("scripts");
eleventyConfig.addPassthroughCopy("css");
But when I run npx eleventy, I get a build error that says,
Language does not exist: sh
Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble rendering liquid (and markdown) template ./scripts/wb-service-worker/node_modules/bs-fetch/README.md
Why is it trying to "render liquid" in a passthrough copy? (I thought the whole point of passthrough copies is it doesn't try to parse them.) How do I get it to stop?
addPassthroughCopy will copy the files through without parsing, but if the file is also in the input directory, eleventy will process it in its normal way too.
You should keep the assets that you want to be passthrough-copied in a seperate folder to the src files that you are inputting to eleventy for processing.
See these docs for more help:
Input Directory
Ignoring Files
Passthrough's handling of input files
I am working with angular 8, I have finished my project and I have used lazy loading, after having my project ready I have done an ng build --prod (therefore I generated several javascript files).
We use Django as a server, and because of our way of working we separate the JS and CSS files in a folder and the index.html in the root, what is the problem?
What the files to be called within the runtime file have an undefined path, I see that it has a src function that returns the location of the file, but I don't know how to modify this function.
Inside to runtime.js we have this
After performing a console.log in the variable "u.p" I can see EMPTY,
How can I modify a U.P. variable/function? I want to place u.p. = "js /" (I have manually replaced u.p with what I need and it works, but I want it to be automatic) I understand that it must be an Angular.json configuration or an extra command when doing the ng build --prod
Quoting from the article at https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8 below:
We can natively require JSON files and C++ addon files with the require function. You don’t even need to specify a file extension to do so.
If a file extension was not specified, the first thing Node will try to resolve is a .js file. If it can’t find a .js file, it will try a .json file and it will parse the .json file if found as a JSON text file. After that, it will try to find a binary .node file. However, to remove ambiguity, you should probably specify a file extension when requiring anything other than .js files.
Here is my little experiment that seems to contradict what is written above.
$ cat foo.js
console.log('I am foo.js!')
require('./bar')
$ cat bar.js
console.log('I am bar.js!')
$ cat bar
console.log('I am bar!')
$ node foo.js
I am foo.js!
I am bar!
$ node bar
I am bar!
The experiment shows that if I do not specify the .js extension name, i.e. I import only bar or try to run only bar, then the first thing Node tries to do is to find a file named exactly as bar. Therefore, it contradicts the following statement from the quoted article.
If a file extension was not specified, the first thing Node will try to resolve is a .js file.
Is the quoted article incorrect or am I misunderstanding something?
The article is incorrect. From the official documentation:
LOAD_AS_FILE(X)
If X is a file, load X as JavaScript text. STOP
If X.js is a file, load X.js as JavaScript text. STOP
If X.json is a file, parse X.json to a JavaScript Object. STOP
If X.node is a file, load X.node as binary addon. STOP
I'm new to using CoffeeScript and this is my first time trying it out. I have two questions.
Compile to particular file
I want to compile a particular coffee file to javascript on the fly.
To do this, I've done the following:
coffee -w -o controllers/loginCtrl.js -c coffeescripts/loginCtrl.coffee
This gives me
ENOENT, open 'c:\path\to\public\testassets\js\controllers\loginCtrl.js\loginCtrl.js'
I believe this is happening because the js file already exists. If I remove the js file, the same command creates a folder called loginCtrl.js and then adds a file inside it.
How do I get coffeeScript to compile to a particular existing file or to a proper path?
Remove unneeded code from compiled file
Assuming that the file gets compiled, the new js file gets compiled as
(function() {
....
}).call(this);
Is it possible for me to remove the first and last lines of this compiled code?
The -o option specifies the output directory. You sholud get the expected output if you remove the file name from this argument.
To compile without the top-level function safety wrapper, use the -b option.
Please read about the coffee command options here: http://coffeescript.org/#usage
With param -o you can set output directory but the name of your js file will be the same as name of coffee file
If you want to prevent coffeescript from wrapping the code into immediately invoked function, you need to add param -b or --bare ("Compile the JavaScript without the top-level function safety wrapper." from official docs)
I have a shell script that collects all the .js files on a page and concats them to be compiled using the closure compiler. However, I don't want a specific js file to optimized any via the compiler. For example, I have the command to compile fileA.js, fileB.js, and fileC.js. How do I notate to skip fileB.js but still place it in the output file scripts.min.js in the correct order? So, fileA.js and fileC.js would be optimized using SIMPLE_OPTIMIZATION and fileB.js wouldn't be touched. Is there a keyword I can place in the comments of the file itself that says, skip this file?
java -jar compiler.jar --js=fileA.js --js=fileB.js --js=fileC.js --js_output_file=scripts.min.js
If I understand your intent here, you may consider processing each file that you want to minify separately, then performing the concatenation as a separate step. In pseudo-code:
minify fileA.js
minify fileC.js
cat fileA.js fileB.js fileC.js >scripts.min.js
There is no keyword that you can place in any scope to say "ignore me". nullptr has the right suggestion. In our project we have created some simple preprocessing comments and use them to control the flow. However, you can only ignore and include a file before the minified code or after the minified code if you want to do it in one pass. So, nullptr's solution is the only one. Remember to use extern files so variable renaming (and not renaming) works properly.