Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit fded3cf

Browse files
authored
Merge pull request #6 from BenBeattieHood/cross-platform-compat
feat(lib): Cross-platform compatability
2 parents 0678d6b + 8151d29 commit fded3cf

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@babel/preset-env": "7.10.4",
4949
"babel-loader": "8.1.0",
5050
"babel-plugin-source-map-support": "2.1.2",
51+
"cross-spawn": "7.0.3",
5152
"find-up": "5.0.0",
5253
"noop2": "2.0.0",
5354
"source-map-support": "0.5.19",
@@ -58,6 +59,7 @@
5859
"devDependencies": {
5960
"@aws-cdk/aws-lambda": "1.54.0",
6061
"@aws-cdk/core": "1.54.0",
62+
"@types/cross-spawn": "6.0.2",
6163
"@typescript-eslint/eslint-plugin": "^3.7.1",
6264
"@typescript-eslint/parser": "^3.7.1",
6365
"eslint-plugin-import": "2.22.0",

src/NodejsFunction.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as os from "os";
44
import * as process from "process";
5-
import { spawnSync } from "child_process";
5+
import * as spawn from "cross-spawn";
66
import findUp from "find-up";
77

88
import * 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();

yarn.lock

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,13 @@
14701470
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
14711471
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
14721472

1473+
1474+
version "6.0.2"
1475+
resolved "https://registry.yarnpkg.com/@types/cross-spawn/-/cross-spawn-6.0.2.tgz#168309de311cd30a2b8ae720de6475c2fbf33ac7"
1476+
integrity sha512-KuwNhp3eza+Rhu8IFI5HUXRP0LIhqH5cAjubUvGXXthh4YYBuP2ntwEX+Cz8GJoZUHlKo247wPWOfA9LYEq4cw==
1477+
dependencies:
1478+
"@types/node" "*"
1479+
14731480
"@types/eslint-visitor-keys@^1.0.0":
14741481
version "1.0.0"
14751482
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
@@ -3869,6 +3876,15 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
38693876
safe-buffer "^5.0.1"
38703877
sha.js "^2.4.8"
38713878

3879+
[email protected], cross-spawn@^7.0.0:
3880+
version "7.0.3"
3881+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
3882+
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
3883+
dependencies:
3884+
path-key "^3.1.0"
3885+
shebang-command "^2.0.0"
3886+
which "^2.0.1"
3887+
38723888
cross-spawn@^5.0.1:
38733889
version "5.1.0"
38743890
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
@@ -3889,15 +3905,6 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
38893905
shebang-command "^1.2.0"
38903906
which "^1.2.9"
38913907

3892-
cross-spawn@^7.0.0:
3893-
version "7.0.3"
3894-
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
3895-
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
3896-
dependencies:
3897-
path-key "^3.1.0"
3898-
shebang-command "^2.0.0"
3899-
which "^2.0.1"
3900-
39013908
crypto-browserify@^3.11.0:
39023909
version "3.12.0"
39033910
resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"

0 commit comments

Comments
 (0)