Webpack 从无服务器上的Web包中排除节点模块
我正在读这篇媒体文章,其中一个项目是用这些依赖项建立的Webpack 从无服务器上的Web包中排除节点模块,webpack,serverless,Webpack,Serverless,我正在读这篇媒体文章,其中一个项目是用这些依赖项建立的 $ 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-
$ 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',
]
},
},
],
},
],
},
};
这似乎遵循了在中记录的模式。但我不太明白的是,为什么这不等于将
includeModules
保留为默认值false
?从这一点来看,两者都将排除节点\u模块
依赖项。includeModules:false
意味着所有依赖项都将成为捆绑包的一部分,从而生成一个JavaScript文件(没有外部依赖项)
externals:[nodeExternals()]
告诉Webpack不要绑定外部依赖项,因此生成的JavaScript文件将只包含您的代码
由于您的代码可能需要这些外部依赖项,includeModules:true
告诉无服务器网页包插件在节点模块
目录下生成的zip包中包含这些依赖项
您可以尝试在.serverless
下查看生成的zip文件,以查看模式之间的差异
注释includeModules:true#将排除的模块添加到yaml
文件中的bundle
,这有误导性
应该是includeModules:true#将排除的模块添加到生成的zip包中
主要是要区分捆绑(由Webpack完成)和打包(由插件完成)。感谢您的精彩解释!简单明了。你应该写博客,如果你还没有!