How to automatically add a module rule to react-scripts' webpack.config.js when using "npm install" - node-modules

I've built my application with create-react-app, and to make it work I have to add
{
test: /node_modules.+js$/,
loader: require.resolve("ify-loader"),
},
to the module.rules array of react-scripts' webpack.config.js file.
What I should I do to make it add automatically when creating the node_modules folder with the command npm install?

Related

Cache transformed node modules with vite/esbuild

vite build uses esbuild to transform both the package dependencies (node modules) as well as the app source code into the target JavaScript specification, i.e. es2015.
I observe that vite/esbuild re-transform the entire sources in ./node_modules every time vite build is run.
How can this build stack be used to keep and reuse the previously transformed files, at least for the entire ./node_modules folder (given dependencies didn't change of course) so that subsequent vite build command invocations run significantly faster?
One way to improve the performance of subsequent Vite build command invocations is by using a caching mechanism. You can use a caching tool such as cache-loader or hard-source-webpack-plugin to cache the transpilation results of the node modules.
This will allow Vite to reuse the previously transpiled files for the node modules, as long as the dependencies haven't changed.
This can greatly speed up the build process.
You can also try to configure esbuild to only transpile the changed files instead of the entire codebase using the -w or --watch option when running the esbuild command. This option tells esbuild to watch the input files and only transpile the files that have been modified.
In Vite, you can configure the esbuild plugin to use the --watch option by adding the following to your vite.config.js file:
const esbuildConfig = {
watch: true,
};
module.exports = {
esbuild: esbuildConfig,
};
Examples :
cache-loader:
Install the package:
npm install cache-loader --save-dev
In your vite.config.js file, configure the cache-loader to be used for transpiling the node modules by adding it as a rule in the build object:
module.exports = {
build: {
...
css: {
...
},
js: {
...
loaderOptions: {
cache: true,
cacheDirectory: 'node_modules/.cache'
}
},
...
}
}
Run your build command ( vite build )
hard-source-webpack-plugin:
install the package:
npm install hard-source-webpack-plugin --save-dev
In your vite.config.js file, import the plugin and add it to the build.plugins array:
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
module.exports = {
build: {
...
plugins: [
new HardSourceWebpackPlugin()
],
...
}
}
Run your build command (vite build)
These examples, as you asked in your comment, are for Vite versions 2.5.8 and 3.x. However, in order to use them with Vite 3.x you need to update the build config to match the new format.
Please don't hesitate to write a comment if you still have a problem or questions!

Yarn install a single package to single workspace

This is my project set up
proj:
package.json - workspaces["app/frontend", "app/backend"]
app
frontend - package.json
backend - package.json
say I cd to proj
I want to do yarn workspace app/frontend add uuid -dev (add a pkg to one of the workspace)
err is Unknown workspace "app/frontend", wonder what is the correct syntax?
yarn workspace frontend add uuid --save-dev
When you define your workspaces in the package.json you should use relative path to the workspace:
"workspaces": [
"app/frontent",
"app/backend"
]
However, when you refer to your workspace in yarn workspace ... command you should use the package name of this workspace (including namespace).
For example, if your frontend/package.json defines
{
name: "#myproj/frontend".
...
}
you will use
yarn workspace #myproj/frontent add uuid --save-dev

npm run watch - Exclude file

As part of my build script using Laravel Mix, I'm needing to make a modification to one of the files inside the resources/assets/sass directory. The problem is that running npm run watch will then modify this file and the watcher re-runs the build process, causing an infinite recursive loop.
Is there any way to exclude an individual file from being watched for changes?
For example:
webpack.mix.js
// Do something with `resources/assets/sass/_modules.scss` first, then continue
mix.sass('resources/assets/sass/app.scss');
app.scss
#include "_modules.scss";
If you're ok with webpack not automatically updating your relative stylesheet URL's, try this:
In webpack.mix.js, add the following option:
processCssUrls: false
Source: https://github.com/JeffreyWay/laravel-mix/blob/master/docs/options.md

PhpStorm: minifying all .SCSS files into a single .CSS file using watchify.js?

I am developing a browser application using a combination of the following npm build tools
Browserify
Coffeeify
Watchify
The scripts part of my package.json file is:
"scripts": {
"build": "browserify -t coffeeify init.coffee -o ../www/bundle.js",
"watch": "watchify -t coffeeify init.coffee -o ../www/bundle.js"
}
So far, I have configured my project so that before I start working on it, I enter the "npm run watch" command and all CoffeeScript files are automatically bundled into the bundle.js file whenever I update one. Now, I additionally want to have several .SCSS files that get automatically compiled into .CSS files and then all bundled and minified into a min.css file. And, ideally I want to accomplish this without having to enter commands other than then one I'm already entering for my CS files ("npm run watch").
Here are my package.json devDependencies:
"devDependencies": {
"coffee-script": "^1.10.0",
"coffeeify": "^2.0.1",
"browserify": "^12.0.1",
"watchify": "^3.6.0",
"node-sass": "^3.4.2",
"yuicompressor": "^2.4.8"
}
So far, I am at the point where I am successfully compiling my .SCSS files into .CSS files using PhpStorm's file watcher and the node-sass npm module. After some reading, I decided to use the yui compressor (it's installed as a Node module too, as you can see above). However, after a lot of searching, I couldn't find a good description of how to do this.
Am I supposed to update the:
"watch": "watchify -t coffeeify init.coffee -o ../www/bundle.js"
line in my package.json file? If so, how?
I want the final result to be such that whenever a file from the SCSS folder is changed, the min.css file in some other folder is automatically recompiled.
(I don't necessarily insist on using the yui compressor, if there is a better option for my purposes.)

Are there ways to perform postCSS processing withouth gulp or grunt. Only with help of maven?

I want to perform postprocessing in my project with help of postCSS.
As I'm new in frontend I read only ways to perform it by frontend build system (grunt or gulp).
But maybe the ways to postpocess only with maven?
You can use postcss-cli to run it through the command line.
The command usage is pretty straight-forward.
postcss [options] [-o output-file|-d output-directory] [input-file]
Also, if you are using npm along with a package.json file, I would advise you to add a run-script:
{
"name": "my-app",
"script": {
"css": "postcss your options -go here"
},
"dependencies": {
"postcss":"^4.1.13"
}
}
So you can simply run npm run css from maven / CLI without having to worry about prefixing your command node_modules/bin and having your options in maven.

Resources