mirror of
https://github.com/react-webpack-generators/generator-react-webpack.git
synced 2025-12-08 18:01:59 +00:00
Refactored postcss functionality in it's own file - #163
This commit is contained in:
parent
ac6eb05171
commit
255d7b94d8
@ -25,49 +25,6 @@ module.exports = generator.Base.extend({
|
||||
this.sourceRoot(baseRootPath);
|
||||
|
||||
this.config.save();
|
||||
|
||||
this.postcssConfig = function() {
|
||||
let esprima = require('esprima');
|
||||
let walk = require('esprima-walk');
|
||||
let escodegen = require('escodegen');
|
||||
|
||||
let baseConfigPath = path.join(this.destinationRoot(), 'cfg/base.js');
|
||||
|
||||
let css = '/\\.css$/';
|
||||
let cssDialects = ['/\\.sass/', '/\\.scss/', '/\\.less/', '/\\.styl/'];
|
||||
|
||||
let postcss = 'var postcss = { postcss: function() { return []; } }';
|
||||
postcss = esprima.parse(postcss);
|
||||
postcss = postcss.body[0].declarations[0].init.properties[0];
|
||||
|
||||
let data = fs.readFileSync(baseConfigPath, 'utf8');
|
||||
let parsed = esprima.parse(data);
|
||||
|
||||
walk.walkAddParent(parsed, function(node) {
|
||||
if(node.type === 'AssignmentExpression' && node.left.object.name === 'module') {
|
||||
node.right.properties.push(postcss);
|
||||
}
|
||||
|
||||
if(node.type === 'Property' && node.key.name === 'test') {
|
||||
if(cssDialects.indexOf(node.value.raw) > -1) {
|
||||
let current = node.parent.properties[1].value.value;
|
||||
current += '!postcss-loader';
|
||||
|
||||
node.parent.properties[1].value.value = current;
|
||||
}
|
||||
|
||||
if(node.value.raw === css) {
|
||||
let current = 'style-loader!css-loader!postcss-loader';
|
||||
|
||||
node.parent.properties[1].value.value = current;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let options = { format: { indent: { style: ' ' } } };
|
||||
let code = escodegen.generate(parsed, options);
|
||||
fs.writeFileSync(baseConfigPath, code, 'utf8');
|
||||
}
|
||||
},
|
||||
|
||||
initializing: function() {
|
||||
@ -178,7 +135,8 @@ module.exports = generator.Base.extend({
|
||||
|
||||
install: function() {
|
||||
if(this.postcss) {
|
||||
this.postcssConfig();
|
||||
let postcss = require('./postcss');
|
||||
postcss.write(path.join(this.destinationRoot(), 'cfg/base.js'));
|
||||
}
|
||||
|
||||
if(!this.options['skip-install']) {
|
||||
|
||||
47
generators/app/postcss.js
Normal file
47
generators/app/postcss.js
Normal file
@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
let fs = require('fs');
|
||||
let esprima = require('esprima');
|
||||
let walk = require('esprima-walk');
|
||||
let escodegen = require('escodegen');
|
||||
|
||||
module.exports = {
|
||||
write: function(path) {
|
||||
let baseConfigPath = path;
|
||||
|
||||
let css = '/\\.css$/';
|
||||
let cssDialects = ['/\\.sass/', '/\\.scss/', '/\\.less/', '/\\.styl/'];
|
||||
|
||||
let postcss = 'var postcss = { postcss: function() { return []; } }';
|
||||
postcss = esprima.parse(postcss);
|
||||
postcss = postcss.body[0].declarations[0].init.properties[0];
|
||||
|
||||
let data = fs.readFileSync(baseConfigPath, 'utf8');
|
||||
let parsed = esprima.parse(data);
|
||||
|
||||
walk.walkAddParent(parsed, function(node) {
|
||||
if(node.type === 'AssignmentExpression' && node.left.object.name === 'module') {
|
||||
node.right.properties.push(postcss);
|
||||
}
|
||||
|
||||
if(node.type === 'Property' && node.key.name === 'test') {
|
||||
if(cssDialects.indexOf(node.value.raw) > -1) {
|
||||
let current = node.parent.properties[1].value.value;
|
||||
current += '!postcss-loader';
|
||||
|
||||
node.parent.properties[1].value.value = current;
|
||||
}
|
||||
|
||||
if(node.value.raw === css) {
|
||||
let current = 'style-loader!css-loader!postcss-loader';
|
||||
|
||||
node.parent.properties[1].value.value = current;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let options = { format: { indent: { style: ' ' } } };
|
||||
let code = escodegen.generate(parsed, options);
|
||||
fs.writeFileSync(baseConfigPath, code, 'utf8');
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user