Skip to content
New issue

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

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

Already on GitHub? # to your account

Provide a simple way to differentiate between build types #232

Closed
pierreis opened this issue Sep 20, 2017 · 6 comments
Closed

Provide a simple way to differentiate between build types #232

pierreis opened this issue Sep 20, 2017 · 6 comments
Labels
Milestone

Comments

@pierreis
Copy link

-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 :)

@HyperBrain
Copy link
Member

HyperBrain commented Sep 20, 2017

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.

@HyperBrain
Copy link
Member

HyperBrain commented Sep 20, 2017

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.

@pierreis
Copy link
Author

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

@HyperBrain
Copy link
Member

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.

@HyperBrain
Copy link
Member

HyperBrain commented Mar 5, 2018

@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")?

@HyperBrain HyperBrain added this to the 5.0.0 milestone Mar 5, 2018
@HyperBrain
Copy link
Member

Released with 5.0.0

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

No branches or pull requests

2 participants