Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Import config/environment in application #89

Open
topaxi opened this issue Apr 17, 2017 · 14 comments
Open

Import config/environment in application #89

topaxi opened this issue Apr 17, 2017 · 14 comments

Comments

@topaxi
Copy link
Contributor

topaxi commented Apr 17, 2017

Is there a way to import config/environment.js in a glimmer application? I haven't found a way yet.

@pittst3r
Copy link
Contributor

@topaxi What are you trying to do? Define your own module config?

@topaxi
Copy link
Contributor Author

topaxi commented Apr 18, 2017

I want a way to define global configuration variables like API endpoints or production / development URLs, disabling Logging in production etc.

I've done these things in Ember.js with the config/environment.js file and as it exists in glimmer applications too, I assumed it would work the same way.

@rmzr7
Copy link

rmzr7 commented Apr 19, 2017

I'm curious about this too

@rootwizzy
Copy link

I also was wondering about the correct way to go about this. I have tried looking this up and the methods used in a standard ember-cli application do not work.

Here is the scenario I think which is same that @topaxi is going for.

Setting up an ENV variable inside the config/envrionment.js where you have access to the node.js process.env grabbing a token saved in your local system.

// app/config/environment.js

'use strict';

module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'foo-app',
    environment: environment,
  };

  ENV.token = process.env['API_TOKEN']

  return ENV;
};

Then wanting to take said ENV variable inside a component inside your app. Normally (in ember-cli) you would simply import your config file via import ENV from 'config/environment' but this doesn't work out of the box in glimmer at the moment.

// app/src/ui/components/foo-component/component.js

import Component, { tracked } from '@glimmer/component';
import ENV from 'config/environemt'

export default class FooComponent extends Component {

  ...

  fooBarToken =  ENV.token

  ...

};

The error thrown has to do with unable to resolve the path

Error: Could not resolve 'config/environment' from /home/$USER/foo-app/tmp/rollup_with_dependencies-cache_path-yRN2N01e.tmp/ui/components/foo-component/component.js
    at Error (native)
    at /home/$USER/foo-app/node_modules/rollup-plugin-node-resolve/dist/rollup-plugin-node-resolve.cjs.js:85:23
...

Adjusting the path to a relative path that points directly to the file throws errors because it is trying to import the node module as an es6 module

// app/src/ui/components/foo-component/component.js
import ENV from '../../../../config/environment'

export default class FooComponent extends Component {
};

throws

Error: 'default' is not exported by config/environment.js
...

I am sure there is probably a way about grabbing these ENV variables without too many band-aids and would appreciate any help on how to go about this as well.

@rmzr7
Copy link

rmzr7 commented Apr 21, 2017

What happens if you enable commonJS module imports as the readme.md for this project says?

@tschoartschi
Copy link
Contributor

tschoartschi commented Sep 4, 2017

I'm also interested in this issue. Does anyone have a working solution?

UPDATE: I created a plugin which tries to include the environment.js like module-map.js and it kind of works but I think the file is created too late this is probably why I get the following error Cannot find module '../config/environment'. during build time. On runtime everything works fine. If this solution goes in the right direction I could provide some code (if the core team needs help).

@artzte
Copy link

artzte commented Nov 5, 2017

I'm not seeing the config/environment.js in the broccoli tree. there is an environment subdirectory with a development.js in it.

@rwjblue
Copy link
Member

rwjblue commented Nov 5, 2017

We should absolutely fix this. It shouldn't be too much work to fix I think...

@tschoartschi
Copy link
Contributor

@artzte if you need a "hack" solution, I can give you my code. @rwjblue would be great to have some "official" solution 😄

@hoodwink73
Copy link

I am using rollup-replace-plugin.

// ember-cli-build.js
  'use strict';

const GlimmerApp = require('@glimmer/application-pipeline').GlimmerApp;
const replace = require('rollup-plugin-replace');

module.exports = function(defaults) {
  let app = new GlimmerApp(defaults, {
    // Add options here
    rollup: {
      plugins: [
        replace({
          ENVIRONMENT: JSON.stringify('process.env.NODE_ENV')
        })
      ]
    }
  });

  return app.toTree();
};

Then using ENVIRONMENT token wherever I need to check against the environment. Is this a good enough hack for now?

@tschoartschi
Copy link
Contributor

as requested by @hoodwink73 on the glimmer slack channel, I created a repo with my hack. You can find it here: https://github.com/tschoartschi/glimmer-env-hack

But it's really a hack. For detailed explanations check the README.md of the repo

@tschoartschi
Copy link
Contributor

@rwjblue there are several "solutions", "suggestions" and "hacks" to this problem. What should be the common approach to use the environment.js? I think it would be great if everyone could rely on a "standard" way to use it.

@chadian
Copy link

chadian commented May 12, 2018

Just wanted to leave a bit from my experiments. It looks like importing is working through some configuration changes.

// ember-cli-build.js
let app = new GlimmerApp(defaults, {
    storeConfigInMeta: true
});
// config.environment.js
module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'my-app', // make sure this is set. The generated meta tag needs it for look up.
    environment,

    // specify your keys
    SOME_KEY: process.env.KEY_VALUE
  };

  return ENV;
};
// in some component, looks like it has to be relative?
// importing from `"my-app/config/environment"` did not work
import env from "../../../../config/environment";

Source:
https://github.com/glimmerjs/glimmer-application-pipeline/blob/master/lib/broccoli/glimmer-app.ts#L517-L540

If this is "official" then I think we could close this, and I could open a PR up for the Glimmer guides. Hope this at least helps the next person to come along. Cheers!

chadian added a commit to chadian/vorfreude that referenced this issue Jul 8, 2018
…nvironment bug

config/environment.js could be used for building (via meta tag) but not for tests.
See: glimmerjs/glimmer-application-pipeline#89
Once this is fixed application environment configuration can be consolidated in one spot.
@givanse
Copy link

givanse commented Aug 21, 2018

Even though there are other solutions in this thread, importing config/environment.js should just work. It also covers the case for importing a library from node_modules.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants