Manually created subfolder from /src is missing in /dist when building - node.js

I created a subfolder in /src called /libs. When I run npm run build, the subfolder isn't included in /dist.
I'm assuming that I have to modify a build script? If so, which file would that be?
Edit #1
This all stems from trying to require a custom module located in src/libs from my controller. I've tried various patterns: ../libs/module_name, ./libs/module_name. The only way to have it work was to hard code the path from the root (i.e. /home/me/app/src/libs/module_name).
If I do: console.log(__dirname) in the controller that is attempting to require the module from /lib, I see a reference to /dist. I went looking into /dist and /libs wasn't there.

lb-tsc is a thin wrapper for TypeScript compiler (tsc), you can find the source in loopback-next:packages/build/bin/compile-package.js
Among other options, it provides a new flag --copy-resources to copy non-TypeScript files from src to dist. I think it may work equally well (if not better) as your cp -r solution.
"scripts": {
...
"build": "lb-tsc es2017 --outDir dist --copy-resources"
...
}
Personally, I would use a different solution:
use src only for TypeScript files to be compiled
put JavaScript sources and other files into a different directory, e.g. lib (instead of src/lib).

With the help of a colleague, I figured it out. In package.json, I appended a copy command to bring the libs folder into dist in the build section.
"scripts": {
...
"build": "lb-tsc es2017 --outDir dist && cp -r src/libs dist",
...
}

For those using Nest CLI, please note that it also does not automatically move your "assets" (non-TS files) to the dist folder during the build process.
For these non-TS files you just need to add them to the "compilerOptions": {}
property in the nest-cli.json file.
"compilerOptions": {
"assets": ["**/myfolder/**"]
...
}
The syntax "**/father-folder/**" causes all folders & files in myfolder to be included in the distribution folder (dist)
My resource for this was from Derryl Thomas
Link to NestJS documentation on assets : Link...

Related

How to copy asset files before deploying cloud functions to Firebase

I have a firebase project where I write typescript functions to be deployed into Google Node Cloud functions.
When I run the firebase deploy --only functions command, it transpiles my code into javascript and put the js output into a folder called lib next to src folder where my typescript functions are.
However, some of my functions need access to local files such as .ttf files or some other file types. Those don't get copied over to the lib folder and therefore, I get errors in runtime Error: ENOENT: no such file or directory, open path/to/file
Question 1 : How do I get the deploy command to copy assets files to the output folder ?
Question 2 : Given that all my functions live in separate files, in separate folders, and so do my assets, how should I reference my assets so that they can be found ? Should I give the path to the asset file relative to the lib folder or relative to where the function lives ?
EDIT -1
See the project structure here :
the code that need the font lives in the some-function.ts file. And it uses the pdfmake library that needs fonts to work.
Here is how I do it in the some-function.ts file :
const fonts: TFontDictionary = {
Poppins: {
normal: './fonts/Poppins/Poppins-Regular.ttf',
bold: './fonts/Poppins/Poppins-Bold.ttf',
italics: './fonts/Poppins/Poppins-Medium.ttf',
bolditalics: './fonts/Poppins/Poppins-Thin.ttf',
}
};
const pdfmake = require('pdfmake');
const printer = new pdfmake(fonts);
So how do I reference such fonts given that they are located in the fonts folder. or event if I put them in a separate folder at the root or src ?
After some digging I came to a solution that I would like to share here.
1- First thing, as suggested by #Dharmaraj, I added a script that removes the lib folder, before building the project, and copy my assets files before deploying.
So in the package.json under functions folder, I updated the build command as follow
"remove-lib":"rm -rf lib",
"copy-assets":"cp -rf src/path/to/your-folder/fonts lib/path/to/your-folder",
"do-build":"tsc",
"build": "npm run remove-lib && npm run do-build && npm run copy-assets",
Actually, adding the remove lib command solved another issue I had with deploying functions. I think this should be the default behaviour because when you rename function files, old ones stick in lib folder causing all sort of issues.
Anyway, now in order to correctly reference your asset, just construct the path as ${__dirname}/fonts/some-font.ttf provided that the fonts folder lives in the same folder as the file your are editing.

How to copy files/folders from src folder during a bin execution with Node js?

I would like to copy the content of my 'three-experience' folder into the current "bin execution environment".
I'm using Typescript, but there is no issue with it at the moment (and I'm mapping on the .js dist files).
I have the following root:
My package.json is mapping in the dist folder like so :
"bin": {
"ts-cli": "dist/main.js",
"vite-config": "dist/bin/vite-config.js"
},
During the execution I'm calling the "vite-config" bin (using shelljs):
shell.exec('vite-config');
It may not be clear but the purpose is to do a npm package that would install a template on your local machine.
I can do it with git. But that's not what I want to do.
I thought about filling a process.env.FOLDER global variable, but I am quite sure that's a bad idea.
Any clue would be appreciated !

