const path = require('path');

module.exports = {
    entry: './src/server/index.ts',
    target: 'node',
    mode: 'development',
    devtool: 'source-map',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: {
                    loader: 'ts-loader',
                    options: { configFile: 'tsconfig.server.json' },
                },
                exclude: /node_modules/,
            },
        ],
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist/server'),
    },
    // Bundle all our source, but leave npm packages as external requires.
    // FiveM resolves bare-name requires from the resource's node_modules.
    externals: [
        ({ request }, callback) => {
            if (!request || request.startsWith('.') || request.startsWith('/') || request.startsWith('@/')) {
                return callback(); // bundle relative imports
            }
            callback(null, `commonjs ${request}`); // leave npm packages external
        },
    ],
};
