Coder Social home page Coder Social logo

nervous-systems / serverless-cljs-plugin Goto Github PK

View Code? Open in Web Editor NEW
73.0 7.0 10.0 37 KB

Serverless plugin for Clojurescript deployment w/ cljs-lambda

License: The Unlicense

JavaScript 59.62% Clojure 40.38%
clojurescript aws-lambda serverless serverless-plugin aws clojure

serverless-cljs-plugin's Introduction

serverless-cljs-plugin

npm version

A Serverless plugin which uses lein/cljs-lambda (or, optionally Lumo) to package services written in Clojurescript.

Leiningen Template

$ lein new serverless-cljs example
example$ lein deps

Will generate an example directory containing a minimal serverless.yml and project.clj demonstrating this plugin's functionality.

Usage

functions:
  echo:
    cljs: example.core/echo

plugins:
 - serverless-cljs-plugin

With the above serverless.yml, serverless deploy will create a zip file containing your functions. Doing this is similar to setting the Serverless packaging.artifact option - cljs-lambda is responsible for the zip contents, and Serverless includes/excludes will be skipped (cljs-lambda offers equivalent functionality).

In the example above, there needn't be a corresponding entry for echo in project.clj.

Lumo

Alternatively you can use the Lumo compiler.

In order to enable it, pass the --lumo switch to either deploy or package:

$ serverless deploy --lumo

Or add the following to your serverless.yml:

custom:
  cljsCompiler: lumo
  • Compiler options

    The source paths and compiler options will be read from the optional file serverless-lumo.edn. Below are the defaults:

    {:source-paths ["src"]
     :compiler     {:output-to     "out/lambda.js"
                    :output-dir    "out"
                    :source-map    false ;; because of a bug in lumo <= 1.8.0
                    :target        :nodejs
                    :optimizations :none}}
  • Lumo Configuration

    As an alternative to cljsCompiler: lumo, cljsCompiler.lumo may be specified as a map of options. These options are passed directly to the lumo process. Currently supported:

    custom:
      cljsCompiler:
        lumo:
          dependencies:
            - andare:0.7.0
          classpath:
            - /tmp/
          localRepo: /xyz
          cache: /cache | none
          index: true | false
          exitOnWarning: true | false

    Note: caching is always on unless you specify "none" in the config.

  • The index.js file

    The index option will materialize a custom index.js in :output-dir's parent folder. This file should be thought as managed by serverless-cljs-plugin and it is necessary for some plugin (e.g.: serverless-offline) to work properly.

    Note: with the default compiler options, index.js will be saved in the project root, overwriting without warning.

  • Exit on compilation warnings

    Lumo generates warnings such as WARNING: Use of undeclared Var to signal failures. You can tune the ones you want to see by using the :warnings compiler option in serverless-lumo.edn, but by default the lumo process emits the warnings, does not throw and returns 0. This means that serverless will keep going in presence of warnings.

License

serverless-cljs-plugin is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying LICENSE file.

serverless-cljs-plugin's People

Contributors

arichiardi avatar karlwestin avatar moea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

serverless-cljs-plugin's Issues

Cannot find module 'serverless-cljs-plugin'

Following the instructions on the README and the guide I get the following error:

  Error --------------------------------------------------

  Cannot find module 'serverless-cljs-plugin'

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Cannot find module 'serverless-cljs-plugin'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at plugins.forEach (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:65:22)
    at Array.forEach (native)
    at PluginManager.loadPlugins (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:64:13)
    at PluginManager.loadServicePlugins (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:91:10)
    at PluginManager.loadAllPlugins (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:60:10)
    at service.load.then (/usr/local/lib/node_modules/serverless/lib/Serverless.js:72:28)
    at <anonymous>
From previous event:
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)
From previous event:
    at __dirname (/usr/local/lib/node_modules/serverless/bin/serverless:18:28)
    at Object.<anonymous> (/usr/local/lib/node_modules/serverless/bin/serverless:34:4)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                     darwin
     Node Version:           8.1.2
     Serverless Version:     1.16.0

