Serverless-webpack: Transpile/transform additional files that are dynamically required at runtime

Created on 20 Jul 2018  路  5Comments  路  Source: serverless-heaven/serverless-webpack

This is a Question

Description

We have an interesting use case I'm having a hard time reasoning about even though it doesn't seem to complex or out there.

We're using a relational database via RDS that our serverless functions interact with along with knex to manage that interaction. We have migrations bundled into all of that of course and my aim is to be able to run those in our cloud environment(s) via remotely invoking a function instead of having to SSH into a leader server of some sort running the codebase.

The crux here is multi-fold:

  • we want to use es6 import/export module syntax in our migrations as well since it's used everywhere else in our codebase and it would feel a little awkward to have a directory where import/export isn't allowed.
  • our migration files are intended to be required dynamically via a directory listing at runtime because that's how knex works.
  • we're using individually: true in our ~package.json~ serverless.yml so we're required to use serverless-webpack's automatic entry resolution in our webpack config

So basically my question is, how do we handle dynamic requires with serverless webpack where the files being dynamically required at runtime also have to be transpiled first and packaged with the function? Or maybe recommendations on an atlernative approach?

Right now we're using babel-node to run migrations via package.json scripts and that works fine but doesn't address the use case of having a migrator serverless function that invokes the migrations.

Similar or dependent issue(s):

  • tgriesser/knex#1232

Additional Data

  • Serverless-Webpack Version you're using: "serverless-webpack": "^5.1.5"
  • Webpack version you're using: "webpack": "^4.2.0"
  • Serverless Framework Version you're using: "serverless": "^1.28.0"
  • Operating System: macOS High Sierra
  • Stack Trace (if available):
question

Most helpful comment

Hi @bericp1

I had posted the issue here primarily due to the final bullet:

Thanks for the issue 馃憤 . It is good to post issues here, even if they might turn out to be solved outside of the plugin. This will help others that run into them and just search for "sls-webpack + issue description" so that they can find a proper solution immediately.

@geovanisouza92 馃挴 for the help

All 5 comments

Options we're considering:

  • Using serverless-webpack's scripts to run babel on the migrations files after the webpack build (we're currently using webpack-copy-plugin to copy them in)
  • Ditch import/export and just use commonjs module.exports :(

@bericp1 This is a webpack concern, not serverless-webpack. Basically you need to include a require context inside your ~webpack configuration~ code.

@geovanisouza92 Interesting I'll investigate that.

I had posted the issue here primarily due to the final bullet:

  • we're using individually: true in our ~package.json~ serverless.yml so we're required to use serverless-webpack's automatic entry resolution in our webpack config

I suppose I thought that since serverless-webpack took control of entry in this secnario we were out of luck including other files in the webpack build.

Looks promising, thanks!

Hi @bericp1

I had posted the issue here primarily due to the final bullet:

Thanks for the issue 馃憤 . It is good to post issues here, even if they might turn out to be solved outside of the plugin. This will help others that run into them and just search for "sls-webpack + issue description" so that they can find a proper solution immediately.

@geovanisouza92 馃挴 for the help

Sorry to dig up an old issue, but for others that get here via google, the knex docs have an example of loading scripts inside webpack bundles here: https://knexjs.org/#custom-migration-sources

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jmparsons picture jmparsons  路  5Comments

bebbi picture bebbi  路  3Comments

tommedema picture tommedema  路  4Comments

vladtamas picture vladtamas  路  5Comments

timoangerer picture timoangerer  路  3Comments