liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

[Question] How to compile per file and keep structure? #65

Open AhmedBHameed opened 5 years ago

AhmedBHameed commented 5 years ago

Hello,

So i'm encounter of creating sub part as external application program that depending on other existing programs. Example for that creating sub project to update some entries in database, so normally i use the existing driver to handle database call. but webpack is bundling all in one big bundle file!!

So how to bundle all files and keep structure of the application?

const path = require("path");
const fs = require("fs");
const CopyPlugin = require("copy-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const NodemonPlugin = require("nodemon-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const ROOT_DIR = path.resolve(__dirname, "../../");
const APP_DIR = path.resolve(__dirname, "../../src");
const BUILD_DIR = path.resolve(__dirname, "../../build");

module.exports = {
  entry: `${APP_DIR}/server.ts`,
  output: {
    path: BUILD_DIR,
    filename: "server.js"
  },
  target: "node",
  externals: [nodeExternals()],
  resolve: {
    extensions: [".ts", ".js"],
    alias: {
      "@src": `${ROOT_DIR}/src/`,
      "@database": `${APP_DIR}/database/`
    }
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyPlugin([{ from: `${APP_DIR}/public`, to: `${BUILD_DIR}/public` }]),
    new NodemonPlugin()
  ],
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: ["ts-loader"],
        exclude: /node_modules/
      }
    ]
  }
};