ajmath / serverless-offline-scheduler

MIT License
96 stars 40 forks source link

Webpack-transpiled code isn't loaded #11

Closed tangrufus closed 5 years ago

tangrufus commented 6 years ago

When using with serverless-webpack v4.1.0, serverless-offline-scheduler v0.3.3 doesn't load the webpack-transpiled code.

Could be related to #8

The Error

When running with $ sls offline start:

/Users/xxx/Code/xxx/my-service/xxx/create.js:1
(function (exports, require, module, __filename, __dirname) { import insertToDb from '../main/insertToDb';
                                                              ^^^^^^
SyntaxError: Unexpected token import

Workaround

Monkey patching scheduler.js#L69-L71 fixed my issue.

Original (version 0.3.3)

Currently, serverless-offline-scheduler loads from /Users/xxx/Code/xxx/my-service/xxx/create.js

https://github.com/ajmath/serverless-offline-scheduler/blob/918627213e8a7bb832b22e1b2340ffb476b829cb/lib/scheduler.js#L69-L71

Hardcoding the webpack output directory

Forcing serverless-offline-scheduler to load from webpack output directory. In my case: /Users/xxx/Code/xxx/my-service/xxx/.webpack/service/create.js

    const funcPath = path.join(
      this.serverless.config.servicePath,
      '.webpack',
      'service',
      this.location || "", filename);

Is it a bug? Or, have I missed anything? Thanks!

alantreadway commented 6 years ago

Just hit this problem myself today - an alternative workaround is to use the --location CLI flag, e.g assuming default Webpack output location:

serverless offline start --location .webpack/service

Package versions:

$ yarn list | grep serverless
├─ serverless-offline-scheduler@0.3.3
├─ serverless-offline-sns@0.35.0
├─ serverless-offline@3.16.0
├─ serverless-pseudo-parameters@1.2.0
├─ serverless-webpack@4.2.0
ajmath commented 5 years ago

I think that @alantreadway's suggestion is the best way to go about this

snypenet commented 5 years ago

I know this is an old issue. I just ran into this myself and the fix provided will not work because using the --location command line parameter interferes with another serverless plugins I am using. The issue is that the code in the lib/subscriber.js assumes the handler.js is always in the root of the project. I was able to get around this by checking for the handler.js in .webpack/service as well.

Output Behavior: https://github.com/serverless-heaven/serverless-webpack#output Can something like this be added based on the behavior of serverless-webpack and determining the output from the webpack.config.js?
const possibleWebpackedCode = ${this.serverless.config.servicePath}\\.webpack\\service; const webpackedFuncPath = path.join( possibleWebpackedCode, this.location || "", filename);

if (fs.existsSync(webpackedFuncPath)) {
  delete require.cache[require.resolve(webpackedFuncPath)];
  return require(webpackedFuncPath)[handlerFunction];
}
esetnik commented 5 years ago

@snypenet if you can submit a PR I'd be happy to test it. I have what I believe is a related issue in #26.