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" ) ;
6
6
7
7
const testFilePattern = "\\.(test|spec)\\.?" ;
8
8
9
9
// custom babel target for each node version
10
10
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" ;
14
14
var unknown = runtimes . indexOf ( current ) === - 1 ;
15
- return unknown ? ' 8.15.0' : current . replace ( / ^ n o d e j s / , '' ) ;
15
+ return unknown ? " 8.15.0" : current . replace ( / ^ n o d e j s / , "" ) ;
16
16
}
17
17
18
18
function haveBabelrc ( functionsDir ) {
19
19
const cwd = process . cwd ( ) ;
20
20
21
21
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 ) => {
24
24
if ( foundBabelrc ) return foundBabelrc ;
25
25
26
26
const indexOf = functionsDir . indexOf ( dir ) ;
27
27
const dirToSearch = functionsDir . substr ( 0 , indexOf ) ;
28
28
29
- return fs . existsSync ( path . join ( cwd , dirToSearch , ' .babelrc' ) ) ;
29
+ return fs . existsSync ( path . join ( cwd , dirToSearch , " .babelrc" ) ) ;
30
30
} , false )
31
31
) ;
32
32
}
33
33
34
- function webpackConfig ( dir , { userWebpackConfig, useBabelrc} = { } ) {
34
+ function webpackConfig ( dir , { userWebpackConfig, useBabelrc } = { } ) {
35
35
var config = conf . load ( ) ;
36
36
var envConfig = conf . loadContext ( config ) . environment ;
37
37
var babelOpts = { cacheDirectory : true } ;
38
38
if ( ! haveBabelrc ( dir ) ) {
39
39
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
+ ]
41
44
] ;
42
45
43
46
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" )
47
50
] ;
48
51
}
49
52
50
53
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 ) ) ;
53
56
54
57
if ( dirPath === functionsPath ) {
55
58
throw new Error (
@@ -66,20 +69,22 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
66
69
// Include environment variables from config if available
67
70
var defineEnv = { } ;
68
71
Object . keys ( envConfig ) . forEach ( key => {
69
- defineEnv [ ' process.env.' + key ] = JSON . stringify ( envConfig [ key ] ) ;
72
+ defineEnv [ " process.env." + key ] = JSON . stringify ( envConfig [ key ] ) ;
70
73
} ) ;
71
74
72
75
// 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" ;
74
77
75
78
// 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" ;
77
82
78
83
var webpackConfig = {
79
84
mode : webpackMode ,
80
85
resolve : {
81
- extensions : [ ' .wasm' , ' .mjs' , ' .js' , ' .json' , ' .ts' ] ,
82
- mainFields : [ ' module' , ' main' ]
86
+ extensions : [ " .wasm" , " .mjs" , " .js" , " .json" , " .ts" ] ,
87
+ mainFields : [ " module" , " main" ]
83
88
} ,
84
89
module : {
85
90
rules : [
@@ -89,23 +94,23 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
89
94
`(node_modules|bower_components|${ testFilePattern } )`
90
95
) ,
91
96
use : {
92
- loader : require . resolve ( ' babel-loader' ) ,
93
- options : { ...babelOpts , babelrc : useBabelrc }
97
+ loader : require . resolve ( " babel-loader" ) ,
98
+ options : { ...babelOpts , babelrc : useBabelrc }
94
99
}
95
100
}
96
101
]
97
102
} ,
98
103
context : dirPath ,
99
104
entry : { } ,
100
- target : ' node' ,
105
+ target : " node" ,
101
106
plugins : [
102
107
new webpack . IgnorePlugin ( / v e r t x / ) ,
103
108
new webpack . DefinePlugin ( defineEnv )
104
109
] ,
105
110
output : {
106
111
path : functionsPath ,
107
- filename : ' [name].js' ,
108
- libraryTarget : ' commonjs'
112
+ filename : " [name].js" ,
113
+ libraryTarget : " commonjs"
109
114
} ,
110
115
optimization : {
111
116
nodeEnv
@@ -133,7 +138,10 @@ function webpackConfig(dir, {userWebpackConfig, useBabelrc} = {}) {
133
138
) ;
134
139
}
135
140
if ( userWebpackConfig ) {
136
- var webpackAdditional = require ( path . join ( process . cwd ( ) , userWebpackConfig ) ) ;
141
+ var webpackAdditional = require ( path . join (
142
+ process . cwd ( ) ,
143
+ userWebpackConfig
144
+ ) ) ;
137
145
138
146
return merge . smart ( webpackConfig , webpackAdditional ) ;
139
147
}
0 commit comments