serverless-heaven / serverless-webpack

Serverless plugin to bundle your lambdas with Webpack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide a simple way to differentiate between build types

pierreis opened this issue · comments

-Probably a simple one.

I do not see a simple way to differentiate during the build process between a local build (for example with serverless-offline), and a build to be executed on AWS.

Would it be possible to include some type of environment variable to distinguish local and AWS builds?

Best option I dound so far is process.argv.indexOf('offline') === -1, which works but doesn't look optimal to me.

--

Less importantly, serverless options are not passed to the build process (notably the stage). This one is less critical – as it is possible to point from serverless.yml to different configuration files, but still. It would be nice :)

The serverless-offline plugin sets the IS_OFFLINE environment variable and serverless invoke local sets IS_LOCAL - at least in the code started.

You could check if they are already available when the webpack config is loaded (never checked that).

If you want to distinguish the settings in your webpack.config.js it is also quite easy:

// webpack config
const slsw = require('serverless-webpack');

const options = slsw.lib.options;
const serverless = slsw.lib.serverless;

Options contains all command line options (as well as the stage) and serverless lets you access the complete serverless state (e.g. serverless.service.custom is everything you defined in custom).
Everything you access witin serverless.service is the content of your serverless.yml, but with all variables resolved.
So you do not need to have multiple webpack configurations anymore, but can configure it dynamically within one configuration file.

Additionally you should set entry: slsw.lib.entries together with output: filename: '[name].js' to activate the plugin's entry point auto-detection. This will automatically add entries for each of your handlers, which prepares them to use individual packaging.

The environment bars are not defined during the build, which is exactly the issue (checked that before).

I checked Serverless. The most robust solution would be that Serverless and serverless-offline would set the local indicator variables in their validate() methods -> That would need 2 PRs in the repositories.

For now, there might be the possibility to check the slsw.serverless.pluginManager for the currently active command. But that is as hacky as checking for the command line is, and has not really any benefits.

@pierreis I found a possible solution for the recognition without having to fix each and every plugin that does anything offline or locally. PR #334 adds a new slsw.lib.webpack.isLocal property that is set to true, if Serverless is invoked by one of the supported plugins (i.e. invoke local, serverless-offline and `serverless-step-functions-offline´).

So any check for slsw.lib.webpack.isLocal can be made to determine if the compile is run locally or in a deployment.

Could you tell me if that will work for you and probably make a test with ("serverless-webpack": "^5.0.0-rc.2")?

Released with 5.0.0