Polymer / pwa-starter-kit

Starter templates for building full-featured Progressive Web Apps from web components.
https://pwa-starter-kit.polymer-project.org
2.36k stars 431 forks source link

[feature request] Add some system of environment variables #307

Closed abdonrd closed 4 years ago

abdonrd commented 5 years ago

Every project needs variables depending on the environment: production, staging, development, etc.

For example, the URL of the API, the Analytics tracking ID, etc.

abdonrd commented 5 years ago

Related:

synopticum commented 5 years ago

A simple temporary workaround from my side:

  1. Create env.js file in the root folder with this content:
    
    const fs = require('fs');
    const env = process.env['uenv'];

fs.writeFileSync('./environments/current.js', window.ENV = '${env}';, 'utf8');

2. Create `/environments` folder with two files: `environments.js` and `current.js`. Place your environment configuration in the first one, like this:

export const ENV = { dev: { static: 'http://localhost:8081', api: 'http://localhost:3000' },

prod: {
    static: 'http://prod:5000',
    api: 'http://prod:3000'
}

};

3. Modify `scripts` section in your package.json:

... "scripts": { "start": "uenv=dev node ./env.js && polymer serve", "build:static": "uenv=dev node ./env.js && polymer build", ... }, ...


4. `import { ENV } from '../environments/environments';` to your component.

Now you're able to use env variables, e.g.: `ENV[window.ENV].api`.
stale[bot] commented 4 years ago

This project is no longer under development and will be transitioning to a read-only repo. Thank you for your contributions.