diff --git a/lib/build.js b/lib/build.js index eea3278b..ac590436 100644 --- a/lib/build.js +++ b/lib/build.js @@ -1,55 +1,58 @@ -var fs = require('fs'); -var path = require('path'); -var conf = require('./config'); -var webpack = require('webpack'); -var merge = require('webpack-merge'); +var fs = require("fs"); +var path = require("path"); +var conf = require("./config"); +var webpack = require("webpack"); +var merge = require("webpack-merge"); const testFilePattern = "\\.(test|spec)\\.?"; // custom babel target for each node version function getBabelTarget(envConfig) { - var key = 'AWS_LAMBDA_JS_RUNTIME'; - var runtimes = ['nodejs8.15.0', 'nodejs6.10.3']; - var current = envConfig[key] || process.env[key] || 'nodejs8.15.0'; + var key = "AWS_LAMBDA_JS_RUNTIME"; + var runtimes = ["nodejs8.15.0", "nodejs6.10.3"]; + var current = envConfig[key] || process.env[key] || "nodejs8.15.0"; var unknown = runtimes.indexOf(current) === -1; - return unknown ? '8.15.0' : current.replace(/^nodejs/, ''); + return unknown ? "8.15.0" : current.replace(/^nodejs/, ""); } function haveBabelrc(functionsDir) { const cwd = process.cwd(); return ( - fs.existsSync(path.join(cwd, '.babelrc')) || - functionsDir.split('/').reduce((foundBabelrc, dir) => { + fs.existsSync(path.join(cwd, ".babelrc")) || + functionsDir.split("/").reduce((foundBabelrc, dir) => { if (foundBabelrc) return foundBabelrc; const indexOf = functionsDir.indexOf(dir); const dirToSearch = functionsDir.substr(0, indexOf); - return fs.existsSync(path.join(cwd, dirToSearch, '.babelrc')); + return fs.existsSync(path.join(cwd, dirToSearch, ".babelrc")); }, false) ); } -function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) { +function webpackConfig(dir, { userWebpackConfig, useBabelrc } = {}) { var config = conf.load(); var envConfig = conf.loadContext(config).environment; var babelOpts = { cacheDirectory: true }; if (!haveBabelrc(dir)) { babelOpts.presets = [ - [require.resolve('@babel/preset-env'), { targets: { node: getBabelTarget(envConfig) } }] + [ + require.resolve("@babel/preset-env"), + { targets: { node: getBabelTarget(envConfig) } } + ] ]; babelOpts.plugins = [ - require.resolve('@babel/plugin-proposal-class-properties'), - require.resolve('@babel/plugin-transform-object-assign'), - require.resolve('@babel/plugin-proposal-object-rest-spread') + require.resolve("@babel/plugin-proposal-class-properties"), + require.resolve("@babel/plugin-transform-object-assign"), + require.resolve("@babel/plugin-proposal-object-rest-spread") ]; } var functionsDir = config.build.functions || config.build.Functions; - var functionsPath = path.join(process.cwd(), functionsDir); - var dirPath = path.join(process.cwd(), dir); + var functionsPath = path.resolve(path.join(process.cwd(), functionsDir)); + var dirPath = path.resolve(path.join(process.cwd(), dir)); if (dirPath === functionsPath) { throw new Error( @@ -66,20 +69,22 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) { // Include environment variables from config if available var defineEnv = {}; Object.keys(envConfig).forEach(key => { - defineEnv['process.env.' + key] = JSON.stringify(envConfig[key]); + defineEnv["process.env." + key] = JSON.stringify(envConfig[key]); }); // Keep the same NODE_ENV if it was specified - var nodeEnv = process.env.NODE_ENV || 'production' + var nodeEnv = process.env.NODE_ENV || "production"; // Set webpack mode based on the nodeEnv - var webpackMode = ['production', 'development'].includes(nodeEnv) ? nodeEnv : 'none' + var webpackMode = ["production", "development"].includes(nodeEnv) + ? nodeEnv + : "none"; var webpackConfig = { mode: webpackMode, resolve: { - extensions: ['.wasm', '.mjs', '.js', '.json', '.ts'], - mainFields: ['module', 'main'] + extensions: [".wasm", ".mjs", ".js", ".json", ".ts"], + mainFields: ["module", "main"] }, module: { rules: [ @@ -89,23 +94,23 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) { `(node_modules|bower_components|${testFilePattern})` ), use: { - loader: require.resolve('babel-loader'), - options: {...babelOpts, babelrc: useBabelrc} + loader: require.resolve("babel-loader"), + options: { ...babelOpts, babelrc: useBabelrc } } } ] }, context: dirPath, entry: {}, - target: 'node', + target: "node", plugins: [ new webpack.IgnorePlugin(/vertx/), new webpack.DefinePlugin(defineEnv) ], output: { path: functionsPath, - filename: '[name].js', - libraryTarget: 'commonjs' + filename: "[name].js", + libraryTarget: "commonjs" }, optimization: { nodeEnv @@ -133,7 +138,10 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) { ); } if (userWebpackConfig) { - var webpackAdditional = require(path.join(process.cwd(), userWebpackConfig)); + var webpackAdditional = require(path.join( + process.cwd(), + userWebpackConfig + )); return merge.smart(webpackConfig, webpackAdditional); }