@@ -3,6 +3,7 @@ import * as path from "path";
33import * as os from "os" ;
44import * as process from "process" ;
55import { spawnSync } from "child_process" ;
6+ import findUp from "find-up" ;
67
78import * as lambda from "@aws-cdk/aws-lambda" ;
89import * 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 ` ) ||
0 commit comments