Skip to content

Commit 488b06c

Browse files
committed
misc(packages): complete rebase
1 parent ac35a31 commit 488b06c

File tree

5 files changed

+125
-151
lines changed

5 files changed

+125
-151
lines changed

packages/generators/init-generator.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import chalk from "chalk";
32
import * as logSymbols from "log-symbols";
43
import * as Generator from "yeoman-generator";
@@ -11,7 +10,7 @@ import { Confirm, Input, List } from "@webpack-cli/webpack-scaffold";
1110
import { WebpackOptions } from "./types";
1211
import entryQuestions from "./utils/entry";
1312
import langQuestionHandler from "./utils/language";
14-
import styleQuestionHandler, { ILoader, StylingType } from "./utils/style";
13+
import styleQuestionHandler, { Loader, StylingType } from "./utils/style";
1514
import tooltip from "./utils/tooltip";
1615

1716
/**
@@ -55,8 +54,6 @@ export default class InitGenerator extends Generator {
5554
config: {
5655
configName: this.isProd ? "prod" : "config",
5756
topScope: [],
58-
// TODO migrate tslint
59-
// tslint:disable: object-literal-sort-keys
6057
webpackOptions: {
6158
mode: this.isProd ? "'production'" : "'development'",
6259
entry: undefined,
@@ -66,7 +63,6 @@ export default class InitGenerator extends Generator {
6663
rules: [],
6764
},
6865
},
69-
// tslint:enable: object-literal-sort-keys
7066
},
7167
};
7268

@@ -87,7 +83,7 @@ export default class InitGenerator extends Generator {
8783
);
8884
}
8985

90-
this.configuration.config.webpackOptions.plugins.push(
86+
(this.configuration.config.webpackOptions.plugins as string[]).push(
9187
"new webpack.ProgressPlugin()",
9288
);
9389

@@ -134,7 +130,7 @@ export default class InitGenerator extends Generator {
134130
const done: () => {} = this.async();
135131
const self: this = this;
136132
let regExpForStyles: string;
137-
let ExtractUseProps: ILoader[];
133+
let ExtractUseProps: Loader[];
138134

139135
process.stdout.write(
140136
`\n${logSymbols.info}${chalk.blue(" INFO ")} ` +
@@ -151,17 +147,17 @@ export default class InitGenerator extends Generator {
151147
])
152148
.then((multiEntriesAnswer: {
153149
multiEntries: boolean,
154-
}) =>
150+
}): Promise<{}> =>
155151
entryQuestions(self, multiEntriesAnswer.multiEntries),
156152
)
157-
.then((entryOption: object | string) => {
153+
.then((entryOption: object | string): void => {
158154
if (typeof entryOption === "string" && entryOption.length > 0) {
159155
this.configuration.config.webpackOptions.entry = `${entryOption}`;
160156
} else if (typeof entryOption === "object") {
161157
this.configuration.config.webpackOptions.entry = entryOption;
162158
}
163159
})
164-
.then(() =>
160+
.then((): Promise<{}> =>
165161
this.prompt([
166162
Input(
167163
"outputDir",
@@ -172,7 +168,7 @@ export default class InitGenerator extends Generator {
172168
)
173169
.then((outputDirAnswer: {
174170
outputDir: string;
175-
}) => {
171+
}): void => {
176172
// As entry is not required anymore and we dont set it to be an empty string or """""
177173
// it can be undefined so falsy check is enough (vs entry.length);
178174
if (
@@ -193,7 +189,7 @@ export default class InitGenerator extends Generator {
193189
`path.resolve(__dirname, '${outputDirAnswer.outputDir}')`;
194190
}
195191
})
196-
.then(() =>
192+
.then((): Promise<{}> =>
197193
this.prompt([
198194
List("langType", "Will you use one of the below JS solutions?", [
199195
"ES6",
@@ -203,11 +199,11 @@ export default class InitGenerator extends Generator {
203199
]),
204200
)
205201
.then((langTypeAnswer: {
206-
langType: boolean;
207-
}) => {
202+
langType: string;
203+
}): void => {
208204
langQuestionHandler(this, langTypeAnswer.langType);
209205
})
210-
.then(() =>
206+
.then((): Promise<{}> =>
211207
this.prompt([
212208
List("stylingType", "Will you use one of the below CSS solutions?", [
213209
"No",
@@ -219,10 +215,10 @@ export default class InitGenerator extends Generator {
219215
]))
220216
.then((stylingTypeAnswer: {
221217
stylingType: string;
222-
}) =>
223-
({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingTypeAnswer.stylingType)),
224-
)
225-
.then((): Promise<Inquirer.Answers> => {
218+
}): void => {
219+
({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingTypeAnswer.stylingType));
220+
})
221+
.then((): Promise<{}> | void => {
226222
if (this.isProd) {
227223
// Ask if the user wants to use extractPlugin
228224
return this.prompt([
@@ -235,7 +231,7 @@ export default class InitGenerator extends Generator {
235231
})
236232
.then((useExtractPluginAnswer: {
237233
useExtractPlugin: string;
238-
}) => {
234+
}): void => {
239235
if (regExpForStyles) {
240236
if (this.isProd) {
241237
const cssBundleName: string = useExtractPluginAnswer.useExtractPlugin;
@@ -246,12 +242,12 @@ export default class InitGenerator extends Generator {
246242
"\n",
247243
);
248244
if (cssBundleName.length !== 0) {
249-
this.configuration.config.webpackOptions.plugins.push(
245+
(this.configuration.config.webpackOptions.plugins as string[]).push(
250246
// TODO: use [contenthash] after it is supported
251247
`new MiniCssExtractPlugin({ filename:'${cssBundleName}.[chunkhash].css' })`,
252248
);
253249
} else {
254-
this.configuration.config.webpackOptions.plugins.push(
250+
(this.configuration.config.webpackOptions.plugins as string[]).push(
255251
"new MiniCssExtractPlugin({ filename:'style.css' })",
256252
);
257253
}
@@ -273,7 +269,7 @@ export default class InitGenerator extends Generator {
273269
});
274270
}
275271

276-
public installPlugins() {
272+
public installPlugins(): void {
277273
const packager = getPackageManager();
278274
const opts: {
279275
dev?: boolean,
@@ -292,7 +288,7 @@ export default class InitGenerator extends Generator {
292288
this.fs.extendJSON(this.destinationPath("package.json"), require(packageJsonTemplatePath)(this.isProd));
293289

294290
const entry = this.configuration.config.webpackOptions.entry;
295-
const generateEntryFile = (entryPath: string, name: string) => {
291+
const generateEntryFile = (entryPath: string, name: string): void => {
296292
entryPath = entryPath.replace(/'/g, "");
297293
this.fs.copyTpl(
298294
path.resolve(__dirname, "./templates/index.js"),
@@ -304,7 +300,7 @@ export default class InitGenerator extends Generator {
304300
if ( typeof entry === "string" ) {
305301
generateEntryFile(entry, "your main file!");
306302
} else if (typeof entry === "object") {
307-
Object.keys(entry).forEach((name) =>
303+
Object.keys(entry).forEach((name: string): void =>
308304
generateEntryFile(entry[name], `${name} main file!`),
309305
);
310306
}

packages/generators/utils/entry.ts

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ interface CustomGenerator extends Generator {
1616
* @returns {Object} An Object that holds the answers given by the user, later used to scaffold
1717
*/
1818

