-
-
Notifications
You must be signed in to change notification settings - Fork 199
Add Encore.configureRuntimeEnvironment() method to the public API #115
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
Closed
Closed
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -13,20 +13,27 @@ const WebpackConfig = require('./lib/WebpackConfig'); | |
const configGenerator = require('./lib/config-generator'); | ||
const validator = require('./lib/config/validator'); | ||
const PrettyError = require('pretty-error'); | ||
const runtimeConfig = require('./lib/context').runtimeConfig; | ||
const logger = require('./lib/logger'); | ||
const parseRuntime = require('./lib/config/parse-runtime'); | ||
|
||
// at this time, the encore executable should have set the runtimeConfig | ||
if (!runtimeConfig) { | ||
throw new Error('Are you trying to require index.js directly?'); | ||
let webpackConfig = null; | ||
let runtimeConfig = require('./lib/context').runtimeConfig; | ||
|
||
function initializeWebpackConfig() { | ||
if (runtimeConfig.verbose) { | ||
logger.verbose(); | ||
} | ||
|
||
webpackConfig = new WebpackConfig(runtimeConfig); | ||
} | ||
|
||
let webpackConfig = new WebpackConfig(runtimeConfig); | ||
if (runtimeConfig.verbose) { | ||
logger.verbose(); | ||
// If runtimeConfig is already set webpackConfig can directly | ||
// be initialized here. | ||
if (runtimeConfig) { | ||
initializeWebpackConfig(); | ||
} | ||
|
||
module.exports = { | ||
const publicApi = { | ||
/** | ||
* The directory where your files should be output. | ||
* | ||
|
@@ -431,17 +438,9 @@ module.exports = { | |
* @returns {*} | ||
*/ | ||
getWebpackConfig() { | ||
try { | ||
validator(webpackConfig); | ||
|
||
return configGenerator(webpackConfig); | ||
} catch (error) { | ||
// prettifies errors thrown by our library | ||
const pe = new PrettyError(); | ||
validator(webpackConfig); | ||
|
||
console.log(pe.render(error)); | ||
process.exit(1); // eslint-disable-line | ||
} | ||
return configGenerator(webpackConfig); | ||
}, | ||
|
||
/** | ||
|
@@ -454,5 +453,98 @@ module.exports = { | |
*/ | ||
reset() { | ||
webpackConfig = new WebpackConfig(runtimeConfig); | ||
} | ||
}, | ||
|
||
/** | ||
* Initialize the runtime environment. | ||
* | ||
* This can be used to configure the Encore runtime if you're | ||
* using Encore without executing the "./node_module/.bin/encore" | ||
* utility (e.g. with karma-webpack). | ||
* | ||
* Encore.configureRuntimeEnvironment( | ||
* // Environment to use (dev, dev-server, production) | ||
* 'dev-server', | ||
* | ||
* // Same options you would use with the | ||
* // CLI utility with their name in | ||
* // camelCase. | ||
* { | ||
* https: true, | ||
* keepPublicPath: true | ||
* } | ||
* ) | ||
* | ||
* Be aware than using this method will also reset the current | ||
* webpack configuration. | ||
* | ||
* @param {string} environment | ||
* @param {object} options | ||
* @returns {exports} | ||
*/ | ||
configureRuntimeEnvironment(environment, options = {}) { | ||
runtimeConfig = parseRuntime( | ||
Object.assign( | ||
{}, | ||
require('yargs/yargs')([environment]).argv, | ||
options | ||
), | ||
process.cwd() | ||
); | ||
|
||
initializeWebpackConfig(); | ||
|
||
return this; | ||
}, | ||
|
||
/** | ||
* Clear the runtime environment. | ||
* | ||
* Be aware than using this method will also reset the | ||
* current webpack configuration. | ||
* | ||
* @returns {void} | ||
*/ | ||
clearRuntimeEnvironment() { | ||
runtimeConfig = null; | ||
webpackConfig = null; | ||
}, | ||
}; | ||
|
||
// Proxy the API in order to prevent calls to most of its methods | ||
// if the webpackConfig object hasn't been initialized yet. | ||
const publicApiProxy = new Proxy(publicApi, { | ||
get: (target, prop) => { | ||
if (typeof target[prop] === 'function') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't be possible, but what about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I missed that part, I added a |
||
// These methods of the public API can be called even if the | ||
// webpackConfig object hasn't been initialized yet. | ||
const safeMethods = [ | ||
'configureRuntimeEnvironment', | ||
'clearRuntimeEnvironment', | ||
]; | ||
|
||
if (!webpackConfig && (safeMethods.indexOf(prop) === -1)) { | ||
throw new Error(`Encore.${prop}() cannot be called yet because the runtime environment doesn't appear to be configured. Make sure you're using the encore executable or call Encore.configureRuntimeEnvironment() first if you're purposely not calling Encore directly.`); | ||
} | ||
|
||
// Either a safe method has been called or the webpackConfig | ||
// object is already available. In this case act as a passthrough. | ||
return (...parameters) => { | ||
try { | ||
const res = target[prop](...parameters); | ||
return (res === target) ? publicApiProxy : res; | ||
} catch (error) { | ||
// prettifies errors thrown by our library | ||
const pe = new PrettyError(); | ||
|
||
console.log(pe.render(error)); | ||
process.exit(1); // eslint-disable-line | ||
} | ||
}; | ||
} | ||
|
||
return target[prop]; | ||
} | ||
}); | ||
|
||
module.exports = publicApiProxy; |
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.
I'm not sure I understand this. Is there a need to continue to read the command line options? The command line options could be totally different (since there is a different executable being run).
I see that parseRuntime() looks for
argv._[0]
for the environment/command. Is that what you're trying to supply here? If so, I think we could set that manually - e.g.options._ = [environment]
. Or, we could re-workparse-runtime.js
so that theargv._[0]
is actually done inencore.js
andparse-runtime
looked likefunction(command, options, cwd) {
.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.
I added that part to make sure that the first parameter passed to
parseRuntime
had the same structure than the one usually supplied by yargs, it doesn't actually keep reading the CLI options since we provide a fakeprocess.argv
(see https://github.com/yargs/yargs/blob/master/docs/api.md#api).I did
options._ = [environment]
at first but switched to calling the API after realizing thatyargs
adds other things to theargv
array that may be used later byparseRuntime
(e.g.:$0
)