-
-
Notifications
You must be signed in to change notification settings - Fork 621
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
cli(init): Refactor Yeoman #323
Merged
+300
−443
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a4e3e1
misc(refactor): refactoring work on init
evenstensberg 5c0b7c1
chore(rebase): rebase to master
evenstensberg 6969f5f
misc(refactor): refactor init
evenstensberg 77f8f53
misc(refactor): refactor init
evenstensberg ee66351
chore(deps): remove redundant dep
evenstensberg 363d29a
misc(jsdocs): change occuring comment
evenstensberg 7fee593
cli(init): move transform method to func
evenstensberg 0cfa9e0
misc(init): revise wrong expression
evenstensberg 9fc6c52
misc(appveyor): only report bundlesize in travis
evenstensberg 5d19f45
misc(comment): add comment to indicate future replacement
evenstensberg 5aaac64
misc(lint): fix linting errors
evenstensberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,173 @@ | ||
"use strict"; | ||
|
||
const yeoman = require("yeoman-environment"); | ||
const Generator = require("yeoman-generator"); | ||
const path = require("path"); | ||
const defaultGenerator = require("../generators/init-generator"); | ||
const runTransform = require("./transformations/index"); | ||
const j = require("jscodeshift"); | ||
const chalk = require("chalk"); | ||
const pEachSeries = require("p-each-series"); | ||
|
||
const runPrettier = require("../utils/run-prettier"); | ||
|
||
const entryTransform = require("./transformations/entry/entry"); | ||
const outputTransform = require("./transformations/output/output"); | ||
const contextTransform = require("./transformations/context/context"); | ||
const resolveTransform = require("./transformations/resolve/resolve"); | ||
const devtoolTransform = require("./transformations/devtool/devtool"); | ||
const targetTransform = require("./transformations/target/target"); | ||
const watchTransform = require("./transformations/watch/watch"); | ||
const watchOptionsTransform = require("./transformations/watch/watchOptions"); | ||
const externalsTransform = require("./transformations/externals/externals"); | ||
const nodeTransform = require("./transformations/node/node"); | ||
const performanceTransform = require("./transformations/performance/performance"); | ||
const statsTransform = require("./transformations/stats/stats"); | ||
const amdTransform = require("./transformations/other/amd"); | ||
const bailTransform = require("./transformations/other/bail"); | ||
const cacheTransform = require("./transformations/other/cache"); | ||
const profileTransform = require("./transformations/other/profile"); | ||
const mergeTransform = require("./transformations/other/merge"); | ||
const parallelismTransform = require("./transformations/other/parallelism"); | ||
const recordsInputPathTransform = require("./transformations/other/recordsInputPath"); | ||
const recordsOutputPathTransform = require("./transformations/other/recordsOutputPath"); | ||
const recordsPathTransform = require("./transformations/other/recordsPath"); | ||
const moduleTransform = require("./transformations/module/module"); | ||
const pluginsTransform = require("./transformations/plugins/plugins"); | ||
const topScopeTransform = require("./transformations/top-scope/top-scope"); | ||
const devServerTransform = require("./transformations/devServer/devServer"); | ||
const modeTransform = require("./transformations/mode/mode"); | ||
const resolveLoaderTransform = require("./transformations/resolveLoader/resolveLoader"); | ||
|
||
const transformsObject = { | ||
entryTransform, | ||
outputTransform, | ||
contextTransform, | ||
resolveTransform, | ||
devtoolTransform, | ||
targetTransform, | ||
watchTransform, | ||
watchOptionsTransform, | ||
externalsTransform, | ||
nodeTransform, | ||
performanceTransform, | ||
statsTransform, | ||
amdTransform, | ||
bailTransform, | ||
cacheTransform, | ||
profileTransform, | ||
moduleTransform, | ||
pluginsTransform, | ||
topScopeTransform, | ||
mergeTransform, | ||
devServerTransform, | ||
modeTransform, | ||
parallelismTransform, | ||
recordsInputPathTransform, | ||
recordsOutputPathTransform, | ||
recordsPathTransform, | ||
resolveLoaderTransform | ||
}; | ||
|
||
/** | ||
* | ||
* Runs yeoman and runs the transformations based on the object | ||
* built up from an author/user | ||
* Maps back transforms that needs to be run using the configuration | ||
* provided. | ||
* | ||
* @param {String} options - An path to the given generator | ||
* @returns {Function} runTransform - Run transformations based on the finished | ||
* yeoman instance | ||
* @param {Object} transformObject - An Object with all transformations | ||
* @param {Object} config - Configuration to transform | ||
* @returns {Object} - An Object with the transformations to be run | ||
*/ | ||
|
||
function creator(options) { | ||
let env = yeoman.createEnv("webpack", null); | ||
const generatorName = options | ||
? replaceGeneratorName(path.basename(options[0])) | ||
: "webpack-default-generator"; | ||
if (options) { | ||
const WebpackGenerator = class extends Generator { | ||
initializing() { | ||
options.forEach(path => { | ||
return this.composeWith(require.resolve(path)); | ||
}); | ||
function mapOptionsToTransform(transformObject, config) { | ||
return Object.keys(transformObject) | ||
.map(transformKey => { | ||
const stringVal = transformKey.substr( | ||
0, | ||
transformKey.indexOf("Transform") | ||
); | ||
if (Object.keys(config.webpackOptions).length) { | ||
if (config.webpackOptions[stringVal]) { | ||
return [ | ||
transformObject[transformKey], | ||
config.webpackOptions[stringVal] | ||
]; | ||
} else { | ||
return [transformObject[transformKey], config[stringVal]]; | ||
} | ||
} else { | ||
return [transformObject[transformKey]]; | ||
} | ||
}; | ||
env.registerStub(WebpackGenerator, generatorName); | ||
} else { | ||
env.registerStub(defaultGenerator, "webpack-default-generator"); | ||
} | ||
}) | ||
.filter(e => e[1]); | ||
} | ||
|
||
env.run(generatorName).on("end", _ => { | ||
if (generatorName !== "webpack-default-generator") { | ||
//HACK / FIXME | ||
env = env.options.env; | ||
return runTransform(env.configuration); | ||
} else { | ||
return runTransform(env.getArgument("configuration")); | ||
} | ||
/** | ||
* | ||
* Runs the transformations from an object we get from yeoman | ||
* | ||
* @param {Object} webpackProperties - Configuration to transform | ||
* @param {String} action - Action to be done on the given ast | ||
* @returns {Promise} - A promise that writes each transform, runs prettier | ||
* and writes the file | ||
*/ | ||
|
||
module.exports = function runTransform(webpackProperties, action) { | ||
// webpackOptions.name sent to nameTransform if match | ||
const webpackConfig = Object.keys(webpackProperties).filter(p => { | ||
return p !== "configFile" && p !== "configPath"; | ||
}); | ||
} | ||
const initActionNotDefined = action && action !== "init" ? true : false; | ||
|
||
/* | ||
* @function replaceGeneratorName | ||
* | ||
* Replaces the webpack-addons pattern with the end of the addons name merged | ||
* with 'generator' | ||
* | ||
* @param { String } name - name of the generator | ||
* @returns { String } name - replaced pattern of the name | ||
*/ | ||
webpackConfig.forEach(scaffoldPiece => { | ||
const config = webpackProperties[scaffoldPiece]; | ||
const transformations = mapOptionsToTransform(transformsObject, config); | ||
const ast = j( | ||
initActionNotDefined | ||
? webpackProperties.configFile | ||
: "module.exports = {}" | ||
); | ||
const transformAction = action || null; | ||
|
||
function replaceGeneratorName(name) { | ||
return name.replace(/(webpack-addons)?([^:]+)(:.*)?/g, "generator$2"); | ||
} | ||
return pEachSeries(transformations, f => { | ||
if (!f[1]) { | ||
return f[0](j, ast, transformAction); | ||
} else { | ||
return f[0](j, ast, f[1], transformAction); | ||
} | ||
}) | ||
.then(_ => { | ||
let configurationName; | ||
if (!config.configName) { | ||
configurationName = "webpack.config.js"; | ||
} else { | ||
configurationName = "webpack." + config.configName + ".js"; | ||
} | ||
|
||
module.exports = { | ||
creator, | ||
replaceGeneratorName | ||
const outputPath = initActionNotDefined | ||
? webpackProperties.configPath | ||
: path.join(process.cwd(), configurationName); | ||
const source = ast.toSource({ | ||
quote: "single" | ||
}); | ||
|
||
runPrettier(outputPath, source); | ||
}) | ||
.catch(err => { | ||
console.error(err.message ? err.message : err); | ||
}); | ||
}); | ||
if (initActionNotDefined && webpackProperties.config.item) { | ||
process.stdout.write( | ||
"\n" + | ||
chalk.green( | ||
`Congratulations! ${ | ||
webpackProperties.config.item | ||
} has been ${action}ed!\n` | ||
) | ||
); | ||
} else { | ||
process.stdout.write( | ||
"\n" + | ||
chalk.green( | ||
"Congratulations! Your new webpack configuration file has been created!\n" | ||
) | ||
); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ => {
can be() => {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noop functions are
_
Google/Chrome convention that is persistent across the source codeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is not a NOOP function.
Here there is a variable
_
that is defined and not used inside the function. This is a NOOP function:_ => {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it is kinda misleading provided we will also have lodash defined in
_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is rather inconsistent
![image](https://user-images.githubusercontent.com/1002461/37202036-b314862a-2389-11e8-85b2-2975cb011a50.png)
I think @ev1stensberg is referring to the common practice in functional programming of using
_
when the parameter is not usedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok!