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

Commit 96182f6

Browse files
committed
fix(webpack): better plugin resolution when deps are flattened
1 parent fe26d67 commit 96182f6

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

package.json

Lines changed: 1 addition & 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+
"find-up": "5.0.0",
5152
"noop2": "2.0.0",
5253
"source-map-support": "0.5.19",
5354
"ts-loader": "8.0.1",

src/NodejsFunction.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33
import * as os from "os";
44
import * as process from "process";
55
import { spawnSync } from "child_process";
6+
import findUp from "find-up";
67

78
import * as lambda from "@aws-cdk/aws-lambda";
89
import * as cdk from "@aws-cdk/core";
@@ -92,19 +93,41 @@ export class NodejsFunction extends lambda.Function {
9293
const outputDir = fs.mkdtempSync(
9394
path.join(os.tmpdir(), "aws-lambda-nodejs-webpack"),
9495
);
95-
const webpackBinPath = require.resolve("webpack-cli");
9696
const webpackConfigPath = path.join(outputDir, "webpack.config.js");
97+
98+
// The code below is mostly to handle cases where this module is used through
99+
// yarn link. I think otherwise just using require.resolve and passing just the babel plugin
100+
// names would have worked.
101+
102+
const webpackBinPath = require.resolve("webpack-cli");
103+
104+
const plugins = [
105+
"webpack",
106+
"babel-loader",
107+
"@babel/preset-env",
108+
"@babel/plugin-transform-runtime",
109+
"babel-plugin-source-map-support",
110+
"noop2",
111+
];
97112
const pluginsPath = path.join(
98113
webpackBinPath.slice(0, webpackBinPath.lastIndexOf("/node_modules")),
99114
"node_modules",
100115
);
116+
const pluginsPaths: any = plugins.reduce(function(acc, pluginName) {
117+
return {
118+
[pluginName]: findUp.sync(pluginName, {
119+
type: "directory",
120+
cwd: pluginsPath,
121+
}),
122+
...acc,
123+
};
124+
}, {});
101125

102126
const webpackConfiguration = `
103127
const { builtinModules } = require("module");
104-
const { NormalModuleReplacementPlugin } = require("${path.join(
105-
pluginsPath,
106-
"webpack",
107-
)}");
128+
const { NormalModuleReplacementPlugin } = require("${
129+
pluginsPaths["webpack"]
130+
}");
108131
109132
module.exports = {
110133
mode: "none",
@@ -120,12 +143,12 @@ export class NodejsFunction extends lambda.Function {
120143
test: /\\.js$/,
121144
exclude: /node_modules/,
122145
use: {
123-
loader: "${path.join(pluginsPath, "babel-loader")}",
146+
loader: "${pluginsPaths["babel-loader"]}",
124147
options: {
125148
cacheDirectory: true,
126149
presets: [
127150
[
128-
"${path.join(pluginsPath, "@babel/preset-env")}",
151+
"${pluginsPaths["@babel/preset-env"]}",
129152
{
130153
"targets": {
131154
"node": "${
@@ -138,10 +161,7 @@ export class NodejsFunction extends lambda.Function {
138161
]
139162
],
140163
plugins: [
141-
"${path.join(
142-
pluginsPath,
143-
"@babel/plugin-transform-runtime",
144-
)}",
164+
"${pluginsPaths["@babel/plugin-transform-runtime"]}",
145165
"${path.join(pluginsPath, "babel-plugin-source-map-support")}"
146166
]
147167
}
@@ -165,7 +185,7 @@ export class NodejsFunction extends lambda.Function {
165185
plugins: [
166186
new NormalModuleReplacementPlugin(
167187
/${props.modulesToIgnore.join("|")}/,
168-
"${path.join(pluginsPath, "noop2")}",
188+
"${pluginsPaths["noop2"]}",
169189
),
170190
]
171191
`) ||

yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,6 +4931,14 @@ find-npm-prefix@^1.0.2:
49314931
resolved "https://registry.yarnpkg.com/find-npm-prefix/-/find-npm-prefix-1.0.2.tgz#8d8ce2c78b3b4b9e66c8acc6a37c231eb841cfdf"
49324932
integrity sha512-KEftzJ+H90x6pcKtdXZEPsQse8/y/UnvzRKrOSQFprnrGaFuJ62fVkP34Iu2IYuMvyauCyoLTNkJZgrrGA2wkA==
49334933

4934+
find-up@5.0.0:
4935+
version "5.0.0"
4936+
resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc"
4937+
integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==
4938+
dependencies:
4939+
locate-path "^6.0.0"
4940+
path-exists "^4.0.0"
4941+
49344942
find-up@^2.0.0, find-up@^2.1.0:
49354943
version "2.1.0"
49364944
resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7"
@@ -7067,6 +7075,13 @@ locate-path@^5.0.0:
70677075
dependencies:
70687076
p-locate "^4.1.0"
70697077

7078+
locate-path@^6.0.0:
7079+
version "6.0.0"
7080+
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286"
7081+
integrity sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==
7082+
dependencies:
7083+
p-locate "^5.0.0"
7084+
70707085
lock-verify@^2.0.2, lock-verify@^2.1.0:
70717086
version "2.2.0"
70727087
resolved "https://registry.yarnpkg.com/lock-verify/-/lock-verify-2.2.0.tgz#12432feb68bb647071c78c44bde16029a0f7d935"
@@ -8289,6 +8304,13 @@ p-limit@^2.0.0, p-limit@^2.2.0:
82898304
dependencies:
82908305
p-try "^2.0.0"
82918306

8307+
p-limit@^3.0.2:
8308+
version "3.0.2"
8309+
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.0.2.tgz#1664e010af3cadc681baafd3e2a437be7b0fb5fe"
8310+
integrity sha512-iwqZSOoWIW+Ew4kAGUlN16J4M7OB3ysMLSZtnhmqx7njIHFPlxWBX8xo3lVTyFVq6mI/lL9qt2IsN1sHwaxJkg==
8311+
dependencies:
8312+
p-try "^2.0.0"
8313+
82928314
p-locate@^2.0.0:
82938315
version "2.0.0"
82948316
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
@@ -8310,6 +8332,13 @@ p-locate@^4.1.0:
83108332
dependencies:
83118333
p-limit "^2.2.0"
83128334

8335+
p-locate@^5.0.0:
8336+
version "5.0.0"
8337+
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834"
8338+
integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==
8339+
dependencies:
8340+
p-limit "^3.0.2"
8341+
83138342
p-map@^2.0.0:
83148343
version "2.1.0"
83158344
resolved "https://registry.yarnpkg.com/p-map/-/p-map-2.1.0.tgz#310928feef9c9ecc65b68b17693018a665cea175"

0 commit comments

Comments
 (0)