Skip to content

Commit 2d8aa6f

Browse files
author
sw-yx
committed
wrap functionsPath and dirPath to guarantee compatibility
1 parent 6a8eec1 commit 2d8aa6f

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

Diff for: lib/build.js

+38-30
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
var conf = require('./config');
4-
var webpack = require('webpack');
5-
var merge = require('webpack-merge');
1+
var fs = require("fs");
2+
var path = require("path");
3+
var conf = require("./config");
4+
var webpack = require("webpack");
5+
var merge = require("webpack-merge");
66

77
const testFilePattern = "\\.(test|spec)\\.?";
88

99
// custom babel target for each node version
1010
function getBabelTarget(envConfig) {
11-
var key = 'AWS_LAMBDA_JS_RUNTIME';
12-
var runtimes = ['nodejs8.15.0', 'nodejs6.10.3'];
13-
var current = envConfig[key] || process.env[key] || 'nodejs8.15.0';
11+
var key = "AWS_LAMBDA_JS_RUNTIME";
12+
var runtimes = ["nodejs8.15.0", "nodejs6.10.3"];
13+
var current = envConfig[key] || process.env[key] || "nodejs8.15.0";
1414
var unknown = runtimes.indexOf(current) === -1;
15-
return unknown ? '8.15.0' : current.replace(/^nodejs/, '');
15+
return unknown ? "8.15.0" : current.replace(/^nodejs/, "");
1616
}
1717

1818
function haveBabelrc(functionsDir) {
1919
const cwd = process.cwd();
2020

2121
return (
22-
fs.existsSync(path.join(cwd, '.babelrc')) ||
23-
functionsDir.split('/').reduce((foundBabelrc, dir) => {
22+
fs.existsSync(path.join(cwd, ".babelrc")) ||
23+
functionsDir.split("/").reduce((foundBabelrc, dir) => {
2424
if (foundBabelrc) return foundBabelrc;
2525

2626
const indexOf = functionsDir.indexOf(dir);
2727
const dirToSearch = functionsDir.substr(0, indexOf);
2828

29-
return fs.existsSync(path.join(cwd, dirToSearch, '.babelrc'));
29+
return fs.existsSync(path.join(cwd, dirToSearch, ".babelrc"));
3030
}, false)
3131
);
3232
}
3333

34-
function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
34+
function webpackConfig(dir, { userWebpackConfig, useBabelrc } = {}) {
3535
var config = conf.load();
3636
var envConfig = conf.loadContext(config).environment;
3737
var babelOpts = { cacheDirectory: true };
3838
if (!haveBabelrc(dir)) {
3939
babelOpts.presets = [
40-
[require.resolve('@babel/preset-env'), { targets: { node: getBabelTarget(envConfig) } }]
40+
[
41+
require.resolve("@babel/preset-env"),
42+
{ targets: { node: getBabelTarget(envConfig) } }
43+
]
4144
];
4245

4346
babelOpts.plugins = [
44-
require.resolve('@babel/plugin-proposal-class-properties'),
45-
require.resolve('@babel/plugin-transform-object-assign'),
46-
require.resolve('@babel/plugin-proposal-object-rest-spread')
47+
require.resolve("@babel/plugin-proposal-class-properties"),
48+
require.resolve("@babel/plugin-transform-object-assign"),
49+
require.resolve("@babel/plugin-proposal-object-rest-spread")
4750
];
4851
}
4952

5053
var functionsDir = config.build.functions || config.build.Functions;
51-
var functionsPath = path.join(process.cwd(), functionsDir);
52-
var dirPath = path.join(process.cwd(), dir);
54+
var functionsPath = path.resolve(path.join(process.cwd(), functionsDir));
55+
var dirPath = path.resolve(path.join(process.cwd(), dir));
5356

5457
if (dirPath === functionsPath) {
5558
throw new Error(
@@ -66,20 +69,22 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
6669
// Include environment variables from config if available
6770
var defineEnv = {};
6871
Object.keys(envConfig).forEach(key => {
69-
defineEnv['process.env.' + key] = JSON.stringify(envConfig[key]);
72+
defineEnv["process.env." + key] = JSON.stringify(envConfig[key]);
7073
});
7174

7275
// Keep the same NODE_ENV if it was specified
73-
var nodeEnv = process.env.NODE_ENV || 'production'
76+
var nodeEnv = process.env.NODE_ENV || "production";
7477

7578
// Set webpack mode based on the nodeEnv
76-
var webpackMode = ['production', 'development'].includes(nodeEnv) ? nodeEnv : 'none'
79+
var webpackMode = ["production", "development"].includes(nodeEnv)
80+
? nodeEnv
81+
: "none";
7782

7883
var webpackConfig = {
7984
mode: webpackMode,
8085
resolve: {
81-
extensions: ['.wasm', '.mjs', '.js', '.json', '.ts'],
82-
mainFields: ['module', 'main']
86+
extensions: [".wasm", ".mjs", ".js", ".json", ".ts"],
87+
mainFields: ["module", "main"]
8388
},
8489
module: {
8590
rules: [
@@ -89,23 +94,23 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
8994
`(node_modules|bower_components|${testFilePattern})`
9095
),
9196
use: {
92-
loader: require.resolve('babel-loader'),
93-
options: {...babelOpts, babelrc: useBabelrc}
97+
loader: require.resolve("babel-loader"),
98+
options: { ...babelOpts, babelrc: useBabelrc }
9499
}
95100
}
96101
]
97102
},
98103
context: dirPath,
99104
entry: {},
100-
target: 'node',
105+
target: "node",
101106
plugins: [
102107
new webpack.IgnorePlugin(/vertx/),
103108
new webpack.DefinePlugin(defineEnv)
104109
],
105110
output: {
106111
path: functionsPath,
107-
filename: '[name].js',
108-
libraryTarget: 'commonjs'
112+
filename: "[name].js",
113+
libraryTarget: "commonjs"
109114
},
110115
optimization: {
111116
nodeEnv
@@ -133,7 +138,10 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
133138
);
134139
}
135140
if (userWebpackConfig) {
136-
var webpackAdditional = require(path.join(process.cwd(), userWebpackConfig));
141+
var webpackAdditional = require(path.join(
142+
process.cwd(),
143+
userWebpackConfig
144+
));
137145

138146
return merge.smart(webpackConfig, webpackAdditional);
139147
}

0 commit comments

Comments
 (0)