-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
feat: add webpack v2 support #226
Conversation
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.
@jwhitmarsh , thank you very much for working on this. I have some comments on the implementation. Could you please update the code? Thank you again!
src/special/webpack.js
Outdated
|
||
return getLoaders(deps, lodash.flatten(mappedLoaders)); | ||
} catch (err) { | ||
return console.error(err); |
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.
This could pollute the logs. I am sorry that, we don't have a nice way to control the logs (maybe debug package). But now, could better just skip it silently.
src/special/webpack.js
Outdated
const postLoaders = getLoaders(deps, module.postLoaders); | ||
return loaders.concat(preLoaders).concat(postLoaders); | ||
const webpack1Loaders = parseWebpack1(filepath, deps); | ||
const webpack2Loaders = parseWebpack2(filepath, deps); |
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.
Why not pass module
for parseWebpack1/2
? I only see one usage on filepath
variable.
src/special/webpack.js
Outdated
.map((rule) => { | ||
const key = rule.use ? 'use' : 'loader'; | ||
|
||
// map simple strings as { loader: string } |
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.
Instead of having three if
clauses, I could suggest to convert the first two cases to Array
, then handle it with the third case logic.
src/special/webpack.js
Outdated
return module.rules | ||
.filter(rule => rule.loaders) | ||
.map(rule => rule.loaders | ||
.map(loader => ({ |
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.
Could better to inline:
.map(rule => rule.loaders.map(loader => ({ loader })));
src/special/webpack.js
Outdated
return []; | ||
} | ||
|
||
const mappedLoaders = []; |
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.
Directly construct mappedLoaders
with [...mapRuleLoader(), ...mapRuleUse()]
.
src/special/webpack.js
Outdated
mappedLoaders.push(...mapRuleLoaders(module)); | ||
mappedLoaders.push(...mapRuleUse(module)); | ||
|
||
return getLoaders(deps, lodash.flatten(mappedLoaders)); |
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.
Is this lodash.flatten
necessary? I think you have good structure at this moment.
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.
It is - mapRuleUse
can return an array of arrays because use
or loader
can be an array. Eg:
module: {
rules: [
{ test: /\.css$/, loader: [{ loader: 'style-loader' }] },
],
},
Rather than test the type of each loader
or use
property it's easier to just flatten the whole lot.
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've made the rest of the changes - thank you for your excellent feedback! I'll wait until we've worked out what's going on here ^ before resubmitting the PR.
Codecov Report
@@ Coverage Diff @@
## master #226 +/- ##
==========================================
+ Coverage 98.15% 98.43% +0.28%
==========================================
Files 25 25
Lines 433 447 +14
==========================================
+ Hits 425 440 +15
+ Misses 8 7 -1
Continue to review full report at Codecov.
|
src/special/webpack.js
Outdated
}))); | ||
.map(rule => rule.loaders.map(loader => ({ | ||
loader, | ||
}))); |
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.
Sorry, I did change this but it looks like it's auto formatted back to this. I'll change it again.
Apologies - I forgot that pushing to the branch would auto trigger this update (I thought i'd have to manually re-submit the PR). Not sure why codecov is now failing? |
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.
Overall looks good, left some small feedback.
src/special/webpack.js
Outdated
@@ -1,12 +1,16 @@ | |||
/* eslint-disable no-console */ |
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 don't think we need this disable rule anymore.
src/special/webpack.js
Outdated
return []; | ||
} | ||
|
||
return getLoaders(deps, lodash.flatten([...mapRuleLoaders(module), ...mapRuleUse(module)])); |
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.
Directly return a complicated computation expression is not a good idea. It will make it hard to debug (inspect the return value). Split it to some small steps, it could make debugging easier.
src/special/webpack.js
Outdated
import path from 'path'; | ||
import lodash from 'lodash'; | ||
|
||
const webpackConfigRegex = /webpack(\..+)?\.config\.(babel\.)?js/; | ||
const loaderTemplates = ['*-webpack-loader', '*-web-loader', '*-loader', '*']; | ||
|
||
function extractLoaders(item) { | ||
if (item.loader) { | ||
if (item.loader && typeof item.loader === 'string') { |
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 am thinking about if we can check item
is a string, then directly return it. If that is possible, we don't need to have .map(loader => ({ lodaer }))
src/special/webpack.js
Outdated
const key = rule.use ? 'use' : 'loader'; | ||
const coercedArray = [].concat(rule[key]); | ||
|
||
// if it's an array, apply the two rules above for each element |
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 comment has been outdated.
src/special/webpack.js
Outdated
} | ||
|
||
return { | ||
loader: value, |
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.
If we check typeof item === 'string'
in extractLoaders
, this could be simplify too.
Thank you @jwhitmarsh for updating the PR. The codecov is failing because the coverage is dropping. It looks like there are two cases not covered in your test case. |
I've made the changes you suggested and updated the tests to the coverage is back up to 100% (for webpack.js). If everything is good with this, let me know if you'd like me to rebase & squash it into one commit. |
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.
Thank you! When apply git merge, I will squash the commits. That does not matter.
src/special/webpack.js
Outdated
function mapRuleLoaders(module) { | ||
return module.rules | ||
.filter(rule => rule.loaders) | ||
.map(rule => rule.loaders.map(loader => loader)); |
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.
Sorry, why do we still need .map(loader => loader)
?
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.
Sorry @lijunle, that was dumb of me :(!
src/special/webpack.js
Outdated
// filter use or loader because 'loader' is a shortcut to 'use' | ||
.filter(rule => rule.use || rule.loader) | ||
// return coerced array, using the relevant key | ||
.map(rule => [].concat(rule[rule.use ? 'use' : 'loader'])); |
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.
[].concat(rule.use || rule.loader)
Thank you @ jwhitmarsh ! It is merged now. Do you want a new version for this? |
@lijunle It's entirely up to you - i've got a few other PR's coming your way shortly, but not sure exactly how long they will take. |
OK, I will publish it on this Saturday. If you have new ideas, please ping
me in this time slot.
2017年10月12日 16:15,"James" <notifications@github.com>写道:
… @lijunle <https://github.com/lijunle> It's entirely up to you - i've got
a few other PR's coming your way shortly, but not sure exactly how long
they will take.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#226 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABPIdO8NSjaWZE-tgfGg9hCs0KrPZotVks5srcq0gaJpZM4Pz6vk>
.
|
@lijunle was this published to npm? the version I've got from latest doesn't have the webpack2 support |
I've updated the
special/webpack.js
file to parse new webpack v2 (and greater) config better, and added some tests to support the new functionality.