-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from serverless-heaven/refactor-configuration
Refactor configuration
- Loading branch information
Showing
9 changed files
with
461 additions
and
186 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
/** | ||
* Plugin configuration. | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
|
||
/** | ||
* Plugin defaults | ||
*/ | ||
const DefaultConfig = { | ||
webpackConfig: 'webpack.config.js', | ||
webpackIncludeModules: false, | ||
packager: 'npm', | ||
packExternalModulesMaxBuffer: 200 * 1024, | ||
config: null | ||
}; | ||
|
||
class Configuration { | ||
|
||
constructor(custom) { | ||
|
||
this._config = {}; | ||
this._hasLegacyConfig = false; | ||
|
||
// Set configuration from sls.service.custom. We fall back to the | ||
// old configuration to keep backwards compatibility. | ||
if (custom) { | ||
if (custom.webpackIncludeModules) { | ||
this._config.webpackIncludeModules = custom.webpackIncludeModules; | ||
this._hasLegacyConfig = true; | ||
} | ||
if (custom.packExternalModulesMaxBuffer) { | ||
this._config.packExternalModulesMaxBuffer = custom.packExternalModulesMaxBuffer; | ||
this._hasLegacyConfig = true; | ||
} | ||
if (_.isString(custom.webpack)) { | ||
this._config.webpackConfig = custom.webpack; | ||
this._hasLegacyConfig = true; | ||
} else { | ||
_.assign(this._config, custom.webpack || {}); | ||
} | ||
} | ||
|
||
// Set defaults for all missing properties | ||
_.defaults(this._config, DefaultConfig); | ||
} | ||
|
||
get webpackConfig() { | ||
return this._config.webpackConfig; | ||
} | ||
|
||
get webpackIncludeModules() { | ||
return this._config.webpackIncludeModules; | ||
} | ||
|
||
get packExternalModulesMaxBuffer() { | ||
return this._config.packExternalModulesMaxBuffer; | ||
} | ||
|
||
get packager() { | ||
return this._config.packager; | ||
} | ||
|
||
get config() { | ||
return this._config.config; | ||
} | ||
|
||
get hasLegacyConfig() { | ||
return this._hasLegacyConfig; | ||
} | ||
|
||
toJSON() { | ||
return _.omitBy(this._config, _.isNil); | ||
} | ||
} | ||
|
||
module.exports = Configuration; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict'; | ||
/** | ||
* Unit tests for Configuration. | ||
*/ | ||
|
||
const chai = require('chai'); | ||
const Configuration = require('./Configuration'); | ||
|
||
const expect = chai.expect; | ||
|
||
describe('Configuration', () => { | ||
describe('defaults', () => { | ||
let expectedDefaults; | ||
|
||
before(() => { | ||
expectedDefaults = { | ||
webpackConfig: 'webpack.config.js', | ||
webpackIncludeModules: false, | ||
packager: 'npm', | ||
packExternalModulesMaxBuffer: 200 * 1024, | ||
config: null | ||
}; | ||
}); | ||
|
||
it('should set default configuration without custom', () => { | ||
const config = new Configuration(); | ||
expect(config).to.have.a.property('_config').that.deep.equals(expectedDefaults); | ||
expect(config).to.have.a.property('hasLegacyConfig').that.is.false; | ||
}); | ||
|
||
it('should set default configuration without webpack property', () => { | ||
const config = new Configuration({}); | ||
expect(config).to.have.a.property('_config').that.deep.equals(expectedDefaults); | ||
expect(config).to.have.a.property('hasLegacyConfig').that.is.false; | ||
}); | ||
}); | ||
|
||
describe('with legacy configuration', () => { | ||
it('should use custom.webpackIncludeModules', () => { | ||
const testCustom = { webpackIncludeModules: { forceInclude: ['mod1'] } }; | ||
const config = new Configuration(testCustom); | ||
expect(config).to.have.a.property('webpackIncludeModules').that.deep.equals(testCustom.webpackIncludeModules); | ||
}); | ||
|
||
it('should use custom.packExternalModulesMaxBuffer', () => { | ||
const testCustom = { packExternalModulesMaxBuffer: 4711 }; | ||
const config = new Configuration(testCustom); | ||
expect(config).to.have.a.property('packExternalModulesMaxBuffer').that.equals(4711); | ||
}); | ||
|
||
it('should use custom.webpack as string', () => { | ||
const testCustom = { webpack: 'myWebpackFile.js' }; | ||
const config = new Configuration(testCustom); | ||
expect(config).to.have.a.property('webpackConfig').that.equals('myWebpackFile.js'); | ||
}); | ||
|
||
it('should detect it', () => { | ||
const testCustom = { webpack: 'myWebpackFile.js' }; | ||
const config = new Configuration(testCustom); | ||
expect(config).to.have.a.property('hasLegacyConfig').that.is.true; | ||
}); | ||
|
||
it('should add defaults', () => { | ||
const testCustom = { | ||
webpackIncludeModules: { forceInclude: ['mod1'] }, | ||
webpack: 'myWebpackFile.js' | ||
}; | ||
const config = new Configuration(testCustom); | ||
expect(config).to.have.a.property('webpackIncludeModules').that.deep.equals(testCustom.webpackIncludeModules); | ||
expect(config._config).to.deep.equal({ | ||
webpackConfig: 'myWebpackFile.js', | ||
webpackIncludeModules: { forceInclude: ['mod1'] }, | ||
packager: 'npm', | ||
packExternalModulesMaxBuffer: 200 * 1024, | ||
config: null | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with a configuration object', () => { | ||
it('should use it and add any defaults', () => { | ||
const testCustom = { | ||
webpack: { | ||
webpackIncludeModules: { forceInclude: ['mod1'] }, | ||
webpackConfig: 'myWebpackFile.js' | ||
} | ||
}; | ||
const config = new Configuration(testCustom); | ||
expect(config._config).to.deep.equal({ | ||
webpackConfig: 'myWebpackFile.js', | ||
webpackIncludeModules: { forceInclude: ['mod1'] }, | ||
packager: 'npm', | ||
packExternalModulesMaxBuffer: 200 * 1024, | ||
config: null | ||
}); | ||
}); | ||
|
||
it('should favor new configuration', () => { | ||
const testCustom = { | ||
webpackIncludeModules: { forceExclude: ['mod2'] }, | ||
webpack: { | ||
webpackIncludeModules: { forceInclude: ['mod1'] }, | ||
webpackConfig: 'myWebpackFile.js' | ||
} | ||
}; | ||
const config = new Configuration(testCustom); | ||
expect(config._config).to.deep.equal({ | ||
webpackConfig: 'myWebpackFile.js', | ||
webpackIncludeModules: { forceInclude: ['mod1'] }, | ||
packager: 'npm', | ||
packExternalModulesMaxBuffer: 200 * 1024, | ||
config: null | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.