diff --git a/index.js b/index.js index 02b4390..91ea0e5 100644 --- a/index.js +++ b/index.js @@ -6,20 +6,20 @@ 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 = (...args) => { - return buildFn(...args).then((res) => { - return lambdaBuild - .run("src/lambda") - .then(function(stats) { - console.log(stats.toString({ color: true })) - return res - }) - .catch(function(err) { - console.error(err) - process.exit(1) - }) - }) + 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) => { @@ -34,7 +34,7 @@ module.exports = (api, projectOptions) => { } } - const forked = fork(path.join(__dirname, 'serve.js')) + fork(require.resolve('netlify-lambda'), ['serve', 'src/lambda', ...(webpackConfig ? ['-c', webpackConfig] : [])]); return serveFn(...args) } } \ No newline at end of file diff --git a/package.json b/package.json index 0fececa..b51a8e5 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "author": "Netlify", "license": "MIT", + "repository": "netlify/vue-cli-plugin-netlify-lambda", "dependencies": { "netlify-lambda": "1.0.0-babel-7-beta" } diff --git a/serve.js b/serve.js deleted file mode 100644 index 40908c3..0000000 --- a/serve.js +++ /dev/null @@ -1,16 +0,0 @@ -const serve = require("netlify-lambda/lib/serve"); -const build = require("netlify-lambda/lib/build"); - -const server = serve.listen(9000) -build.watch("src/lambda", null, function(err, stats) { - if (err) { - console.error(err); - return; - } - - stats.compilation.chunks.forEach(function(chunk) { - server.clearCache(chunk.name); - }); - - console.log(stats.toString({ color: true })); -}); \ No newline at end of file diff --git a/ui.js b/ui.js new file mode 100644 index 0000000..fba480c --- /dev/null +++ b/ui.js @@ -0,0 +1,40 @@ +const fs = require('fs'); +const path = require('path'); + +function getConfigData(data) { + return (data.vue && data.vue.pluginOptions && data.vue.pluginOptions.netlify) || {}; +} + +module.exports = api => api.describeConfig({ + id: 'com.netlify.netlify-lambda', + name: 'Netlify configuration', + description: 'Configure Netlify Functions', + link: 'https://github.com/netlify/vue-cli-plugin-netlify-lambda', + files: { + vue: { + js: ['vue.config.js'], + }, + }, + onRead({ data, cwd }) { + return { + prompts: [ + { + name: 'webpackConfig', + type: 'input', + default: null, + value: getConfigData(data).webpackConfig, + validate: input => fs.existsSync(path.join(cwd, input)), + message: 'Webpack config module', + description: 'Additional webpack configuration', + }, + ], + }; + }, + async onWrite({ api: writeApi, prompts }) { + const result = {}; + for (const prompt of prompts) { + result[`pluginOptions.netlify.${prompt.id}`] = await writeApi.getAnswer(prompt.id); + } + writeApi.setData('vue', result); + }, +});