NiGhTTraX/ts-monorepo

would be cool to see a lerna + typescript + serverless-webpack example

derek-lewis opened this issue · 7 comments

Would be great to see an example of:

/example

  • /core (@exmaple/core - a shared lib)
    --/lib/test.ts
  • /services
    --/serverless-api (AWS API Gateway)
    --- depends on @example/core

I have an example working. However, I am seeing an error on the @example/lib dependancy...

offline: Failure: Module parse failed: The keyword 'interface' is reserved (11:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

I haven't used serverless-webpack, but would the example be different from the included webpack example?

offline: Failure: Module parse failed: The keyword 'interface' is reserved (11:0)

Usually this means your loaders are not set up to compile code outside the current module. Notice how the loader in the example doesn't have an include config that would limit the compilation scope. The CRA example is similar.

I'm on the same path, trying to use a monorepo with typescript and serverless webpack. This basically works with the webpack example. But in most serverless projects I did, sooner or later I encountered some webpack-incompatible modules. But if you want to exclude some webpack-incompatible packages, the example does not work any more.

If you exclude them in webpack.config.js with nodeExternals like so

externals: [
    nodeExternals({}),
    nodeExternals({
      modulesDir: path.resolve(__dirname, "../../node_modules"),
    }),
  ]

you'll get a runtime error


{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module '@nighttrax/bar'\nRequire stack:\n- /var/task/src/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    // stack omitted for brevity
}

After this I tried to set the node_modules path to the root in the serverless webpack config:

custom:
  webpack:
    webpackConfig: "./webpack.config.js"
    keepOutputDirectory: true
    packager: "npm"
    includeModules:
      nodeModulesRelativeDir: "../../"

This leads to a webpack compile time error: npm ERR! 404 Not Found - GET https://registry.npmjs.org/@nighttrax%2fbar - Not found

Not sure why, but I believe this is due to the way serverless-webpack handles dependencies. I believe they try to bundle with webpack and do an npm install in the webpack ouput folder.

I extended the repo to showcase this (NPM branch): https://github.com/florianbepunkt/ts-monorepo/tree/npm

In case anyone finds a solution for this, a heads up would be nice.

I managed to build a perfect solution using workspaces and serverless-optimize. The only complication was that mongodb needs to be extracted into a lambda layer and installed locally as a devDep.

@derek-lewis Do you have an example repo by any chance?

I will look to get an example repo up for you 👍🏼

@derek-lewis i have created a pull request for serverless application #303