【发布时间】:2026-02-06 03:00:02
【问题描述】:
我正在阅读这篇 Medium 文章 https://medium.com/@awesome1888/how-to-use-serverless-locally-with-webpack-and-docker-5e268f71715,其中设置了具有这些依赖项的项目,
$ npm install serverless serverless-offline serverless-webpack webpack webpack-node-externals babel-loader @babel/core @babel/preset-env @babel/plugin-proposal-object-rest-spread --save-dev
这个serverless.yml 文件,
service: my-first-lambda
# enable required plugins, in order to make what we want
plugins:
- serverless-webpack
- serverless-offline
# serverless supports different cloud environments to run at.
# we will be deploying and running this project at AWS cloud with Node v8.10 environment
provider:
name: aws
runtime: nodejs8.10
region: eu-central-1
stage: dev
# here we describe our lambda function
functions:
hello: # function name
handler: src/handler.main # where the actual code is located
# to call our function from outside, we need to expose it to the outer world
# in order to do so, we create a REST endpoint
events:
- http:
path: hello # path for the endpoint
method: any # HTTP method for the endpoint
custom:
webpack:
webpackConfig: 'webpack.config.js' # name of webpack configuration file
includeModules: true # add excluded modules to the bundle
packager: 'npm' # package manager we use
还有这个webpack.config.js:
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');
module.exports = {
entry: slsw.lib.entries,
target: 'node',
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
externals: [nodeExternals()],
output: {
libraryTarget: 'commonjs',
// pay attention to this
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
// ... and this
presets: [['@babel/env', { targets: { node: '8.10' } }]],
plugins: [
'@babel/plugin-proposal-object-rest-spread',
]
},
},
],
},
],
},
};
这似乎遵循https://github.com/serverless-heaven/serverless-webpack#node-modules--externals 中记录的模式。但我不太明白的是,为什么这不等于将includeModules 保留为默认值false?从https://www.npmjs.com/package/webpack-node-externals 看来,两者都将排除node_modules 依赖项。
【问题讨论】:
标签: webpack serverless