postcss / postcss-load-config

Autoload Config for PostCSS
MIT License
645 stars 73 forks source link

Custom plugins examples #17

Closed ai closed 8 years ago

ai commented 8 years ago

We need to add examples, how to set custom plugins in config. Like ./my-plugin.

This config doesn’t work:

module.exports = {
  plugins: {
    './postcss-plugin': 2
  }
}
Module build failed: Error: Cannot find module './postcss-plugin'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at load (/home/ai/Dev/test/node_modules/postcss-load-plugins/lib/loadPlugins.js:21:50)
    at /home/ai/Dev/test/node_modules/postcss-load-plugins/lib/loadPlugins.js:27:18
    at Array.forEach (native)
    at loadPlugins (/home/ai/Dev/test/node_modules/postcss-load-plugins/lib/loadPlugins.js:26:24)
    at /home/ai/Dev/test/node_modules/postcss-load-config/index.js:20:18

This config doesn’t work too:

module.exports = {
  plugins: [
    require('./postcss-plugin')(2)
  ]
}
Module build failed: Error: Cannot find module '0'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at load (/home/ai/Dev/test/node_modules/postcss-load-plugins/lib/loadPlugins.js:21:50)
    at /home/ai/Dev/test/node_modules/postcss-load-plugins/lib/loadPlugins.js:27:18
    at Array.forEach (native)
    at loadPlugins (/home/ai/Dev/test/node_modules/postcss-load-plugins/lib/loadPlugins.js:26:24)
    at /home/ai/Dev/test/node_modules/postcss-load-config/index.js:20:18
michael-ciniawsky commented 8 years ago

yep, the question is if it's not better to 'enforce' one format, since using a function as config is now possible ?

ai commented 8 years ago

Yeap, I think one format is better, than multiple. It is easy to show one format in docs, rather than "use this format for this case, and that format for different".

michael-ciniawsky commented 8 years ago

i will try

modules.exports = {
 parser: ...,
 plugins: [
   require('postcss-plugin')
 ]
}

simply 'passthrough' the array, but it's a potential error pit, if to fragile I leave it for now, anything should be possible with 'standard formatted' config()/config by now :)

ai commented 8 years ago

Array in JS syntax and object in package.js on and JSON config? Looks good.

michael-ciniawsky commented 8 years ago

config.plugins can be an {Object} or an {Array} in JS, .postcssrc (JSON) is an standard format {Object} only during the lack of require(), the array will be checked if plugins are required and returned

michael-ciniawsky commented 8 years ago

When finished is ping you and you can finally approve it before npm publish ? :)

ai commented 8 years ago

@michael-ciniawsky we had some problem with array. I will retest it, when you ping me with 1.0 pre-release.

Sure, ping me before release.

michael-ciniawsky commented 8 years ago

:+1: yep maybe leave it enterily abd curently postcss-load-config v1.0.0-alpha4 there is no array support 😛

michael-ciniawsky commented 8 years ago

Using an Array works know, this should resolve the issue requiring a local plugin (develop, testing) aswell.

module.exports = (ctx) => {
  return {
   parser: 'sugarss',
   plugins: [
     require('./postcss-dev')(ctx.dev)
   ]
 } 
}
ai commented 8 years ago

@michael-ciniawsky I saw that you updated examples. They become much better.

Few extra changes, that I suggest:

  1. Make first example simpler. Most of developers will not use context. Let’s move context example to the bottom of doc.
  2. Split array and object example with explicit “or you can use arrays”
michael-ciniawsky commented 8 years ago

@ai 👍