Skip to content

Commit

Permalink
enhancements: add better scaffold to generator
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed May 5, 2017
1 parent 907a1f7 commit c58f793
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
51 changes: 19 additions & 32 deletions lib/creator/yeoman/utils/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,9 @@ const InputValidate = require('webpack-addons').InputValidate;
const validate = require('./validate');

module.exports = (self, answer) => {
let webpackEntryPoint;
let entryIdentifiers;
let entryProperties;

let result;

const validateProperties = (value) => {
const valLength = value.length;
const values = value.split(',');
if(!valLength) {
return 'Please specify an answer!';
}
else if(valLength && values.length == entryIdentifiers.length) {
return true;
} else {
return 'You forgot to add one or more property to your entry point!';
}
};

if(answer['entryType'] === 'Multiple') {
result = self.prompt([
InputValidate(
Expand All @@ -29,24 +13,27 @@ module.exports = (self, answer) => {
validate
)
]).then( (multipleEntriesAnswer) => {
webpackEntryPoint = {};
let webpackEntryPoint = {};
entryIdentifiers = multipleEntriesAnswer['multipleEntries'].split(',');
return self.prompt([
function forEachPromise(obj, fn) {
return obj.reduce(function (promise, prop) {
const trimmedProp = prop.trim();
return promise.then(function (n) {
webpackEntryPoint = Object.assign({}, n);
return fn(trimmedProp);
});
}, Promise.resolve());
}
return forEachPromise(entryIdentifiers, (entryProp) => self.prompt([
InputValidate(
'objectProperties',
'Type the location of those modules seperated by comma',
validateProperties
`${entryProp}`,
`What is the location of '${entryProp}'?`,
validate
)
]).then( (objectPropAnswer) => {
entryProperties = objectPropAnswer['objectProperties'].split(',');
for(let k = 0; k < entryIdentifiers.length; k++) {
if(entryProperties[k].charAt(0) == '(' || entryProperties[k].charAt(0) == '[') {
webpackEntryPoint[entryIdentifiers[k]] = entryProperties[k];
} else {
webpackEntryPoint[entryIdentifiers[k]] = `'${entryProperties[k]}.js'`;
}
return webpackEntryPoint;
}
])).then(propAns => {
const prop = Object.keys(propAns);
webpackEntryPoint[prop] = propAns[prop];
return webpackEntryPoint;
});
});
}
Expand All @@ -57,7 +44,7 @@ module.exports = (self, answer) => {
'Type the location of module you would like to use',
validate
)
]).then( (singularAnswer) => `'${singularAnswer['singularEntry']}.js'`);
]).then( (singularAnswer) => singularAnswer['singularEntry']);
}
return result;
};
32 changes: 32 additions & 0 deletions lib/creator/yeoman/utils/tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
uglify: () => {
return (`/*
* We've enabled UglifyJSPlugin for you! This minifies your app
* in order to load faster and run less javascript.
*
* https://github.com/webpack-contrib/uglifyjs-webpack-plugin
*
*/`);
},
commonsChunk: () => {
return (`/*
* We've enabled commonsChunkPlugin for you. This allows your app to
* load faster and it splits the modules you provided as entries across
* different bundles!
*
* https://webpack.js.org/plugins/commons-chunk-plugin/
*
*/`);
},
cssPlugin: () => {
return(
`/*
* We've enabled ExtractTextPlugin for you. This allows your app to
* use css modules that will be moved into a seperate CSS file instead of inside
* one of your module entries!
*
* https://github.com/webpack-contrib/extract-text-webpack-plugin
*
*/`);
}
};
16 changes: 16 additions & 0 deletions lib/creator/yeoman/webpack-generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Generator = require('yeoman-generator');
const chalk = require('chalk');

const createCommonsChunkPlugin = require('webpack-addons').createCommonsChunkPlugin;

Expand All @@ -9,6 +10,7 @@ const RawList = require('webpack-addons').RawList;
const entryQuestions = require('./utils/entry');
const getDefaultLoaders = require('./utils/module');
const getDefaultPlugins = require('./utils/plugins');
const tooltip = require('./utils/tooltip');
const validate = require('./utils/validate');

module.exports = class WebpackGenerator extends Generator {
Expand All @@ -22,11 +24,15 @@ module.exports = class WebpackGenerator extends Generator {
};
}
prompting() {

process.stdout.write(`\n${chalk.red.bold('Remember to add string quotes to your bundle entries and file extensions!')}\n\n`);

let done = this.async();
let self = this;
let oneOrMoreEntries;
this.configuration.config.webpackOptions.module = getDefaultLoaders();
this.configuration.config.webpackOptions.plugins = getDefaultPlugins();
this.configuration.config.topScope.push(tooltip.uglify());
this.prompt([
RawList(
'entryType',
Expand All @@ -48,6 +54,7 @@ module.exports = class WebpackGenerator extends Generator {
)
]).then( (outputTypeAnswer) => {
if(!this.configuration.config.webpackOptions.entry.length) {
this.configuration.config.topScope.push(tooltip.commonsChunk());
this.configuration.config.webpackOptions.output = {
filename: '\'[name]-[chunkhash].js\'',
chunkFilename: '\'[chunkhash].js\''
Expand All @@ -67,6 +74,7 @@ module.exports = class WebpackGenerator extends Generator {
)
]).then( (extractAnswer) => {
if(extractAnswer['extractPlugin'].length !== 0) {
this.configuration.config.topScope.push(tooltip.cssPlugin());
this.configuration.config.webpackOptions.plugins.push(
'new ExtractTextPlugin(\'' + extractAnswer['extractPlugin'] + '.css\')'
);
Expand Down Expand Up @@ -99,5 +107,13 @@ module.exports = class WebpackGenerator extends Generator {
});
});
}
installPlugins() {
this.npmInstall(['uglifyjs-webpack-plugin'], { 'save-dev': true });
this.configuration.config.webpackOptions.plugins.forEach( (plugin) => {
if(plugin.indexOf('new ExtractTextPlugin') >= 0) {
this.npmInstall(['extract-text-webpack-plugin'], { 'save-dev': true });
}
});
}

};

0 comments on commit c58f793

Please sign in to comment.