How should i exclude all node_modules in electron-packager - node.js

I'm using electron-packager for creating an electron.exe of my app. I need to exclude all node modules . I tried the following
--ignore=node_modules
The above is not working. Any idea how to exclude all folders/ remove node modules in final build.

If you install modules as devDependencies, they will all be pruned automatically before packaging.
If you're using electron-builder you can define glob patterns as files in the config. In this case !**/node_modules/* will exclude all of node_modules.
If you're using electron-packager you can use the ignore option and regular expression to exclude files.

Related

How to remove cpp files from production build via webpack?

I use webpack 4 and electron-builder to bundle and build my Electron app. I noticed that native node modules inside the node_modules directory of the app.asar bundle still contain their C++ source files.
Is there a way to exclude certain file extensions from the build step?
electron-builder can exclude files in the files section of your package.json.
Default pattern / is not added to your custom if some of your patterns is not ignore (i.e. not starts with !). package.json and /node_modules// (only production dependencies will be copied) is added to your custom in any case. All default ignores are added in any case — you don’t need to repeat it if you configure own patterns.
Example
"!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme}",

Pack & unpack node_modules of an Electron Native application to be patched

First of all, I am generating an electron native application using electron-builder, npm and npx.
The next commands/steps are being executed to compile and generate the electron native application:
npm run build -- --prod --build-optimizer (to compile app)
npx electron-builder build --windows (to generate an electron windows app)
Later, I obtain a myApp folder which contains:
/win-unpacked
electron-builder-effective-config.yaml
myApp.exe
My application uses the node_modules folder which contains all node dependencies used in my application.
My question is: are there any way to unpack the native application or similar and patch new changes inside node_modules?
After perform an investigation, I have discovered that node dependencies are packed in system cache inside an app.asar file which contains a dist folder with some .js files.
In developer tools:
Inside app.asar:
Are there any way to "deploy" node modules folder with the aim to perform patch operations of each package and change the code inside node modules folders?
I will appreciate any kind of help.
Asar is a read only archiv format, so you cant patch any files in the archive.
But what you can do is to disable the asar option in your build config.
So in your package.json define it like this:
"build": {
"appid": "........",
"win": {........},
"asar": false
}
if you build this, there is no asar archive anymore and you can overwrite any file...
what you can also do is using asar programatically. So you can unpack the asar archive, updating files and package new archive. See here how you can use it
Contrary to what is being said here, patching a .asar archive is totally possible. I have published a library on NPM called patch-asar that does specifically this.

TypeScript build into single file with all external dependencies (node_modules) within it

Is it possible to build a TS source into a single file, which would also contain node_modules imported packages' source?
That would be very helpful in a serverless project. I have done this before on a non-TS project but was using webpack (for another reason).
It seems this was briefly possible before but was due a bug https://github.com/Microsoft/TypeScript/issues/13414 ?
You will need to use a bundler such as webpack to bundle your compiled code and all your node_modules dependencies. The TypeScript compiler (tsc) just transforms TypeScript code into JavaScript, and won't deal with bundling.

Precompile jsx for React in django-compressor

Using Cygwin on Windows 10 (64 bits)
Installed babel-cli, react and babel-preset-react on top of Node.js, which was installed via Windows Installer (since there is no support for Cygwin.) I installed those Node packages using yarn, in the global modules folder.
In the django-compressor documentation (https://django-compressor.readthedocs.io/en/latest/reactjs/) they say that a precompiler setting does the trick:
COMPRESS_PRECOMPILERS = (
('text/jsx', 'cat {infile} | babel > {outfile}'),
)
However, babel is not recognizing jsx (throwing errors when encountering virtual dom elements.) It's obvious because I am not passing the react preset to the command. But there is no way I can use that preset because I installed in the global module folder and now I am unable to make babel find and use it.
I need one of these possible solutions:
How can I make babel use a preset globally installed (how should I use babel --presets react in a way that works)?
How do I use a .babelrc file in a Django project?
If I were to install the preset locally (which I seem to dislike a lot) how do I make it live with my Django project without making mess out of my project directory structure?
Okay, I banged my head a few times and found a solution:
There is no way to make babel work with presets installed globally. It is mandatory to install them locally. All react, react-dom, babel-core, babel-loader, babel-preset-react (and perhaps babel-preset-es2015 if latest ECMA syntax is desired) must be installed locally.
There is no place to put a .babelrc file where the precompiler would use it. No use to have a babel section in package.json either. Instead, the precompiler setting should pass the presets in command line like:
COMPRESS_PRECOMPILERS = (
('text/jsx', 'cat {infile} | babel --presets react,es2015 > {outfile}'),
)
Just assume that the folder where manage.py lives is also the root for Nodejs packages. Not tested when deployed and running from a wsgi file but maybe the wsgi should also live next to manage.py.
Also, it looks like all these files and folders: node_modules, package.json and yarn.lock (if installing with yarn as I am,) must be included in version control.
EDITED:
I no loger think node_modules should go into version control.

is good practice to include my modules in node_modules for make require search easy,if not why not?

is good practice to include my modules in node_modules for make require search easy,if not why not?
Explanation:
In node.js cms calipso( https://github.com/cliftonc/calipso)
their modules not inside node_modules: then the include the modules without auto option:
calipso = require(path.join(rootpath, 'lib/calipso'));
vs if it was inside node_modules:
calipso = require('calipso');
node_modules is typically ignored in the version control (GIT, etc) and most of the developers assume that this folder contains only packages listed in the package.json. I can imagine the approach on updating the modules just by removing this folder completely and executing npm install. Considering these I would rather say that keeping own modules in node_modules is not consistent with the node.js workflow.
Update: this is assuming that "my modules" is actually just a set of files. If your modules are "npm" modules, that can be restored by executing "npm install" then this is completely fine to keep them in node_modules.

Resources