I have a React app that imports a library (in the node_modules directory, of course).
The name of the library is @lib/schema
.
In the react app, modules of this library are imported like so:
import { module_name } from "@lib/schema"
There is a certain file in the @lib/schema
that is currently being included in the final bundle, which is located in this path:
src/path/to/large_file.js
The file is never used by the app and thus unnecessarily increases the size of the bundle.
The React app currently uses react-scripts
to create the bundle.
Questions:
react-scripts
, how do I configure it to exclude src/path/to/large_file.js
?webpack
, how do I configure it to exclude src/path/to/large_file.js
?If you want to completely avoid importing the file, you can create an alias for it that points to an empty module.
1.Modify your Webpack configuration:
In your webpack.config.js, add an alias for the large file:
const path = require('path');
module.exports = {
// ... other configurations
resolve: {
alias: {
'@lib/schema/src/path/to/large_file.js': path.resolve(__dirname,
'path/to/empty.js'),
},
},
};
2.Create an empty module:
Create a file named empty.js in the specified path (e.g., path/to/empty.js)
Conclusion By following the above methods, you can effectively exclude src/path/to/large_file.js from your final bundle, whether you are using react-scripts or a custom Webpack configuration. Choose the method that best fits your project's setup.