How to use multiples files for the Meteor server? - javascript

I'm trying to split my server in multiples files, and actually I want to make cmdb_login.js and main.js.
The thing is that I don't know and I don't find how to say in main.js, use cmdb_login.js.
I have tried import './cmdb_login.js';
My app structure is:
If you need the code of the two .js files just ask me.
Thank you for the help

As always, Meteor combines all files eagerly, unless they are placed in a path which includes a folder named imports.
I.e. in your case, your cmdb_login.js will be combined (and then executed) before main.js in the server JavaScript code.
A proper way is to put almost all your files in an imports folder, and explicitly call them in a single main.js file (or whatever its name, provided that it is not in an imports folder), using an import statement like the one you tried (e.g. import './imports/cmdb_login.js').

Related

How can I run this JavaScript file?

I have this project:
but I don't know how to run the generate_js.js in my browser so I can see use console.log inside of the file.
The whole purpose of this project is to run generate_js.js, which is then supposed to generate an icon.js file which contains the svg icons in a particular object format/structure. But what happens when I run generate_js.js is that a folder generate_js is created with all the icons as js files, and each file has an export statement.
I am trying to understand how is all of this working, but I can't. I need to be able to see what's happening on my browser. I need to use console.log to see, but I don't know how to run this project on my browser. There is no index.html file, and when I created one, and linked the project_js.js with a script tag, I keep getting Uncaught ReferenceError: require is not defined error.
How can I run this JavaScript/project on my browser?
Here's the relevant part of package json:
Note: This project is installed as a dependency inside another project, and inside the node_modules, there's a dist folder which contains the icons.js file that I referred to. In it, all the icons are created as objects. I need to change the way the object structure a bit, so I need to understand how are they generated by this generate.js file, but I need to be able to console.log what's happening inside the file.

Parceljs import javascript file as string

I have a very particular use case. I want to import a javascript file as a string and inject it into html responses at will in a service worker. I can't see how to do this using parceljs beyond hosting the javascript file somewhere and doing fetch at runtime to load the js file into memory. However, I want to do this at build time. How best to do this?
Note: Ideally the dependencies of javascript file I am importing should be bundled into the string.
Seems to be possible in parcel 2 with
import js from "bundle-text:./b.ts";
console.log(js);
https://v2.parceljs.org/configuration/plugin-configuration/#predefined-(offical)-named-pipelines

ember.js include custom js

Attempting to wrap my head around Ember.js.
Seems I understand the complex things, but miss out on the little things.
How would one go about adding an example.js file?
For simplicity, let's say the example.js file only contains:
(function(){
console.log("example is alive in console");
})(window);
This should display "example is alive in console" within the browser console.
I have tried:
adding app.import('vendor/javascripts/example.js'); within ember-cli-build.js and adding <script src="{{rootURL}}vendor/javascripts/example.js"></script> to index.html
Console is showing
ⓧ GET http://localhost:4200/vendor/javascripts/example.js
DEBUG: -------------------------------
DEBUG: Ember : 2.11.3
DEBUG: Ember Data : 2.12.1
DEBUG: jQuery : 3.2.1
DEBUG: -------------------------------
ⓧ GET http://localhost:4200/vendor/javascripts/example.js
All of the answers I have found stated that just adding custom.js to vendor file works. Sadly, I am missing something.
When modifying ember-cli-build.js you MUST RESTART the ember server manually. The livereload server will not pick up the changes.
This works for me when I don't nest assets in the /vendor directory. The ember-cli build process bundles JS files in /vendor into a single vendor.js file, which you can see linked in app/index.html. So place your example.js file at the root of /vendor, and then add the import to ember-cli-build.js:
app.import('vendor/example.js`);
Now when you start the server, your code from example.js should execute, since it will be included in assets/vendor.js.
Firstly, Ember.js has Convention Over Configuration approach, and your URL can do a lot of things than a normal HTML website.
Whatever you may want to do with your custom.js file it is not ember way of having it as a path. You need routes for navigation across the app. Although routes do much more than navigation. You specify the structure of your app that a user can browse through using Router's map function in app/router.js file.
However if you want to include custome.js file in your app, and have custom.js do some set of tasks for your app. You can simply go ahead and create a directory with any name, javascript for instance inside app directory. Have your javascript files placed inside it. Then you can import these files as simply as referencing any other files in ember:
import customObject from 'yourApp/javascript/custom.js';
Here, your custom.js should be exporting customObject.
Do read the guides if you want to learn more. And the API docs if you actually want to learn more.
Note: At the time of writing this answer current ember-cli version is #2.12.0

What is the difference between JS files in dist/ folder and the one in root?

I am totally new to NodeJS and I wonder what's the difference between those two.
For example, in this project (https://github.com/fikriauliya/hipku), we have index.js and dist/hipku.js. They are similar except the last line:
module.exports = publicMethods; vs return publicMethods;
I guess dist/hipku.js is generated from index.js? How is it generated and why does it need to be generated?
Things in the dist folder are usually the product of building from index.js in this case. You'll notice it gets minified, and that folder would eventually be used on production sites. If you look at the package.json file, you'll notice that index.js is the main file, so if you're doing any edits, that would be the place to do so.
It depends on how you want to use this package, in browser or server side.
server side
index.js is the entry of NPM package. When you do require('hipku'), actually NodeJS locates the file module node_modules/hipku and run index.js ends up with the object adhere to module.exports
browser
Just load dist/hipku.js into your browser by <script>, it will register hipku into your global namespace, then you can use it's API.

include a js file in routes file node (not node mudule)

I am trying to create a file that will contain some functions that will be used throughout the entire project. I would like these functions to have access to all the modules that I am using in the project(which is why I don't want to use another module)
in my app.js I can use require to a static folder containing the js file and it works fine however if I go to routes/index.js and try to require the same folder it cannot find it.
Im guessing thats because its not leaving the routes folder. How can I move to the parent of the routes folder.
I have tried
require('controllers/cf');
require('./controllers/cf');
require('cf')
require('../controllers/cf');
require('../~path to project~/controllers/cf');
none of these work. I know this is simple however the solutions I have found dont work unless I make a node module then it has no problems but then other modules are not within my scope
This will work.
var path = require('path');
require(path.join(__dirname, '../controllers/cf'));

Categories

Resources