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

webpack'd lambda includes entire node_modules directory #53

Closed
doapp-ryanp opened this issue Oct 26, 2016 · 3 comments
Closed

webpack'd lambda includes entire node_modules directory #53

doapp-ryanp opened this issue Oct 26, 2016 · 3 comments

Comments

@doapp-ryanp
Copy link

Running serverless webpack produces my webpack'd .js file as well as a node_modules directory that contains EVERY sub-directory in my node-modules directory.

Project layout

node_modules
pages
  - handler.js
package.json
serverless.yml
webpack.config.js

pages/handler.js

'use strict';

let Rx      = require('rxjs/Rx');
let request = require('request');

var doGet = function(url) {
  return Rx.Observable.create(function(observer) {
    request(
      {
        method: 'GET'
        , uri:  url
        , gzip: true
      }, function(error, response, body) {
        if (error) {
          observer.error();
        }
        else if (response.statusCode != 200) {
          observer.error(response);
        }
        else {
          observer.next(response);
        }
        observer.complete();
      })
  });
};

var responseStream = doGet('http://mydomain/test.json')
  .mergeMap(function(response) {
    let d = JSON.parse(response.body);
    return doGet(d.Category[1].Subcategory[1].path);
  })
  .mergeMap(function(response) {
    let d = JSON.parse(response.body);
    return doGet(d.items[0].link);
  });

module.exports.hello = (event, context, callback) => {
  responseStream.subscribe(
    r => {
      const response = {
        statusCode: 200,
        body:       JSON.stringify({
          message:  'HELLO WORLD',
          httpBody: r,
          input:    event,
        }),
      };

      callback(null, response);
    },
    error => {
      callback(error);
    }
  );
};

package.json

{
  "name": "backend",
  "version": "0.0.1",
  "description": "test",
  "repository": {
    "url": "https://github.com/rynop/test"
  },
  "author": "Ryan Pendergast <[email protected]>",
  "license": "MIT",
  "dependencies": {
    "moment": "^2.15.2",
    "request": "^2.75.0",
    "rxjs": "^5.0.0-rc.1"
  },
  "devDependencies": {
    "webpack-node-externals": "^1.5.4",
    "serverless-webpack": "^1.0.0-rc.2"
  }
}

webpack.config.js

'use strict';

let nodeExternals = require('webpack-node-externals'),
    path          = require('path');

module.exports = {
  entry:     {
    pageGet: './pages/handler.js',
  },
  target:    'node',
  externals: [nodeExternals()], // exclude external modules
  output:    {
    libraryTarget: 'commonjs',
    path:          '/tmp/.webpack',
    filename:      'handler.js'
  },
};

Running serverless webpack produces (at /tmp/.webpack):

serverless webpack
Serverless: Bundling with Webpack...
Time: 48ms
     Asset     Size  Chunks             Chunk Names
handler.js  2.97 kB       0  [emitted]  pageGet
Serverless: Packing external modules: rxjs@^5.0.0-rc.1, request@^2.75.0

~ $ls -al /tmp/.webpack
handler.js
node_modules/ <--- this contains 100 dirs
package.json
  • Why does /tmp/.webpack/node_modules not just contain request andrxjs(as you can see I'm usingnodeExternals()`?
  • The smaller the code (and zip size) the quicker the lambda starts (and lowers the overall response time). How do I make it so only the files/code I use within require()d external modules, get pulled into a single webpack'd .js file? Browserify does this very nicely, can't figure out how to pull this off in webpack
@martinmicunda
Copy link

I am facing exactly same issue when I use command:

$ serverless deploy function -f myFunction

however when I run

$ serverless deploy 

it seems to pack node_modules correctly (only dependencies are in .zip file and devDependencies are not).

@thenikso
Copy link
Contributor

I think that #48 should resolve this issue. it will be included in v1 when it's released. if you could try it beforehand and confirm that this issue is fixed it would be even better!

Please reopen if it is not the case

@martinmicunda
Copy link

could you publish new beta version to npm with this fix? Thanks

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

3 participants