Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostCSS update and fix #313

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ Stateless functional components where introduced in React v0.14. They have a muc
___Note___: You will still be able to set properties for stateless components!

## Adding PostCSS plugins
If you have enabled [PostCSS](https://github.com/postcss/postcss) at generation time, install your PostCSS plugins via npm and *require* it in **postcss** function in *cfg/base.js*.
If you have enabled [PostCSS](https://github.com/postcss/postcss) at generation time, install your PostCSS plugins via npm and *require* it in the **plugins** array in *postcss.config.js*.

Example for autoprefixer:
```bash
cd my-new-project
npm install autoprefixer
```
Require in *cfg/base.js*
Require in *postcss.config.js*
```JavaScript
...
postcss: function () {
return [
module.exports = () => ({
plugins: [
require('autoprefixer')({
browsers: ['last 2 versions', 'ie >= 8']
})
];
}
]
});
...
```

Expand Down
5 changes: 4 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ class AppGenerator extends Generators.Base {
}
}
});

if(this.postcss) {
this.copy('../../generators/app/templates/postcss.config.js', 'postcss.config.js');
}
}

install() {

// Currently buggy!
if(this.postcss) {
const postcss = require('./postcss');
postcss.write(path.join(this.destinationRoot(), 'conf/webpack/Base.js'));
Expand Down
53 changes: 30 additions & 23 deletions generators/app/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,37 @@ module.exports = {
// of the chain. If we have a preprocessor, we will add
// it before the initial loader
const cssDialects = [
'\\.cssmodule\\.css$',
'^.((?!cssmodule).)*\\.css$'
];

const cssModuleDialects = [
'\\.cssmodule\\.css$'
];

const preprocessorDialects = [
'\\.cssmodule\\.(sass|scss)$',
'^.((?!cssmodule).)*\\.(sass|scss)$',
'\\.cssmodule\\.less$',
'^.((?!cssmodule).)*\\.less$',
'\\.cssmodule\\.styl$',
'^.((?!cssmodule).)*\\.styl$'
];

// Prepare postCSS statement for inclusion
const postcssFunction = 'var postcss = { postcss: function() { return []; } }';
const postcssAst = esprima.parse(postcssFunction);
const preprocessorModuleDialects = [
'\\.cssmodule\\.(sass|scss)$',
'\\.cssmodule\\.less$',
'\\.cssmodule\\.styl$'
];

const postcssConfig = 'const postcssQuery = { query: { importLoaders: 1 } };';
const postcssAst = esprima.parse(postcssConfig);
const postcss = postcssAst.body[0].declarations[0].init.properties[0];

// The postcss loader item to add
const postcssLoaderObject = 'var postcss = [{ loader: \'postcss-loader\'}]';
const postcssLoaderObject = 'var postcss = [{ loader: \'postcss-loader\' }];';
const postcssLoaderAst = esprima.parse(postcssLoaderObject);
const postcssLoader = postcssLoaderAst.body[0].declarations[0].init.elements[0];


// Add postcss to the loaders array
walk.walkAddParent(ast, (node) => {


// Add the postcss key to the global configuration

if(
node.type === 'MethodDefinition' &&
node.key.name === 'defaultSettings'
) {
const returnStatement = node.value.body.body[1];
returnStatement.argument.properties.push(postcss);
}

// Parse all property nodes that use a regex.
// This should only be available under module.(pre)loaders
if(
Expand All @@ -67,14 +60,28 @@ module.exports = {
typeof node.value.regex !== 'undefined'
) {

// Enable importLoaders on non cssmodule dialacts
if(
cssDialects.indexOf(node.value.regex.pattern) !== -1 ||
preprocessorDialects.indexOf(node.value.regex.pattern) !== -1
) {
node.parent.properties[1].value.elements[1].properties.push(postcss);
}

// Regular css usage
if(cssDialects.indexOf(node.value.regex.pattern) !== -1) {
if(
cssDialects.indexOf(node.value.regex.pattern) !== -1 ||
cssModuleDialects.indexOf(node.value.regex.pattern) !== -1
) {
const loaderData = node.parent.properties[1];
loaderData.value.elements.push(postcssLoader);
}


if(preprocessorDialects.indexOf(node.value.regex.pattern) !== -1) {
// CSS preprocessors
if(
preprocessorDialects.indexOf(node.value.regex.pattern) !== -1 ||
preprocessorModuleDialects.indexOf(node.value.regex.pattern) !== -1
) {
const loaderData = node.parent.properties[1];
const lastElm = loaderData.value.elements.pop();
loaderData.value.elements.push(postcssLoader);
Expand Down
3 changes: 3 additions & 0 deletions generators/app/templates/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = () => ({
plugins: []
});
50 changes: 37 additions & 13 deletions test/generators/app/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,46 +225,70 @@ describe('react-webpack:app with PostCSS support', () => {
]);
});

it('should insert the postcss loader into the style pipes', () => {
it('should insert the postcss loader into the cssmodule style pipes', () => {

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: cssModulesQuery
},
{ loader: 'postcss-loader' }`);
assert.fileContent('conf/webpack/Base.js', `{ loader: 'css-loader' },
{ loader: 'postcss-loader' }`);

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: cssModulesQuery
},
{ loader: 'postcss-loader' },
{ loader: 'sass-loader' }`);
assert.fileContent('conf/webpack/Base.js', `{ loader: 'css-loader' },
{ loader: 'postcss-loader' },
{ loader: 'sass-loader' }`);

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: cssModulesQuery
},
{ loader: 'postcss-loader' },
{ loader: 'less-loader' }`);
assert.fileContent('conf/webpack/Base.js', `{ loader: 'css-loader' },
{ loader: 'postcss-loader' },
{ loader: 'less-loader' }`);

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: cssModulesQuery
},
{ loader: 'postcss-loader' },
{ loader: 'stylus-loader' }`);
assert.fileContent('conf/webpack/Base.js', `{ loader: 'css-loader' },
});

it('should insert the postcss loader into the NON cssmodule style pipes', () => {

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: { importLoaders: 1 }
},
{ loader: 'postcss-loader' }`);

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: { importLoaders: 1 }
},
{ loader: 'postcss-loader' },
{ loader: 'sass-loader' }`);

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: { importLoaders: 1 }
},
{ loader: 'postcss-loader' },
{ loader: 'less-loader' }`);

assert.fileContent('conf/webpack/Base.js', `{
loader: 'css-loader',
query: { importLoaders: 1 }
},
{ loader: 'postcss-loader' },
{ loader: 'stylus-loader' }`);
});

it('should append the postcss function to the base config', () => {

assert.fileContent('conf/webpack/Base.js', 'postcss: function () {');
it('should generate the postCSS config', () => {
assert.file([
'postcss.config.js',
]);
});

it('should generate required source files', () => {
Expand Down
10 changes: 5 additions & 5 deletions utils/configopts.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"name": "postcss",
"value": "postcss",
"packages": [
{ "name": "postcss", "version": "^5.0.21" },
{ "name": "postcss-loader", "version": "^0.9.1" }
{ "name": "postcss", "version": "^5.2.6" },
{ "name": "postcss-loader", "version": "^1.1.1" }
]
}
]
Expand All @@ -57,7 +57,7 @@
"name": "cssmodules",
"value": "cssmodules",
"packages": [
{ "name": "react-css-modules", "version": "^3.7.10" }
{ "name": "react-css-modules", "version": "^4.0.3" }
]
}
]
Expand All @@ -74,7 +74,7 @@
"value": "sass",
"suffix": ".sass",
"packages": [
{ "name": "sass-loader", "version": "^3.2.0" },
{ "name": "sass-loader", "version": "^4.0.2" },
{ "name": "node-sass", "version": "^3.7.0" }
]
},
Expand All @@ -83,7 +83,7 @@
"value": "scss",
"suffix": ".scss",
"packages": [
{ "name": "sass-loader", "version": "^3.2.0" },
{ "name": "sass-loader", "version": "^4.0.2" },
{ "name": "node-sass", "version": "^3.7.0" }
]
},
Expand Down