Easy modular Lodash builds. Adds the Lodash webpack & Babel plugins to your Gatsby build
npx @tessl/cli install tessl/npm-gatsby-plugin-lodash@6.15.0Gatsby Plugin Lodash adds the Lodash webpack and Babel plugins to your Gatsby build for easy modular, small Lodash builds. This plugin automatically optimizes Lodash usage by tree-shaking unused features and transforming imports to reduce bundle size.
npm install gatsby-plugin-lodash lodashThis plugin does not export any functions for direct import. It works as a Gatsby plugin that automatically configures your build process.
Add the plugin to your gatsby-config.js:
// Simple usage - enables all Lodash features
module.exports = {
plugins: [`gatsby-plugin-lodash`]
};With options to disable specific features:
// Advanced usage - disable specific feature sets to reduce bundle size
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-lodash`,
options: {
disabledFeatures: [`shorthands`, `cloning`]
}
}
]
};Gatsby Plugin Lodash optimizes Lodash usage through a dual-plugin approach:
build-javascript stage, lodash-webpack-plugin is configured to enable tree-shaking of unused Lodash features based on the specified feature sets@sigmacomputing/babel-plugin-lodash transforms Lodash imports during the Babel compilation phase to ensure optimal module resolutionThis architecture ensures that only the Lodash utilities actually used in your code are included in the final JavaScript bundle, significantly reducing bundle size without requiring manual import optimization.
Automatically configures the lodash-webpack-plugin during the build-javascript stage to enable tree-shaking of unused Lodash features.
/**
* Gatsby lifecycle hook that configures webpack with lodash-webpack-plugin
* @param {object} context - Gatsby webpack config context
* @param {object} context.actions - Gatsby actions for setting webpack config
* @param {string} context.stage - Current build stage
* @param {object} options - Plugin options
* @param {string[]} options.disabledFeatures - Array of feature sets to disable
*/
function onCreateWebpackConfig(
{ actions, stage },
{ disabledFeatures = [] }
): void;Automatically configures @sigmacomputing/babel-plugin-lodash for import transformations.
/**
* Gatsby lifecycle hook that configures Babel with @sigmacomputing/babel-plugin-lodash
* @param {object} context - Gatsby Babel config context
* @param {object} context.actions - Gatsby actions for setting Babel config
*/
function onCreateBabelConfig({ actions }): void;interface PluginOptions {
/** Array of Lodash feature sets to disable for smaller bundle size */
disabledFeatures?: string[];
}Available Feature Sets:
All feature sets are enabled by default. You can selectively disable them by including their names in the disabledFeatures array:
"shorthands" - Lodash shorthand syntax support"cloning" - Object cloning functionality"currying" - Function currying support"caching" - Caching functionality"collections" - Collection methods"exotics" - Exotic object support"guards" - Type guard functions"metadata" - Metadata functionality"deburring" - String deburring"unicode" - Unicode support"chaining" - Method chaining"memoizing" - Memoization support"coercions" - Type coercions"flattening" - Array/object flattening"paths" - Object path utilities"placeholders" - Placeholder functionalityUsage Examples:
// Disable shorthand syntax and cloning to reduce bundle size
{
resolve: `gatsby-plugin-lodash`,
options: {
disabledFeatures: [`shorthands`, `cloning`]
}
}
// Minimal Lodash build - disable many features
{
resolve: `gatsby-plugin-lodash`,
options: {
disabledFeatures: [
`shorthands`, `cloning`, `currying`, `caching`,
`exotics`, `guards`, `metadata`, `deburring`,
`unicode`, `chaining`, `memoizing`, `coercions`,
`flattening`, `placeholders`
]
}
}Webpack Plugin: During the build-javascript stage, the plugin adds lodash-webpack-plugin to your webpack configuration with the specified feature sets enabled/disabled.
Babel Plugin: The plugin configures @sigmacomputing/babel-plugin-lodash in your Babel pipeline to transform Lodash imports for optimal tree-shaking.
Automatic Optimization: Together, these plugins ensure that only the Lodash utilities you actually use are included in your final bundle, significantly reducing JavaScript bundle size.
The plugin automatically manages these dependencies:
lodash-webpack-plugin@^0.11.6 - Webpack plugin for modular Lodash builds@sigmacomputing/babel-plugin-lodash@^3.3.5 - Babel plugin for Lodash import transformations@babel/runtime@^7.20.13 - Babel runtime helpersgatsby@^5.0.0-next - Compatible with Gatsby v5 and abovelodash - You must install Lodash separately to use its utilities