const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CleanBuildDir = require('clean-webpack-plugin');
function localExternals(context, request, callback) {
if (/\.\//.test(request)) {
return callback(null, `commonjs ${request}`);
}
return callback();
}
const entitesConfig = {
target: 'node',
mode: 'development',
node: {
__dirname: false,
__filename: false,
},
entry: () => {
const files = fs.readdirSync(path.resolve(__dirname, 'src/entities'));
const entries = {};
files.forEach(file => {
entries[file.split('.')[0]] = path.resolve(__dirname, 'src/entities', file);
});
return entries;
},
output: {
path: path.resolve(__dirname, 'dist/entities'),
filename: '[name].js',
libraryTarget: 'commonjs',
},
resolve: {
extensions: ['.ts', '.js'],
},
externals: [nodeExternals(), localExternals],
module: {
rules: [
{
test: /\.(t|j)s$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
plugins: [new CleanBuildDir()],
};
const mainConfig = {
target: 'node',
mode: 'development',
node: {
__dirname: false,
__filename: false,
},
entry: path.resolve(__dirname, 'src/main.ts'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
resolve: {
extensions: ['.ts', '.js'],
},
externals: [nodeExternals(), localExternals],
module: {
rules: [
{
test: /\.(t|j)s$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
plugins: [new CleanBuildDir()],
};
module.exports = [entitesConfig, mainConfig];