Skip to content

Instantly share code, notes, and snippets.

@Lacka90
Last active July 21, 2018 10:16
Show Gist options
  • Save Lacka90/c8d8488bd161090ea4aa03ecd6c5f5ed to your computer and use it in GitHub Desktop.
Save Lacka90/c8d8488bd161090ea4aa03ecd6c5f5ed to your computer and use it in GitHub Desktop.
Webpack Frontend Dotenv loader
<p>
Convict: {{ convict | json }}
Dotenv: {{ dotenv | json }}
</p>
import { Component } from '@angular/core';
import { convict, dotenv } from './config';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
convict = convict;
dotenv = dotenv;
}
import * as loadedConvict from '!val-loader!./convict-loader';
import * as loadedDotenv from '!val-loader!./dotenv-loader';
export interface IConvict {
env: 'production' | 'development' | 'test' | 'local' | 'stage';
api: string;
}
export interface IDotenv {
test: string;
}
export const convict = loadedConvict as IConvict;
export const dotenv = loadedDotenv as IDotenv;
const convict = require('convict');
const dotenv = require('dotenv');
dotenv.config();
const config = convict({
env: {
doc: 'The applicaton environment.',
format: ['production', 'development', 'test', 'local', 'stage'],
default: 'local',
env: 'NODE_ENV',
},
api: {
doc: 'The applicaton api.',
format: String,
default: 'localhost',
env: 'API',
},
});
// Perform validation
config.validate({ allowed: 'strict' });
module.exports = () => {
return { code: 'module.exports = ' + JSON.stringify(config.getProperties()) };
};
const env = {
test: process.env.TEST || 'default_Test2',
};
module.exports = () => {
return { code: 'module.exports = ' + JSON.stringify(env) };
};
{
...
"compilerOptions": {
...
"typeRoots": [
"typings.d.ts", <- add custom types
"node_modules/@types"
],
...
}
}
declare module '!val-loader!*' {
const contents: any;
export = contents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment