forked from netlify/vue-cli-plugin-netlify-lambda
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (36 loc) · 1.23 KB
/
index.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
const { fork } = require('child_process')
const path = require('path')
const lambdaBuild = require('netlify-lambda/lib/build')
module.exports = (api, projectOptions) => {
const {build, serve} = api.service.commands;
const buildFn = build.fn;
const serveFn = serve.fn;
const webpackConfig = projectOptions.pluginOptions && projectOptions.pluginOptions.netlify && projectOptions.pluginOptions.netlify.webpackConfig;
build.fn = async (...args) => {
try {
const [res, stats] = await Promise.all([
buildFn(...args),
lambdaBuild.run('src/lambda', webpackConfig),
]);
console.log(stats.toString({ color: true }));
return res;
} catch (err) {
console.error(err);
process.exit(1);
}
}
serve.fn = (...args) => {
const devServer = api.service.projectOptions.devServer = api.service.projectOptions.devServer || {}
if (!devServer.proxy) {
devServer.proxy = {}
}
devServer.proxy['/.netlify/functions'] = {
"target": "http://localhost:9000",
"pathRewrite": {
"^/\\.netlify/functions": ""
}
}
fork(require.resolve('netlify-lambda'), ['serve', 'src/lambda', ...(webpackConfig ? ['-c', webpackConfig] : [])]);
return serveFn(...args)
}
}