-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
84 lines (71 loc) · 2.39 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { CheckerPlugin } = require("awesome-typescript-loader");
const path = require("path");
module.exports = (nextConfig = {}) => {
if (!nextConfig.pageExtensions) {
nextConfig.pageExtensions = ["jsx", "js"];
}
if (nextConfig.pageExtensions.indexOf("ts") === -1) {
nextConfig.pageExtensions.unshift("ts");
}
if (nextConfig.pageExtensions.indexOf("tsx") === -1) {
nextConfig.pageExtensions.unshift("tsx");
}
return Object.assign({}, nextConfig, {
webpack(config, options) {
if (!options.defaultLoaders) {
throw new Error(
"This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade"
);
}
const { dir, defaultLoaders, dev, isServer } = options;
const { useCheckerPlugin, loaderOptions } =
nextConfig.awesomeTypescriptOptions || {};
// cacheDirectory option is unavailable in case of useBabel option
// use useCache option of awesome-typescript-loader instead
const fixBabelConfig = omit(defaultLoaders.babel.options, [
"cacheDirectory",
]);
config.resolve.extensions.push(".ts", ".tsx");
if (dev && !isServer) {
config.module.rules.push({
test: /\.tsx?$/,
loader: "hot-self-accept-loader",
include: [path.join(dir, "pages")],
options: {
extensions: /\.tsx?$/,
},
});
}
config.module.rules.push({
test: /\.tsx?$/,
include: [dir],
exclude: /node_modules/,
use: [
{
loader: "awesome-typescript-loader",
options: Object.assign(
{
transpileOnly: true,
useBabel: true,
useCache: true,
forceIsolatedModules: true,
cacheDirectory: "node_modules/.cache/awesome-typescript-loader",
babelOptions: fixBabelConfig,
},
loaderOptions
),
},
],
});
if (useCheckerPlugin) config.plugins.push(new CheckerPlugin());
if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options);
}
return config;
},
});
};
const omit = (obj, keysToOmit) =>
Object.keys(obj)
.filter(key => keysToOmit.indexOf(key) < 0)
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {});