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

Commit 0161d11

Browse files
author
Ben
committed
feat(lib): Cross-platform compatability
1 parent 0678d6b commit 0161d11

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-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: 14 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,17 @@ export class NodejsFunction extends lambda.Function {
123123
};
124124
}, {});
125125

126+
const nodeifyPath = (path: string) => path.replace(/\\/g, '\\\\');
127+
126128
const webpackConfiguration = `
127129
const { builtinModules } = require("module");
128130
const { NormalModuleReplacementPlugin } = require("${
129-
pluginsPaths["webpack"]
131+
nodeifyPath(pluginsPaths["webpack"])
130132
}");
131133
132134
module.exports = {
133135
mode: "none",
134-
entry: "${entryFullPath}",
136+
entry: "${nodeifyPath(entryFullPath)}",
135137
target: "node",
136138
resolve: {
137139
modules: ["node_modules", "."],
@@ -144,12 +146,12 @@ export class NodejsFunction extends lambda.Function {
144146
test: /\\.js$/,
145147
exclude: /node_modules/,
146148
use: {
147-
loader: "${pluginsPaths["babel-loader"]}",
149+
loader: "${nodeifyPath(pluginsPaths["babel-loader"])}",
148150
options: {
149151
cacheDirectory: true,
150152
presets: [
151153
[
152-
"${pluginsPaths["@babel/preset-env"]}",
154+
"${nodeifyPath(pluginsPaths["@babel/preset-env"])}",
153155
{
154156
"targets": {
155157
"node": "${
@@ -162,8 +164,8 @@ export class NodejsFunction extends lambda.Function {
162164
]
163165
],
164166
plugins: [
165-
"${pluginsPaths["@babel/plugin-transform-runtime"]}",
166-
"${pluginsPaths["babel-plugin-source-map-support"]}"
167+
"${nodeifyPath(pluginsPaths["@babel/plugin-transform-runtime"])}",
168+
"${nodeifyPath(pluginsPaths["babel-plugin-source-map-support"])}"
167169
]
168170
}
169171
}
@@ -178,15 +180,15 @@ export class NodejsFunction extends lambda.Function {
178180
externals: [...builtinModules, "aws-sdk"],
179181
output: {
180182
filename: "[name].js",
181-
path: "${outputDir}",
183+
path: "${nodeifyPath(outputDir)}",
182184
libraryTarget: "commonjs2",
183185
},
184186
${(props.modulesToIgnore &&
185187
`
186188
plugins: [
187189
new NormalModuleReplacementPlugin(
188-
/${props.modulesToIgnore.join("|")}/,
189-
"${pluginsPaths["noop2"]}",
190+
/${nodeifyPath(props.modulesToIgnore.join("|"))}/,
191+
"${nodeifyPath(pluginsPaths["noop2"])}",
190192
),
191193
]
192194
`) ||
@@ -196,12 +198,12 @@ export class NodejsFunction extends lambda.Function {
196198
fs.writeFileSync(webpackConfigPath, webpackConfiguration);
197199

198200
// 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], {
201+
const webpack = spawn.sync(webpackBinPath, ["--config", webpackConfigPath], {
200202
cwd: process.cwd(),
201203
});
202204

203205
if (webpack.status !== 0) {
204-
console.error("webpack had an error when bundling.");
206+
console.error(`webpack had an error when bundling. Return status was ${webpack.status}`);
205207
console.error(
206208
webpack?.output?.map(out => {
207209
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+
"@types/cross-spawn@6.0.2":
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+
cross-spawn@7.0.3, 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)