ATM I have a folder in my NodeJS application where I store my JS files with a couple of functions. I require these files in my main.js at the top an use them as usual.
my-app/
├── node_modules/
├── my_jsfiles/
│ ├── functions_1.js
│ └── functions_2.js
├── package.json
└── main.js
main.js:
const myFuncs1 = require('./my_jsfiles/functions_1.js')
const myFuncs2 = require('./my_jsfiles/functions_2.js')
myFuncs1.someFuncsInside()
myFuncs2.someFuncsInside()
APPROACH: Now that I am going to use my_jsfiles in more applications I would like to make my own NodeJS module, which works so far, but I stuck at the point how I can include multiple js files instead of just calling functions from the index.js
my-app/
├── node_modules/
│ ├── my-jsfunctions/
│ │ ├── index.js
│ │ ├── functions_1.js
│ │ └── functions_2.js
├── package.json
└── main.js
main.js:
const myFuncs = require('my-jsfunctions')
//How do I call functions from functions_1.js and functions_2.js?
I know that I can export functions from the index.js
exports.someFunction = function () {
console.log("This is a message from the index.js");
}
But what is the propper way to call functions from the other files, because I do not want to have just one index.js file with million lines of code.
you just need to import all your functions into index.js file and export from there
my-app/
├── node_modules/
├── my_jsfiles/
│ ├── functions_1.js
│ └── functions_2.js
├── package.json
└── main.js
function_1.js
function functions_1_1(){
}
module.exports={functions_1_1}
function_2.js
function functions_2_1(){
}
module.exports={functions_2_1}
index.js
const {functions_1_1} = require("./function_1.js");
const {functions_2_1} = require("./function_2.js");
module.exports={functions_1_1,functions_2_1}
main.js
const {functions_1_1,functions_2_1} =require("./my_jsfiles/index.js");
functions_1_1()
functions_2_1()
you should just be able to do
const myFuncs1 = require('my_jsfiles/functions_1.js')
const myFuncs2 = require('my_jsfiles/functions_2.js')
isn't it working?
From your file index.js on my-jsfunctions, you can export function from other files like so
export * from './functions_1';
export * from './functions_2';
then you can import function
const func1 = require('my-jsfunctions').func1;
const func2 = require('my-jsfunctions').func2;
Related
I have two projects: my-lib a Vue 3 library and my-project a Vue 3 project that uses this library.
My-lib:
I can compile the library and compile its declaration files. This what I have in the dist folder of the library:
./dist
├── demo.html
├── index.common.js
├── index.common.js.map
├── index.umd.js
├── index.umd.js.map
├── index.umd.min.js
├── index.umd.min.js.map
├── src
│ ├── components
│ │ └── book
│ │ ├── BookToolBar.d.ts
│ │ └── BookToolBar.d.ts.map
│ ├── index.common.d.ts
│ ├── index.common.d.ts.map
│ └── shared
│ └── security
│ ├── AuthenticationResult.d.ts
│ └── AuthenticationResult.d.ts.map
└── tsconfig.tsbuildinfo
This is a piece from package.json of the library:
"name": "my-lib",
"version": "0.1.0",
"private": true,
"files": [
"dist/**.*"
],
"main": "dist/index.common.js",
"unpkg": "dist/index.umd.min.js",
"types": "dist/src/index.common.d.ts",
This is a dist/src/index.common.d.ts file:
export { BookToolBar } from "#/components/book/BookToolBar";
export { AuthenticationResult } from "#/shared/security/AuthenticationResult";
My-project:
The problem is that VS code ignores my declaration files in the following code:
import { AuthenticationResult } from "my-lib";
class AuthenticationResultChild extends AuthenticationResult {...}
However, the following code works fine:
import { AuthenticationResult } from "my-lib/dist/src/shared/security/AuthenticationResult";
class AuthenticationResultChild extends AuthenticationResult {...}
Could anyone say, how to make VS code work with declarations using the first variant (import { AuthenticationResult } from "my-lib")?
I am a beginner with Flask and I have created a project with an MVC structure
CODE :
.
├── app
│. │── main.py
│ ├── controllers
│ │ ├── dbscan.py
│ │ ├── home.py
│ │ └── kmeans.py
│ ├── models
│ │ ├── dbscan.py
│ │ └── kmeans.py
│ ├── static
│ │ └── XXXX
│ └── views
│ └── XXXX
└── main.py
File main.py
from flask import Flask
app = Flask(
__name__,
template_folder='app/views',
static_folder='app/static'
)
if __name__ == "__main__":
app.run(debug=True)
File home.py
from flask import render_template
#app.route("/")
def home():
return render_template('home.html')
ISSUE :
I already understood the problem but I don't know how to fix it...
There is no link between the controllers files and the main.py
So, how I can link all routes present in controllers folder and main.py ?
UPDATE :
home.py
from flask import Blueprint, render_template
home_hdl = Blueprint('home_hdl', __name__, template_folder='views')
#home_hdl.route("/")
def home():
return render_template('home.html')
main.py
from flask import Flask
from app.controllers.home import home_hdl
app = Flask(
__name__,
template_folder='app/views',
static_folder='app/static'
)
app.register_blueprint(home_hdl)
if __name__ == "__main__":
app.run(debug=True)
Folders
.
├── app
│ ├── controllers
│ │ ├── __init__.py
│ │ ├── dbscan.py
│ │ ├── home.py
│ │ └── kmeans.py
│ ├── main.py
│ ├── models
│ │ └── XXXX
│ ├── static
│ │ └── XXXX
│ └── views
│ └── XXXX
└── XXXX
Try this
Define a handler in each of the files you have under the controllers folder (the ones that will handle routing).
E.g. for the dbscan.py, you could have
from flask import Blueprint
dbscan_handlers = Blueprint('dbscan_handlers', __name__, template_folder='views')
# Your routes (in the dbscan.py file) will then be something like this
#dbscan_handlers.route('/xyz/'):
.....
#dbscan_handlers.route('/def/'):
....
Then you register the handler in main.py
from controllers.dbscan import dbscan_handlers
app.register_blueprint(dbscan_handlers)
Note that to do from controllers.abc import xyz, you need to have an __init.py__ file in controllers. The file can be empty.
When someone accesses your app, it will go to main.py and if the route is of the pattern /xyz/, it will be handled by dbscan.py file
I have a react project inside of a folder and I want react-scripts to target and compile from a folder. It looks like this
project
│ README.md
│ package.json
│
└───react
│ │ jsconfig.json
│ │
│ └───src
│ │
│ └───public
│
└───api
│ tsconfig.json
│
└───src
from the project/package.json I want to run react-scripts start and have it compile the /react folder. How can I do this?
I solved the issue with the use off react-app-rewired
See this stackoverflow post on details of how it was done.
const path = require('path');
module.exports = {
paths: function (paths, env) {
paths.appIndexJs = path.resolve(__dirname, 'react/src/index.js');
paths.appSrc = path.resolve(__dirname, 'react/src');
paths.appPublic = path.resolve(__dirname, 'react/public');
paths.appHtml = path.resolve(__dirname, 'react/public/index.html');
return paths;
}
}
I have this directory structure
├── components
│ ├── quarks
│ │ └── index.js
│ │ └── ...
│ ├── bosons
│ │ └── index.js
│ │ └── GridLayout.vue
│ │ └── ...
│ ├── atoms
│ │ └── ButtonStyle.vue
│ │ └── InputStyle.vue
│ │ └── index.js
│ │ └── ...
│ ├── .......
└─────
I'd like to ignore the index.js within each folder, but I'm not getting it, I've tried it in several ways
const path = require('path')
const chokidar = require('chokidar')
const ROOT_PATH = path.resolve('components')
const watcher = chokidar.watch(ROOT_PATH, {
ignored: ROOT_PATH + '/*/index.js', //does not work
ignoreInitial: true
})
already tried:
'./components/**/index.js',
'./components/*/index.js',
'components/*/index.js',
'components/**/index.js',
'ROOT_PATH + '/**/index.js'
Anyone have any idea how to make it work?
The chokidar documentation specifies that the ignored parameter is anymatch-compatiable so this could be completed in many ways.
Here is a regular expression solution...
Any index.js file, even in the root folder:
{
ignored: /(^|[\/\\])index\.js$/,
// ...
}
Only index.js file in a sub-folder:
{
ignored: /[\/\\]index\.js$/,
// ...
}
Also note in your example you use signoreInitial this is not an option, perhaps you meant ignoreInitial?
Alternatively with callback:
{
ignored: (path) => { return path.endsWith('\\index.js') || path.endsWith('/index.js'); },
// ...
}
Chokidar seems bugged, defective to ignore files on MacOS, that's the impression I have.
So before running my action, I'm checking to see if the file is the same one I want to ignore.
chokidar
.watch('components', { ignoreInitial: true })
.on('all', (event, filename) => {
filename !== 'index.js'
// action here
})
What worked for me on Mac was using **:
ignored: ['**/node_modules'],
So if the other options don't work because of bugs, go for this one:
ignored: ['**/index.js'],
Below are my codes
// config
requirejs.config({
paths: {
jquery: 'library/jquery',
jsBarcode: 'library/jsBarcode.all.min',
q: 'library/q.min',
},
shim: {
jsBarcode: {
deps: ['jquery'],
export: 'JsBarcode',
},
}
});
// Main entry
require(['jquery', 'q', 'jsBarcode'], function (j, q, barcode) {
window.Q = q;
console.log(barcode); // get undefined
});
Directory layout
└── webcontroller
├── bootstrap.min.css
├── image
│ └── load_trans.gif
├── scripts
│ ├── library
│ │ ├── jquery.js
│ │ ├── jsBarcode.all.min.js
│ │ └── q.min.js
│ ├── main.js
│ ├── promise_factory.js
│ ├── require.js
│ └── view.js
└── style.css
Loading sequences
The order for loading scripts is under my expectations.
Problems
However, barcode is undefined all the time.
Any one have ideas about this problem?
Updated
However, below codes can dump something out....
console.log(JsBarcode);
Fix the typo export -> exports, it must be the root cause. Also you have three excessive commas although it seems not to cause troubles. Finally there must be some global JsBarcode defined in jsBarcode.all.min.js that's why your console.log dumps it.
jsBarcode: {
deps: ['jquery'],
exports: 'JsBarcode'
}