PostCSS plugin to define global data that will be injected into PostCSS for use in other plugins.
npx @tessl/cli install tessl/npm-csstools--postcss-global-data@3.1.0PostCSS Global Data is a PostCSS plugin that enables temporary injection of global CSS data into the PostCSS processing pipeline without affecting the final output. This plugin serves as a data provider for other PostCSS plugins that need access to global CSS rules (such as custom media queries, custom properties, or other CSS variables) to generate appropriate fallbacks or perform transformations.
npm install @csstools/postcss-global-data --save-dev (requires postcss as peer dependency)import postcssGlobalData from '@csstools/postcss-global-data';For CommonJS:
const postcssGlobalData = require('@csstools/postcss-global-data');For TypeScript usage with PostCSS types:
import postcssGlobalData, { type pluginOptions } from '@csstools/postcss-global-data';
import type { PluginCreator, Plugin, Root } from 'postcss';import postcss from 'postcss';
import postcssGlobalData from '@csstools/postcss-global-data';
postcss([
postcssGlobalData({
files: [
'./src/css/variables.css',
'./src/css/media-queries.css',
]
})
]).process(YOUR_CSS /*, processOptions */);Creates a PostCSS plugin instance that injects CSS from specified files during processing.
/**
* Creates a PostCSS plugin that injects global CSS data
* @param opts - Configuration options for the plugin
* @returns Plugin creator function or configuration object with plugins array
*/
function postcssGlobalData(opts?: pluginOptions): PluginCreator<pluginOptions> | PluginConfigWithLateRemover;
// When lateRemover: false (default), returns a standard plugin creator
interface PluginCreator<T> {
postcssPlugin: string;
prepare(): Plugin;
}
// When lateRemover: true, returns object with plugins array
interface PluginConfigWithLateRemover {
postcssPlugin: string;
plugins: [PluginCreator<pluginOptions>, LateRemoverPlugin];
}
interface LateRemoverPlugin {
postcssPlugin: string;
OnceExit(root: Root): void;
}
// Referenced PostCSS types
interface Plugin {
postcssPlugin: string;
Once?(root: Root, helpers: Helpers): void;
OnceExit?(root: Root): void;
}
interface Root {
// PostCSS AST root node
}
interface Helpers {
result: Result;
postcss: typeof postcss;
}
interface Result {
// PostCSS result object containing messages and other metadata
messages: Array<{ type: string; plugin: string; file?: string; parent?: string }>;
}Configuration Options:
export type pluginOptions = {
/** List of files to be used as context */
files?: Array<string>;
/** Remove nodes in a separate plugin object, this object can be added later in your list of plugins */
lateRemover?: boolean;
/** Add global CSS to the start of files, defaults to `false` */
prepend?: boolean;
};Standard Mode (lateRemover: false)
Returns a single PostCSS plugin that injects CSS during the Once phase and removes it during the OnceExit phase.
const plugin = postcssGlobalData({
files: ['./global.css'],
prepend: false
});
postcss([plugin]).process(css);Late Remover Mode (lateRemover: true)
Returns an object with a plugins array containing two separate plugins, allowing other plugins to run between injection and removal.
const globalDataConfig = postcssGlobalData({
files: ['./global.css'],
lateRemover: true
});
const [globalData, globalDataLateRemover] = globalDataConfig.plugins;
postcss([
globalData,
// other plugins can access the injected data here
someOtherPlugin(),
globalDataLateRemover
]).process(css);The plugin supports multiple file path formats for flexible module resolution:
path.resolve(filePath)node_modules:// prefix: Uses require.resolve() to locate package filesnode_modules: prefix: Alternative syntax for package resolutionpostcssGlobalData({
files: [
'./local-file.css', // Relative path
'/absolute/path/to/file.css', // Absolute path
'node_modules://package-name/file.css', // Package file with :// syntax
'node_modules:package-name/file.css' // Package file with : syntax
]
});Specifies which CSS files to inject into the PostCSS processing pipeline.
files?: Array<string>[] (empty array)node_modules: prefixed pathsControls the plugin execution strategy. When true, returns separate injection and removal plugins for advanced plugin ordering.
lateRemover?: booleanfalsefalse: Returns single plugin object with built-in cleanuptrue: Returns object with plugins array containing two separate pluginsDetermines whether injected CSS is added before or after existing content. When true, CSS is prepended; when false, it's appended.
prepend?: booleanfalsefalse: CSS is appended after existing contenttrue: CSS is prepended before existing contentWarning: Prepending styles before @import statements will create invalid stylesheets.
Once phase, the plugin reads specified CSS filesOnceExit phase, injected nodes are removed from the stylesheetThe plugin automatically registers file dependencies with PostCSS, enabling proper watch mode functionality and build system integration.
/**
* The plugin throws Error instances in these cases:
* @throws {Error} When files cannot be read or resolved
* @throws {Error} When file paths are malformed
* @throws {Error} When module resolution fails for node_modules: paths
*/Error scenarios:
Failed to read ${filePath} with error ${errorMessage}node_modules: or node_modules:// prefixed paths cannot be resolvedThe plugin automatically registers file dependencies with PostCSS for proper build system integration:
// Automatically adds dependency messages to PostCSS result
interface DependencyMessage {
type: 'dependency';
plugin: 'postcss-global-data';
file: string; // Resolved file path
parent?: string; // Parent file path
}This enables:
postcss([
postcssGlobalData({
files: ['./media-queries.css']
}),
postcssCustomMedia() // Can now generate fallbacks using global media queries
])postcss([
postcssGlobalData({
files: [
'./css-variables.css',
'./custom-properties.css'
]
}),
postcssCustomProperties() // Can access global custom properties
])postcss([
postcssGlobalData({
files: [
'node_modules:open-props/props.css', // External package
'./src/design-tokens.css' // Local tokens
],
prepend: true
})
])// Main plugin configuration type (exported from the package)
export type pluginOptions = {
files?: Array<string>;
lateRemover?: boolean;
prepend?: boolean;
};
// Return types based on configuration
type PostCSSGlobalDataReturn<T extends pluginOptions> =
T["lateRemover"] extends true
? PluginConfigWithLateRemover
: PluginCreator<pluginOptions>;
// Standard mode return type (lateRemover: false)
interface StandardModeReturn extends PluginCreator<pluginOptions> {
postcssPlugin: "postcss-global-data";
prepare(): Plugin;
}
// Late remover mode return type (lateRemover: true)
interface LateRemoverModeReturn {
postcssPlugin: "postcss-global-data";
plugins: [PluginCreator<pluginOptions>, LateRemoverPlugin];
}
interface LateRemoverPlugin {
postcssPlugin: "postcss-global-data/late-remover";
OnceExit(root: Root): void;
}// Plugin hooks used internally
interface PluginHooks {
Once(root: Root, postcssHelpers: Helpers): void; // CSS injection phase
OnceExit(): void; // CSS removal phase
}
// PostCSS helpers provided to plugins
interface Helpers {
result: Result;
postcss: typeof postcss;
}