Repository: bkonkle/ignore-styles Branch: master Commit: 5cce433de827 Files: 13 Total size: 11.0 KB Directory structure: gitextract_olydr7i2/ ├── .babelrc ├── .editorconfig ├── .gitignore ├── .nvmrc ├── .travis.yml ├── CHANGLELOG.md ├── LICENSE ├── README.md ├── ignore-styles.d.ts ├── ignore-styles.js ├── lib/ │ └── ignore-styles.js ├── package.json └── test/ └── test-ignore-styles.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .babelrc ================================================ { "presets": ["es2015-loose"], "plugins": ["transform-object-assign"] } ================================================ FILE: .editorconfig ================================================ # http://editorconfig.org root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true ================================================ FILE: .gitignore ================================================ # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules ================================================ FILE: .nvmrc ================================================ v5 ================================================ FILE: .travis.yml ================================================ language: node_js node_js: 5 install: npm install script: - npm test before_cache: npm prune branches: only: - master # force container based infra # http://docs.travis-ci.com/user/workers/container-based-infrastructure/#Routing-your-build-to-container-based-infrastructure sudo: false cache: directories: - node_modules ================================================ FILE: CHANGLELOG.md ================================================ See the [Releases](https://github.com/bkonkle/ignore-styles/releases) page. ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Brainspace Corporation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # ignore-styles [![Version][version-svg]][package-url] [![Build Status][travis-svg]][travis-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] [![Standard][standard-svg]][standard-url] 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][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`: ```js import 'ignore-styles' ``` In ES5: ```js require('ignore-styles') ``` To customize the extensions used: ```js import register from 'ignore-styles' register(['.sass', '.scss']) ``` To customize the extensions in ES5: ```js 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: ```js 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][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: ```js 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 [travis-svg]: https://img.shields.io/travis/bkonkle/ignore-styles/master.svg?style=flat-square [travis-url]: https://travis-ci.org/bkonkle/ignore-styles [license-image]: http://img.shields.io/badge/license-MIT-green.svg?style=flat-square [license-url]: LICENSE [downloads-image]: https://img.shields.io/npm/dm/ignore-styles.svg?style=flat-square [downloads-url]: http://npm-stat.com/charts.html?package=ignore-styles [version-svg]: https://img.shields.io/npm/v/ignore-styles.svg?style=flat-square [package-url]: https://npmjs.org/package/ignore-styles [standard-svg]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square [standard-url]: http://standardjs.com/ [default-extensions]: https://github.com/bkonkle/ignore-styles/blob/master/ignore-styles.js#L1 [react-css-modules]: https://github.com/gajus/react-css-modules [ignore-loader]: https://www.npmjs.com/package/ignore-loader ================================================ FILE: ignore-styles.d.ts ================================================ /// declare module 'ignore-styles' { type Handler = (m: NodeModule, filename: string) => any export const DEFAULT_EXTENSIONS: string[] export let oldHandlers: { [ext: string]: Handler } export function noOp(): void export function restore(): void export default function register( extensions?: string[], handler?: Handler ): void } ================================================ FILE: ignore-styles.js ================================================ export const DEFAULT_EXTENSIONS = [ '.css', '.scss', '.sass', '.pcss', '.stylus', '.styl', '.less', '.sss', '.gif', '.jpeg', '.jpg', '.png', '.svg', '.mp4', '.webm', '.ogv', '.aac', '.mp3', '.wav', '.ogg' ] export let oldHandlers = {} export function noOp () {} export function restore () { for (const ext in oldHandlers) { if (oldHandlers[ext] === undefined) { delete require.extensions[ext] } else { require.extensions[ext] = oldHandlers[ext] } } oldHandlers = {} } export default function register (extensions = DEFAULT_EXTENSIONS, handler = noOp) { restore() for (const ext of extensions) { oldHandlers[ext] = require.extensions[ext] require.extensions[ext] = handler } } // Run at import register() ================================================ FILE: lib/ignore-styles.js ================================================ 'use strict'; exports.__esModule = true; exports.noOp = noOp; exports.restore = restore; exports.default = register; var DEFAULT_EXTENSIONS = exports.DEFAULT_EXTENSIONS = ['.css', '.scss', '.sass', '.pcss', '.stylus', '.styl', '.less', '.sss', '.gif', '.jpeg', '.jpg', '.png', '.svg', '.mp4', '.webm', '.ogv']; var oldHandlers = exports.oldHandlers = {}; function noOp() {} function restore() { for (var ext in oldHandlers) { if (oldHandlers[ext] === undefined) { delete require.extensions[ext]; } else { require.extensions[ext] = oldHandlers[ext]; } } exports.oldHandlers = oldHandlers = {}; } function register() { var extensions = arguments.length <= 0 || arguments[0] === undefined ? DEFAULT_EXTENSIONS : arguments[0]; var handler = arguments.length <= 1 || arguments[1] === undefined ? noOp : arguments[1]; restore(); for (var _iterator = extensions, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var ext = _ref; oldHandlers[ext] = require.extensions[ext]; require.extensions[ext] = handler; } } // Run at import register(); //# sourceMappingURL=ignore-styles.js.map ================================================ FILE: package.json ================================================ { "name": "ignore-styles", "version": "5.0.1", "description": "Ignore imported style files when running in Node", "main": "lib/ignore-styles.js", "types": "ignore-styles.d.js", "scripts": { "test": "standard ignore-styles.js && mocha --require babel-register", "build": "babel ignore-styles.js -s -o lib/ignore-styles.js", "watch": "babel ignore-styles.js -s -o lib/ignore-styles.js -w", "prepublish": "npm run build" }, "repository": { "type": "git", "url": "git+https://github.com/bkonkle/ignore-styles.git" }, "author": "Brandon Konkle ", "license": "MIT", "keywords": [ "webpack", "css", "testing" ], "devDependencies": { "babel-cli": "^6.3.17", "babel-plugin-transform-object-assign": "^6.3.13", "babel-preset-es2015-loose": "^6.1.3", "babel-preset-react": "^6.3.13", "babel-register": "^6.3.13", "chai": "^3.4.1", "mocha": "^2.3.3", "standard": "^7.1.2" } } ================================================ FILE: test/test-ignore-styles.js ================================================ /* global describe, it, afterEach */ import { expect } from 'chai' import register, * as ignoreStyles from '../ignore-styles' describe('ignore-styles', () => { afterEach(() => { ignoreStyles.oldHandlers = {} }) describe('register()', () => { afterEach(() => { delete require.extensions['.blargh'] }) it('adds a no-op function as the handler for the given extensions', () => { register(['.blargh']) expect(require.extensions['.blargh']).to.equal(ignoreStyles.noOp) }) it('saves the old handler so that it can be restored later', () => { register(['.blargh']) expect(ignoreStyles.oldHandlers).to.have.property('.blargh', undefined) }) it('allows for a custom function to be provided instead of the no-op', () => { const customHandler = () => ({soup: 'No soup for you!'}) register(['.blargh'], customHandler) expect(require.extensions['.blargh']).to.equal(customHandler) }) }) describe('restore', () => { afterEach(() => { delete require.extensions['.fake'] }) it('returns the handlers back to their previous state', () => { function fakeHandler () {} require.extensions['.fake'] = fakeHandler register(['.fake']) expect(require.extensions['.fake']).to.equal(ignoreStyles.noOp) ignoreStyles.restore() expect(require.extensions['.fake']).to.equal(fakeHandler) }) }) })