This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
webpack.config.js
68 lines (65 loc) · 1.8 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const path = require('path');
const { readFileSync } = require('fs');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { yamlParse } = require('yaml-cfn');
const conf = {
prodMode: process.env.NODE_ENV === 'production',
templatePath: './template.yaml',
};
const cfn = yamlParse(readFileSync(conf.templatePath));
const entries = Object.values(cfn.Resources)
// Find nodejs functions
.filter(v => v.Type === 'AWS::Serverless::Function')
.filter(v =>
(v.Properties.Runtime && v.Properties.Runtime.startsWith('nodejs')) ||
(!v.Properties.Runtime && cfn.Globals.Function.Runtime)
)
.map(v => ({
// Isolate handler src filename
handlerFile: v.Properties.Handler.split('.')[0],
// Build handler dst path
CodeUriDir: v.Properties.CodeUri.split("/")
.filter((x: string) => x !== ".")
.slice(1, -1)
.join("/")
}))
.reduce(
(entries, v) =>
Object.assign(
entries,
// Generate {outputPath: inputPath} object
{[`${v.CodeUriDir}/${v.handlerFile}`]: `./src/${v.handlerFile}.ts`}
),
{}
);
console.log(`Building for ${conf.prodMode ? 'production' : 'development'}...`)
module.exports = {
// http://codys.club/blog/2015/07/04/webpack-create-multiple-bundles-with-entry-points/#sec-3
entry: entries,
target: 'node',
mode: conf.prodMode ? 'production' : 'development',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
},
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: "[name].js",
libraryTarget: 'commonjs2',
},
devtool: 'source-map',
plugins: conf.prodMode ? [
new UglifyJsPlugin({
parallel: true,
extractComments: true,
sourceMap: true,
}),
] : [],
};