Created
June 22, 2020 23:37
-
-
Save trulysinclair/ea294fb2b5bf39fad2e1edec9aad1b68 to your computer and use it in GitHub Desktop.
This is part of my article Yarn 2 and TypeScript: Adding Webpack, https://medium.com/@thegrimsilence/yarn-2-and-typescript-adding-webpack-9dd9d24001f7.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import TsCheckerPlugin from "fork-ts-checker-webpack-plugin"; | |
import { resolve } from "path"; | |
import { Configuration } from "webpack"; | |
import { cpus } from "os"; | |
const PnpPlugin = require("pnp-webpack-plugin"); | |
const isProductionBuild: boolean = process.env.NODE_ENV == "production"; | |
const config: Configuration = { | |
entry: { | |
app: resolve(__dirname, "path-to-your-file.js"), | |
}, | |
mode: isProductionBuild ? "production" : "development", | |
module: { | |
rules: [ | |
{ | |
// If you're unfamiliar with Regex, this looks for .ts and optionally .tsx (react) | |
test: /\.tsx?$/, | |
use: [ | |
{ | |
loader: "thread-loader", // Throw ts-loader into multi-threading to speed things up. | |
options: { | |
workers: cpus.length - 1, | |
poolTimeout: Infinity, | |
}, | |
}, | |
{ | |
loader: "ts-loader", | |
options: { | |
// If you're like me and keep your tsconfig bare, | |
// you'll want to keep track of its location | |
configFile: resolve(__dirname, "path-to-tsconfig.json"), | |
// Skip type-checking, just convert to JavaScript, let FTCWPlugin handle it. | |
transpileOnly: true, | |
}, | |
}, | |
], | |
}, | |
{ | |
enforce: "pre", | |
test: /\.js$/, | |
loader: "source-map-loader", | |
}, | |
], | |
}, | |
node: { | |
// Relative to context of this file, rather than live resolution | |
__dirname: true, | |
}, | |
output: { | |
filename: "[name].js", | |
path: resolve(__dirname, "dist"), | |
}, | |
plugins: [ | |
new TsCheckerPlugin({ | |
async: !isProductionBuild, // Only report after a run, freeing the process to work faster | |
typescript: { | |
build: true, // Build mode speeds up consequential builds (evertyhing after the first build, based on the prior build) | |
configFile: resolve(__dirname, "path-to-tsconfig.json"), | |
mode: "write-tsbuildinfo", | |
profile: !isProductionBuild, // Don't slow down production by profiling, only in development do we need this information. | |
}, | |
}), | |
], | |
resolve: { | |
// Tell Webpack what extensions we're looking for | |
extensions: [".tsx", ".ts", ".js"], | |
plugins: [PnpPlugin], | |
}, | |
resolveLoader: { | |
plugins: [PnpPlugin.moduleLoader(module)], | |
}, | |
target: "async-node", | |
}; | |
export default config; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment