Skip to content
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

Merged
merged 5 commits into from
Oct 11, 2017

Conversation

jwhitmarsh
Copy link
Contributor

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.

Copy link
Member

@lijunle lijunle left a 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!


return getLoaders(deps, lodash.flatten(mappedLoaders));
} catch (err) {
return console.error(err);
Copy link
Member

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.

const postLoaders = getLoaders(deps, module.postLoaders);
return loaders.concat(preLoaders).concat(postLoaders);
const webpack1Loaders = parseWebpack1(filepath, deps);
const webpack2Loaders = parseWebpack2(filepath, deps);
Copy link
Member

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.

.map((rule) => {
const key = rule.use ? 'use' : 'loader';

// map simple strings as { loader: string }
Copy link
Member

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.

return module.rules
.filter(rule => rule.loaders)
.map(rule => rule.loaders
.map(loader => ({
Copy link
Member

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 })));

return [];
}

const mappedLoaders = [];
Copy link
Member

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()].

mappedLoaders.push(...mapRuleLoaders(module));
mappedLoaders.push(...mapRuleUse(module));

return getLoaders(deps, lodash.flatten(mappedLoaders));
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
Copy link

codecov bot commented Oct 10, 2017

Codecov Report

Merging #226 into master will increase coverage by 0.28%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/special/webpack.js 100% <100%> (+4.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 842959d...5a8113b. Read the comment docs.

})));
.map(rule => rule.loaders.map(loader => ({
loader,
})));
Copy link
Contributor Author

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.

@jwhitmarsh
Copy link
Contributor Author

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?

Copy link
Member

@lijunle lijunle left a 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.

@@ -1,12 +1,16 @@
/* eslint-disable no-console */
Copy link
Member

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.

return [];
}

return getLoaders(deps, lodash.flatten([...mapRuleLoaders(module), ...mapRuleUse(module)]));
Copy link
Member

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.

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') {
Copy link
Member

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 }))

const key = rule.use ? 'use' : 'loader';
const coercedArray = [].concat(rule[key]);

// if it's an array, apply the two rules above for each element
Copy link
Member

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.

}

return {
loader: value,
Copy link
Member

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.

@lijunle
Copy link
Member

lijunle commented Oct 10, 2017

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.

@jwhitmarsh
Copy link
Contributor Author

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.

Copy link
Member

@lijunle lijunle left a 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.

function mapRuleLoaders(module) {
return module.rules
.filter(rule => rule.loaders)
.map(rule => rule.loaders.map(loader => loader));
Copy link
Member

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)?

Copy link
Contributor Author

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 :(!

// 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']));
Copy link
Member

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)

@lijunle lijunle merged commit f8fca26 into depcheck:master Oct 11, 2017
@lijunle
Copy link
Member

lijunle commented Oct 11, 2017

Thank you @ jwhitmarsh ! It is merged now. Do you want a new version for this?

@jwhitmarsh
Copy link
Contributor Author

@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.

@lijunle
Copy link
Member

lijunle commented Oct 12, 2017 via email

@mattfysh
Copy link

@lijunle was this published to npm? the version I've got from latest doesn't have the webpack2 support

@mattfysh mattfysh mentioned this pull request Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants