Skip to content

Add capability to pass other options as function #59

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 1 commit into from
Apr 30, 2016

Conversation

jescalan
Copy link
Contributor

In its current implementation, parser, syntax, and stringifier can only be passed as string which are required internally. This patch allows users to pass in any of these options as a function instead of a string, which is important to be able to do if the option is passed from elsewhere and not required directly as is done internally.

This patch is made in a manner that is non-breaking, although honestly making a breaking change to the way options are handled would provide a much cleaner syntax and easier user experience. If you're open to breaking changes, I'd be happy to modify this -- my proposal would be to just have the postcss webpack option always ask for an object or function that returns an object. This way we have a flexible data structure that's able to accommodate whatever the user needs to pass, it's expandable for future options, and all the config is in one place instead of spread across querystrings and webpack options. For example:

module.exports = {
  loaders: [...],
  postcss: {
    plugins: [...],
    syntax: someModule
    // and any number of other options
  }
}

syntax, parser, and stringifier can now be returned from an object
in options.postcss and therefore passed as function, not strings
only.
@ai
Copy link
Contributor

ai commented Mar 17, 2016

Yeap, we should update API. I agree that we should support only one syntax.

Let’s change syntax in 2 steps:

  1. Add support for two syntaxes. And print a warning for old one.
  2. After few months remove old syntax.

@ai
Copy link
Contributor

ai commented Mar 17, 2016

Because this changes is very important and we should be cautious, I will accept this PR few hours later (I am in the hangover right now :D).

@jescalan
Copy link
Contributor Author

Awesome! Let me know if there's anything else I can do to help.

There is one drawback here I realized that I feel like should be brought up at least. If we swap over to accepting all syntax options only through the webpack postcss option, using webpack's require decorator will no longer be as effective without the querystring options. I don't ever use this feature personally since my settings are always inside the config file, I'm not sure if anyone does honestly, but it's possible that some others do so it might be worth asking around.

@ai
Copy link
Contributor

ai commented Mar 17, 2016

@Jenius require() query string should overrides config options

@jescalan
Copy link
Contributor Author

Ah yeah I see what you're saying here, disregard the last comment then. Agreed.

@ai
Copy link
Contributor

ai commented Mar 17, 2016

Sorry, I need to spend all my today and tomorrow time for work. I will accept it on weekend.

@ai
Copy link
Contributor

ai commented Mar 20, 2016

I thought about this issue and there is a one problem. All current loaders uses query string for options. Right now we move plugins to separate webpack.config.js only because plugin options can be passed in query string.

But why we need to move all options here?

@jescalan
Copy link
Contributor Author

Sorry I'm not entirely sure I understand, but I tried to make it clear what my use case was at least in the initial description:

This patch allows users to pass in any of these options as a function instead of a string, which is important to be able to do if the option is passed from elsewhere and not required directly as is done internally.

Sorry if I misunderstood!

@ai ai closed this Apr 23, 2016
@jescalan
Copy link
Contributor Author

???

@ai
Copy link
Contributor

ai commented Apr 23, 2016

@Jenius GET parameters is a webpack API. Why we should add extra non-standard way?

@jescalan
Copy link
Contributor Author

So some of the options come in as strings are are require-ed internally. You can see this here. The user in this case has no control over where the modules can be require-ed from. This also makes it impossible to take a library that the user has loaded up and initialized manually and use it along with postcss.

Passing a string and requiring internally limits the loading of any other options to only libraries that can be found directly in node's require pathway and gives the user no options when it comes to customizing the behavior of the loaded module.

I made this PR because I have a use case where this is limiting enough to require a fork -- this is a real issue that affected me, not some craziness I made up.

If you don't want to accept it, that's ok but I want to make sure that you do understand the reasons behind it and the fact that this is an issue which will require myself and other users who need this functionality to rely on a fork indefinitely.

@ai
Copy link
Contributor

ai commented Apr 23, 2016

@Jenius maybe we need to fix require instead of changing a options?

How did other webpack loaders fix this issue?

Can you show me some examples of your problem to understand it better?

@jescalan
Copy link
Contributor Author

Well, webpack has an established way of handling this use case, which is documented here. There are a number of other loaders that use this method to solve the issue.

This is exactly the pattern I submitted in this PR, so I think it should be the right way to handle it.

@ai
Copy link
Contributor

ai commented Apr 23, 2016

And what is your problem exactly?

Sorry for asking so many questions, but API is very important thing. Especially, when you have so bad API basement as in webpack :D.

@jescalan
Copy link
Contributor Author

I'm sorry, I have tried my best to explain why this is necessary a number of times in this issue, and I'm not sure how I can make it more clear. This was my last attempt to explain it. Was there anything that I did a bad job of explaining there that I can try to re-word?

@ai
Copy link
Contributor

ai commented Apr 23, 2016

Sorry, I am not native speaker. Can you just show me a code? Abstract things is always hard for me, I think better with real use case and real code example.

@ai ai reopened this Apr 23, 2016
@MoOx
Copy link
Contributor

MoOx commented Apr 23, 2016

+1 for this PR, but I would keep support for current syntax, and not deprecate it (because it's clearly enough for 99% of users (since nobody raised this issue before).

@jescalan
Copy link
Contributor Author

Yeah, this particular PR does keep the current syntax, just adds the secondary syntax as well.

@jescalan
Copy link
Contributor Author

So for a code example, usage would look like this in a webpack config file:

var autoprefixer = require('autoprefixer')
var customParser = require('./custom_parser')

module.exports = {
  module: {
    loaders: [{ test: /\.css$/, loader: 'postcss' }]
  },
  postcss: { plugins: [autoprefixer], parser: customParser }
}

Hope this helps!

@kylemac
Copy link

kylemac commented Apr 25, 2016

the code in this PR's readme.md commit is also a real world code example, from a month ago

@ai
Copy link
Contributor

ai commented Apr 25, 2016

Thanks for clarification.

I well will accept this PR and release new version, when I will find some stable place (I am in travel). I think on this week.

@ai ai merged commit 0231cba into webpack-contrib:master Apr 30, 2016
opts.stringifier = options.stringifier;
}

if ( params.syntax && !opts.syntax ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I removed && !opts.syntax to allow override syntax in query (somebody may need it)

@ai
Copy link
Contributor

ai commented Apr 30, 2016

Released in 0.9

@jescalan
Copy link
Contributor Author

jescalan commented May 1, 2016

🎉

@ai
Copy link
Contributor

ai commented May 1, 2016

Sorry for long delay 😞

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.

4 participants