Skip to content

New API #128

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

Merged
merged 17 commits into from
Mar 6, 2017
Merged

New API #128

merged 17 commits into from
Mar 6, 2017

Conversation

andywer
Copy link
Owner

@andywer andywer commented Mar 2, 2017

Introduces the new core API. Resolves #125.

Still using module.loaders and webpack-merge's smart merge, though.

@aaronjensen

@andywer
Copy link
Owner Author

andywer commented Mar 2, 2017

@jvanbruegge @eXon Feel free to review as well 😉

})
return (context, util) => util.addLoader(
Object.assign({
test: context.fileType('text/css'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with sass too?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so, but never tried it to be honest... Good point 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, use a post hook to query all loaders with style loader and use that mime type

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a follow-up issue for this: #130.

@@ -44,6 +42,7 @@ function typescript (options) {

function pre (context) {
const registeredTypes = context.fileType.all()

if (!('application/x-typescript' in registeredTypes)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would remove this pre hook, as it is now in the default mime types and the 2.0 update is a breaking one, so you have to update

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

})
}
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add group to this list

t.deepEqual(block(context), {})
t.deepEqual(block.post(context).module.loaders, [
t.deepEqual(block(context, { addLoader })({}), {})
t.deepEqual(block.post(context, { addLoader })({}), {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addLoader is a pure function, so it's usually a better idea to include/use the real one and do state based testing rather than setting it up as a spy. In general, I'd consider pushing towards doing snapshot testing for blocks, but in the meantime at least using the real utility functions would make these tests more valuable imo.

Also, I'm not entirely convinced that unit testing blocks is worthwhile. You get more bang for the buck by actually testing them in createConfig, I think.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have got mixed feelings about unit testing the blocks, too. Integration tests are definitely worth more. But I didn't want to open another construction site right now...

The reason why I didn't use the real helpers was just that the unit test would then not only test this unit, but also the utils. Also using a spy makes testing quite easy.

But yeah, we should consider turning the block unit tests into integration tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I didn't use the real helpers was just that the unit test would then not only test this unit, but also the utils. Also using a spy makes testing quite easy.

Right, but it's misusing spies imo. Spies should only be used for side effecting things. These are pure functions. It's ok to "test" them, because what you're really testing is that they do what you think they're doing in this specific context. I wouldn't even consider it an integration test. You shouldn't have things that return values be spies, it's like replacing merge with a spy, or mapValues.

I don't think there's anything in webpack-blocks that warrants testing w/ sinon, there are no side effects. It's all pure functions which is part of its beauty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, this is a good video about spies vs mocks vs not using them https://www.youtube.com/watch?v=URSWYvyc42M

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't even consider it an integration test.

It's not an integration test, right now it's a unit test. But maybe we talk past each other here. It will probably be best to turn it into an integration test, using it in createConfig() and look at the resulting config.

Agree? Might do it in another PR, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and it would still be a unit test even if it used the real util functions. That was my point. I'd urge you to remove sinon as a dependency and do that instead (another pull is fine) or port to a test that uses createConfig (and still remove sinon). I don't think sinon is a good fit for functional projects and I wouldn't want to encourage contributors to misuse it.

"babel": {
"plugins": [
[
"transform-object-rest-spread",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so, too. But it seems quite a lot of features that once were part of the es2017 preset have been removed from it recently. Object-spread isn't included anymore... :-/

https://babeljs.io/docs/plugins/preset-es2017/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed, it's in experimental.

const configPartial = setter(context, getCompleteConfig(mergedConfig))
return merge.smart(mergedConfig, configPartial)
(config, setter) => {
const updateFunction = setter(context, helpers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helpers -> util

// Now apply them to the config: Remove the existing loaders for that
// file type, add the new loader and add the plugin

return _addPlugin(_addLoader(_removeLoaders(prevConfig)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good place to use group

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it isn't a group of blocks, it's just three update functions... I already thought about util.aggregate([ _removeLoaders, ... ]) or something like that, but I'm hesitating adding more new stuff to util.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned flow before, it's a lodash function. It's what you need here (though you probably don't need to pull it in from lodash unless you want to). And I'd say it's a must have. It's a key part of function composition which is what this api should drive people to.

I prefer my implementation of flow because it flattens and removes nils allowing you to compose like this:

flow(
  option && doThing(),
  option2 && [doThing1(), doThing2],
  doOtherThing(),
)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that might be a candidate to be included in util...

@aaronjensen aaronjensen mentioned this pull request Mar 3, 2017
@andywer
Copy link
Owner Author

andywer commented Mar 6, 2017

So as far as I can see there are no immediate issues left to fix for this PR.

One topic that might be considered part of this PR is:
Should we add a flow() helper as pointed out by @aaronjensen?

One topic that relates to this PR, but can be settled later:
Should the built-in utility functions be dropped and moved to a separate package? (#125)

@andywer andywer merged commit dc3f5b3 into release/2.0 Mar 6, 2017
@andywer andywer deleted the feature/new-api branch March 6, 2017 21:31
@andywer
Copy link
Owner Author

andywer commented Mar 6, 2017

Merged it, since the open topics don't block this PR.

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.

3 participants