• This repository has been archived on 26/Dec/2022
  • Stars
    star
    211
  • Rank 186,867 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ignore imported style files when running in Node

ignore-styles

Version Build Status License Downloads Standard

A babel/register style hook to ignore style imports when running in Node. This is for projects that use something like Webpack to enable CSS imports in JavaScript. When you try to run the project in Node (to test in Mocha, for example) you'll see errors like this:

SyntaxError: /Users/brandon/code/my-project/src/components/my-component/style.sass: Unexpected token (1:0)
> 1 | .title
| ^
2 |   font-family: serif
3 |   font-size: 10em
4 |

To resolve this, require ignore-styles with your mocha tests:

mocha --require ignore-styles

See DEFAULT_EXTENSIONS for the full list of extensions ignored, and send a pull request if you need more.

Note: This is not for use inside Webpack. If you want to ignore extensions in Webpack you'll want to use a loader like ignore-loader. This is for use in Node outside of your normal Webpack build.

Installation

$ npm install --save-dev ignore-styles

More Examples

To use this with multiple Mocha requires:

mocha --require babel-register --require ignore-styles

You can also use it just like babel/register:

import 'ignore-styles'

In ES5:

require('ignore-styles')

To customize the extensions used:

import register from 'ignore-styles'
register(['.sass', '.scss'])

To customize the extensions in ES5:

require('ignore-styles').default(['.sass', '.scss']);

Custom handler

By default, a no-op handler is used that doesn't actually do anything. If you'd like to substitute your own custom handler to do fancy things, pass it as a second argument:

import register from 'ignore-styles'
register(undefined, (module, filename) => {
  module.exports = {styleName: 'fake_class_name'}
})

The first argument to register is the list of extensions to handle. Leaving it undefined, as above, uses the default list. The handler function receives two arguments, module and filename, directly from Node.

Why is this useful? One example is when using something like react-css-modules. You need the style imports to actually return something so that you can test the components, or the wrapper component will throw an error. Use this to provide test class names.

Another use case would be to simply return the filename of an image so that it can be verified in unit tests:

const _ = require('lodash')
const path = require('path')

register(undefined, (module, filename) => {
  if (_.some(['.png', '.jpg'], ext => filename.endsWith(ext))) {
    module.exports = path.basename(filename)
  }
})

If the filename ends in '.png' or '.jpg', then the basename of the file is returned as the value of the module on import.

License

The MIT License (MIT)

Copyright (c) 2015 Brainspace Corporation

More Repositories

1

rust-example-caster-api

DEPRECATED: A demo Rust API implementation using Tokio, Axum, async-graphql, and SeaORM
Rust
129
star
2

node-knex-typescript-example

An example API application using Node, Knex, and TypeScript
TypeScript
76
star
3

babel-preset-es2015-loose

A Babel preset that uses modify-babel-preset to modify babel-preset-es2015 and enable loose mode where available.
JavaScript
63
star
4

django-baseviews

A small collection of Django view classes to build upon.
Python
46
star
5

nakago

A lightweight async dependency injection toolkit for Rust
Rust
42
star
6

nestjs-example-caster-api

A demo API implementation using Nest.js, GraphQL, and Prisma
TypeScript
37
star
7

jsx-chai

[DEPRECATED] JSX assertions for Chai using Algolia's react-element-to-jsx-string
JavaScript
35
star
8

django-reporter

Custom email-based reports for any Django project
Python
28
star
9

unirouter

[DEPRECATED] A minimal routing system built with uniloc and redux
JavaScript
24
star
10

chef-cookbooks

Cookbooks for using Chef with Django on Ubuntu
Ruby
21
star
11

bs-knex

[DEPRECATED] BuckleScript interface to the node 'knex' library
OCaml
19
star
12

wonka-dom

WonkaDOM - DOM event management with the Wonka FRP library
Reason
11
star
13

wonka-mario

Mario game logic implemented with the Wonka FRP library
Reason
10
star
14

purescript-manifold

[ABANDONED] Reactive state management for PureScript using Signal and Aff
PureScript
7
star
15

mission-ctrl

[DEPRECATED] A multi-process build and execution environment for React/Redux apps
JavaScript
7
star
16

reactifier

[DEPRECATED] A blog post aggregator bringing together React developers from across the globe.
JavaScript
7
star
17

update-ip

An extensible dynamic IP updater written in Python.
Python
6
star
18

stateful-wonka

State management based on Wonka streams
Reason
6
star
19

burn-transformers

A WIP port of the HuggingFace Transformers library for the Burn deep learning framework
Rust
6
star
20

graft

[DEPRECATED] Massively Maintainable GraphQL
TypeScript
5
star
21

generator-react

A Yeoman generator for React-related projects
TypeScript
5
star
22

signal

Lightweight FRP inspired by Elm's classic events
Reason
3
star
23

chokidar-sync

Filesystem sync for local development using chokidar and rsync
TypeScript
2
star
24

space-unicorn-api

[WIP] An API to power easy GitHub Webhook and Slack integration
TypeScript
1
star
25

storyverse

The Storyverse Project
TypeScript
1
star
26

assembly-line

[DEPRECATED] A Yeoman generator for npm libraries compiled by Babel and packaged for the browser with Webpack
JavaScript
1
star
27

ignore-if

[DEPRECATED] Ignore a higher-order function if a condition is met, such as NODE_ENV === 'test'
JavaScript
1
star
28

cultivar

[DEPRECATED] Composable event handling with Wonka for HTTP and beyond
TypeScript
1
star
29

go-garbage

Go Garbage collection examples
Go
1
star
30

rust-demo-dependency-injection

A demo project showing dependency injection approaches
Rust
1
star
31

docs

My personal documentation site to keep things that I'd like to remember about Django, system administration, Mac OS X, and more.
JavaScript
1
star
32

go-example-caster-api

A demo Go GraphQL API implementation using gqlgen and ent.
Go
1
star
33

expo-typescript-example

An example React Native application using Expo and TypeScript
TypeScript
1
star