-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix: add environment configuration replacement #1364
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6c7ce30
fix: add environment configuration replacement
TheLarkInn d8f399f
remove debuggers
TheLarkInn 17a36b8
fix: move mobile test above config test because it relies on prod bui…
TheLarkInn a2de51f
fix: removed unneeded interface moved config paths
TheLarkInn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as fs from 'fs'; | ||
|
||
interface WebpackPlugin { | ||
apply(compiler: any): void; | ||
} | ||
|
||
interface NgCliEnvrionmentConfig { | ||
env?: string; | ||
file?: string; | ||
alias?: string; | ||
} | ||
|
||
export class NgCliEnvironmentPlugin implements WebpackPlugin { | ||
_file: string; | ||
_alias: string; | ||
_env: string; | ||
|
||
constructor(config: any) { | ||
if (typeof config === 'string') { | ||
config = {env: config}; | ||
} | ||
if (typeof config.env !== 'string') { | ||
throw new Error('must provide env') | ||
} | ||
const ALIAS = { | ||
'"dev"': 'dev', | ||
'development': 'dev', | ||
'"development"': 'dev', | ||
'"prod"': 'prod', | ||
'production': 'prod', | ||
'"production"': 'prod', | ||
'"test"': 'test', | ||
'testing': 'test', | ||
'"testing"': 'test', | ||
}; | ||
const ENV = config.env.toLowerCase(); | ||
|
||
this._file = config.file || 'environment'; | ||
this._alias = config.alias || ALIAS; | ||
this._env = this._alias[ENV] || ENV; | ||
} | ||
|
||
isEnvFile(file: string): boolean { | ||
return file.indexOf(this._file + '.') !== -1; | ||
} | ||
|
||
replaceFile(file: string): any { | ||
return file | ||
.replace(this._file, this._file + '.' + this._env); | ||
} | ||
|
||
updateResult(result: any): any { | ||
['request', 'userRequest', 'resource'] | ||
.filter((key) => { return result[key]; }) | ||
.forEach((key) => { | ||
result[key] = this.replaceFile(result[key]); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
apply(compiler: any): void { | ||
compiler.plugin('normal-module-factory', (normalModuleFactory: any) => { | ||
normalModuleFactory.plugin('after-resolve', (result, callback) => { | ||
var _resource = result['resource']; | ||
if (!this.isEnvFile(_resource)) { | ||
return callback(null, result); | ||
} | ||
var envFile = this.replaceFile(_resource); | ||
|
||
fs.stat(envFile, (err, stats) => { | ||
if (err || !stats.isFile()) { | ||
var errorText = (!err && stats.isFile()) ? 'Is not a file.' : 'Does not exist.'; | ||
console.log('\nWARNING:\n' + envFile + '\n' + errorText + ' ' + 'Using file\n' + _resource + '\n'); | ||
return callback(null, result); | ||
} | ||
// mutate result | ||
var newResult = this.updateResult(result); | ||
return callback(null, newResult); | ||
}); | ||
|
||
}); | ||
}); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.