Skip to content

Commit 283e089

Browse files
committedMay 28, 2019
feat(init): support ts in configuration
1 parent 093a36d commit 283e089

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed
 

‎packages/generators/init-generator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class InitGenerator extends Generator {
5555
config: {
5656
configName: this.isProd ? "prod" : "config",
5757
topScope: [],
58+
// TODO migrate tslint
5859
// tslint:disable: object-literal-sort-keys
5960
webpackOptions: {
6061
mode: this.isProd ? "'production'" : "'development'",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
compilerOptions: {
3+
noImplicitAny: true,
4+
module: "es6",
5+
target: "es5",
6+
jsx: "react",
7+
allowJs: true
8+
}
9+
};

‎packages/generators/utils/language.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ export enum LangType {
44
}
55

66
interface ModuleRule extends Object {
7-
include: string[];
7+
include?: string[];
8+
exclude?: string[];
89
loader: string;
9-
options: {
10+
options?: {
1011
plugins: string[];
1112
presets: Preset[][];
1213
};
@@ -21,8 +22,11 @@ type Preset = string | object;
2122
*
2223
* @returns {Function} A callable function that adds the babel-loader with env preset
2324
*/
24-
export function getBabelPlugin(): ModuleRule {
25+
export function getBabelLoader(): ModuleRule {
2526
return {
27+
// TODO migrate tslint
28+
// tslint:disable: object-literal-sort-keys
29+
test: "/\.js$/",
2630
include: ["path.resolve(__dirname, 'src')"],
2731
loader: "'babel-loader'",
2832
options: {
@@ -36,23 +40,60 @@ export function getBabelPlugin(): ModuleRule {
3640
]
3741
]
3842
},
39-
test: `${new RegExp(/\.js$/)}`
43+
// tslint:enable: object-literal-sort-keys
44+
};
45+
}
46+
47+
export function getTypescriptLoader(): ModuleRule {
48+
return {
49+
// TODO migrate tslint
50+
// tslint:disable: object-literal-sort-keys
51+
test: "/\.tsx?$/",
52+
loader: "'ts-loader'",
53+
include: ["path.resolve(__dirname, 'src')"],
54+
exclude: ["/node_modules/"],
55+
// tslint:enable: object-literal-sort-keys
4056
};
4157
}
4258

4359
export default function language(self, langType) {
4460
switch (langType) {
4561
case LangType.ES6:
46-
self.configuration.config.webpackOptions.module.rules.push(
47-
getBabelPlugin(),
48-
);
4962
self.dependencies.push(
5063
"babel-loader",
5164
"@babel/core",
5265
"@babel/preset-env",
5366
);
67+
self.configuration.config.webpackOptions.module.rules.push(
68+
getBabelLoader(),
69+
);
5470
break;
71+
5572
case LangType.Typescript:
73+
self.dependencies.push(
74+
"typescript",
75+
"ts-loader",
76+
);
77+
self.configuration.config.webpackOptions.module.rules.push(
78+
getTypescriptLoader(),
79+
);
80+
self.configuration.config.webpackOptions.resolve = {
81+
extensions: [ "'.tsx'", "'.ts'", "'.js'" ],
82+
};
83+
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).map((entry) => {
92+
tsEntryOption[entry] = jsEntryOption[entry].replace(jsExtension, ".ts");
93+
});
94+
}
95+
self.configuration.config.webpackOptions.entry = tsEntryOption;
96+
self.log(jsEntryOption.replace(jsExtension, ".ts"), jsEntryOption);
5697
break;
5798
}
5899
}

‎packages/utils/run-prettier.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function runPrettier(outputPath: string, source: string, cb?: Fun
1919
try {
2020
prettySource = prettier.format(source, {
2121
filepath: outputPath,
22-
parser: "babel",
22+
parser: "babylon",
2323
singleQuote: true,
2424
tabWidth: 1,
2525
useTabs: true,
@@ -29,7 +29,8 @@ export default function runPrettier(outputPath: string, source: string, cb?: Fun
2929
"\n" +
3030
chalk.yellow(
3131
`WARNING: Could not apply prettier to ${outputPath}` +
32-
" due validation error, but the file has been created\n",
32+
" due validation error, but the file has been created\n" +
33+
err,
3334
),
3435
);
3536
prettySource = source;

0 commit comments

Comments
 (0)
Please sign in to comment.