Skip to content

Instantly share code, notes, and snippets.

@impankratov
Created January 21, 2018 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save impankratov/da930b50d5887f096bbf117660660445 to your computer and use it in GitHub Desktop.
Save impankratov/da930b50d5887f096bbf117660660445 to your computer and use it in GitHub Desktop.
Microservices multi-config
// Somewhere in microservice folder
// Can be used to create a bundle for this specific microservice
import { resolve } from 'path';
import * as webpack from 'webpack';
import config, { Env } from '../../webpack.config';
export default (env: Env = {}): webpack.Configuration => {
env.BUNDLE_FOLDER = resolve(__dirname, 'bin');
const localConfig: webpack.Configuration = {
// Override all entries of the root configuration
// with single entry of current microservice
entry: {
cache: './src/cache/index.ts'
}
};
return { ...config(env), ...localConfig };
};
// Main config, at the root of the project
// Used to generate bundles for each of the microservice
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import { resolve } from 'path';
import * as webpack from 'webpack';
export interface Env {
NODE_ENV?: string;
BUNDLE_FOLDER?: string;
}
export default ({
NODE_ENV = 'dev',
BUNDLE_FOLDER = resolve(__dirname, 'bin')
}: Env = {}): webpack.Configuration => {
const TS_CONFIG_FILE = `tsconfig.${NODE_ENV}.json`;
const defaultConfig: webpack.Configuration = {
entry: {
// Add microservices here
api: './src/api/index.ts',
cache: './src/cache/index.ts'
},
plugins: [
new CleanWebpackPlugin([BUNDLE_FOLDER])
],
output: {
libraryTarget: 'commonjs',
path: BUNDLE_FOLDER,
filename: '[name]/bundle.js'
}
};
return defaultConfig;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment