Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Created April 24, 2020 23:37
Show Gist options
  • Save jgcmarins/2860f547f5d785dce24ca0eadbe3abdd to your computer and use it in GitHub Desktop.
Save jgcmarins/2860f547f5d785dce24ca0eadbe3abdd to your computer and use it in GitHub Desktop.
Webpack configs for Node.js backends to run both locally and on AWS Lambda
module.exports = {
presets: [
'@babel/preset-react',
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
'@babel/preset-typescript',
],
plugins: [
'relay',
'babel-plugin-idx',
'babel-plugin-styled-components',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-transform-async-to-generator',
'@babel/plugin-proposal-async-generator-functions',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
],
env: {
test: {
presets: [
'@babel/preset-react',
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
plugins: [
'relay',
'babel-plugin-idx',
'babel-plugin-styled-components',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-transform-async-to-generator',
'@babel/plugin-proposal-async-generator-functions',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
],
},
},
};
// common config shared between multiple servers
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
target: 'node',
optimization: {
minimizer: [new TerserPlugin()],
},
externals: [{ 'aws-sdk': 'aws-sdk' }],
resolve: {
extensions: ['.ts', '.tsx', '.js', 'jsx', '.json', '.mjs'],
},
output: {
path: path.resolve(__dirname, 'build'),
filename: '[name].js',
library: 'index',
libraryTarget: 'commonjs2',
},
plugins: [
new webpack.DefinePlugin({
'process.env.BROWSER': false,
__DEV__: process.env.NODE_ENV !== 'production',
}),
],
module: {
rules: [
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
{
test: /\.([jt]sx?)$/,
loader: 'babel-loader',
options: {
configFile: './babel.config.js',
},
exclude: /node_modules/,
},
],
},
};
// config to run local server for development
// the entry file is the index.ts since it creates a http server
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const ReloadServerPlugin = require('reload-server-webpack-plugin');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
entry: {
graphql: './src/graphql/index.ts',
},
watch: true,
stats: {
warnings: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new ReloadServerPlugin({
script: path.resolve('build', 'graphql.js'),
}),
],
});
// config to build a graphql.js to run on AWS Lambda
// the entry file is index.lambda.ts which is an event handler
// compatible for AWS Lambda
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'production',
entry: {
graphql: './src/graphql/index.lambda.ts',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment