Specialized Babel preset for processing dependencies with specific optimization for node_modules transformation in Gatsby projects. This preset is designed to handle third-party code with different configuration than application code.
Creates a Babel configuration specifically optimized for transforming dependencies (node_modules).
/**
* Dependencies-specific Babel preset factory
* @param {*} _ - Unused first parameter
* @param {DependenciesOptions} options - Configuration options
* @returns {DependenciesBabelConfig} Babel configuration optimized for dependencies
*/
function dependenciesPreset(_, options = {});
interface DependenciesOptions {
/** Build stage for different Gatsby build phases */
stage?: "build-javascript" | "build-html" | "develop" | "develop-html";
}
interface DependenciesBabelConfig {
sourceType: "unambiguous";
presets: Array<[string, object]>;
plugins: Array<string | [string, object]>;
}Usage Examples:
// Basic usage in Webpack configuration
{
test: /\.js$/,
include: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
["babel-preset-gatsby/dependencies", { stage: "build-javascript" }]
]
}
}
}
// Programmatic usage
import dependenciesPreset from "babel-preset-gatsby/dependencies";
const config = dependenciesPreset(null, {
stage: "build-javascript"
});Uses "unambiguous" source type to handle both CommonJS and ES Modules in dependencies:
{
sourceType: "unambiguous"
}This allows Babel to automatically detect the module format and handle it appropriately.
@babel/preset-env: Environment-specific transformations optimized for dependencies
[
"@babel/preset-env",
{
useBuiltIns: "usage",
corejs: 3,
modules: false,
targets: /* browser targets from cached config */,
exclude: [
"transform-typeof-symbol",
/* ...other excluded transforms */
]
}
]Core Plugins:
@babel/plugin-transform-runtime: Runtime helper optimization with absolute path
@babel/plugin-syntax-dynamic-import: Syntax support for dynamic imports
Production-Only Plugins:
stage === "build-javascript"The preset behaves differently based on the build stage:
develop, develop-html)// Development configuration
const devConfig = dependenciesPreset(null, { stage: "develop" });
// - Uses browser targets from cached config
// - Keeps PropTypes for debugging
// - Optimizes for fast rebuildsbuild-javascript, build-html)// Production configuration
const prodConfig = dependenciesPreset(null, { stage: "build-javascript" });
// - Includes PropTypes removal plugin
// - Optimizes for bundle size
// - Uses production browser targetsThe preset includes optimized @babel/plugin-transform-runtime configuration:
[
"@babel/plugin-transform-runtime",
{
corejs: false, // No core-js polyfills (handled by preset-env)
helpers: true, // Transform helper functions
regenerator: true, // Transform async/await and generators
useESModules: true, // Use ES module helpers
absoluteRuntime: path // Use specific runtime version
}
]Uses absolute path to ensure consistent @babel/runtime version:
const absoluteRuntimePath = path.dirname(
require.resolve("@babel/runtime/package.json")
);This prevents version conflicts when different dependencies expect different runtime versions.
Uses loadCachedConfig() to access Gatsby's browser targets:
const pluginBabelConfig = loadCachedConfig();
const targets = pluginBabelConfig.browserslist;Typically configured in Gatsby's Webpack configuration for node_modules processing:
// Example Webpack rule (from Gatsby internals)
{
test: /\.jsx?$/,
include: modulePath => {
// Include node_modules that need transformation
return /node_modules/.test(modulePath);
},
use: {
loader: "babel-loader",
options: {
presets: [
["babel-preset-gatsby/dependencies", { stage }]
]
}
}
}Excludes transforms that make code slower:
transform-typeof-symbol: Often unnecessary and impacts performanceUses useBuiltIns: "usage" with core-js@3:
Preserves ES modules (modules: false) for better tree-shaking:
The preset handles common dependency transformation issues:
Using sourceType: "unambiguous" handles mixed module formats:
// Handles both formats automatically
// CommonJS
module.exports = something;
// ES Modules
export default something;Absolute runtime path prevents resolution issues:
// Prevents errors like:
// Cannot resolve '@babel/runtime/helpers/...'For advanced use cases, you can extend the dependencies preset:
// Custom Webpack configuration
{
test: /\.js$/,
include: /specific-package/,
use: {
loader: "babel-loader",
options: {
presets: [
["babel-preset-gatsby/dependencies", { stage: "build-javascript" }]
],
plugins: [
// Additional plugins for specific dependencies
["transform-specific-feature", { option: true }]
]
}
}
}Target specific packages or patterns based on your needs:
// Transform only specific problematic packages
{
test: /\.js$/,
include: modulePath => {
return /node_modules\/(package1|package2)/.test(modulePath);
},
use: {
loader: "babel-loader",
options: {
presets: [["babel-preset-gatsby/dependencies"]]
}
}
}