Have you seen this before?
Any ideas?

Thank you.

Consider dropping lein in favor of direct aws cli calls?

Hello folks ;)

Just putting there this idea in order for me to understand what it would take to have something that just calls the AWS command line tool directly without spinning a JVM (this maybe is the same as the other issue in cljs-lambda).

It would make startup time way faster and little or no change to the current plugin.

โ˜ฎ๏ธ ๐Ÿ‘‹

Serverless plugin "serverless-cljs-plugin" not found

I just scaffolded out a project and ran "serverless deploy". I get this error:

Serverless Error ---------------------------------------

Serverless plugin "serverless-cljs-plugin" not found. Make sure it's installed and listed in the "plugins" section of your serverless config file.

Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Forums: forum.serverless.com
Chat: gitter.im/serverless/serverless

Your Environment Information -----------------------------
OS: darwin
Node Version: 6.9.1
Serverless Version: 1.24.0

Expose index.js generation

Sometimes you want to generate an index.js that is appropriate for you compilation level, but you don't quite want to use the compiler provided by serverless-cljs-plugin.

For instance one wicked dev (me ๐Ÿ˜„) might want to use a custom script, or clj -m cljs.main for compilation but still be able to use the index.js provided by the plugin.

I know this sounds odd, and I am opening an issue to see if it is completely out of the world or not.

Respect individually option

Hi there again,

opening this PR because I have two issues. One is major and a feature request. I have:

...
package:
  individually: true

JS code is zipped correctly so I have two lambdas. However the serverless-cljs-plugin just packages everything in a zip.

I saw the implementation and it is no handling it. That's ok, is there anything I need to know if I want to tackle this problem?

For instance, there is an index.js there, which is generated. What was the purpose of it?

Thanks a lot for your patience ๐Ÿ˜„

PS.: I was able to create a zip with the lumo compiler but I am still finishing it off.

How to pass maven/cljsjs dependencies to the lumo compiler

Hi @moea!

A problem that it is probably also linked with anmonteiro/lumo#239 is that there is no way at the moment to pass additional classpath entries to the lumo compiler.

So for instance, in a normal lumo script I would do:

$ boot with-cp -d ... -d ... -w -f cp # dumps the resolved classpath to the cp file

or

$ lein classpath > cp

and then:

$ lumo -c $(cat cp) .....other args...

This is necessary because Maven dependencies needs to be solved and passed in to lumo as "classpath". Node.js does not have any classpath of course, but this is how lumo has inherited dependency resolution from the Maven world.

Probably there is no clean solution at the moment and I am open to suggestions. In my code I try to avoid using Maven dependencies completely when using lumo but I thought it might be useful to write this down.

Cljs plugin does not exclude dev dependencies

I have run against this problem a couple of times now and this I have decided to investigate.

So given that we are not using the zipService provided by the Serverless Framework to zip up our stuff we don't mimic (I guess by design) its standard behavior. That's unfortunate.

There are two solutions to this (but maybe more, open to suggestions):

  • Make so that we use the zipService above in index.js and then both lumo and lein would just reopen and append to the zip file.
  • Reimplement the same behavior in both plugins.

I think option one is more work but also probably the right thing to do, but I am also wondering if there is a way not to skip the packaging step (just provide a custom zip name to it).

Any other idea?

How To Use REPL?

Hi, I scaffold a new project with lein new serverless-cljs ok-then and cd into the directory with project.clj.

I then add one function to my core.clj like this:

(ns ok-then.core
  (:require [cljs-lambda.macros :refer-macros [defgateway]]))

(defgateway echo [event ctx]
  {:status  200
   :headers {:content-type (-> event :headers :content-type)}
   :body    (event :body)})

(defn ok []
  (println "ok"))

How can I call this function from the REPL?

Always generate index.js if not present

Hello @moea,

I wanted to ask and propose here this change before working on it.

