-
-
Notifications
You must be signed in to change notification settings - Fork 199
Store externals in an array. This fixes #471 #472
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
84a0dc0
Store externals in an array. This fixes #471
3d6fb55
Ensure a copy of the externals is returned when generating a Webpack …
Lyrkan 13b1780
Use ES6 rest parameters for the addExternals method.
fc1e1f0
Update documentation for Encore.addExternals
76e6a95
Use ES6 syntax for adding externals to be consistent with the new API
4fc6dc3
Include a regex in the tests of addExternals.
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
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
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 |
---|---|---|
|
@@ -940,24 +940,17 @@ describe('WebpackConfig object', () => { | |
it('Adds new externals', () => { | ||
const config = createConfig(); | ||
|
||
expect(config.externals).to.deep.equals({}); | ||
expect(config.externals).to.deep.equals([]); | ||
|
||
config.addExternals({ 'jquery': 'jQuery', 'react': 'react' }); | ||
config.addExternals({ 'lodash': 'lodash' }); | ||
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. Can you update this test by adding, for example, another 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. I'll update this to include a test for a regex. |
||
config.addExternals(/^(jquery|\$)$/i); | ||
|
||
expect(config.externals).to.deep.equals({ | ||
'jquery': 'jQuery', | ||
'react': 'react', | ||
'lodash': 'lodash' | ||
}); | ||
}); | ||
|
||
it('Calling it with an invalid argument', () => { | ||
Lyrkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const config = createConfig(); | ||
|
||
expect(() => { | ||
config.addExternals('foo'); | ||
}).to.throw('must be an object'); | ||
expect(config.externals).to.deep.equals([ | ||
{ 'jquery': 'jQuery', 'react': 'react'}, | ||
{ 'lodash': 'lodash' }, | ||
/^(jquery|\$)$/i | ||
]); | ||
}); | ||
}); | ||
|
||
|
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.
Nitpicking but I'm not sure that rest parameters are needed there since this is not part of the exposed API.
It adds a bit of unneeded complexity to the call in
index.js
(you have to useapply
instead of just doingwebpackConfig.addExternals(externals)
).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.
apply
is not required, it could have been written aswebpackConfig.addExternals(...externals)
using ES6 syntax, but I figuredwebpackConfig.addExternals.apply(webpackConfig, externals)
might be more performant as it simply passes the array as the arguments list instead of possibly building a new one. I debated whether or not to use ES6 rest parameters here as I could have simply used thearguments
array instead, but I was trying to be a little more explicit in the API declaration. From what I can tell, Babble will convert the ES6 syntax toapply
regardless so they should be equally performant. I will change it to the ES6 syntax if you want.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.
Do you mean Babel? If that's the case it's not relevant here since our code don't go through Babel.
Having
...externals
may be a bit easier to understand (and Node probably optimize it... and even if that's not the case it won't be a big overhead anyway :)) but I still don't really see the benefits of using rest parameters here instead of simply passing the array directly.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.
The reason for using rest parameters was to simplify the API and the function itself. By using rest parameters or the
arguments
array, there's no need to check if the parameter is an array in order to perform the concatenation. Users can still pass an array if they want, it will just add the array to the externals list instead of appending each element. Both mechanisms work.I meant Babel, but seeing as how it is not used, it's irrelevant. Nonetheless I would hope Node internally just converts the ES6 syntax to an
apply
call. I will change the code to the ES6 syntax to be consistent with the new API.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.
Hmm, I agree with @Lyrkan. Look at the "mixed" syntax: https://webpack.js.org/configuration/externals/#combining-syntaxes
Basically, when you call
addExternal()
, there should only be 3 valid things that you can pass to it:The current implementation allows you to pass these are multiple arguments for multiple externals. That's nice - but I'd rather just accept 1 argument and check that it's one of these 3 types.
@deAtog Could you make that change? If not - just let us know. Sorry for the delay :)
Uh oh!
There was an error while loading. Please reload this page.
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.
Consider the following:
With my initial patch set this would be functionally equivalent to:
With the change to rest parameters it is functionally equivalent to:
Switching to rest parameters does not invalidate the second example above, it just produces a slightly different, but still valid configuration. Given the name of the function, I tend to prefer the last version as users don't have to remember to pass an array to addExternals function.
I purposely removed the type checking as just about anything can be provided as an external and it would be better to let Webpack report an issue with an external rather than Encore enforce rules which may change in the future. That is after all where this issue originally started.
Also, there are 5 things an external can be:
When it is not one of these 5, Webpack currently ignores it, so I don't know why Encore should produce an error in that case.