Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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?

1 Answer

0 votes
by (44.4k points)

 This 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 

    }),

  ],

};

This command makes the bundle which will be sent to 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 this: 

+ out.zip

+--+ out

   +--+ index.js

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...