Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pack individual functions using the new packageFunction hook #60

Closed
m3rls opened this issue Nov 22, 2016 · 12 comments
Closed

Pack individual functions using the new packageFunction hook #60

m3rls opened this issue Nov 22, 2016 · 12 comments

Comments

@m3rls
Copy link

m3rls commented Nov 22, 2016

Hi,

I've noticed that when trying to deploy individual functions (with package -> individually: true in serverless.yml) the change made in #48 has fixed the exception (thank you!), but the function isn't being packed.

In my use case, this leads to a ~19MB .zip being uploaded compared to a ~600kb .zip when I do a full deploy.

A Pull Request was merged and released along with the serverless 1.2.0 milestone yesterday that added a before:deploy:function:packageFunction hook, which I have (naively and probably incorrectly) added to index.js as follows:

      'before:deploy:function:packageFunction': () => BbPromise.bind(this)
        .then(this.validate)
        .then(this.compile),

So now when I run serverless deploy function --function <myFunction> it packages it (as illustrated below):

Without hook:

➜  api git:(master) ✗ serverless deploy function --function myFunction
Serverless: Deploying function: myFunction...
Serverless: Packaging function: myFunction...
Serverless: Uploading function: myFunction (19.9 MB)...
Serverless: Successfully deployed function: myFunction

With hook:

➜  api git:(master) ✗ serverless deploy function --function myFunction
Serverless: Deploying function: myFunction...
Serverless: Bundling with Webpack...
Time: 2337ms
     Asset     Size  Chunks             Chunk Names
handler.js  1.92 MB       0  [emitted]  main
Serverless: Packaging function: myFunction...
Serverless: Uploading function: myFunction (606.73 KB)...
Serverless: Successfully deployed function: myFunction

Would it be possible to please have support for this hook added at some point? I'm happy to make a pull request for it if the way I've added it is acceptable, but didn't want to just assume it was correct.

Thanks

@activefx
Copy link

@m3rls would you be willing to share your webpack config? I'm having trouble packaging functions individually and would appreciate it

@m3rls
Copy link
Author

m3rls commented Nov 27, 2016

@activefx sure, it's pretty minimal:

module.exports = {
  entry: './handler.js',
  target: 'node',
  module: {
    loaders: [
      { test: /\.json$/, loader: 'json' }, {
        test: /\.js$/,
        loaders: ['babel'],
        include: __dirname,
        exclude: /node_modules/,
      }
    ]
  }
};

I've had to use the latest version of serverless-webpack from this repo rather than the one from npmjs.com until a new release is pushed up, due to #48

@activefx
Copy link

Thank you for sharing @m3rls, I must be missing something else then

@thenikso
Copy link
Contributor

thenikso commented Dec 23, 2016

it would be great indeed. I've checked out the new "packageFunction" related methods in Serverless and adding a hook on deploy:function:packageFunction (here) should do the trick. In particular we would want to set the funcPackageConfig.artifact cheched here to provide our bundled one.

Anyone willing to give it a try?

@johnf
Copy link

johnf commented Jan 26, 2017

The solution @m3rls described works for me. I've created a pull request in #91

@HyperBrain
Copy link
Member

This has to be checked again as soon as #130 is merged, to see if there is still anything to do here.

@HyperBrain
Copy link
Member

PR #130 has been merged and published (current version is 2.2.0).
There should be full support of serverless deploy function --function=<myfunction> now. @m3rls , @johnf , @activefx - can you try if that works for you?

@johnf johnf mentioned this issue Jul 30, 2017
5 tasks
@johnf
Copy link

johnf commented Jul 30, 2017

So all my files actually live in a src directory. My webpack config was dealing with this previously.
If I move everything to the root it works prefectly.

#176 is a possible fix. If others agree I'll update docs etc.

in serverless.yaml I added

custom:
  webpackBasePath: './src'

@HyperBrain
Copy link
Member

HyperBrain commented Jul 30, 2017

@johnf I think you did not declare your handlers properly in your serverless.yml. Serverless itself relies on the fact that you specify your handlers there relative to your service root directory.

In your case this would be:

functions:
  myfunction:
    handler: src/handler.myFunc

The new package/deploy function support works with and without the webpack plugin. Imo, having a separate base path definition only for the webpack plugin would introduce a different behavior when the plugin is enabled or not.
It would be a weird behavior, if you turn off the plugin and your service won't build and deploy anymore.

As an example, we have our handlers separated from the actual implementations in handlers/<function> directories and the code in src. So we just specify:

functions:
  func1:
    handler: handlers/func1/handler.handle
  func2:
    handler: handlers/func2/handler.handle
  ...

There is no need to set a base path - Serverless (and the plugin) know exactly where to find the handlers. We could even distribute the handlers to completely independent paths under the service directory to create a separation. Defining a common base path would not make any sense at all.

Additionally, other plugins will most likely be not compatible with the webpack plugin if you introduce a plugin specific base path definition.

So I suggest just to declare the handlers properly and everything will work.

@johnf
Copy link

johnf commented Jul 31, 2017

@HyperBrain thanks don't know why I didn't think if that it's fairly obvious. Have updated and looks good now

@HyperBrain
Copy link
Member

BTW: I implemented the support of the service wide package: individually: true. It is available in the v3.0.0-individual-packaging branch. Can you test, if the branch works, if you set the flag at service level?
Please comment the results in #177 .

@HyperBrain HyperBrain mentioned this issue Aug 5, 2017
@HyperBrain
Copy link
Member

I'll close this task as the problem is solved and individual packaging is now fully supported in 3.0.0-rc.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants