Skip to content

Instantly share code, notes, and snippets.

@MattJeanes
Created July 24, 2017 12:30
Show Gist options
  • Save MattJeanes/1753027da4aca18a3b903fedef5d57e5 to your computer and use it in GitHub Desktop.
Save MattJeanes/1753027da4aca18a3b903fedef5d57e5 to your computer and use it in GitHub Desktop.
Webpack config for serverside node applications with hacks for TypeORM and SQLite3
import { CheckerPlugin } from "awesome-typescript-loader";
import * as path from "path";
import * as webpack from "webpack";
module.exports = (env: any) => {
const prod = env && env.prod as boolean;
console.log(prod ? "Production" : "Dev" + " main build");
const analyse = env && env.analyse as boolean;
if (analyse) { console.log("Analysing build"); }
const outputDir = "./";
const bundleConfig: webpack.Configuration = {
target: "node",
entry: { main: "./app/main.ts" },
stats: { modules: false },
context: __dirname,
resolve: { extensions: [".ts", ".js", ".json"] },
devtool: prod ? "source-map" : "eval-source-map",
output: {
filename: "[name].js",
publicPath: "/dist/",
path: path.join(__dirname, outputDir),
},
module: {
rules: [
{
test: /typeorm(\\|\/).+?\.js$/,
use: {
loader: "string-replace-loader",
options: {
multiple: [
{ search: "PlatformTools_1.PlatformTools.load(\"mssql\")", replace: "require(\"mssql/tedious\")" },
{ search: "PlatformTools_1.PlatformTools.load", replace: "require" },
],
},
},
},
{
test: /sqlite3(\\|\/)lib(\\|\/)sqlite3\.js$/,
use: {
loader: "string-replace-loader",
options: {
multiple: [
{ search: "var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));", replace: "" },
{ search: "require(binding_path);", replace: "require('./binding/node-v57-win32-x64/node_sqlite3.node')" },
],
},
},
},
{ test: /rc(\\|\/).+?\.js$/, use: "shebang-loader" },
{ test: /\.ts$/, use: ["awesome-typescript-loader?silent=true"] },
{ test: /\.node$/, use: "node-native-loader" },
],
unknownContextRegExp: /^$/,
unknownContextCritical: false,
},
plugins: [
new CheckerPlugin(),
new webpack.IgnorePlugin(/^cls-bluebird$/),
new webpack.IgnorePlugin(/^mongodb$/),
new webpack.IgnorePlugin(/^aws-sdk$/),
new webpack.IgnorePlugin(/sharp(\\|\/)lib(\\|\/)icc/),
//new webpack.BannerPlugin({ banner: "require(\"source-map-support\").install();", raw: true, entryOnly: false }),
],
node: {
__dirname: false,
},
};
return bundleConfig;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment