Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3.0.0
  • Loading branch information
jonathantneal committed Oct 3, 2018
commit 00bbb43a8548dd6a0ef18c85b9f945ed55910eb6
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes to React App Rewire PostCSS

### 3.0.0 (October 3, 2018)

- Updated: Support for Create React App 2.
- Changed: The old PostCSS plugins are no longer erased, making
configuration-less usage of this plugin seemingly invisible. The plugins are
still replaced the same way.

### 2.0.0 (September 17, 2018)

- Changed: A new configuration is returned, instead of the existing
Expand Down
56 changes: 17 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
export default (config, options) => {
const clone = Object.assign({}, config);
const postcssLoader = findRule(clone.module.rules, postcssLoaderMatcher);

postcssLoader.loader = require.resolve('postcss-loader');

delete postcssLoader.options.plugins;

Object.assign(postcssLoader.options, options);

return clone;
};

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

return rules[index];
// find any first matching rule that contains postcss-loader
filterPostCSSLoader(config.module.rules).forEach(rule => {
filterPostCSSLoader(rule.oneOf).forEach(oneOf => {
filterPostCSSLoader(oneOf.use).forEach(use => {
// use the latest version of postcss-loader
use.loader = require.resolve('postcss-loader');

// update the options with your custom configuration
Object.assign(use.options, options);
})
})
});

// return the mutated configuration
return config;
};

const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result;

const rules = Array.isArray(rulesSource)
? rulesSource
: ruleChildren(rulesSource);

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

return result;
}
);

return result;
};

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

const postcssLoaderMatcher = rule => rule.loader && /\bpostcss-loader\b/.test(rule.loader);
// return a filtered array that includes postcss-loader
const filterPostCSSLoader = array => array.filter(object => JSON.stringify(object).includes('postcss-loader'));
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-app-rewire-postcss",
"version": "2.0.0",
"version": "3.0.0",
"description": "Configure PostCSS in Create React App without ejecting",
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
"license": "CC0-1.0",
Expand Down Expand Up @@ -28,13 +28,13 @@
"postcss-loader": "^3.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-eslint": "^9.0.0",
"eslint": "^5.6.0",
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.6.1",
"eslint-config-dev": "^2.0.0",
"pre-commit": "^1.2.2",
"rollup": "^0.66.0",
"rollup": "^0.66.3",
"rollup-plugin-babel": "^4.0.1"
},
"eslintConfig": {
Expand Down
21 changes: 21 additions & 0 deletions test-cra1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Loading