From 4c93f7361fa8e795989268e5e895a352ea8d8e77 Mon Sep 17 00:00:00 2001 From: Gligor Kotushevski Date: Tue, 28 Nov 2023 11:41:00 +1300 Subject: [PATCH 1/3] exposing nodeModulesRelativeDir --- .gitignore | 1 + index.js | 4 ++-- src/config.js | 1 + src/webpack.config.js | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index bf412f8c..9fda5080 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,4 @@ tsconfig.tsbuildinfo .serverless .webpack .cache-loader +.idea/* \ No newline at end of file diff --git a/index.js b/index.js index cbe5466a..293c5e2c 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,7 @@ function applyWebpackOptions(custom, config) { // Traverse up the tree to find the path to the nearest package.json // // Certain plugins like serverless-plugin-typescript change the cwd, so when - // searching, reset the the cwd to the service path + // searching, reset the cwd to the service path packagePath: path.relative( config.servicePath, pkgUp.sync({ cwd: config.servicePath }) @@ -57,7 +57,7 @@ function applyUserConfig(config, userConfig, servicePath, runtime) { ); } - // Concat externals if provided + // Concat rawFileExtensions if provided if (userConfig.rawFileExtensions) { userConfig.rawFileExtensions = config.options.rawFileExtensions.concat( userConfig.rawFileExtensions diff --git a/src/config.js b/src/config.js index 9e8e4d88..d0d04b2d 100644 --- a/src/config.js +++ b/src/config.js @@ -27,6 +27,7 @@ module.exports = { // Or if we want to exclude all packages in the node_modules: // externals: "all" externals: ["knex", "sharp"], + nodeModulesRelativeDir: "./", // Set default file extensions to use the raw-loader with rawFileExtensions: ["pem", "txt"], minifyOptions: {}, diff --git a/src/webpack.config.js b/src/webpack.config.js index faa4a15d..6c44b690 100644 --- a/src/webpack.config.js +++ b/src/webpack.config.js @@ -31,6 +31,7 @@ const aliases = config.options.aliases; const servicePath = config.servicePath; const nodeVersion = config.nodeVersion; const externals = config.options.externals; +const nodeModulesRelativeDir = config.options.nodeModulesRelativeDir; const copyFiles = config.options.copyFiles; const experiments = config.options.experiments; const esbuildNodeVersion = "node" + nodeVersion; @@ -432,6 +433,7 @@ module.exports = { stats: ENABLE_STATS ? "normal" : "errors-only", devtool: ENABLE_SOURCE_MAPS ? "source-map" : false, externals: computedExternals, + nodeModulesRelativeDir, mode: isLocal ? "development" : "production", performance: { // Turn off size warnings for entry points From 253cc973bbc25317daf6b0bf35ca59a622bd42a9 Mon Sep 17 00:00:00 2001 From: Gligor Kotushevski Date: Wed, 29 Nov 2023 11:23:54 +1300 Subject: [PATCH 2/3] moving nodeModulesRelativeDir to serverless-webpack config where it belongs --- index.js | 4 ++++ src/config.js | 1 - src/webpack.config.js | 2 -- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 293c5e2c..e6ba8626 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,10 @@ function applyWebpackOptions(custom, config) { includeModules: { forceExclude: config.options.forceExclude, forceInclude: config.options.forceInclude, + nodeModulesRelativeDir: + config.options.nodeModulesRelativeDir != null + ? config.options.nodeModulesRelativeDir + : "./", // Generate relative path for the package.json // For cases where the services are nested and don't have their own package.json // Traverse up the tree to find the path to the nearest package.json diff --git a/src/config.js b/src/config.js index d0d04b2d..9e8e4d88 100644 --- a/src/config.js +++ b/src/config.js @@ -27,7 +27,6 @@ module.exports = { // Or if we want to exclude all packages in the node_modules: // externals: "all" externals: ["knex", "sharp"], - nodeModulesRelativeDir: "./", // Set default file extensions to use the raw-loader with rawFileExtensions: ["pem", "txt"], minifyOptions: {}, diff --git a/src/webpack.config.js b/src/webpack.config.js index 6c44b690..faa4a15d 100644 --- a/src/webpack.config.js +++ b/src/webpack.config.js @@ -31,7 +31,6 @@ const aliases = config.options.aliases; const servicePath = config.servicePath; const nodeVersion = config.nodeVersion; const externals = config.options.externals; -const nodeModulesRelativeDir = config.options.nodeModulesRelativeDir; const copyFiles = config.options.copyFiles; const experiments = config.options.experiments; const esbuildNodeVersion = "node" + nodeVersion; @@ -433,7 +432,6 @@ module.exports = { stats: ENABLE_STATS ? "normal" : "errors-only", devtool: ENABLE_SOURCE_MAPS ? "source-map" : false, externals: computedExternals, - nodeModulesRelativeDir, mode: isLocal ? "development" : "production", performance: { // Turn off size warnings for entry points From cfb2c5394c261c02856520aa24527ef7e9f23589 Mon Sep 17 00:00:00 2001 From: Gligor Kotushevski Date: Wed, 29 Nov 2023 11:42:29 +1300 Subject: [PATCH 3/3] add brief docs about the new config supported --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b03febdd..3ff7677a 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,8 @@ custom: packagerOptions: # Run a custom script in the package process scripts: # https://github.com/serverless-heaven/serverless-webpack#custom-scripts - echo hello > test + nodeModulesRelativeDir: '../' # Useful for monorepos if you have your node_modules in the root directory + # https://github.com/serverless-heaven/serverless-webpack#node-modules--externals rawFileExtensions: # An array of file extensions to import using the Webpack raw-loader. - csv # Defaults to ['pem', 'txt'] minifyOptions: # Options for ESBuildMinifyPlugin (https://esbuild.github.io/api/#simple-options)