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

enh(webpack-scaffold): improve prompt and doc #794

Merged
merged 7 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 33 additions & 21 deletions INIT.md
Expand Up @@ -3,74 +3,86 @@
`webpack-cli init` is used to initialize `webpack` projects quickly by scaffolding configuration and installing modules required for the project as per user preferences.

## Initial Setup
A. **Local setup**

Follow given steps to locally setup `webpack-cli init` by installing dependencies:
### a. Local setup

Those are the steps necessary to setup `webpack-cli init` locally:
misterdev marked this conversation as resolved.
Show resolved Hide resolved

1. Create `package.json` through npm

```shell
$ npm init
npm init
```

2. Install `webpack` and `webpack-cli` as devDependencies

```shell
$ npm install --save-dev webpack webpack-cli
npm install --save-dev webpack webpack-cli
```

3. Install `@webpack-cli/init` package to add init scaffold

```shell
$ npm install --save-dev @webpack-cli/init
npm install --save-dev @webpack-cli/init
```

B. **Global Setup**
### b. Global Setup

Follow following steps to setup `webpack-cli init` globally:
1. Install `webpack` and `webpack-cli` globally
```shell
$ npm install -g webpack webpack-cli
npm install -g webpack webpack-cli
```

2. Install `@webpack-cli/init` package to add init scaffold
```shell
$ npm install -g @webpack-cli/init
npm install -g @webpack-cli/init
```

## Usage
A. **For local setup**:

### a. Running locally

```shell
$ npx webpack-cli init
npx webpack-cli init
```

B. **For global setup**
### b. Running globally

```shell
$ webpack-cli init
webpack-cli init
```

### Description of questions asked by generator

1. `Will your application have multiple bundles? (Y/n)`
1. `Will your application have multiple bundles? (y/N)`

