Conditional Preprocessor for Webpack
if-loader is a webpack module bundler preprocessor that enables C-style conditional compilation using 'if' directives within JavaScript source files. It allows developers to include or exclude blocks of code based on predefined conditions, similar to the `#ifdef` mechanism in C/C++. The current stable version is 1.0.2. This package is no longer maintained, with its last commit dating back over nine years, indicating it is abandoned. While it offers a straightforward way to manage code variants for different build environments, more actively maintained alternatives exist that provide similar or enhanced functionality and better integration with modern webpack features and tooling. Its primary differentiation was its minimalist approach to conditional code exclusion using comment-based directives.
Common errors
-
Module not found: Error: Can't resolve 'if-loader' in '<path>'
cause The 'if-loader' package is not installed or Webpack cannot locate it.fixInstall the package: `npm install if-loader --save-dev` or `yarn add if-loader --dev`. -
Error: Cannot read properties of undefined (reading 'replace') at Object.module.exports (<path>/if-loader/index.js:XX:YY)
cause The loader's options are not correctly passed, or a required option for a directive is missing, leading to an undefined value in its internal processing.fixVerify the `options` object within your `webpack.config.js` for `if-loader` is correctly structured and that all directives referenced in your code (e.g., `version-a`) have a corresponding boolean value in the options.
Warnings
- breaking The `if-loader` package is effectively abandoned, with no activity on its GitHub repository for over nine years. This means no updates, bug fixes, or security patches will be released. Consider migrating to more actively maintained conditional compilation loaders for Webpack.
- gotcha The loader's conditional logic operates on comments and is not aware of JavaScript string literals. Placing directives like `// #if` inside a string (e.g., `"Hello, // #if world!"`) can lead to unexpected parsing errors or incorrect code removal.
- gotcha Since `if-loader` performs a raw text transformation by removing comment blocks, it can interfere with other loaders or tools that also process or rely on comment syntax, such as code formatters, linters (e.g., ESLint), or minifiers, potentially leading to errors or unexpected behavior.
Install
-
npm install if-loader -
yarn add if-loader -
pnpm add if-loader
Imports
- if-loader
import ifLoader from 'if-loader';
module: { rules: [{ test: /\.js$/, use: [{ loader: 'if-loader', options: { 'version-a': true } }] }] } - Conditional Block (line comment)
/* #if SOME_CONDITION */ console.log('This will not be processed correctly in single-line comment format.');// #if SOME_CONDITION console.log('This code is conditional.'); // #end - Conditional Block (block comment)
if (SOME_CONDITION) { ... }/* #if SOME_CONDITION */ (function() { console.log('Multi-line conditional block.'); })(); /* #end */
Quickstart
/* webpack.config.js */
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'if-loader',
options: {
'version-a': process.env.BUILD_ENV === 'alpha',
'version-b': process.env.BUILD_ENV === 'beta',
'version-c': true // Always enabled
},
},
],
},
],
},
};
/* src/app.js */
console.log('Common code.');
/* #if version-a */
console.log('This is for version-a build.');
/* #end */
// #if version-b
console.log('This is for version-b build.');
// #end
/* #if version-a,version-c */
console.log('This is for version-a OR version-c build.');
/* #end */
// #if version-d
console.log('This will never be included.');
// #end
// To run:
// BUILD_ENV=alpha webpack --config webpack.config.js
// BUILD_ENV=beta webpack --config webpack.config.js
// webpack --config webpack.config.js (for version-c only)