Skip to content

Commit 488b06c

Browse files
committedMay 28, 2019
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

+21-25
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

+39-57
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

+16-19
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
}

‎packages/generators/utils/style.ts

+30-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export enum StylingType {
77
PostCSS = "PostCSS",
88
}
99

10-
export enum Loader {
10+
export enum LoaderName {
1111
CSS = "css-loader",
1212
SASS = "sass-loader",
1313
STYLE = "style-loader",
@@ -22,7 +22,7 @@ export enum StyleRegex {
2222
PostCSS = "/\.css$/",
2323
}
2424

25-
export interface ILoader {
25+
export interface Loader {
2626
loader: string;
2727
options?: {
2828
importLoaders?: number;
@@ -31,29 +31,32 @@ export interface ILoader {
3131
};
3232
}
3333

34-
export default function style(self, stylingType) {
35-
const ExtractUseProps: ILoader[] = [];
36-
let regExpForStyles = null;
34+
export default function style(self, stylingType: string): {
35+
ExtractUseProps: Loader[],
36+
regExpForStyles: StyleRegex,
37+
} {
38+
const ExtractUseProps: Loader[] = [];
39+
let regExpForStyles: StyleRegex = null;
3740

3841
switch (stylingType) {
3942
case StylingType.CSS:
4043
regExpForStyles = StyleRegex.CSS;
4144

4245
self.dependencies.push(
43-
Loader.CSS,
46+
LoaderName.CSS,
4447
);
4548
if (!self.isProd) {
4649
self.dependencies.push(
47-
Loader.STYLE,
50+
LoaderName.STYLE,
4851
);
4952
ExtractUseProps.push(
5053
{
51-
loader: `"${Loader.STYLE}"`,
54+
loader: `"${LoaderName.STYLE}"`,
5255
},
5356
);
5457
}
5558
ExtractUseProps.push({
56-
loader: `"${Loader.CSS}"`,
59+
loader: `"${LoaderName.CSS}"`,
5760
options: {
5861
sourceMap: true,
5962
},
@@ -65,28 +68,28 @@ export default function style(self, stylingType) {
6568

6669
self.dependencies.push(
6770
"node-sass",
68-
Loader.SASS,
69-
Loader.CSS,
71+
LoaderName.SASS,
72+
LoaderName.CSS,
7073
);
7174
if (!self.isProd) {
7275
self.dependencies.push(
73-
Loader.STYLE,
76+
LoaderName.STYLE,
7477
);
7578
ExtractUseProps.push(
7679
{
77-
loader: `"${Loader.STYLE}"`,
80+
loader: `"${LoaderName.STYLE}"`,
7881
},
7982
);
8083
}
8184
ExtractUseProps.push(
8285
{
83-
loader: `"${Loader.CSS}"`,
86+
loader: `"${LoaderName.CSS}"`,
8487
options: {
8588
sourceMap: true,
8689
},
8790
},
8891
{
89-
loader: `"${Loader.SASS}"`,
92+
loader: `"${LoaderName.SASS}"`,
9093
options: {
9194
sourceMap: true,
9295
},
@@ -99,28 +102,28 @@ export default function style(self, stylingType) {
99102

100103
self.dependencies.push(
101104
"less",
102-
Loader.LESS,
103-
Loader.CSS,
105+
LoaderName.LESS,
106+
LoaderName.CSS,
104107
);
105108
if (!self.isProd) {
106109
self.dependencies.push(
107-
Loader.STYLE,
110+
LoaderName.STYLE,
108111
);
109112
ExtractUseProps.push(
110113
{
111-
loader: `"${Loader.STYLE}"`,
114+
loader: `"${LoaderName.STYLE}"`,
112115
},
113116
);
114117
}
115118
ExtractUseProps.push(
116119
{
117-
loader: `"${Loader.CSS}"`,
120+
loader: `"${LoaderName.CSS}"`,
118121
options: {
119122
sourceMap: true,
120123
},
121124
},
122125
{
123-
loader: `"${Loader.LESS}"`,
126+
loader: `"${LoaderName.LESS}"`,
124127
options: {
125128
sourceMap: true,
126129
},
@@ -141,29 +144,29 @@ export default function style(self, stylingType) {
141144
self.dependencies.push(
142145
"precss",
143146
"autoprefixer",
144-
Loader.CSS,
145-
Loader.POSTCSS,
147+
LoaderName.CSS,
148+
LoaderName.POSTCSS,
146149
);
147150
if (!self.isProd) {
148151
self.dependencies.push(
149-
Loader.STYLE,
152+
LoaderName.STYLE,
150153
);
151154
ExtractUseProps.push(
152155
{
153-
loader: `"${Loader.STYLE}"`,
156+
loader: `"${LoaderName.STYLE}"`,
154157
},
155158
);
156159
}
157160
ExtractUseProps.push(
158161
{
159-
loader: `"${Loader.CSS}"`,
162+
loader: `"${LoaderName.CSS}"`,
160163
options: {
161164
importLoaders: 1,
162165
sourceMap: true,
163166
},
164167
},
165168
{
166-
loader: `"${Loader.POSTCSS}"`,
169+
loader: `"${LoaderName.POSTCSS}"`,
167170
options: {
168171
plugins: `function () {
169172
return [

‎packages/utils/scaffold.ts

+19-23
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,26 @@ export default function runTransform(transformConfig: TransformConfig, action: s
7070
return astTransform(j, ast, f, config[f] as any, transformAction);
7171
}
7272
return astTransform(j, ast, f, config.webpackOptions[f], transformAction);
73-
}
74-
)
75-
.then(
76-
(): void | PromiseLike<void> => {
77-
let configurationName: string;
78-
if (!config.configName) {
79-
configurationName = "webpack.config.js";
80-
} else {
81-
configurationName = "webpack." + config.configName + ".js";
82-
}
83-
84-
const projectRoot = findProjectRoot();
85-
const outputPath: string = initActionNotDefined
86-
? transformConfig.configPath
87-
: path.join(projectRoot || process.cwd(), configurationName);
88-
const source: string = ast.toSource({
89-
quote: "single",
73+
})
74+
.then((): void | PromiseLike<void> => {
75+
let configurationName: string;
76+
if (!config.configName) {
77+
configurationName = "webpack.config.js";
78+
} else {
79+
configurationName = "webpack." + config.configName + ".js";
80+
}
81+
const projectRoot = findProjectRoot();
82+
const outputPath: string = initActionNotDefined
83+
? transformConfig.configPath
84+
: path.join(projectRoot || process.cwd(), configurationName);
85+
const source: string = ast.toSource({
86+
quote: "single",
87+
});
88+
runPrettier(outputPath, source);
89+
})
90+
.catch((err: Error): void => {
91+
console.error(err.message ? err.message : err);
9092
});
91-
runPrettier(outputPath, source);
92-
93-
})
94-
.catch((err: IError) => {
95-
console.error(err.message ? err.message : err);
96-
});
9793
});
9894
let successMessage: string =
9995
chalk.green(`Congratulations! Your new webpack configuration file has been created!\n\n`) +

0 commit comments

Comments
 (0)
Please sign in to comment.