【问题标题】:Webpack configuration for AWS Lambda?AWS Lambda 的 Webpack 配置?
【发布时间】:2018-06-02 00:49:22
【问题描述】:

我正在使用 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 来进行这种转换?


This answer 对我不起作用。

【问题讨论】:

  • 您的.babelrc 中有什么内容?

标签: javascript node.js amazon-web-services webpack aws-lambda


【解决方案1】:

我设法让这个工作。

这里是 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

【讨论】:

  • 你在 webpack 配置中修改了什么?也许加粗,这样更清楚。
  • 我添加了output.library 并修复了我的打包脚本。
  • 在您的 AWS Lambda 中,您的处理程序字符串设置为什么?
  • @AzizJaved 处理程序字符串是index.handler
  • 我相信这个“​​hello world”示例有效,但是如果您在处理程序代码中使用来自node_modules 的任何依赖项,它将停止工作,因为您可以通过使用@987654330 阻止所有node_modules 捆绑@。如果您还想捆绑依赖项,只需从 webpack 配置中删除 nodeExternals(),或者将 node_modules 目录添加到最终的 zip 文件中。
猜你喜欢
  • 2018-03-08
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多