Electron builder not copying my dist/node_modules directory in the right place

I've got one node application which I'm already launching with Electron. Now, I want to package it with electron-builder, but I'm dealing with such an strange behaviour. Usually, when I build my application, a dist folder gets generated, containing all the transpiled javascript files and also the package.json. Then, I have to run npm i --production again there to set up my production dependencies. So what I want electron-builder to do is to copy exactly the dist directory as it is once everything is installed:
electron-builder build --linux AppImage
This is my package.json related config:
"main": "dist/app.js",
"build": {
"appId": "my.app",
"files": [
"dist/**/*"
]
},
I guess that with this configuration I'm telling it to copy the dist directory and its contents. The AppImage file gets built and if I unzip it, I can see a resources dir gets created inside, containing one app.asar file. If I extract the asar content, I can see node_modules gets placed at the same level than dist, and not inside of it!! So I have:
AppImage
-- resources
---- app.asar
------ node_modules
------ dist (everything except node_modules)
When I launch the application it obviously complains of not finding the node_modules. The docs say there's some default files pattern which can take precedence of what user has defined...
I also have tried:
"files": {
"from": "dist",
"to": "dist",
"filter": ["**/*"]
}
And seems not being able to find the package.json file.
Somebody asked this similar question years ago, with no answer. Any help please?
Finally I found the way to do it, I had to tweak the configuration with a property that I found in the docs: includeSubNodeModules, which defaults to false and seems to include the node_modules subfolders in the output. Also I had to explicitly omit the root node_modules folder since I don't want it in the output.
"build": {
"appId": "myApplication",
"files": [
"dist/**/*",
"!node_modules"
],
"includeSubNodeModules": true
},

Can I tell babel to replace an output directory?

The Situation
I'm working on a project in Node.js and using babel to transpile my code. My package.json has a build command defined like this:
"scripts": {
"build": "yarn run babel src -d lib",
},
The Problem
This transpiles fine, taking the content of src and outputing the result to lib, but there are two issues:
lib will contain old files from past transpiles even if they no longer have a matching file in src.
Babel will not rename files with a changed case if my OS is case insensitive. For example, if I had transpiled a file named src/Foo.js and later renamed it to src/foo.js then future transpiles will still be named lib/Foo.js
The Question
Can I tell babel to wipe away the contents of the lib directory before transpiling or do I need to just insert a rm into the build script?
Babel does not have functionality to do this. It is very common to use a rimraf or some other means to delete the directory before running Babel. rm directly is certainly also an option, but that does get more complicated if you want to support Windows too, hence the rimraf usage.
Babel CLI has a flag to remove the output directory: --delete-dir-on-start
Couldn't find any online documentation for it, but it's listed in babel --help:
--delete-dir-on-start Delete the out directory before compilation
Was added in this PR back in 2017.

Standard way of developing a node module using Coffeescript?

Is there a recommended way of developing a node module if I want to write it in Coffeescript, but don't want to force the module's users to require the coffee-script module?
Put your CoffeeScript codes in the src folder and the compiled JavaScript codes in lib folder.
Then in your package.json file, declare main to be the js file in the lib folder. Then the users of your package will require the js file instead of the coffee file.
You may take #TrevorBurnham's repository as an example.
I ended with only a src folder on my git repository; a .gitignore file with an line for lib; and an empty .npmignore file. The empty .npmignore file is needed because if it's not on your module, your .gitignore is used instead.
I just added a Cakefile with a task to build my src directory using coffee --compile --output lib/ src/ and a pretest and prepublish task to package.json to build before testing and publishing.
"scripts": {
"pretest": "cake build",
"prepublish": "cake build",
}
This solution keeps my git repository clean (without compiled code), but adds my javascript code to lib when publishing to npm.
I'm just getting started with CoffeeScript, but I'd suggest the following:
Store your CoffeeScript code in src/*.coffee,
Write a main.js in the project's root that NPM will catch, and have it simply do something like require('coffee-script'); require('./src/my_lib.coffee').
There. You never, ever compile your code; it's all handled transparently. You don't check compiled code into git, nor do you publish superfluous compiled JavaScript alongside the uncompiled CoffeeScript to NPM.
Edit:
In more recent versions coffee-scirpt, you should require('coffee-script/register');

Resources