> *Property/key resolved: [entry](https://webpack.js.org/configuration/entry-context/#entry)*

This is used to determine if your app will have multiple [entry points](https://webpack.js.org/configuration/entry-context/#entry).
If you want to have multiple entry points, answer yes. If you want to have only one, answer no.

2. `Which will be your application entry point? (src/index)`

> *Property/key to resolve: [entry](https://webpack.js.org/configuration/entry-context/#entry)*
> *Property/key resolved: [entry](https://webpack.js.org/configuration/entry-context/#entry)*

What we are meaning here, is if you want to provide your bundle a single or multiple [entry points](https://webpack.js.org/configuration/entry-context/#entry). If you have more than one entry point to your app, answer yes. If you only have one, answer no.
This allows webpack to know from which file to start bundling your application. The default answer `src/index` will tell webpack to look for `index.js` inside a folder named `src`.
misterdev marked this conversation as resolved.
Show resolved Hide resolved

2. `Which folder will your generated bundles be in? [default: dist]`
3. `In which folder do you want to store your generated bundles? (dist)`

> *Property/key to resolve: [output.path](https://webpack.js.org/configuration/output/#output-path)*
> *Property/key resolved: [output.path](https://webpack.js.org/configuration/output/#output-path)*

This answers to the [output directory](https://webpack.js.org/configuration/output/#output-path) of your application. The output directory is where servers or your `index.html` will read the generated bundle from.
The output directory is where your bundled application will be. Your `index.html` will read the generated files from this folder, that is usually named `dist`.

4. `Will you be using ES2015? (Y/n)`

> *Property/key to resolve: [module.rules](https://webpack.js.org/configuration/module/#module-rules) (for .js files)*
> *Property/key resolved: [module.rules](https://webpack.js.org/configuration/module/#module-rules) (for .js files)*

If you answer `Yes` to this question, we will add [`ES2015`](https://babeljs.io/learn-es2015/) to your webpack configuration, which will allow you to use modern JavaScript in your project.
This enables webpack to parse [`ES2015`](https://babeljs.io/learn-es2015/) code. Answer `Yes` if you to use modern JavaScript in your project.
misterdev marked this conversation as resolved.
Show resolved Hide resolved

5. `Will you use one of the below CSS solutions?`

> *Property/key to resolve: [module.rules](https://webpack.js.org/configuration/module/#module-rules) (for .scss,.less,.css,.postCSS files)*
> *Property/key resolved: [module.rules](https://webpack.js.org/configuration/module/#module-rules) (for .scss,.less,.css,.postCSS files)*

If you use any sort of style in your project, such as [`.less`](http://lesscss.org/), [`.scss`](http://sass-lang.com/), [`.css`](https://developer.mozilla.org/en-US/docs/Web/CSS) or [`postCSS`](http://postcss.org/) you will need to declare this here. If you don't use CSS, answer no.

Expand Down
2 changes: 1 addition & 1 deletion packages/generators/add-generator.ts
Expand Up @@ -147,7 +147,7 @@ export default class AddGenerator extends Generator {
.then((_: void) => {
if (action === "entry") {
return this.prompt([
Confirm("entryType", "Will your application have multiple bundles?"),
Confirm("entryType", "Will your application have multiple bundles?", false),
])
.then((entryTypeAnswer: {
entryType: boolean,
Expand Down
10 changes: 5 additions & 5 deletions packages/generators/init-generator.ts
Expand Up @@ -87,7 +87,7 @@ export default class InitGenerator extends Generator {
);

return this.prompt([
Confirm("entryType", "Will your application have multiple bundles?"),
Confirm("entryType", "Will your application have multiple bundles?", false),
])
.then((entryTypeAnswer: {
entryType: boolean;
Expand All @@ -100,7 +100,7 @@ export default class InitGenerator extends Generator {
return this.prompt([
Input(
"outputType",
"Which folder will your generated bundles be in? [default: dist]:",
"In which folder do you want to store your generated bundles? (dist):",
),
]);
}
Expand All @@ -110,7 +110,7 @@ export default class InitGenerator extends Generator {
return this.prompt([
Input(
"outputType",
"Which folder will your generated bundles be in? [default: dist]:",
"In which folder do you want to store your generated bundles? (dist):",
),
]);
})
Expand Down Expand Up @@ -165,11 +165,11 @@ export default class InitGenerator extends Generator {
.then((_: void) => {
return this.prompt([
List("stylingType", "Will you use one of the below CSS solutions?", [
"No",
"CSS",
"SASS",
"LESS",
"CSS",
"PostCSS",
"No",
]),
]);
})
Expand Down
3 changes: 2 additions & 1 deletion packages/generators/types/yeoman-generator.d.ts
Expand Up @@ -69,7 +69,8 @@ declare module "yeoman-generator" {
name: string;
message: string | ((answers: Object) => string);
choices?: string[] | ((answers: Object) => string);
default?: string | number | string[] | number[] | ((answers: Object) => (string | number | string[] | number[]));
default?: string | number | boolean | string[] | number[]
| ((answers: Object) => (string | number | boolean | string[] | number[]));
validate?: ((input: string) => boolean | string);
when?: ((answers: Object) => boolean) | boolean;
store?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/utils/entry.ts
Expand Up @@ -89,7 +89,7 @@ export default function entry(self: IEntry, answer: {
.prompt([
InputValidate(
"singularEntry",
"Which module will be the first to enter the application? [default: ./src/index]",
"Which will be your application entry point? (src/index)",
),
])
.then((singularEntryAnswer: {
Expand Down
Expand Up @@ -31,13 +31,13 @@ callback();
}"
`;

exports[`utils createRegularFunction should stringify an regular function 1`] = `
exports[`utils createRegularFunction should stringify a regular function 1`] = `
"function () {
return 'app.js'
}"
`;

exports[`utils createRequire should stringify an require statement 1`] = `"const webpack = require('webpack');"`;
exports[`utils createRequire should stringify a require statement 1`] = `"const webpack = require('webpack');"`;

exports[`utils parseValue should parse value 1`] = `
Collection {
Expand Down
21 changes: 15 additions & 6 deletions packages/webpack-scaffold/__tests__/index.test.ts
Expand Up @@ -8,7 +8,7 @@ describe("utils", () => {
});
});
describe("createRegularFunction", () => {
it("should stringify an regular function", () => {
it("should stringify a regular function", () => {
expect(utils.createRegularFunction("app.js")).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -41,20 +41,20 @@ describe("utils", () => {
});
});
describe("createRequire", () => {
it("should stringify an require statement", () => {
it("should stringify a require statement", () => {
expect(utils.createRequire("webpack")).toMatchSnapshot();
});
});
describe("Inquirer", () => {
it("should make an List object", () => {
it("should make a List object", () => {
expect(utils.List("entry", "does it work?", ["Yes", "Maybe"])).toEqual({
choices: ["Yes", "Maybe"],
message: "does it work?",
name: "entry",
type: "list",
});
});
it("should make an RawList object", () => {
it("should make a RawList object", () => {
expect(
utils.RawList("output", "does it work?", ["Yes", "Maybe"]),
).toEqual({
Expand All @@ -64,7 +64,7 @@ describe("utils", () => {
type: "rawlist",
});
});
it("should make an CheckList object", () => {
it("should make a CheckList object", () => {
expect(
utils.CheckList("context", "does it work?", ["Yes", "Maybe"]),
).toEqual({
Expand All @@ -81,8 +81,17 @@ describe("utils", () => {
type: "input",
});
});
it("should make an Confirm object", () => {
it("should make a Confirm object", () => {
expect(utils.Confirm("context", "what is your context?")).toEqual({
default: true,
message: "what is your context?",
name: "context",
type: "confirm",
});
});
it("should make a Confirm object with No as default", () => {
expect(utils.Confirm("context", "what is your context?", false)).toEqual({
default: false,
message: "what is your context?",
name: "context",
type: "confirm",
Expand Down
6 changes: 4 additions & 2 deletions packages/webpack-scaffold/index.ts
Expand Up @@ -5,7 +5,8 @@ export interface IInquirerScaffoldObject {
name: string;
message: string;
choices?: ((answers: Object) => string) | string[];
default?: string | number | string[] | number[] | ((answers: Object) => (string | number | string[] | number[]));
default?: string | number | boolean | string[] | number[]
| ((answers: Object) => (string | number | boolean | string[] | number[]));
validate?: ((input: string) => boolean | string);
when?: ((answers: Object) => boolean) | boolean;
store?: boolean;
Expand Down Expand Up @@ -110,8 +111,9 @@ export function InputValidate(name: string, message: string, cb?: (input: string
};
}

export function Confirm(name: string, message: string): IInquirerScaffoldObject {
export function Confirm(name: string, message: string, defaultChoice: boolean = true): IInquirerScaffoldObject {
return {
default: defaultChoice,
message,
name,
type: "confirm",
Expand Down