无法将googleapi与Webpack捆绑在一起

时间:2020-03-03 11:17:49

标签: javascript node.js webpack google-api

我想让webpack输出一个包含googleapis内联包(或至少在输出目录中最小化)的包。

我的目标是让代码访问AWS Lambda(NodeJS 10.x运行时)在服务器端部署的Google Play开发者API(通过googleapis包)。当前可以在本地运行。

这是我的Javascript:

import { google } from 'googleapis';

exports.handler = async (event) => {
  // Service Account authentication 
  let auth = new google.auth.GoogleAuth({
    credentials: {
      client_email: "abcde@api-123456.iam.gserviceaccount.com",
      private_key: "PRIVATE_KEY_HERE"
    },
    scopes: ['https://www.googleapis.com/auth/androidpublisher']
  });
  const authClient = await auth.getClient();

  // Google Play Developer API
  const androidPublisher = new androidpublisher_v3.Androidpublisher({
    auth: authClient,
  });
  console.log("Google Play Developer API created");
  console.log(androidPublisher);

  return {
    statusCode: 200,
    body: JSON.stringify({ 'message': 'hello' })
  }
}

这是我的webpack.config.js

const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: [
    './example.js',
  ],
  target: 'node',
  externals: [nodeExternals()],
  devtool: 'inline-source-map',
  output: {
    filename: 'handler.js',
  },
  plugins: []
}

最后是我的package.json

{
  "name": "test_gapi",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "googleapis": "^47.0.0",
    "webpack-node-externals": "^1.7.2"
  },
  "devDependencies": {
    "webpack": "^4.42.0"
  }
}

我尝试运行webpack mode --development,发现输出handler.js不包含googleapis软件包代码,而是包含require(googleapis)

如果我将压缩后的dist目录原样部署到AWS Lambda(约11 KB),它将无法找到该模块。但是,如果我将node_modules目录添加到zip中,则AWS Lambda可以很好地执行代码,但是zip工件大约为10 MB。

所以我想知道为什么webpack不将依赖项绑定到输出javascript文件中,并且如果可以做到,可以在生产中将其最小化。

0 个答案:

没有答案