Ignore Styles

5.0.1 · active · verified Wed Apr 22

ignore-styles is a utility that provides a Node.js `require` hook to prevent style and asset imports (like CSS, SASS, images, videos) from causing `SyntaxError` crashes when running JavaScript code directly in Node.js, typically during testing environments (e.g., Mocha) that don't process these file types. It is currently at version 5.0.1 and primarily sees updates to extend the list of default ignored file extensions, with major version increments reflecting these additions. A key differentiator is its specific purpose for Node.js execution *outside* of a Webpack build process, contrasting with Webpack-specific loaders like `ignore-loader`. It offers flexibility through a customizable list of file extensions to ignore and an optional custom handler function for more advanced scenarios, such as returning mock data for `react-css-modules`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and register `ignore-styles` with a custom list of extensions and a handler function to mock image and style imports for Node.js environments.

import register from 'ignore-styles';
import path from 'path';

// Register with custom extensions and a handler for images
register(['.css', '.scss', '.sass', '.png', '.jpg'], (module, filename) => {
  const ext = path.extname(filename);
  if (['.png', '.jpg'].includes(ext)) {
    // For image files, return just the basename
    module.exports = path.basename(filename);
  } else {
    // For style files, return an empty object or a mock for CSS Modules
    module.exports = {}; // or { className: 'mocked_class' } for react-css-modules
  }
});

// Example usage in a test file (assuming a component 'MyComponent' imports styles and images)
// import styles from './MyComponent.module.css';
// import logo from './logo.png';

console.log('ignore-styles hook registered.');
console.log('This setup will prevent Node.js from throwing SyntaxErrors for .css, .scss, .sass, .png, and .jpg imports.');
console.log('Image imports will resolve to their basename; style imports to an empty object.');

view raw JSON →