In order to really decouple the compilation from everything else around serverless we could make so that whenever there is no index.js file in the root of the project (so in case of a project generated by a lein template this won't happen), we trigger the generation. Probably after the cljsLambdaBuild in index.js.

This would make it easier to add a new compiler (like shadow-cljs, or vanilla script) without duplicating the index generation part.

My plan would be to use lumo from the serverless plugin like this (wishful thinking here):

`lumo -c ${path.resolve(__dirname, 'serverless-cljs-plugin')} ` +
           `-m serverless-lumo.index ` + // <-- main called here
           `--functions '${fns}'`

What do you think? Is it something worth working on?

Serveless invoke local does not work

Hello folks!

I am trying to invoke a function locally:

After having executed serverless package to create my zip file:

$ serverless package
Serverless: Targeting /home/arichiardi/git/logpoc/.serverless/logpoc.zip
Serverless: Packaging service...
Serverless: Executing "lein update-in :cljs-lambda assoc :functions '[{:name "logpoc-dev-bunyan-lambda" :invoke logpoc.core/bunyan-lambda}]' -- cljs-lambda build :output /home/arichiardi/git/logpoc/.serverless/logpoc.zip :quiet"
Serverless: Returning artifact path /home/arichiardi/git/logpoc/.serverless/logpoc.zip

I try the following, which fails:

$ serverless invoke local -f bunyan-lambda
Serverless: Targeting /home/arichiardi/git/logpoc/.serverless/bunyan-lambda.zip
{ Error: Cannot find module './/goog/bootstrap/nodejs.js'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/arichiardi/git/logpoc/index.js:2:1)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at AwsInvokeLocal.invokeLocalNodeJs (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:156:33)
    at AwsInvokeLocal.invokeLocal (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
    at AwsInvokeLocal.tryCatcher (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/home/arichiardi/.nvm/versions/node/v8.1.3/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5) code: 'MODULE_NOT_FOUND' }

Am I doing something wrong here?

Thank you this package is really great for mixed cljs/js project on AWS Lambda!

Merge of :source-paths is not working properly

I have noticed that the merging on :source-paths has a problem: if I have a serverless-lumo.edn like

{:source-paths ["src"]
 :compiler-options {:verbose true
                    :language-in :ecmascript5
                    :optimizations :none
                    :source-map true}}

Then the output of the lumo command is:

Invoking the Lumo compiler w/ inputs ["src",["src"]]

Will have a look.

Failed to find base.js

Hello @moea,
I am writing here because I need some help with the following error and maybe you know a bit better what is going on. I basically get:

{
  "errorMessage": "Cannot find module './out/goog/bootstrap/nodejs.js'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._load (module.js:417:25)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "Object.<anonymous> (/var/task/index.js:1:63)",
    "Module._compile (module.js:570:32)",
    "Object.Module._extensions..js (module.js:579:10)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)"
  ]
}

If I invoke the deployed lambda. My configuration is the default:

{:source-paths ["src"]
 :compiler {:language-in :ecmascript5
            :optimizations :none
            :source-map false
            :verbose true}}

I checked, and the file is in out. Any hint is appreciated ๐Ÿ˜„ Maybe I am missing some AWS configuration?

Add lumo cache

Hi @moea!

According to this, we cannot make use of the cache at the moment, but when ready, I have implemented already the feature in my feature/lumo-cache branch.

I did it because it seemed fairly straightforward and I wanted to try.

It should cut out some compilation time in the future.

`serverless deploy` does not work and I don't see instructions for fixing it?

After executing

npm install -g serverless
npm install -g serverless-cljs-plugin
lein new serverless-cljs myapp
cd myapp

and changing the config to runtime: node6.10 I want to run

serverless deploy

but it fails with

  Serverless Error ---------------------------------------

  Serverless plugin "serverless-cljs-plugin" not found. Make sure it's installed and listed in the "plugins" section of your serverless config file.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                     darwin
     Node Version:           6.10.0
     Serverless Version:     1.25.0

I have no idea why. Any idea? Have I missed some setup step?

Artifact name mis-interpolated

