@@ -2,7 +2,7 @@ import * as fs from "fs";
22import * as path from "path" ;
33import * as os from "os" ;
44import * as process from "process" ;
5- import { spawnSync } from "child_process " ;
5+ import * as spawn from "cross-spawn " ;
66import findUp from "find-up" ;
77
88import * as lambda from "@aws-cdk/aws-lambda" ;
@@ -123,15 +123,20 @@ export class NodejsFunction extends lambda.Function {
123123 } ;
124124 } , { } ) ;
125125
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+
126131 const webpackConfiguration = `
127132 const { builtinModules } = require("module");
128133 const { NormalModuleReplacementPlugin } = require("${
129- pluginsPaths [ "webpack" ]
134+ escapePathForNodeJs ( pluginsPaths [ "webpack" ] )
130135 } ");
131136
132137 module.exports = {
133138 mode: "none",
134- entry: "${ entryFullPath } ",
139+ entry: "${ escapePathForNodeJs ( entryFullPath ) } ",
135140 target: "node",
136141 resolve: {
137142 modules: ["node_modules", "."],
@@ -144,12 +149,12 @@ export class NodejsFunction extends lambda.Function {
144149 test: /\\.js$/,
145150 exclude: /node_modules/,
146151 use: {
147- loader: "${ pluginsPaths [ "babel-loader" ] } ",
152+ loader: "${ escapePathForNodeJs ( pluginsPaths [ "babel-loader" ] ) } ",
148153 options: {
149154 cacheDirectory: true,
150155 presets: [
151156 [
152- "${ pluginsPaths [ "@babel/preset-env" ] } ",
157+ "${ escapePathForNodeJs ( pluginsPaths [ "@babel/preset-env" ] ) } ",
153158 {
154159 "targets": {
155160 "node": "${
@@ -162,8 +167,8 @@ export class NodejsFunction extends lambda.Function {
162167 ]
163168 ],
164169 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" ] ) } "
167172 ]
168173 }
169174 }
@@ -178,15 +183,15 @@ export class NodejsFunction extends lambda.Function {
178183 externals: [...builtinModules, "aws-sdk"],
179184 output: {
180185 filename: "[name].js",
181- path: "${ outputDir } ",
186+ path: "${ escapePathForNodeJs ( outputDir ) } ",
182187 libraryTarget: "commonjs2",
183188 },
184189 ${ ( props . modulesToIgnore &&
185190 `
186191 plugins: [
187192 new NormalModuleReplacementPlugin(
188- /${ props . modulesToIgnore . join ( "|" ) } /,
189- "${ pluginsPaths [ "noop2" ] } ",
193+ /${ escapePathForNodeJs ( props . modulesToIgnore . join ( "|" ) ) } /,
194+ "${ escapePathForNodeJs ( pluginsPaths [ "noop2" ] ) } ",
190195 ),
191196 ]
192197 ` ) ||
@@ -196,12 +201,12 @@ export class NodejsFunction extends lambda.Function {
196201 fs . writeFileSync ( webpackConfigPath , webpackConfiguration ) ;
197202
198203 // 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 ] , {
200205 cwd : process . cwd ( ) ,
201206 } ) ;
202207
203208 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 } ` ) ;
205210 console . error (
206211 webpack ?. output ?. map ( out => {
207212 return out ?. toString ( ) ;
0 commit comments