From 89ce919eee5b253f84298f95688d96962d0196bc Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 11:02:26 +0300 Subject: [PATCH 1/8] Store and assert config file --- app/index.js | 3 +++ test/test-creation.js | 1 + 2 files changed, 4 insertions(+) diff --git a/app/index.js b/app/index.js index 55136f7..93c72d0 100644 --- a/app/index.js +++ b/app/index.js @@ -32,7 +32,10 @@ var ReactWebpackGenerator = module.exports = function ReactWebpackGenerator(args this.installDependencies({ skipInstall: options['skip-install'] }); }); + this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname, '../package.json'))); + + this.config.save(); }; util.inherits(ReactWebpackGenerator, yeoman.generators.Base); diff --git a/test/test-creation.js b/test/test-creation.js index b006aa5..c5c36ca 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -48,6 +48,7 @@ describe('react-webpack generator', function() { it('should generate dotfiles', function(done) { react.run({}, function() { helpers.assertFile([].concat(expected, [ + '.yo-rc.json', '.editorconfig', '.gitignore', '.jshintrc' From 88d79d2ec296aca356d38cb301371246cc6c76ec Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 11:52:51 +0300 Subject: [PATCH 2/8] Add question about desired styles lang --- app/index.js | 19 +++++++++++++++++++ test/test-creation.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/app/index.js b/app/index.js index 93c72d0..455071b 100644 --- a/app/index.js +++ b/app/index.js @@ -63,6 +63,25 @@ ReactWebpackGenerator.prototype.askForReactRouter = function () { }.bind(this)); }; +ReactWebpackGenerator.prototype.askForStylesLanguage = function () { + var done = this.async(); + this.prompt({ + type : 'list', + name : 'stylesLanguage', + message : 'Which styles language you want to use?', + choices: [ + {name: 'CSS', value: 'css'}, + {name: 'SASS', value: 'sass'}, + {name: 'LESS', value: 'less'}, + {name: 'Stylus', value: 'stylus'} + ], + default : 'css' + }, function (props) { + this.config.set('styles-language', props.stylesLanguage); + done(); + }.bind(this)); +}; + ReactWebpackGenerator.prototype.readIndex = function readIndex() { this.indexFile = this.engine(this.read('../../templates/common/index.html'), this); }; diff --git a/test/test-creation.js b/test/test-creation.js index c5c36ca..dbbfa44 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -111,6 +111,46 @@ describe('react-webpack generator', function() { }); + describe('Generator', function () { + + it('should contain info about used style lang', function (done) { + react.run({}, function() { + assert.ok(react.config.get('styles-language')); + done(); + }); + }); + + it('by default should use css style lang', function (done) { + react.run({}, function() { + assert.equal(react.config.get('styles-language'), 'css'); + done(); + }); + }); + + var assertStyle = function (lang, done) { + helpers.mockPrompt(react, { + stylesLanguage: lang + }); + react.run({}, function() { + assert.equal(react.config.get('styles-language'), lang); + done(); + }); + }; + + it('should use sass style lang', function (done) { + assertStyle('sass', done); + }); + + it('should use less style lang', function (done) { + assertStyle('less', done); + }); + + it('should use stylus style lang', function (done) { + assertStyle('stylus', done); + }); + + }); + describe('Subgenerators', function() { var generatorTest = function(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { From ac43179354bc490d5b5307f47d4dfb0bc240dd85 Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 12:16:08 +0300 Subject: [PATCH 3/8] Use corresponding stylesheet depending on config variable --- script-base.js | 12 ++++++++++++ templates/styles/Component.less | 3 +++ templates/styles/Component.sass | 2 ++ templates/styles/Component.styl | 2 ++ 4 files changed, 19 insertions(+) create mode 100644 templates/styles/Component.less create mode 100644 templates/styles/Component.sass create mode 100644 templates/styles/Component.styl diff --git a/script-base.js b/script-base.js index 6ee2e39..1964fc4 100644 --- a/script-base.js +++ b/script-base.js @@ -38,6 +38,18 @@ var Generator = module.exports = function Generator() { this.stylesSuffix = '.css'; + switch(this.config.get('styles-language')) { + case 'sass': + this.stylesSuffix = '.sass'; + break; + case 'less': + this.stylesSuffix = '.less'; + break; + case 'stylus': + this.stylesSuffix = '.styl'; + break; + } + this.sourceRoot(path.join(__dirname, sourceRoot)); }; diff --git a/templates/styles/Component.less b/templates/styles/Component.less new file mode 100644 index 0000000..2bb362b --- /dev/null +++ b/templates/styles/Component.less @@ -0,0 +1,3 @@ +.<%= classedName %>{ + border: 1px dashed #f00; +} \ No newline at end of file diff --git a/templates/styles/Component.sass b/templates/styles/Component.sass new file mode 100644 index 0000000..9811abb --- /dev/null +++ b/templates/styles/Component.sass @@ -0,0 +1,2 @@ +.<%= classedName %> + border: 1px dashed #f00 diff --git a/templates/styles/Component.styl b/templates/styles/Component.styl new file mode 100644 index 0000000..4cc329f --- /dev/null +++ b/templates/styles/Component.styl @@ -0,0 +1,2 @@ +.<%= classedName %> + border 1px dashed #f00 From 6442075d717feacfc876375c05b701ac96c01a9e Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 16:03:43 +0300 Subject: [PATCH 4/8] Move webpack configs to tempates --- app/index.js | 2 ++ templates/common/{root/webpack.config.js => _webpack.config.js} | 1 - .../{root/webpack.dist.config.js => _webpack.dist.config.js} | 0 3 files changed, 2 insertions(+), 1 deletion(-) rename templates/common/{root/webpack.config.js => _webpack.config.js} (99%) rename templates/common/{root/webpack.dist.config.js => _webpack.dist.config.js} (100%) diff --git a/app/index.js b/app/index.js index 455071b..edde6ea 100644 --- a/app/index.js +++ b/app/index.js @@ -94,6 +94,8 @@ ReactWebpackGenerator.prototype.createIndexHtml = function createIndexHtml() { ReactWebpackGenerator.prototype.packageFiles = function () { this.reactRouter = this.env.options.reactRouter; this.template('../../templates/common/_package.json', 'package.json'); + this.template('../../templates/common/_webpack.config.js', 'webpack.config.js'); + this.template('../../templates/common/_webpack.dist.config.js', 'webpack.dist.config.js'); this.copy('../../templates/common/Gruntfile.js', 'Gruntfile.js'); this.copy('../../templates/common/gitignore', '.gitignore'); }; diff --git a/templates/common/root/webpack.config.js b/templates/common/_webpack.config.js similarity index 99% rename from templates/common/root/webpack.config.js rename to templates/common/_webpack.config.js index 4c69e48..d66ea2b 100644 --- a/templates/common/root/webpack.config.js +++ b/templates/common/_webpack.config.js @@ -38,7 +38,6 @@ module.exports = { exclude: 'node_modules', loader: 'jshint' }], - loaders: [{ test: /\.jsx$/, loader: 'react-hot!jsx-loader?harmony' diff --git a/templates/common/root/webpack.dist.config.js b/templates/common/_webpack.dist.config.js similarity index 100% rename from templates/common/root/webpack.dist.config.js rename to templates/common/_webpack.dist.config.js From 0799d1e65ce52f9543ff36da1394d1b77319220c Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 16:07:56 +0300 Subject: [PATCH 5/8] Proxy selected style lang to templates --- app/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/index.js b/app/index.js index edde6ea..94ba957 100644 --- a/app/index.js +++ b/app/index.js @@ -77,6 +77,7 @@ ReactWebpackGenerator.prototype.askForStylesLanguage = function () { ], default : 'css' }, function (props) { + this.env.options.stylesLanguage = props.stylesLanguage; this.config.set('styles-language', props.stylesLanguage); done(); }.bind(this)); @@ -93,6 +94,7 @@ ReactWebpackGenerator.prototype.createIndexHtml = function createIndexHtml() { ReactWebpackGenerator.prototype.packageFiles = function () { this.reactRouter = this.env.options.reactRouter; + this.stylesLanguage = this.env.options.stylesLanguage; this.template('../../templates/common/_package.json', 'package.json'); this.template('../../templates/common/_webpack.config.js', 'webpack.config.js'); this.template('../../templates/common/_webpack.dist.config.js', 'webpack.dist.config.js'); From ab98751f9097c7b9fdf7d0206ab9f0dd82f6d3a2 Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 16:08:48 +0300 Subject: [PATCH 6/8] Use styles within templates --- templates/common/_package.json | 5 ++++- templates/common/_webpack.config.js | 11 ++++++++++- templates/common/_webpack.dist.config.js | 11 ++++++++++- templates/javascript/Component.jsx | 5 ++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/templates/common/_package.json b/templates/common/_package.json index 3b8c387..d17a184 100644 --- a/templates/common/_package.json +++ b/templates/common/_package.json @@ -35,7 +35,10 @@ "grunt-open": "~0.2.3", "jshint-loader": "~0.8.0", "grunt-contrib-copy": "~0.5.0", - "grunt-contrib-clean": "~0.6.0", + "grunt-contrib-clean": "~0.6.0",<% if (stylesLanguage === 'sass') { %> + "sass-loader": "^0.2.0",<% } %><% if (stylesLanguage === 'less') { %> + "less-loader": "^0.7.7",<% } %><% if (stylesLanguage === 'stylus') { %> + "stylus-loader": "^0.4.0",<% } %> "react-hot-loader": "^0.4.5" } } diff --git a/templates/common/_webpack.config.js b/templates/common/_webpack.config.js index d66ea2b..76e5f35 100644 --- a/templates/common/_webpack.config.js +++ b/templates/common/_webpack.config.js @@ -41,7 +41,16 @@ module.exports = { loaders: [{ test: /\.jsx$/, loader: 'react-hot!jsx-loader?harmony' - }, { + },<% if (stylesLanguage === 'sass') { %> { + test: /\.sass/, + loader: 'style-loader!css-loader!sass-loader?outputStyle=expanded' + },<% } %><% if (stylesLanguage === 'less') { %> { + test: /\.less/, + loader: 'style-loader!css-loader!less-loader' + },<% } %><% if (stylesLanguage === 'stylus') { %> { + test: /\.styl/, + loader: 'style-loader!css-loader!stylus-loader' + },<% } %> { test: /\.css$/, loader: 'style-loader!css-loader' }, { diff --git a/templates/common/_webpack.dist.config.js b/templates/common/_webpack.dist.config.js index 287c0be..45ef333 100644 --- a/templates/common/_webpack.dist.config.js +++ b/templates/common/_webpack.dist.config.js @@ -49,7 +49,16 @@ module.exports = { }, { test: /\.css$/, loader: 'style-loader!css-loader' - }, { + },<% if (stylesLanguage === 'sass') { %> { + test: /\.sass/, + loader: 'style-loader!css-loader!sass-loader?outputStyle=expanded' + },<% } %><% if (stylesLanguage === 'less') { %> { + test: /\.less/, + loader: 'style-loader!css-loader!less-loader' + },<% } %><% if (stylesLanguage === 'stylus') { %> { + test: /\.styl/, + loader: 'style-loader!stylus-loader!less-loader' + },<% } %> { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192' }] diff --git a/templates/javascript/Component.jsx b/templates/javascript/Component.jsx index 7ce6a48..ae0869a 100644 --- a/templates/javascript/Component.jsx +++ b/templates/javascript/Component.jsx @@ -5,7 +5,10 @@ 'use strict'; var React = require('react/addons'); -require('../../styles/<%= classedName %>.css'); +<% if (stylesLanguage === 'css') { %>require('../../styles/<%= classedName %>.css');<% } %> +<% if (stylesLanguage === 'sass') { %>require('../../styles/<%= classedName %>.sass');<% } %> +<% if (stylesLanguage === 'less') { %>require('../../styles/<%= classedName %>.less');<% } %> +<% if (stylesLanguage === 'stylus') { %>require('../../styles/<%= classedName %>.styl');<% } %> var <%= classedName %> = React.createClass({ render: function () { From 651ff70086fd4ec6b034f9851f788303206b230d Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 16:16:56 +0300 Subject: [PATCH 7/8] Inline pkg.mainInput --- templates/common/_webpack.config.js | 2 +- templates/common/_webpack.dist.config.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/common/_webpack.config.js b/templates/common/_webpack.config.js index 76e5f35..8e41144 100644 --- a/templates/common/_webpack.config.js +++ b/templates/common/_webpack.config.js @@ -20,7 +20,7 @@ module.exports = { devtool: false, entry: [ 'webpack/hot/only-dev-server', - './src/scripts/components/<%= pkg.mainInput %>.jsx' + './src/scripts/components/<% if (reactRouter) { %>main<% } else { %><%= scriptAppName %><% } %>.jsx' ], stats: { diff --git a/templates/common/_webpack.dist.config.js b/templates/common/_webpack.dist.config.js index 45ef333..9a27ac8 100644 --- a/templates/common/_webpack.dist.config.js +++ b/templates/common/_webpack.dist.config.js @@ -18,7 +18,7 @@ module.exports = { debug: false, devtool: false, - entry: './src/scripts/components/<%= pkg.mainInput %>.jsx', + entry: './src/scripts/components/<% if (reactRouter) { %>main<% } else { %><%= scriptAppName %><% } %>.jsx', stats: { colors: true, From ef377565dcdad47e1b1d54c34ad3be85424b0601 Mon Sep 17 00:00:00 2001 From: ColCh Date: Sun, 2 Nov 2014 16:17:54 +0300 Subject: [PATCH 8/8] Proxy stylesLanguage to component subgenerator --- script-base.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script-base.js b/script-base.js index 1964fc4..37e4a73 100644 --- a/script-base.js +++ b/script-base.js @@ -19,6 +19,7 @@ var Generator = module.exports = function Generator() { this.scriptAppName = this._.camelize(this._.capitalize(this.appname)) + generalUtils.appName(this); this.classedFileName = this._.capitalizeFile(this.name); this.classedName = this._.capitalizeClass(this.name); + this.stylesLanguage = this.config.get('styles-language'); if (typeof this.options.appPath === 'undefined') { this.options.appPath = this.options.appPath || 'src/scripts'; @@ -38,7 +39,7 @@ var Generator = module.exports = function Generator() { this.stylesSuffix = '.css'; - switch(this.config.get('styles-language')) { + switch(this.stylesLanguage) { case 'sass': this.stylesSuffix = '.sass'; break;