@@ -2,7 +2,7 @@ import * as fs from "fs";
2
2
import * as path from "path" ;
3
3
import * as os from "os" ;
4
4
import * as process from "process" ;
5
- import { spawnSync } from "child_process " ;
5
+ import * as spawn from "cross-spawn " ;
6
6
import findUp from "find-up" ;
7
7
8
8
import * as lambda from "@aws-cdk/aws-lambda" ;
@@ -123,15 +123,20 @@ export class NodejsFunction extends lambda.Function {
123
123
} ;
124
124
} , { } ) ;
125
125
126
+ // NodeJs reserves '\' as an escape char; but pluginsPaths etc are inlined directly in the
127
+ // TemplateString below, so will contain this escape character on paths computed when running
128
+ // the Construct on a Windows machine, and so we need to escape these chars before writing them
129
+ const escapePathForNodeJs = ( path : string ) => path . replace ( / \\ / g, '\\\\' ) ;
130
+
126
131
const webpackConfiguration = `
127
132
const { builtinModules } = require("module");
128
133
const { NormalModuleReplacementPlugin } = require("${
129
- pluginsPaths [ "webpack" ]
134
+ escapePathForNodeJs ( pluginsPaths [ "webpack" ] )
130
135
} ");
131
136
132
137
module.exports = {
133
138
mode: "none",
134
- entry: "${ entryFullPath } ",
139
+ entry: "${ escapePathForNodeJs ( entryFullPath ) } ",
135
140
target: "node",
136
141
resolve: {
137
142
modules: ["node_modules", "."],
@@ -144,12 +149,12 @@ export class NodejsFunction extends lambda.Function {
144
149
test: /\\.js$/,
145
150
exclude: /node_modules/,
146
151
use: {
147
- loader: "${ pluginsPaths [ "babel-loader" ] } ",
152
+ loader: "${ escapePathForNodeJs ( pluginsPaths [ "babel-loader" ] ) } ",
148
153
options: {
149
154
cacheDirectory: true,
150
155
presets: [
151
156
[
152
- "${ pluginsPaths [ "@babel/preset-env" ] } ",
157
+ "${ escapePathForNodeJs ( pluginsPaths [ "@babel/preset-env" ] ) } ",
153
158
{
154
159
"targets": {
155
160
"node": "${
@@ -162,8 +167,8 @@ export class NodejsFunction extends lambda.Function {
162
167
]
163
168
],
164
169
plugins: [
165
- "${ pluginsPaths [ "@babel/plugin-transform-runtime" ] } ",
166
- "${ pluginsPaths [ "babel-plugin-source-map-support" ] } "
170
+ "${ escapePathForNodeJs ( pluginsPaths [ "@babel/plugin-transform-runtime" ] ) } ",
171
+ "${ escapePathForNodeJs ( pluginsPaths [ "babel-plugin-source-map-support" ] ) } "
167
172
]
168
173
}
169
174
}
@@ -178,15 +183,15 @@ export class NodejsFunction extends lambda.Function {
178
183
externals: [...builtinModules, "aws-sdk"],
179
184
output: {
180
185
filename: "[name].js",
181
- path: "${ outputDir } ",
186
+ path: "${ escapePathForNodeJs ( outputDir ) } ",
182
187
libraryTarget: "commonjs2",
183
188
},
184
189
${ ( props . modulesToIgnore &&
185
190
`
186
191
plugins: [
187
192
new NormalModuleReplacementPlugin(
188
- /${ props . modulesToIgnore . join ( "|" ) } /,
189
- "${ pluginsPaths [ "noop2" ] } ",
193
+ /${ escapePathForNodeJs ( props . modulesToIgnore . join ( "|" ) ) } /,
194
+ "${ escapePathForNodeJs ( pluginsPaths [ "noop2" ] ) } ",
190
195
),
191
196
]
192
197
` ) ||
@@ -196,12 +201,12 @@ export class NodejsFunction extends lambda.Function {
196
201
fs . writeFileSync ( webpackConfigPath , webpackConfiguration ) ;
197
202
198
203
// to implement cache, create a script that uses webpack API, store cache in a file with JSON.stringify, based on entry path key then reuse it
199
- const webpack = spawnSync ( webpackBinPath , [ "--config" , webpackConfigPath ] , {
204
+ const webpack = spawn . sync ( webpackBinPath , [ "--config" , webpackConfigPath ] , {
200
205
cwd : process . cwd ( ) ,
201
206
} ) ;
202
207
203
208
if ( webpack . status !== 0 ) {
204
- console . error ( " webpack had an error when bundling." ) ;
209
+ console . error ( ` webpack had an error when bundling. Return status was ${ webpack . status } ` ) ;
205
210
console . error (
206
211
webpack ?. output ?. map ( out => {
207
212
return out ?. toString ( ) ;
0 commit comments