Last active
July 21, 2018 10:16
-
-
Save Lacka90/c8d8488bd161090ea4aa03ecd6c5f5ed to your computer and use it in GitHub Desktop.
Webpack Frontend Dotenv loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
Convict: {{ convict | json }} | |
Dotenv: {{ dotenv | json }} | |
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) }; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const env = { | |
test: process.env.TEST || 'default_Test2', | |
}; | |
module.exports = () => { | |
return { code: 'module.exports = ' + JSON.stringify(env) }; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
... | |
"compilerOptions": { | |
... | |
"typeRoots": [ | |
"typings.d.ts", <- add custom types | |
"node_modules/@types" | |
], | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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