Skip to content

Commit

Permalink
feature: Adding Typescript block (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartorn authored Jul 24, 2018
1 parent 5b5c7c2 commit b5d18c9
Show file tree
Hide file tree
Showing 5 changed files with 2,693 additions and 1,348 deletions.
2 changes: 2 additions & 0 deletions lib/blocks/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const handleAssets = require("./assets");
const handleJs = require("./babel");
const handleTs = require("./typescript");
const handleCss = require("./css");
const handleScss = require("./scss");
const configEntries = require("./entry");
Expand All @@ -14,6 +15,7 @@ const utilities = require("./utility");
module.exports = {
handleAssets,
handleJs,
handleTs,
handleCss,
handleScss,
configEntries,
Expand Down
71 changes: 71 additions & 0 deletions lib/blocks/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const { cpus } = require("os");

const { ensureConfig, safeMerge, deduplicate } = require("../utils");

module.exports = blockConfig => (processEnv, argv) => argConfig => {
const isDev = processEnv.NODE_ENV === "development";
const isHotReload = processEnv.HOT_RELOAD === "true";

const defaultConf = {
useCache: isDev,
mode: processEnv.NODE_ENV,
hot: isHotReload,
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
extensions: [".ts", ".tsx"]
};

const mergedConf = safeMerge(defaultConf, blockConfig);
const config = ensureConfig(argConfig);

config.module.rules.push({
test: mergedConf.test,
exclude: mergedConf.exclude,
use: [
{
loader: "thread-loader",
options: {
// Let's leave 1 cpu free, for async type checking
workers: Math.max(cpus().length - (mergedConf.hot ? 1 : 0), 1)
}
},
mergedConf.useCache && {
loader: "cache-loader"
},
{
loader: "babel-loader",
options: {
cacheDirectory: false,
presets: [
[
"@thc/babel-preset-react",
{
mode: mergedConf.mode,
hot: mergedConf.hot
}
]
],
babelrc: false
}
},
{
loader: "ts-loader",
options: {
transpileOnly: true // Leave type checking to plugin
}
}
].filter(Boolean)
});

config.plugins.push(
new ForkTsCheckerWebpackPlugin({
tslint: true,
async: mergedConf.hot
})
);

config.resolve.extensions = deduplicate(config.resolve.extensions, mergedConf.extensions);

return config;
};
Loading

0 comments on commit b5d18c9

Please sign in to comment.