Skip to content

Commit

Permalink
misc(packages): complete rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
misterdev committed May 28, 2019
1 parent ac35a31 commit 488b06c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 151 deletions.
46 changes: 21 additions & 25 deletions packages/generators/init-generator.ts
@@ -1,4 +1,3 @@

import chalk from "chalk";
import * as logSymbols from "log-symbols";
import * as Generator from "yeoman-generator";
Expand All @@ -11,7 +10,7 @@ import { Confirm, Input, List } from "@webpack-cli/webpack-scaffold";
import { WebpackOptions } from "./types";
import entryQuestions from "./utils/entry";
import langQuestionHandler from "./utils/language";
import styleQuestionHandler, { ILoader, StylingType } from "./utils/style";
import styleQuestionHandler, { Loader, StylingType } from "./utils/style";
import tooltip from "./utils/tooltip";

/**
Expand Down Expand Up @@ -55,8 +54,6 @@ export default class InitGenerator extends Generator {
config: {
configName: this.isProd ? "prod" : "config",
topScope: [],
// TODO migrate tslint
// tslint:disable: object-literal-sort-keys
webpackOptions: {
mode: this.isProd ? "'production'" : "'development'",
entry: undefined,
Expand All @@ -66,7 +63,6 @@ export default class InitGenerator extends Generator {
rules: [],
},
},
// tslint:enable: object-literal-sort-keys
},
};

Expand All @@ -87,7 +83,7 @@ export default class InitGenerator extends Generator {
);
}

this.configuration.config.webpackOptions.plugins.push(
(this.configuration.config.webpackOptions.plugins as string[]).push(
"new webpack.ProgressPlugin()",
);

Expand Down Expand Up @@ -134,7 +130,7 @@ export default class InitGenerator extends Generator {
const done: () => {} = this.async();
const self: this = this;
let regExpForStyles: string;
let ExtractUseProps: ILoader[];
let ExtractUseProps: Loader[];

process.stdout.write(
`\n${logSymbols.info}${chalk.blue(" INFO ")} ` +
Expand All @@ -151,17 +147,17 @@ export default class InitGenerator extends Generator {
])
.then((multiEntriesAnswer: {
multiEntries: boolean,
}) =>
}): Promise<{}> =>
entryQuestions(self, multiEntriesAnswer.multiEntries),
)
.then((entryOption: object | string) => {
.then((entryOption: object | string): void => {
if (typeof entryOption === "string" && entryOption.length > 0) {
this.configuration.config.webpackOptions.entry = `${entryOption}`;
} else if (typeof entryOption === "object") {
this.configuration.config.webpackOptions.entry = entryOption;
}
})
.then(() =>
.then((): Promise<{}> =>
this.prompt([
Input(
"outputDir",
Expand All @@ -172,7 +168,7 @@ export default class InitGenerator extends Generator {
)
.then((outputDirAnswer: {
outputDir: string;
}) => {
}): void => {
// 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 (
Expand All @@ -193,7 +189,7 @@ export default class InitGenerator extends Generator {
`path.resolve(__dirname, '${outputDirAnswer.outputDir}')`;
}
})
.then(() =>
.then((): Promise<{}> =>
this.prompt([
List("langType", "Will you use one of the below JS solutions?", [
"ES6",
Expand All @@ -203,11 +199,11 @@ export default class InitGenerator extends Generator {
]),
)
.then((langTypeAnswer: {
langType: boolean;
}) => {
langType: string;
}): void => {
langQuestionHandler(this, langTypeAnswer.langType);
})
.then(() =>
.then((): Promise<{}> =>
this.prompt([
List("stylingType", "Will you use one of the below CSS solutions?", [
"No",
Expand All @@ -219,10 +215,10 @@ export default class InitGenerator extends Generator {
]))
.then((stylingTypeAnswer: {
stylingType: string;
}) =>
({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingTypeAnswer.stylingType)),
)
.then((): Promise<Inquirer.Answers> => {
}): void => {
({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingTypeAnswer.stylingType));
})
.then((): Promise<{}> | void => {
if (this.isProd) {
// Ask if the user wants to use extractPlugin
return this.prompt([
Expand All @@ -235,7 +231,7 @@ export default class InitGenerator extends Generator {
})
.then((useExtractPluginAnswer: {
useExtractPlugin: string;
}) => {
}): void => {
if (regExpForStyles) {
if (this.isProd) {
const cssBundleName: string = useExtractPluginAnswer.useExtractPlugin;
Expand All @@ -246,12 +242,12 @@ export default class InitGenerator extends Generator {
"\n",
);
if (cssBundleName.length !== 0) {
this.configuration.config.webpackOptions.plugins.push(
(this.configuration.config.webpackOptions.plugins as string[]).push(
// TODO: use [contenthash] after it is supported
`new MiniCssExtractPlugin({ filename:'${cssBundleName}.[chunkhash].css' })`,
);
} else {
this.configuration.config.webpackOptions.plugins.push(
(this.configuration.config.webpackOptions.plugins as string[]).push(
"new MiniCssExtractPlugin({ filename:'style.css' })",
);
}
Expand All @@ -273,7 +269,7 @@ export default class InitGenerator extends Generator {
});
}

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

const entry = this.configuration.config.webpackOptions.entry;
const generateEntryFile = (entryPath: string, name: string) => {
const generateEntryFile = (entryPath: string, name: string): void => {
entryPath = entryPath.replace(/'/g, "");
this.fs.copyTpl(
path.resolve(__dirname, "./templates/index.js"),
Expand All @@ -304,7 +300,7 @@ export default class InitGenerator extends Generator {
if ( typeof entry === "string" ) {
generateEntryFile(entry, "your main file!");
} else if (typeof entry === "object") {
Object.keys(entry).forEach((name) =>
Object.keys(entry).forEach((name: string): void =>
generateEntryFile(entry[name], `${name} main file!`),
);
}
Expand Down
96 changes: 39 additions & 57 deletions packages/generators/utils/entry.ts
Expand Up @@ -16,7 +16,10 @@ interface CustomGenerator extends Generator {
* @returns {Object} An Object that holds the answers given by the user, later used to scaffold
*/

export default function entry(self: CustomGenerator, multiEntries: boolean): Promise<{}> {
export default function entry(
self: CustomGenerator,
multiEntries: boolean,
): Promise<{}> {
let entryIdentifiers: string[];
let result: Promise<{}>;
if (multiEntries) {
Expand All @@ -26,8 +29,8 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
"multipleEntries",
"How do you want to name your bundles? (separated by comma)",
validate,
"pageOne, pageTwo",
),
"pageOne, pageTwo"
)
])
.then(
(multipleEntriesAnswer: { multipleEntries: string }): Promise<void | {}> => {
Expand All @@ -53,7 +56,7 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
!n[val].includes("path") &&
!n[val].includes("process")
) {
n[val] = `\'${n[val].replace(/"|'/g, "").concat(".js")}\'`;
n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`;
}
webpackEntryPoint[val] = n[val];
}
Expand All @@ -66,59 +69,37 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
);
}, Promise.resolve());
}

return forEachPromise(
entryIdentifiers,
(entryProp: string): Promise<void | {}> =>
self.prompt([
InputValidate(
`${entryProp}`,
`What is the location of "${entryProp}"? [example: ./src/${entryProp}]`,
validate
)
])
).then(
(entryPropAnswer: object): object => {
Object.keys(entryPropAnswer).forEach(
(val: string): void => {
if (
entryPropAnswer[val].charAt(0) !== "(" &&
entryPropAnswer[val].charAt(0) !== "[" &&
!entryPropAnswer[val].includes("function") &&
!entryPropAnswer[val].includes("path") &&
!entryPropAnswer[val].includes("process")
) {
n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`;
`What is the location of "${entryProp}"?`,
validate,
`src/${entryProp}`,
),
]))
.then(
(entryPropAnswer: object): object => {
Object.keys(entryPropAnswer).forEach(
(val: string): void => {
if (
entryPropAnswer[val].charAt(0) !== "(" &&
entryPropAnswer[val].charAt(0) !== "[" &&
!entryPropAnswer[val].includes("function") &&
!entryPropAnswer[val].includes("path") &&
!entryPropAnswer[val].includes("process")
) {
entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`;
}
webpackEntryPoint[val] = entryPropAnswer[val];
}
webpackEntryPoint[val] = n[val];
});
} else {
n = {};
);
return webpackEntryPoint;
}
return fn(trimmedProp);
});
}, Promise.resolve());
}
return forEachPromise(entryIdentifiers, (entryProp: string): Promise<{} | void> =>
self.prompt([
InputValidate(
`${entryProp}`,
`Which will be the entry point of "${entryProp}"?`,
validate,
`src/${entryProp}`,
),
]),
).then((entryPropAnswer: object): object => {
Object.keys(entryPropAnswer).forEach((val: string): void => {
if (
entryPropAnswer[val].charAt(0) !== "(" &&
entryPropAnswer[val].charAt(0) !== "[" &&
!entryPropAnswer[val].includes("function") &&
!entryPropAnswer[val].includes("path") &&
!entryPropAnswer[val].includes("process")
) {
entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`;
}
);
);
}
);
} else {
Expand All @@ -128,15 +109,16 @@ export default function entry(self: CustomGenerator, multiEntries: boolean): Pro
"singularEntry",
"Which will be your application entry point?",
"src/index",
),
)
])
.then((singularEntryAnswer: {
singularEntry: string,
}): string => {
let { singularEntry } = singularEntryAnswer;
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
if (singularEntry.length <= 0) {
self.usingDefaults = true;
.then(
(singularEntryAnswer: { singularEntry: string }): string => {
let { singularEntry } = singularEntryAnswer;
singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`;
if (singularEntry.length <= 0) {
self.usingDefaults = true;
}
return singularEntry;
}
);
}
Expand Down
35 changes: 16 additions & 19 deletions packages/generators/utils/language.ts
Expand Up @@ -16,6 +16,20 @@ interface ModuleRule extends Object {

type Preset = string | object;

function updateEntryExt(self, newExt: string): void {
const jsEntryOption = self.configuration.config.webpackOptions.entry;
const jsExtension = new RegExp("\.js(?!.*\.js)");
let tsEntryOption = {};
if (typeof jsEntryOption === "string") {
tsEntryOption = jsEntryOption.replace(jsExtension, newExt);
} else if (typeof jsEntryOption === "object") {
Object.keys(jsEntryOption).forEach((entry: string): void => {
tsEntryOption[entry] = jsEntryOption[entry].replace(jsExtension, newExt);
});
}
self.configuration.config.webpackOptions.entry = tsEntryOption;
}

/**
*
* Returns an module.rule object that has the babel loader if invoked
Expand All @@ -24,8 +38,6 @@ type Preset = string | object;
*/
export function getBabelLoader(): ModuleRule {
return {
// TODO migrate tslint
// tslint:disable: object-literal-sort-keys
test: "/\.js$/",
include: ["path.resolve(__dirname, 'src')"],
loader: "'babel-loader'",
Expand All @@ -40,23 +52,19 @@ export function getBabelLoader(): ModuleRule {
]
]
},
// tslint:enable: object-literal-sort-keys
};
}

export function getTypescriptLoader(): ModuleRule {
return {
// TODO migrate tslint
// tslint:disable: object-literal-sort-keys
test: "/\.tsx?$/",
loader: "'ts-loader'",
include: ["path.resolve(__dirname, 'src')"],
exclude: ["/node_modules/"],
// tslint:enable: object-literal-sort-keys
};
}

export default function language(self, langType) {
export default function language(self, langType: string): void {
switch (langType) {
case LangType.ES6:
self.dependencies.push(
Expand All @@ -81,18 +89,7 @@ export default function language(self, langType) {
extensions: [ "'.tsx'", "'.ts'", "'.js'" ],
};

// Update the entry files extensions to .ts
const jsEntryOption = self.configuration.config.webpackOptions.entry;
const jsExtension = new RegExp("\.js(?!.*\.js)");
let tsEntryOption = {};
if (typeof jsEntryOption === "string") {
tsEntryOption = jsEntryOption.replace(jsExtension, ".ts");
} else if (typeof jsEntryOption === "object") {
Object.keys(jsEntryOption).forEach((entry) => {
tsEntryOption[entry] = jsEntryOption[entry].replace(jsExtension, ".ts");
});
}
self.configuration.config.webpackOptions.entry = tsEntryOption;
updateEntryExt(self, ".ts");
break;
}
}

0 comments on commit 488b06c

Please sign in to comment.