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

cli(init): Better defaults #451

Merged
merged 9 commits into from
May 22, 2018
25 changes: 21 additions & 4 deletions packages/generators/init-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ module.exports = class InitGenerator extends Generator {
constructor(args, opts) {
super(args, opts);
this.isProd = false;
this.dependencies = ["webpack", "webpack-cli", "uglifyjs-webpack-plugin"];
(this.usingDefaults = false),
(this.dependencies = [
"webpack",
"webpack-cli",
"uglifyjs-webpack-plugin",
"babel-plugin-syntax-dynamic-import",
"path"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't path core Node.js module?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it can be accessed only after defining.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but these are packages that would get installed by npm/yarn, right? Definition part would be in topScope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly. path needs to be removed

]);
this.configuration = {
config: {
webpackOptions: {},
Expand Down Expand Up @@ -93,20 +100,25 @@ module.exports = class InitGenerator extends Generator {
.then(outputTypeAnswer => {
// As entry is not required anymore and we dont set it to be an empty string or """""
// it can be undefined so falsy check is enough (vs entry.length);
if (!this.configuration.config.webpackOptions.entry) {
if (
!this.configuration.config.webpackOptions.entry &&
!this.usingDefaults
) {
this.configuration.config.webpackOptions.output = {
filename: "'[name].[chunkhash].js'",
chunkFilename: "'[name].[chunkhash].js'"
};
} else {
} else if (!this.usingDefaults) {
this.configuration.config.webpackOptions.output = {
filename: "'[name].[chunkhash].js'"
};
}
if (outputTypeAnswer["outputType"].length) {
outputPath = outputTypeAnswer["outputType"];
}
this.configuration.config.webpackOptions.output.path = `path.resolve(__dirname, '${outputPath}')`;
if (!this.usingDefaults) {
this.configuration.config.webpackOptions.output.path = `path.resolve(__dirname, '${outputPath}')`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am kinda worried about this level of nesting, won't it impact performance ?
I remember some times when you have to this level deep, it is better to assign them to a variable and go just one level deeper.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. It's not a super-big generator, so it might be a premature optimization?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that we have to think about optimisation in advance

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

}
})
.then(() => {
return this.prompt([
Expand Down Expand Up @@ -394,9 +406,13 @@ module.exports = class InitGenerator extends Generator {
done();
});
}
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this commented code? If so, it can be removed

installPlugins() {
const asyncNamePrompt = this.async();
const defaultName = this.isProd ? "prod" : "config";
if(this.isProd) {
this.dependencies = this.dependencies.filter(p => p !== "uglifyjs-webpack-plugin");
}
this.prompt([
Input(
"nameType",
Expand All @@ -415,6 +431,7 @@ module.exports = class InitGenerator extends Generator {
this.runInstall(packager, this.dependencies, opts);
});
}
*/

writing() {
this.config.set("configuration", this.configuration);
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/utils/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = (self, answer) => {
singularEntry = singularEntry.replace(/"/g, "'");
}
if (singularEntry.length <= 0) {
singularEntry = "'./src/index.js'";
self.usingDefaults = true;
}
return singularEntry;
});
Expand Down
7 changes: 5 additions & 2 deletions packages/generators/utils/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
module.exports = _ => {
return {
test: `${new RegExp(/\.js$/)}`,
exclude: "/node_modules/",
include: ["path.resolve(__dirname, 'src')"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sokra just wondering. If the entry folder is different, then this would need to change, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, can't we take this from the config file?

If not present then use this as a default value

loader: "'babel-loader'",
options: {
presets: ["'env'"]
presets: ["'env'", {
modules: false
}],
plugins: ["'syntax-dynamic-import'"]
}
};
};
4 changes: 3 additions & 1 deletion packages/utils/recursive-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ module.exports = function recursiveTransform(j, ast, value, action, key) {
} else if (key === "merge") {
utils.parseMerge(j, p, value);
} else {
utils.addProperty(j, p, key, value);
if (value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not an else if?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old commit

utils.addProperty(j, p, key, value);
}
}
});
}
Expand Down