webpack-contrib / postcss-loader

PostCSS loader for webpack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature: Expose webpack entry name to postcss.config.js function

nolimitdev opened this issue · comments

Feature Proposal

module.exports = (api) => {
  // `api.file` - path to the file
  // `api.mode` - `mode` value of webpack, please read https://webpack.js.org/configuration/mode/
  // `api.webpackLoaderContext` - loader context for complex use cases
  // `api.env` - alias `api.mode` for compatibility with `postcss-cli`
  // `api.options` - the `postcssOptions` options
  // + add new `api.entry` - webpack entry name
}

where api.entry comes from webpack.config.js ...

module.exports = (env, options) => {
    return {
        ...
        entry : {
            foo: './resources/foo.js',
            bar: './resources/bar.js',
        },
        ...
    }
}

Feature Use Case

For example when we need some small configuration diff for postcss plugins e.g. for foo entry include some file for importFrom option of https://www.npmjs.com/package/postcss-custom-properties#user-content-importfrom and for entry bar include another file. The same example coud be for autoprefixer, for mixins etc.

I do not know if this feature is possible, if webpack exposes current entry to loaders. In api.webpackLoaderContext I found list of entries but not currently processed entry. Thank you.

It is impossible and should be not used

Anyway you have api.webpackLoaderContext and can do it, but again it is unsafe and should be not used

I write above that I do not see currently processed entry name in api.webpackLoaderContext. Is it really there? I see there just list of all entries.

You can get it using api.webpackLoaderContext._compiler and use the webpack API to get it

Anyway I strongly recommend to avoid it, we have this.resourcePath, there are no different between entry point and module in loader context, each entry point is module, if you need to process only specific files, you can add checks in own configuration and use api.file to get path on the file. Feel free to feedback