Webpack configuration for AWS Lambda?

sdgfsdh

I am using AWS Lambda, for which I need to transpile some modern JavaScript to Node 6.10.

Here is my code:

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

Here is what I would like to transpile to (roughly speaking):

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

Here is what I am currently generating:

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');
};

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

Here is my Webpack configuration:

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 
    }),
  ],
};

Note that I am also using a .babelrc to enable async/await etc:

{
  "presets": [ 
    [
      "env", {
        "targets": {
            "node": "6.10"
          }
      }
    ]
  ], 
  "plugins": [ "transform-object-rest-spread", "transform-async-generator-functions" ]
}

How do I configure Webpack to make this transformation?


This answer did not work for me.

sdgfsdh

I managed to get this working.

Here is the Webpack configuration:

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 
    }),
  ],
};

Here is my command for making the bundle that is sent to AWS Lambda:

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

Note the -j setting. This strips the path from the file inside the zip.

So the output is:

+ out.zip
+--+ index.js

And not:

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Webpack and AWS Lambda issue - handler missing on module

分類Dev

aws cli lambda`update-function-configuration`は既存の環境変数を削除します

分類Dev

Pandas & AWS Lambda

分類Dev

Pandas & AWS Lambda

分類Dev

AWS Lambda Payloads

分類Dev

AWS Lambda NoClassDefFoundError

分類Dev

AWS Lambda To Atlas

分類Dev

AWS Lambda To Atlas

分類Dev

Python AWS Lambda Certificates

分類Dev

NoDecodeDelegateForThisImageFormat `TIFF \" 'aws lambda

分類Dev

AWS Lambda Publishing to SNS

分類Dev

AWS Lambda different IP address for each Lambda

分類Dev

Kafka Connect AWS Lambda Sink

分類Dev

Deploy lambda functions in an AWS Organization

分類Dev

aws lambda SES function timeout

分類Dev

AWS Lambda with request-promise

分類Dev

AWS - Lambda Function not waiting for await

分類Dev

Reusing Mysql Connection in Lambda on AWS

分類Dev

Create IoT Policy with AWS Lambda

分類Dev

AWS Lambda Log Output Issue

分類Dev

aws lambda @ edge + Cloudfront ERROR()

分類Dev

Angular + Webpack + jQuery. Where is the configuration file?

分類Dev

Webpackエラー:configuration.module

分類Dev

Webpack : Invalid configuration object/ Invalid Module Entry

分類Dev

Multiple configuration vs Multiple entries in webpack

分類Dev

Is there a way to see Angular's webpack configuration?

分類Dev

netlify lambda configuration in package.json

分類Dev

Invoke AWS Lambda function when multiple Lambda function is done

分類Dev

AWS Lambda timeout when another (long) Lambda is invoked