We have a React component that uses data provided by some long computation. The provided data (result of a long computation) is always the same (for all users).
What i want to do is to ensure that this computation is executed once (during minification), instead of every time user loads the page.
Webpack uses TerserPlugin for minification by default, when in production mode.
Is TerserPlugin clever enough to figure out that the long computation always returns the same result and optimise it?
If so, is there a way to check that the computation does not execute at runtime? console.log would definitely prevent the optimisation.
If not, maybe another plugins can do that? For example UglifyJS.
Simplified webpack configuration:
...
module.exports = {
mode: 'production',
entry: {
app: ['...'],
},
output: {
path: '...',
filename: "[name].bundle.js",
publicPath: "/"
},
module: {
rules: [
{
test: /\.html$/,
loader: "file-loader?name=[name].[ext]"
},
{
test: /\.(jsx?|tsx?)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
}
},
...
]
},
resolve: {
extensions: [.ts", ".tsx", ".js", ".jsx"],
modules: [
path.join(__dirname, "./src"),
]
},
};
The only way i can imagine is to create a script that creates a json file with desired data
Is this a good solution or is there a better, simpler one? Some downsides are for example: linter may complain that imports don't exist, more complicated setup.
Here is a demo with example use case.
If the data is the same within all the users, you should cache the data on the backend if a backend is present.
If the data is unique the each client but you want the client to do less computing, look at react.memo.
Related
I'm setting up react with webpack, here is my webpack config file \n
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: path.join(__dirname, "src", "index.js"),
devtool: 'sourcemaps',
cache: true,
mode: 'development',
output: {
path:path.resolve(__dirname, "dist"),
filename: './dist/bundle.js'
},
module: {
rules: [
{
test: /\.?js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['#babel/preset-env', '#babel/preset-react']
}
}
},
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "public/templates/", "budget_account2.html"),
}),
],
}
when i run npm run-script watch then it throw error: \n
Invalid configuration object. Webpack has been initialized using a
configuration object that does not match the API schema.
configuration.devtool should match pattern "^(inline-|hidden-|eval-)?(nosources-)?(cheap-(module-)?)?source-map$".
BREAKING CHANGE since webpack 5: The devtool option is more strict.
Please strictly follow the order of the keywords in the pattern.
Thanks all
It's exactly as the error says.
configuration.devtool should match pattern "^(inline-|hidden-|eval-)?(nosources-)?(cheap-(module-)?)?source-map$".
See a description of the various options here.
To follow it, your devtool value should:
Optionally start with inline-, hidden-, or eval-, depending on where you want the source map to be
Optionally followed by nosources-, if you want the source code to be excluded from the map
Optionally followed by cheap or cheap-module - cheap indicates a fast rebuild time, and module preserves the original lines
Followed by source-map at the end.
Figure out which one(s) of the above you want, construct a string that matches, and use that. For a random example, you could have inline-nosources-cheap-source-map.
(You may also use none, to omit source maps entirely)
I have a website that developed using react which has a single page, but the production bundle size is 1.11 MiB. I'm using firestore,firebase storage, material-UI, react-redux for this app app works well everything is fine except the bundle size.
I used webpack-bundle-analyzer to analyse the bundle size, It seems like nodemodules tooks large size. Here I have added the screenshot.
My webpack config file
const path = require('path');
var CompressionPlugin = require('compression-webpack-plugin');
const TerserPlugin = require("terser-webpack-plugin");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
mode: 'production',
entry: './src/public/index.js',
plugins: [
new BundleAnalyzerPlugin({
generateStatsFile : true
}),
new CompressionPlugin({
deleteOriginalAssets: false,
filename: "[path][base].gz",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
// Compress all assets, including files with `0` bytes size
// minRatio: Infinity
// Compress all assets, excluding files with `0` bytes size
// minRatio: Number.MAX_SAFE_INTEGER
minRatio: 0.8,
})
],
optimization: {
nodeEnv: 'production',
minimize: true,
concatenateModules: true,
},
module: {
rules: [{
test: /\.(jpe?g|png|gif|svg)$/,
loader: 'image-webpack-loader',
// This will apply the loader before the other ones
enforce: 'pre',
},
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env'],
plugins: [
["#babel/plugin-proposal-object-rest-spread", {
"definitions": {
"process.env": {
"NODE_ENV": "\"production\""
}
}
}],
['babel-plugin-transform-imports',
{
'#material-ui/core': {
// Use "transform: '#material-ui/core/${member}'," if your bundler does not support ES modules
'transform': '#material-ui/core/esm/${member}',
'preventFullImport': true
},
'#material-ui/icons': {
// Use "transform: '#material-ui/icons/${member}'," if your bundler does not support ES modules
'transform': '#material-ui/icons/esm/${member}',
'preventFullImport': true
}
}
]
]
}
}
}
]
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js'
}
};
I don't know what I missed here to reduce node-modules size. If we have any other option to reduce bundle size please give me a suggestion for that.
MaterialUI, lodash and webpack support Tree Shaking.
Since you used webpack-bundle-analyser, I am assuming your build process utilises webpack. So your options are the following:
You can use Webpack's Tree Shaking Guide.
Material UI also has a Minimizing Bundle Size Guide.
In general, a large part of bundle size reduction is dead code elimination.
Once you have done that, you might want to follow React's Code Splitting guide to ensure that the initial load is faster.
You could import lodash funcions separataly
import get from 'lodash/get'
instead of
import { get } from 'lodash'
But I bet this will not reduce drastically your bundle size
If you want to reduce the build size, you can import functions in this way
import groupBy from 'lodash/groupBy';
Instead of this
import { groupBy } from 'lodash';
The former will fetch the contents of the entire lodash library and the latter will only fetch one specific chunk of the it. Thus reducing the build sizes by a huge amount.
Make sure you run webpack in production mode. To do that, you run webpack with the -p flag, like this:
webpack -p
This little flag does these two things to optimize your bundle size:
Enable minification using UglifyJs. It removes unnecessary white
spaces, new lines, unreachable code, etc.
Set the NODE_ENV to “production”. This tells some packages, like
React, to not include debug code.
if it doesn't work for you please try this
Being new to webpack, the answer might be staring me down but I don't see it. No matter how I try to pass them along, the file-loader options aren't found.
I'm using file-loader and I'm trying to pass a publicPath (or simply anything, at first) along as an option. I went into the file loader source code and added a log for all the options it detected, but they always come up empty.
webpack.config.prod.js
var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'idlink-1.1.1.js',
publicPath: ''
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.DefinePlugin({'process.env.NODE_ENV': '"production"'})
],
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel',
query: { presets: ['react', 'es2015', 'stage-1'] }
},
{test: /\.css$/, loader: "style-loader!css-loader" },
{test: /\.scss$/, loaders: ["style", "css", "sass"]},
{test: /\.less$/, loader: "style-loader!css-loader!less-loader" },
{
test: /\.(jpe?g|png|gif|svg|pdf)$/i,
loader: "file",
options: { publicPath: 'https://apps.ixordocs.be/'}
},
{test: /\.gif$/, loader: "url-loader?mimetype=image/png" }
]
},
}
I've also tried with
loader: "file-loader"
as well as added the options as one string like this
loader: "file?name=[name].[ext]&publicPath=https://apps.ixordocs.be/"
Some context info:
I don't want to have a hardcoded publicPath defined in my output: {}, i want to grab it dynamically from a parameter placed on the div that my plugin is loaded into.
I've tried using the __webpack_public_path__ variable and it works, but not for images. The public path is grabbed from the parameter, set, and used to fetch a chunk. Somehow it has no effect on images though. If I hardcode a publicPath under output, it DOES work for images. This leads me to believe there is a problem with the loader's communication to the variable, so the idea is to get the options working and eventually try to pass a dynamic publicPath in there.
Your question is totally valid based on the documentation of the loader on both loader's GitHub repo and webpack docs. The problem is the publicPath and outputPath features are implemented in a pull request that is merged but not yet released to a new version of loader, and the README on npm doesn't mention the features for the same reason.
You can still use these features by installing from the GitHub repo with npm install webpack/file-loader --save-dev and your options should work. If not try replacing options with query.
{
test: /\.(jpe?g|png|gif|svg|pdf)$/i,
loader: "file-loader",
query: { publicPath: 'https://apps.ixordocs.be/'}
}
Using URLs for publicPath is also valid because it happens often that you want to load your assets from a CDN or another server.
So I have just started using mocha and chai (testing in browser), currently my webpack.config file for my app looks like this:
var webpack = require('webpack');
module.exports ={
entry: ["./js/app.js"],
output: {
path: "./js",
filename:"bundle.js"
},
module:{
loaders: [
{
test: [/\.es6$/, /\.js$/],
exclude: /(css|plugins|node_modules)/,
loader: 'babel-loader',
query: {
cacheDirectory: true,
presets: ['react', 'es2015']
}
},
{
test: [/\.scss$/],
loaders: ['style', 'css', 'sass']
}
]
},
plugins:[
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
],
resolve: {
extensions: ['', '.js', '.es6', '.jsx']
},
watch: false
}
Now my test folder is in the root directory, but let's say I want to use import/export, and import some react component for testing, what is the best practice?
do I have to create multiple entry point and output? One for my main app, one for the testing stuff, but how do I output the bundles in different directory. Or should I just create another webpack.config for the testing folder itself?
Each test you write will be in its own .js file. Your test runner will execute each of your test files independently.
Also your tests will not reference your bundle but, instead, import the required file(s) directly. You don't want to include the entire bundle during testing, as that adds a lot of code you do not need and can muddy your test development. Including only what is necessary means there is a much smaller footprint on what can interfere with your test results.
A useful link that covers getting setup for testing React components can be found:
https://www.toptal.com/react/how-react-components-make-ui-testing-easy
Also, Facebooks page for Reacts testing utilities:
https://facebook.github.io/react/docs/test-utils.html
I have followed as many tips as I can find in packaging my Webpack ReactJS app for production. Unfortunately, the file size is still 3MB. What am I doing wrong?
Here is my Webpack Config file:
var path = require('path')
var webpack = require('webpack')
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'webpack-hot-middleware/client',
'./index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle-webpack.js',
publicPath: './'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
screw_ie8: true,
warnings: false
}
})
],
module: {
loaders: [
{
test: /\.js$/,
loaders: [ 'babel' ],
exclude: /node_modules/,
include: __dirname
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{test: /node_modules\/react-chatview/, loader: 'babel' }
]
}
}
Any help would be greatly appreciated!
I use the following command to package it:
> NODE_ENV=production webpack -p
I get the following output:
bundle-webpack.js 3.1 MB 0 [emitted] main
Best,
Aaron
Looks like you've still got a fair amount of dev stuff there, e.g. hot module replacement.
Take a look at webpack.config.prod.js at React Transform Boilerplate as a guide.
You may also be able to optimise your imports by including only the parts of the packages you need and leaving out the rest. See: Reduce Your bundle.js File Size By Doing This One Thing .
So, it turns out that David L. Walsh was correct that I had too much development stuff in my app. However, the answer provided did not resolve the issue.
I resolved the issue using 3 steps.
Remove all the "hot-reloading" plugins from my webpack configuration, as David instructed.
Remove the hot reloading "react-transform" plugin from my .babelrc file.
Change the "devtool" parameter to "source-map" from "cheap-module-eval-source-map"
After following those steps, the final bundle file was 340kb while the source map was still 3MB. Fortunately, I don't have to include the source map in my application, so it can be downloaded at 340kb, which is still fairly large, but reasonable for modern browsers running on modern internet connections.
I would up-vote David's answer, but I don't have enough reputation points yet to do so.