19-
export default function entry(self: CustomGenerator, multiEntries: boolean): Promise<{}> {
19+
export default function entry(
20+
self: CustomGenerator,
21+
multiEntries: boolean,
22+
): Promise<{}> {
2023
let entryIdentifiers: string[];
2124
let result: Promise<{}>;
2225
if (multiEntries) {
@@ -26,8 +29,8 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
2629
"multipleEntries",
2730
"How do you want to name your bundles? (separated by comma)",
2831
validate,
29-
"pageOne, pageTwo",
30-
),
32+
"pageOne, pageTwo"
33+
)
3134
])
3235
.then(
3336
(multipleEntriesAnswer: { multipleEntries: string }): Promise<void | {}> => {
@@ -53,7 +56,7 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
5356
!n[val].includes("path") &&
5457
!n[val].includes("process")
5558
) {
56-
n[val] = `\'${n[val].replace(/"|'/g, "").concat(".js")}\'`;
59+
n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`;
5760
}
5861
webpackEntryPoint[val] = n[val];
5962
}
@@ -66,59 +69,37 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
6669
);
6770
}, Promise.resolve());
6871
}
72+
6973
return forEachPromise(
7074
entryIdentifiers,
7175
(entryProp: string): Promise<void | {}> =>
7276
self.prompt([
7377
InputValidate(
7478
`${entryProp}`,
75-
`What is the location of "${entryProp}"? [example: ./src/${entryProp}]`,
76-
validate
77-
)
78-
])
79-
).then(
80-
(entryPropAnswer: object): object => {
81-
Object.keys(entryPropAnswer).forEach(
82-
(val: string): void => {
83-
if (
84-
entryPropAnswer[val].charAt(0) !== "(" &&
85-
entryPropAnswer[val].charAt(0) !== "[" &&
86-
!entryPropAnswer[val].includes("function") &&
87-
!entryPropAnswer[val].includes("path") &&
88-
!entryPropAnswer[val].includes("process")
89-
) {
90-
n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`;
79+
`What is the location of "${entryProp}"?`,
80+
validate,
81+
`src/${entryProp}`,
82+
),
83+
]))
84+
.then(
85+
(entryPropAnswer: object): object => {
86+
Object.keys(entryPropAnswer).forEach(
87+
(val: string): void => {
88+
if (
89+
entryPropAnswer[val].charAt(0) !== "(" &&
90+
entryPropAnswer[val].charAt(0) !== "[" &&
91+
!entryPropAnswer[val].includes("function") &&
92+
!entryPropAnswer[val].includes("path") &&
93+
!entryPropAnswer[val].includes("process")
94+
) {
95+
entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`;
96+
}
97+
webpackEntryPoint[val] = entryPropAnswer[val];
9198
}
92-
webpackEntryPoint[val] = n[val];
93-
});
94-
} else {
95-
n = {};
99+
);
100+
return webpackEntryPoint;
96101
}
97-
return fn(trimmedProp);
98-
});
99-
}, Promise.resolve());
100-
}
101-
return forEachPromise(entryIdentifiers, (entryProp: string): Promise<{} | void> =>
102-
self.prompt([
103-
InputValidate(
104-
`${entryProp}`,
105-
`Which will be the entry point of "${entryProp}"?`,
106-
validate,
107-
`src/${entryProp}`,
108-
),
109-
]),
110-
).then((entryPropAnswer: object): object => {
111-
Object.keys(entryPropAnswer).forEach((val: string): void => {
112-
if (
113-
entryPropAnswer[val].charAt(0) !== "(" &&
114-
entryPropAnswer[val].charAt(0) !== "[" &&
115-
!entryPropAnswer[val].includes("function") &&
116-
!entryPropAnswer[val].includes("path") &&
117-
!entryPropAnswer[val].includes("process")
118-
) {
119-
entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`;
120-
}
121-
);
102+
);
122103
}
123104
);
124105
} else {
@@ -128,15 +109,16 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
128109
"singularEntry",
129110
"Which will be your application entry point?",
130111
"src/index",
131-
),
112+
)
132113
])
133-
.then((singularEntryAnswer: {
134-
singularEntry: string,
135-
}): string => {
136-
let { singularEntry } = singularEntryAnswer;
137-
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
138-
if (singularEntry.length <= 0) {
139-
self.usingDefaults = true;
114+
.then(
115+
(singularEntryAnswer: { singularEntry: string }): string => {
116+
let { singularEntry } = singularEntryAnswer;
117+
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
118+
if (singularEntry.length <= 0) {
119+
self.usingDefaults = true;
120+
}
121+
return singularEntry;
140122
}
141123
);
142124
}

packages/generators/utils/language.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ interface ModuleRule extends Object {
1616

1717
type Preset = string | object;
1818

19+
function updateEntryExt(self, newExt: string): void {
20+
const jsEntryOption = self.configuration.config.webpackOptions.entry;
21+
const jsExtension = new RegExp("\.js(?!.*\.js)");
22+
let tsEntryOption = {};
23+
if (typeof jsEntryOption === "string") {
24+
tsEntryOption = jsEntryOption.replace(jsExtension, newExt);
25+
} else if (typeof jsEntryOption === "object") {
26+
Object.keys(jsEntryOption).forEach((entry: string): void => {
27+
tsEntryOption[entry] = jsEntryOption[entry].replace(jsExtension, newExt);
28+
});
29+
}
30+
self.configuration.config.webpackOptions.entry = tsEntryOption;
31+
}
32+
1933
/**
2034
*
2135
* Returns an module.rule object that has the babel loader if invoked
@@ -24,8 +38,6 @@ type Preset = string | object;
2438
*/
2539
export function getBabelLoader(): ModuleRule {
2640
return {
27-
// TODO migrate tslint
28-
// tslint:disable: object-literal-sort-keys
2941
test: "/\.js$/",
3042
include: ["path.resolve(__dirname, 'src')"],
3143
loader: "'babel-loader'",
@@ -40,23 +52,19 @@ export function getBabelLoader(): ModuleRule {
4052
]
4153
]
4254
},
43-
// tslint:enable: object-literal-sort-keys
4455
};
4556
}
4657

4758
export function getTypescriptLoader(): ModuleRule {
4859
return {
49-
// TODO migrate tslint
50-
// tslint:disable: object-literal-sort-keys
5160
test: "/\.tsx?$/",
5261
loader: "'ts-loader'",
5362
include: ["path.resolve(__dirname, 'src')"],
5463
exclude: ["/node_modules/"],
55-
// tslint:enable: object-literal-sort-keys
5664
};
5765
}
5866

59-
export default function language(self, langType) {
67+
export default function language(self, langType: string): void {
6068
switch (langType) {
6169
case LangType.ES6:
6270
self.dependencies.push(
@@ -81,18 +89,7 @@ export default function language(self, langType) {
8189
extensions: [ "'.tsx'", "'.ts'", "'.js'" ],
8290
};
8391

84-
// Update the entry files extensions to .ts
85-
const jsEntryOption = self.configuration.config.webpackOptions.entry;
86-
const jsExtension = new RegExp("\.js(?!.*\.js)");
87-
let tsEntryOption = {};
88-
if (typeof jsEntryOption === "string") {
89-
tsEntryOption = jsEntryOption.replace(jsExtension, ".ts");
90-
} else if (typeof jsEntryOption === "object") {
91-
Object.keys(jsEntryOption).forEach((entry) => {
92-
tsEntryOption[entry] = jsEntryOption[entry].replace(jsExtension, ".ts");
93-
});
94-
}
95-
self.configuration.config.webpackOptions.entry = tsEntryOption;
92+
updateEntryExt(self, ".ts");
9693
break;
9794
}
9895
}

0 commit comments

Comments
 (0)