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

Commit

Permalink
Added comment to explain escapePathForNodeJs
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBeattieHood authored Dec 9, 2020
1 parent 0161d11 commit 8151d29
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/NodejsFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,20 @@ export class NodejsFunction extends lambda.Function {
};
}, {});

const nodeifyPath = (path: string) => path.replace(/\\/g, '\\\\');
// NodeJs reserves '\' as an escape char; but pluginsPaths etc are inlined directly in the
// TemplateString below, so will contain this escape character on paths computed when running
// the Construct on a Windows machine, and so we need to escape these chars before writing them
const escapePathForNodeJs = (path: string) => path.replace(/\\/g, '\\\\');

const webpackConfiguration = `
const { builtinModules } = require("module");
const { NormalModuleReplacementPlugin } = require("${
nodeifyPath(pluginsPaths["webpack"])
escapePathForNodeJs(pluginsPaths["webpack"])
}");
module.exports = {
mode: "none",
entry: "${nodeifyPath(entryFullPath)}",
entry: "${escapePathForNodeJs(entryFullPath)}",
target: "node",
resolve: {
modules: ["node_modules", "."],
Expand All @@ -146,12 +149,12 @@ export class NodejsFunction extends lambda.Function {
test: /\\.js$/,
exclude: /node_modules/,
use: {
loader: "${nodeifyPath(pluginsPaths["babel-loader"])}",
loader: "${escapePathForNodeJs(pluginsPaths["babel-loader"])}",
options: {
cacheDirectory: true,
presets: [
[
"${nodeifyPath(pluginsPaths["@babel/preset-env"])}",
"${escapePathForNodeJs(pluginsPaths["@babel/preset-env"])}",
{
"targets": {
"node": "${
Expand All @@ -164,8 +167,8 @@ export class NodejsFunction extends lambda.Function {
]
],
plugins: [
"${nodeifyPath(pluginsPaths["@babel/plugin-transform-runtime"])}",
"${nodeifyPath(pluginsPaths["babel-plugin-source-map-support"])}"
"${escapePathForNodeJs(pluginsPaths["@babel/plugin-transform-runtime"])}",
"${escapePathForNodeJs(pluginsPaths["babel-plugin-source-map-support"])}"
]
}
}
Expand All @@ -180,15 +183,15 @@ export class NodejsFunction extends lambda.Function {
externals: [...builtinModules, "aws-sdk"],
output: {
filename: "[name].js",
path: "${nodeifyPath(outputDir)}",
path: "${escapePathForNodeJs(outputDir)}",
libraryTarget: "commonjs2",
},
${(props.modulesToIgnore &&
`
plugins: [
new NormalModuleReplacementPlugin(
/${nodeifyPath(props.modulesToIgnore.join("|"))}/,
"${nodeifyPath(pluginsPaths["noop2"])}",
/${escapePathForNodeJs(props.modulesToIgnore.join("|"))}/,
"${escapePathForNodeJs(pluginsPaths["noop2"])}",
),
]
`) ||
Expand Down

0 comments on commit 8151d29

Please sign in to comment.