Serverless-webpack: Typescript

Created on 18 Aug 2016  路  1Comment  路  Source: serverless-heaven/serverless-webpack

Hi,

Im trying to use your excellent plugin with typescript.

My package.json:

{
  "name": "my-second-service",
  "version": "1.0.0",
  "description": "Me messing around with lambdas",
  "private": true,
  "main": "handler.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Mike cann",
  "license": "MIT",
  "devDependencies": {
    "aws-sdk-typescript": "0.0.3",
    "serverless-webpack": "^1.0.0-beta.2",
    "ts-loader": "^0.8.2",
    "typescript": "^1.8.10",
    "webpack": "^1.13.1"
  },
  "dependencies": {}
}

My webpack.config.js:

module.exports = {
  entry: './handler.ts',
  module: {
    loaders: [
      { test: /\.ts(x?)$/, loader: 'ts-loader' }
    ]
  }
};

in serverless.yml:

service: my-second-service

plugins:
  - serverless-webpack

provider:
  name: aws
  runtime: nodejs4.3

functions:
  hello:
    handler: handler.hello
    events:
        - http:
            path: greet
            method: get

and handler.ts:

export const hello = (event, context, cb) => cb(null,
  { message: 'Go Serverless Webpack v1.0! Your function executed successfully!', event }
);

Now this all runs fine when doing serverless webpack serve I am able to test in the browser. But when I deploy it to AWS I get errors like:

{"errorMessage":"Cannot find module 'handler'","errorType":"Error","stackTrace":["Module.require (module.js:353:17)","require (internal/module.js:12:17)"]}

So I ran serverless webpack --out dist so I could inspect the contents. For some reason instead of "handler.js" being in there like I expected, there was "handler.ts", so no wonder im getting an error about cannot find module.

So I modified my webpack.config.js to be:

module.exports = {
  entry: './handler.ts',
  output: {
    filename: "handler.js"
  },
  module: {
    loaders: [
      { test: /\.ts(x?)$/, loader: 'ts-loader' }
    ]
  }
};

when running serverless webpack --out dist I now correctly get handler.js in the dist dir but when I try to run serverless webpack serve or deploy I get the following console error:

  Assertion Error ----------------------------------------

     rimraf: missing path

     For debugging logs, run again after setting SLS_DEBUG env var.

  Get Support --------------------------------------------
     Docs:          v1.docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues

     Please report this error. We think it might be a bug.

Any ideas whats going on here?

Cheers.

Most helpful comment

So what you want to do is have your webpack.config.js output section to look like this:

const path = require('path');
{
 ...
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, './.webpack'),
    filename: 'handler.js'
  },
};

That said, I added an example here https://github.com/elastic-coders/serverless-webpack/tree/master/examples/typescript and find out that there was a problem with setting a relative path in the webpack output dir. So that's also fixed now. Beta 3 will be out soonish with that fix as well.

Let me know if that fixes it and feel free to reopen this if it doesn't!

>All comments

So what you want to do is have your webpack.config.js output section to look like this:

const path = require('path');
{
 ...
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, './.webpack'),
    filename: 'handler.js'
  },
};

That said, I added an example here https://github.com/elastic-coders/serverless-webpack/tree/master/examples/typescript and find out that there was a problem with setting a relative path in the webpack output dir. So that's also fixed now. Beta 3 will be out soonish with that fix as well.

Let me know if that fixes it and feel free to reopen this if it doesn't!

Was this page helpful?
0 / 5 - 0 ratings