Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Jul 18, 2018
0 parents commit f7352ef
Show file tree
Hide file tree
Showing 14 changed files with 9,432 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{*.md,*.snap}]
trim_trailing_whitespace = false

[package.json]
indent_size = 2
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
coverage/
9 changes: 9 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,9 @@
{
"root": true,
"extends": [
"eslint-config-moxy/es6",
"eslint-config-moxy/addons/node",
"eslint-config-moxy/addons/object-spread",
"eslint-config-moxy/addons/jest"
]
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules/
npm-debug.*
coverage/
8 changes: 8 additions & 0 deletions .travis.yml
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "node"
- "lts/*"
# Report coverage
after_success:
- "npm i codecov"
- "node_modules/.bin/codecov"
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Made With MOXY Lda <hello@moxy.studio>

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.
81 changes: 81 additions & 0 deletions README.md
@@ -0,0 +1,81 @@
# react-app-rewire-css-modules-extensionless

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-url]

[npm-url]:https://npmjs.org/package/react-app-rewire-css-modules-extensionless
[npm-image]:http://img.shields.io/npm/v/react-app-rewire-css-modules-extensionless.svg
[downloads-image]:http://img.shields.io/npm/dm/react-app-rewire-css-modules-extensionless.svg
[travis-url]:https://travis-ci.org/moxystudio/react-app-rewire-css-modules-extensionless
[travis-image]:http://img.shields.io/travis/moxystudio/react-app-rewire-css-modules-extensionless/master.svg
[codecov-url]:https://codecov.io/gh/moxystudio/react-app-rewire-css-modules-extensionless
[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/react-app-rewire-css-modules-extensionless/master.svg
[david-dm-url]:https://david-dm.org/moxystudio/react-app-rewire-css-modules-extensionless
[david-dm-image]:https://img.shields.io/david/moxystudio/react-app-rewire-css-modules-extensionless.svg
[david-dm-dev-url]:https://david-dm.org/moxystudio/react-app-rewire-css-modules-extensionless?type=dev
[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/react-app-rewire-css-modules-extensionless.svg
[greenkeeper-image]:https://badges.greenkeeper.io/moxystudio/react-app-rewire-css-modules-extensionless.svg
[greenkeeper-url]:https://greenkeeper.io

Adds CSS modules for CRA apps without requiring the `.modules.css` extension, using [`react-app-rewired`](https://github.com/timarney/react-app-rewired).


## Installation

`$ npm install --save-dev eslint react-app-rewire-css-modules-extensionless`


## Usage

In the `config-overrides.js` you created for `react-app-rewired` add this code:

```js
module.exports = (config, env) => {
config = require('react-app-rewire-css-modules-extensionless')(config, env, { /* options */ });

// You may apply other rewires as well

return config;
}
```

If you are using the `compose` utility of `react-app-rewired`:

```js
const { compose } = require('react-app-rewired');

module.exports = compose(
require('react-app-rewire-css-modules-extensionless')({ /* options */ })
// ... other rewires
)
```

Available options:


| Name | Description | Type | Default |
| ------ | ------------- | -------- | ------- |
| include | The loader include condition | string/Array/RegExp/Function | *src folder* |
| exclude | The loader exclude condition | string/Array/RegExp/Function | |
| localIdentName | The localIdentName option to pass to the `css-loader` | string | `[hash:base64:5]!` for production, `[name]__[local]___[hash:base64:5]!` otherwise |


## Usage with Storybook

When using `@storybooks/storybook` with CRA via `getstorybook`, create a `.storybook/webpack.config.js` file and add this code:

```js
module.exports = (config, env) => {
require('react-app-rewire-css-modules-extensionless')(config, env, { /* options */ })
};
```


## Tests

`$ npm test`
`$ npm test -- --watch` during development


## License

[MIT License](http://opensource.org/licenses/MIT)
65 changes: 65 additions & 0 deletions index.js
@@ -0,0 +1,65 @@
'use strict';

const path = require('path');
const cloneDeep = require('lodash.clonedeep');

const getChildrenRules = (loader) => loader.use || loader.oneOf || (Array.isArray(loader.loader) && loader.loader) || [];

const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result;
const rules = Array.isArray(rulesSource) ? rulesSource : getChildrenRules(rulesSource);

rules.some((rule, index) => (
result = ruleMatcher(rule) ? { index, rules } : findIndexAndRules(getChildrenRules(rule), ruleMatcher))
);

return result;
};

const findRule = (rulesSource, ruleMatcher) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);

return rules[index];
};

const addBeforeRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);

rules.splice(index, 0, value);
};

const cssRuleMatcher = (rule) => rule.test && String(rule.test) === String(/\.css$/);
const cssLoaderMatcher = (rule) => rule.loader && rule.loader.indexOf(`${path.sep}css-loader${path.sep}`) !== -1;

const createRewire = (options) => (config, env) => {
options = {
include: path.resolve('src'),
exclude: undefined,
camelCase: undefined,
localIdentName: env === 'production' ? '[hash:base64:5]!' : '[name]__[local]___[hash:base64:5]!',
...options,
};

const cssRule = findRule(config.module.rules, cssRuleMatcher);
const cssModulesRule = cloneDeep(cssRule);
const cssModulesRuleCssLoader = findRule(cssModulesRule, cssLoaderMatcher);

cssModulesRule.include = options.include;
cssModulesRule.exclude = options.exclude;
cssModulesRuleCssLoader.options.camelCase = options.camelCase;
cssModulesRuleCssLoader.options.localIdentName = options.localIdentName;

addBeforeRule(config.module.rules, cssRuleMatcher, cssModulesRule);

return config;
};

module.exports = (...args) => {
if (args[0] && args[0].module) {
const [config, env, options] = args;

return createRewire(options)(config, env);
}

return createRewire(args[0]);
};

0 comments on commit f7352ef

Please sign in to comment.