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

How to use variables with different values for production? #66

Closed
oren opened this issue May 27, 2017 · 4 comments
Closed

How to use variables with different values for production? #66

oren opened this issue May 27, 2017 · 4 comments
Labels

Comments

@oren
Copy link

oren commented May 27, 2017

I am switching to preact-cli from the preact boilerplate and I noticed that there is no webpack configuration file. The only missing feature for me is the ability to change the URL of my server: during development I am accessing localhost:3000 and during production api.my-server.com. With the preact boilerplate I use webpack's DefinePlugin:

new webpack.DefinePlugin({
  'process.env.NODE_ENV': JSON.stringify(ENV),
  'API_HOST': (ENV === 'production') ? JSON.stringify('https://api.my-server.com') : JSON.stringify('http://localhost:3000')
}),

(Here is the code as part of the entire project)

How do I achieve the same capability with preact-cli? I don't mind not using webpack at all. I know it can be done with grep and sed but that might not work on windows machines.

Thanks!

@developit
Copy link
Member

developit commented May 29, 2017

Hiya - you can use process.env.NODE_ENV, it'll be "development" or "production" just like in your setup:
https://github.com/developit/preact-cli/blob/master/src/lib/webpack-config.js#L245

const API_HOST = process.env.NODE_ENV === 'production' ? 'https://api.my-server.com' : 'http://localhost:3000';

Because of the way Webpack + Uglify work, this will actually get statically compiled out in production, so the above code will turn into simply:

const API_HOST = 'https://api.my-server.com';

@oren
Copy link
Author

oren commented May 29, 2017

what setup? currently the cli doesn't have webpack config. I am afraid to copy my entire webpack config just because of this need. can i have empty webpack config that only have those lines?

@developit
Copy link
Member

developit commented May 29, 2017

In the CLI's existing internal webpack config, this is already in place. You can just use the value in you code like I showed above, it already works. This shouldn't require it, but what you're referring to is being implemented in #56 :)

@oren
Copy link
Author

oren commented May 29, 2017

awesome. thanks

@oren oren closed this as completed May 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants