-
-
Notifications
You must be signed in to change notification settings - Fork 210
refactor: improved loading algorithm of options and configs #234
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
Conversation
549488b
to
7acce91
Compare
/cc @michael-ciniawsky friendly ping |
package.json
Outdated
@@ -33,6 +33,7 @@ | |||
"docs": "jsdoc2md lib/index.js > LOADER.md", | |||
"pretest": "npm run lint && npm run test:build", | |||
"test": "jest", | |||
"test-only": "npm run test:build && jest", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this ? 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky when i write code i want run only test to avoid regression (without lint), full test with lint and test i do before PR 😄
lib/index.js
Outdated
to: file, | ||
from: file, | ||
map: sourceMap | ||
? sourceMap === 'inline' | ||
? { inline: true, annotation: false } | ||
: { inline: false, annotation: false } | ||
: false | ||
}, config.options) | ||
}, options, config.options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will options
from webpack.config.js
e.g
{
test: /\.css$/,
use: [ { loader: 'postcss-loader', options: { parser: 'sugarss' } ]
}
be assigned when postcss.config.js
is present ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky using options
allow change map
, it is bad, for some it is misleading behavior, but to
and from
now also we can change through config.options
. I think about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using options allow change map, it is bad, for some it is misleading behavior
? 🙃 You mean that currently one has to set map
via { sourceMap: true || 'inline' }
(webpack.config.js
) exclusively?
but to and from now also we can change through config.options
They shouldn't be changeable, since File I/O is handled by the webpack (Runner) 😛
lib/index.js
Outdated
|
||
return postcssrc(rc.ctx, rc.path, { argv: false }) | ||
}).then((config) => { | ||
if (!config) config = {} | ||
|
||
if (config.file) this.addDependency(config.file) | ||
|
||
const sourceMap = options.sourceMap | ||
delete options.sourceMap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This caused problems on 'multiple' runs for some weird reason, but maybe because it was defined in the wrong place, not sure here tbh ¯_(ツ)_/¯. It work for the first loader run and was then missing for all further runs, which lead to const sourceMap
being always undefined
. Besides that delete options.sourceMap
=> options.sourceMap = null
(perf) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky weird, should works, I will test in the near future
lib/index.js
Outdated
}, | ||
options: {} | ||
Promise.resolve().then(() => { | ||
if (!options.config || (options.config && !options.config.path)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes to config loading behaviour ? See comment below where options
get reassigned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky we don't load postcss config file when config
not present or present but without path
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postcss-load-config
has it's config lookup algo backed in via cosmiconfig, options.config.path
should only be used for cases like
|– .config
| |– postcss.config.js
|– src
| |– style.css
|
|– webpack.config.js
The current trigger for loading postcss.config.js
is if no other options are set in webpack.config.js
=>postcss.config.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky current behavior not works as expected we should fix this, see #234 (comment) (point 2).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky two solution:
- Load
postcss
config always and them merge option from loader with config options (i think loader options more important thanpostcss
config option, but ). - Load
postcss
config only whenconfig: true
orconfig: { path: '/path/to/config'}
(best way, no problems with perf and understandable using)
e794dab
to
bce5503
Compare
@michael-ciniawsky we should finish and merge this PR asap, i spend about 5 hours for migrate to new postcss loader in most of my projects, in half the configuration is not loaded or source maps not working, current loading algorithm is misleading and incorrect. My solution:
This may seem small breaking changed, but it is bugfix, because now postcss config don't loading in most use case. |
fab9590
to
62e2a2b
Compare
let options = Object.assign({ | ||
|
||
// Disable override `to` option | ||
if (config.options.to) delete config.options.to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disable override important options from postcss config. it is break build.
options.parser = params.parser | ||
options.syntax = params.syntax | ||
options.stringifier = params.stringifier | ||
const options = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always have params as object, because we have const options = loaderUtils.getOptions(this) || {}
lib/index.js
Outdated
// Read options from options.config only when options.config is object | ||
if (typeof options.config === 'object' && | ||
options.config !== null && | ||
Object.prototype.toString.call(options.config) === '[object Object]' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Special for config: true
62e2a2b
to
96132e3
Compare
parser: params.parser, | ||
syntax: params.syntax, | ||
stringifier: params.stringifier, | ||
map: params.sourceMap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postcss-load-config
return map
, we should also return map
from parseOptions
(consistently and don't misleading for future options).
c8848bd
to
75965c0
Compare
|
||
return config | ||
}) | ||
}).then((config) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config
is always object parseOptions
return object and postcss-load-config
also return object
I'm not in favour of changing the config loader behaviour to
👍
What kind of options besides |
Why now algorithm don't work as expected:
I want to use
I want to use config (absolutly,
Seems works as expected.
I want to use config and have source map, also i have
I can't change this in many boilerplate (many of popular them use this option also in config). It is using for all loader options from config. config.js module.exports = {
// ...
sourceMap: false,
// ...
}; And in
|
The current loading algo is crap for sure, but still the behaviour shouldn't change :) ❌ postcss.config.js module.exports = ({ file, options, env }) => ({
// Maybe support options for `map` (PostCSS),
// but `true` || `inline` handles that fine for the 99% cases,
// webpack also sets specific requirements here
sourceMap: options.sourceMap
}); The reason for not supporting webpack.config.js {
test: '..',
use: [
{ loader: 'style-loader', options: { sourceMap: env === 'development' ? true : false} },
{ loader: 'css-loader', options: { sourceMap: env === 'development' ? true : false } },
// { loader: 'postcss-loader', options: { sourceMap: env === 'development' ? true : false } },
// Usage worst case scenario
{
loader: 'postcss-loader',
options: { config: { ctx: { sourceMap: env === 'development' ? true : false } } }
},
{ loader: 'sass-loader', options: { sourceMap: env === 'development' ? true : false } }
]
} All we need is to find a better way to filter the loader options and either parse the options, if we have additional ones ( |
@michael-ciniawsky |
@TrySound Maybe you can help us and with this? |
@michael-ciniawsky now using |
@michael-ciniawsky also adding any options to |
@evilebottnawi Please reduce the PR here only to your proposed |
@michael-ciniawsky this PR contain only relevant changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does the sourceMap
config in postcss.config.js
look like ? A small example please
if (options.config.path) { | ||
rc.path = path.resolve(options.config.path) | ||
Promise.resolve().then(() => { | ||
if (options.exec || options.parser || options.syntax || options.stringifier || options.plugins) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky why? your ugly algo is don't work as expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
|
||
if (options.config.ctx) { | ||
rc.ctx.options = options.config.ctx | ||
const rc = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky we don't need rc if we don't load config
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -32,6 +32,7 @@ | |||
"docs": "jsdoc2md lib/index.js > LOADER.md", | |||
"pretest": "npm run lint && npm run test:build", | |||
"test": "jest", | |||
"test-only": "npm run test:build && jest && npm run clean", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky How can I run only tests?
// Disable override `map` option | ||
if (config.options.map) delete config.options.map | ||
|
||
let postcssOption = Object.assign({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this renamed? Should be options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky because options
related to loader
(const options = loaderUtils.getOptions(this) || {}
), postcssOptions
related to postcss
options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works atm due to lexical scope, as long as there is no need to rename it please leave it as is
} | ||
|
||
return postcssrc(rc.ctx, rc.path, { argv: false }) | ||
}).then((config) => { | ||
if (!config) config = {} | ||
.then((config) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong ident
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michael-ciniawsky don't understand, please provide example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code indentation is off :), at least looks it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return postcssrc(rc.ctx, rc.path, { argv: false })
.then((config) => {
// Get `sourceMap` option from loader options when no `map` option in `postcss.config.js`
if (!config.options.map) config.options.map = options.sourceMap
return config
})
Where?
@michael-ciniawsky |
But that's not the only two options for |
@michael-ciniawsky Do your have specific examples of why this code is not working (example of config or failed tests), all tests are passed, or is this a subjective non-constructive mono solution? |
531041a
to
22ed94f
Compare
You mean the config resolving here ? |
@michael-ciniawsky i mean where algo don't work as expected |
@michael-ciniawsky #259 Thanks for respect my work and my time. |
const length = Object.keys(options)
.filter((option) => {
// if (option === 'exec') return
if (option === 'config') return
if (option === 'sourceMap') return
return option
})
.length
if (length) {
return parseOptions.call(this, options)
}
const rc = {
path: path.dirname(file),
ctx: {
file: {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file)
},
options: {}
}
}
if (options.config) {
if (options.config.path) {
rc.path = path.resolve(options.config.path)
}
if (options.config.ctx) {
rc.ctx.options = options.config.ctx
}
}
return postcssrc(rc.ctx, rc.path, { argv: false }) You can also take that and I close #259, it was just as another approach to test and discuss for now https://github.com/postcss/postcss-loader/blob/master/lib/index.js#L99 postcss.config.js module.exports = (ctx) => ({
map: { inline: true, annotation: false }
}) Will this work ? |
Why are you closing this now? 🙃 |
@michael-ciniawsky because I'm tired of non constructive argument and ignoring, for me it will be faster to create fork.
No, because your say:
|
What do you mean by non-constructive argument and ignoring ? Ignoring is just plain wrong... Constrcutive or not is subjective, but
let options = Object.assign({
to: file,
from: file,
map: sourceMap // <=
? sourceMap === 'inline'
? { inline: true, annotation: false }
: { inline: false, annotation: false }
: false
}, config.options) This would require additional changes to work with all possible options |
Because i do only
Just don't ignore #259 and merge PR to solve issue |
kk, can we please in general agree to take it a bit more relaxed and avoid the childish behaviour ?
When I closed the PR is still changed the loading behaviour, that was the reason
Yes, that's the two ways to handle sourcemaps,
I answered all of them and the second isn't even addressed at me 🙃
I hearing this for the third+ time now, what about other people's time ? This is just trying to personify matters to passive aggressively offend && push others to do what you want? As I said I don't mind closing my PR's, waste my time in your speaking and in general the least thing I intend to do is to actively disrespect others. Which is disagree I did in anyway, but if you felt that way I apologize |
@michael-ciniawsky thanks for apologize, let's first fix unexpected behavior with sourceMap #259 |
@michael-ciniawsky and then solve problems: should we respect map from |
Yes, but we need to add a check / refactor https://github.com/postcss/postcss-loader/blob/master/lib/index.js#L99 properly to support 'full' PostCSS Sourcemap Options |
Also check if some possible PostCSS Sourcemap Options doesn't play well with webpack (I don't know 💯 yet) |
Honestly, I have no idea why this fixes the glitch. But read about it here: webpack-contrib/postcss-loader#234 (comment) 6 And it worked. :|
Honestly, I have no idea why this fixes the glitch. But read about it here: webpack-contrib/postcss-loader#234 (comment) 6 And it worked. :|
Honestly, I have no idea why this fixes the glitch. But read about it here: webpack-contrib/postcss-loader#234 (comment) 6 And it worked. :|
Why?
config.path
, now it is not working as expected if i passconfig.path
andsourceMap: false
my config file not loading ->options.config && !sourceMap && length > 1
(i want to load config and setsourceMap
use loader options ), also sometimes i want setup some option from options loader.rc
object because it is not necessary if i use onlyoptions
withoutoptions.config
)postcss
throwloader.options
, now it is possibly only usingpostcss
config.