-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathindex.js
67 lines (55 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { join } = require('path');
const { readdirSync, existsSync } = require('fs');
// @see https://babeljs.io/docs/en/next/babel-preset-typescript.html#options
exports.typescript = {
allExtensions: true,
isTSX: true,
}
exports.babel = function (config) {
config.presets.push(['@babel/preset-typescript', exports.typescript]);
}
exports.webpack = function (config, env, opts) {
const { src, production } = env;
// search for new entrypoint
const files = readdirSync(src);
for (const str of files) {
if (/\.tsx?$/i.test(str)) {
config.entry.bundle[0] = join(src, str);
break;
}
}
// enable ts-driven extensions
config.resolve.extensions.unshift('.ts', '.tsx');
// update babel, prettier?, eslint? loader patterns
const target = String(/\.jsx?$/);
config.module.rules.forEach(obj => {
if (String(obj.test) === target) {
obj.test = /\.(t|j)sx?$/;
}
});
if (!production) {
config.devtool = 'inline-source-map';
}
if (opts.prettier) {
opts.prettier.parser = 'babel-ts';
}
const tsconfig = join(env.cwd, 'tsconfig.json');
const hasConfig = existsSync(tsconfig);
if (opts.eslint && hasConfig) {
let parser = opts.eslint.parserOptions || {};
parser.project = parser.project || tsconfig;
opts.eslint.parserOptions = parser;
}
if (hasConfig) {
const TypeChecker = require('fork-ts-checker-webpack-plugin');
config.plugins.push(
new TypeChecker({
eslint: !!opts.eslint,
logger: env.log || console,
vue: !!config.resolve.alias['vue$'],
reportFiles: ['src/**'],
tsconfig
})
);
}
}