Hey there! I'm following the instructions in the README without any tweaks, using the lein template as described and am getting an odd error trying to upload the artifact.

src> lein new serverless-cljs serverless-cljs-example
src> cd serverless-cljs-example
serverless-cljs-example> lein deps
serverless-cljs-example> SLS_DEBUG=* AWS_PROFILE=<MY-PROFILE> serverless deploy
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command create
[...]
Serverless: [AWS s3 200 0.243s 0 retries] putObject({ Body:
   <Buffer 7b 22 41 57 53 54 65 6d 70 6c 61 74 65 46 6f 72 6d 61 74 56 65 72 73 69 6f 6e 22 3a 22 32 30 31 30 2d 30 39 2d 30 39 22 2c 22 44 65 73 63 72 69 70 74 ... 3944 more bytes>,
  Bucket:
   'serverless-cljs-example-serverlessdeploymentbuck-lqiyoq34buwi',
  Key:
   'serverless/serverless-cljs-example/dev/1553968044501-2019-03-30T17:47:24.501Z/compiled-cloudformation-template.json',
  ContentType: 'application/json',
  Metadata:
   { filesha256: 'k9Rs/1BMSyWQNTvj6FEXACL+PSeNaW0vi3Hi7FHR6yk=' } })
Serverless: Uploading artifacts...

  Error --------------------------------------------------

  ENOENT: no such file or directory, stat '/Users/daemian/src/serverless-cljs-example/.serverless/Users/daemian/src/serverless-cljs-example/.serverless/serverless-cljs-example.zip'

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: ENOENT: no such file or directory, stat '/Users/daemian/src/serverless-cljs-example/.serverless/Users/daemian/src/serverless-cljs-example/.serverless/serverless-cljs-example.zip'
    at Object.statSync (fs.js:871:3)
    at BbPromise.map (/usr/local/Cellar/serverless/1.39.0/libexec/lib/node_modules/serverless/lib/plugins/aws/deploy/lib/uploadArtifacts.js:118:24)

You can see in the above that the artifact path is duplicated...

/Users/daemian/src/serverless-cljs-example/.serverless/Users/daemian/src/serverless-cljs-example/.serverless/serverless-cljs-example.zip

but that file is actually at:

/Users/daemian/src/serverless-cljs-example/.serverless/serverless-cljs-example.zip

Finding Issue #34 I tried the --lumo deploy flag but encountered the same issue.

I also tried updating the generated project.clj to reference :npm {:dependencies [[serverless-cljs-plugin "0.2.2"]]} instead of the generated "0.1.2", but no dice.

OS version: Mac OSX 10.13.6
NPM version: 6.7.0
serverless version: 1.39.0

`serverless deploy -f <name>` does not work

$ serverless deploy -f greet
Serverless: Targeting /Users/r627543/Code/fido/packages/functions/example/.serverless/greet.zipServerless: Packaging function: greet...
Serverless: Executing "lein update-in :cljs-lambda assoc :functions '[{:name "example-dev-greet" :invoke example.core/greet}]' -- cljs-lambda build :output /Users/r627543/Code/fido/packages/functions/example/.serverless/greet.zip :quiet"
Serverless: Returning artifact path /Users/r627543/Code/fido/packages/functions/example/.serverless/greet.zip

  Error --------------------------------------------------

  ENOENT: no such file or directory, open '/Users/r627543/Code/fido/packages/functions/example/Users/r627543/Code/fido/packages/functions/example/.serverless/greet.zip'

Running serverless deploy works just fine.

Shadow-cljs compiler

Hi @moea!

Given that now serverless-cljs-plugin is very flexible regarding compilers, what about adding shadow-cljs?

There should be a very low amount of work to do there, because it already provides its on shadow-cljs.edn file for configuring things, we'd just need to trigger the command line command with the necessary parameters (:functions definitions I guess).

I myself like to work with the lumo for the speed of development and then compile production with something more solid (as you've seen, the lumo compiler is in its very early stages).

What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.