AWS Lambda的Webpack配置?

9

我正在使用AWS Lambda,需要将一些现代JavaScript转译为Node 6.10。

以下是我的代码:

export const handler = function(event, context, callback) {
  console.log('Hello, world');
  callback(null, 'OK');
};

这是我希望转化的内容(大致如下):
exports.handler = function(event, context, callback) {
  console.log('Hello, world');
  callback(null, 'OK');
};

这是我目前生成的内容:

module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


Object.defineProperty(exports, "__esModule", {
  value: true
});
const handler = exports.handler = function (event, context, callback) {
  console.log('Hello, world');
  callback(null, 'OK');
};

/***/ })
/******/ ]);

这是我的Webpack配置:

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

const debug = process.env.NODE_ENV !== 'production';

module.exports = {
  context: __dirname,
  entry: './index.js',
  output: {
    path: __dirname + '/out',
    filename: 'index.js',
    libraryTarget: 'commonjs2'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            babelrc: true
          }
        }
      }
    ],
  },
  target: 'node',
  externals: [ nodeExternals() ],
  plugins: [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({ 
      mangle: !debug, 
      sourcemap: debug 
    }),
  ],
};

请注意,我还使用 .babelrc 来启用 async/await 等功能:
{
  "presets": [ 
    [
      "env", {
        "targets": {
            "node": "6.10"
          }
      }
    ]
  ], 
  "plugins": [ "transform-object-rest-spread", "transform-async-generator-functions" ]
}

我该如何配置Webpack来进行这个转换?


这个答案对我没有用。


你的 .babelrc 文件里有什么内容? - Noel Llevares
1个回答

9

我成功地让它工作了。

这是Webpack的配置:

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

const debug = process.env.NODE_ENV !== 'production';

module.exports = {
  context: __dirname,
  entry: './index.js',
  output: {
    path: path.join(__dirname, 'out'),
    filename: 'index.js',
    library: "index",
    libraryTarget: 'commonjs2'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            babelrc: true
          }
        }
      }
    ],
  },
  target: 'node',
  externals: [ nodeExternals() ],
  plugins: [
    new webpack.optimize.UglifyJsPlugin({ 
      mangle: !debug, 
      sourcemap: debug 
    }),
  ],
};

这是我制作发送到AWS Lambda的捆绑包的命令:

zip -j out.zip ./out/index.js

请注意-j设置。这会从zip文件中删除文件的路径。
因此输出结果为:
+ out.zip
+--+ index.js

而不是:

+ out.zip
+--+ out
   +--+ index.js

1
你在webpack配置中修改了什么?或许可以加粗以使其更清晰。 - Horia Coman
1
我添加了 output.library 并修复了打包脚本。 - sdgfsdh
在您的 AWS Lambda 中,您的处理程序字符串设置为什么? - azizj
@AzizJaved 处理程序字符串为 index.handler - sdgfsdh
我相信这个“hello world”示例可以工作,但如果你在处理程序代码中使用了任何来自node_modules的依赖项,它将停止工作,因为你使用了nodeExternals()阻止所有node_modules绑定。如果你想要捆绑依赖项,只需从Webpack配置中删除nodeExternals(),或者将node_modules目录添加到最终的zip文件中。 - Michal Moravcik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接