Babel preset used for internal Gatsby packages, providing JavaScript/TypeScript compilation with browser and Node.js environment targeting
npx @tessl/cli install tessl/npm-babel-preset-gatsby-package@3.15.0Babel preset used for internal Gatsby packages, providing JavaScript/TypeScript compilation with browser and Node.js environment targeting. This preset configures a comprehensive compilation pipeline with modern JavaScript features, React JSX, Flow types, TypeScript support, and custom Gatsby-specific transformations.
npm install --save-dev babel-preset-gatsby-packageconst preset = require("babel-preset-gatsby-package");For use in Babel configuration:
{
"presets": ["babel-preset-gatsby-package"]
}{
"presets": ["babel-preset-gatsby-package"]
}{
"presets": [
["babel-preset-gatsby-package", {
"browser": true,
"nodeVersion": "18.0.0",
"debug": false,
"esm": true
}]
]
}const preset = require("babel-preset-gatsby-package");
const config = preset(null, {
browser: true,
debug: true,
nodeVersion: "16.0.0",
availableCompilerFlags: ["GATSBY_MAJOR", "CUSTOM_FLAG"],
keepDynamicImports: ["/path/to/file.js"]
});The preset configures the following Babel components:
@babel/preset-env with configurable browser/Node.js targets@babel/preset-react for JSX transformation@babel/preset-flow for Flow annotations and TypeScript plugin for .ts/.tsx files@babel/plugin-transform-runtime for efficient polyfillsMain preset factory function that generates Babel configuration based on target environment and options.
/**
* Creates Babel configuration for internal Gatsby packages
* @param {object} context - Babel context (typically unused)
* @param {PresetOptions} options - Configuration options
* @returns {BabelConfig} Complete Babel configuration object
*/
function preset(context, options = {});
interface PresetOptions {
/** Target browser environment instead of Node.js (default: false) */
browser?: boolean;
/** Enable debug output for preset-env (default: false) */
debug?: boolean;
/** Node.js version to target (default: "18.0.0") */
nodeVersion?: string;
/** Output ES modules instead of CommonJS (default: false) */
esm?: boolean;
/** Available compiler flags for conditional compilation (default: ["GATSBY_MAJOR"]) */
availableCompilerFlags?: string[];
/** File paths where dynamic imports should be preserved (default: null) */
keepDynamicImports?: string[] | null;
}
interface BabelConfig {
/** Array of Babel presets with configuration */
presets: Array<string | [string, object]>;
/** Array of Babel plugins with configuration */
plugins: Array<string | [string, object] | false>;
/** File-specific overrides for TypeScript/TSX */
overrides: Array<{
test: string[];
plugins: Array<[string, object]>;
}>;
}The preset automatically configures different targeting modes:
Browser Mode (browser: true):
esmodules: true when esm: trueNode.js Mode (default):
esm: trueCustom build-time constant replacement system using the COMPILER_OPTIONS environment variable.
// Environment variable format: "KEY=value,KEY2=value2"
// Default: COMPILER_OPTIONS="GATSBY_MAJOR=5"
// In source code, compiler flags are replaced:
const version = _CFLAGS_.GATSBY_MAJOR; // becomes "5" at build timeEnvironment Variables:
COMPILER_OPTIONS: Comma-separated key=value pairs for compiler flagsNODE_ENV / BABEL_ENV: Controls test environment behaviorNODE_ENV or BABEL_ENV equals "test", enables babel-plugin-dynamic-import-nodeControls whether dynamic imports are transformed or preserved for specific files.
// Configuration
{
keepDynamicImports: ["/absolute/path/to/file.js", "/another/file.js"]
}
// In preserved files, dynamic imports remain untransformed:
const module = import("./dynamic-module"); // stays as-isConfigured with:
Configured for JSX transformation with default settings.
Configured for Flow type annotation removal.
// Always included plugins:
"@babel/plugin-proposal-nullish-coalescing-operator"
"@babel/plugin-proposal-optional-chaining"
"@babel/plugin-transform-runtime"
"@babel/plugin-syntax-dynamic-import"
"@sigmacomputing/babel-plugin-lodash"
// Test environment only:
"babel-plugin-dynamic-import-node" // when NODE_ENV/BABEL_ENV === "test"Replaces _CFLAGS_.FLAGNAME expressions with string literals based on COMPILER_OPTIONS.
// Plugin configuration
[
"./babel-transform-compiler-flags",
{
flags: { GATSBY_MAJOR: "5" }, // from COMPILER_OPTIONS
availableFlags: ["GATSBY_MAJOR"] // from preset options
}
]Preserves dynamic imports in specified files by preventing their transformation.
// Plugin configuration (when keepDynamicImports is provided)
[
"./babel-transform-mark-to-keep-dynamic-import",
{
keepDynamicImports: ["/absolute/path/to/file.js"]
}
]Automatic TypeScript/TSX compilation through file-specific overrides:
// Override configuration for .ts/.tsx files
{
test: ["**/*.ts", "**/*.tsx"],
plugins: [["@babel/plugin-transform-typescript", { isTSX: true }]]
}{
"presets": [
["babel-preset-gatsby-package", {
"debug": true,
"nodeVersion": "18.0.0"
}]
]
}{
"presets": [
["babel-preset-gatsby-package", {
"browser": true,
"esm": true
}]
]
}# Set environment variable
export COMPILER_OPTIONS="GATSBY_MAJOR=5,CUSTOM_FLAG=production"{
"presets": [
["babel-preset-gatsby-package", {
"availableCompilerFlags": ["GATSBY_MAJOR", "CUSTOM_FLAG"]
}]
]
}{
"presets": [
["babel-preset-gatsby-package", {
"keepDynamicImports": [
"/app/src/dynamic-loader.js",
"/app/src/lazy-components.js"
]
}]
]
}