-
-
Notifications
You must be signed in to change notification settings - Fork 194
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
feat: support for receiving a {Function}
(options.modifyVars
)
#230
Conversation
Codecov Report
@@ Coverage Diff @@
## master #230 +/- ##
==========================================
+ Coverage 97.84% 97.89% +0.04%
==========================================
Files 8 8
Lines 93 95 +2
Branches 9 10 +1
==========================================
+ Hits 91 93 +2
Misses 2 2
Continue to review full report at Codecov.
|
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.
👍 So far :)
@@ -34,6 +34,10 @@ function getOptions(loaderContext) { | |||
} | |||
} | |||
|
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 (typeof options.modifyVars === 'string') {
try {
loaderContext.addDependency(options.modifyVars);
// Load variables from external file
options.modifyVars = require(options.modifyVars);
} catch (err) {
loaderContext.emitError(new Error(`Less Loader (modifyVars)\n\n${err.message}`));
}
}
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.
require
will be cached and the reload will not work
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.
delete require.cache[options.modifyVars]
(before ... = require(options.modifyVars)
)
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 (typeof options.modifyVars === 'string') {
try {
loaderContext.addDependency(options.modifyVars);
// Clean the variables cache (require.cache)
delete require.cache(options.modifyVars);
// Load variables from external file
options.modifyVars = require(options.modifyVars);
} catch (err) {
loaderContext.emitError(new Error(`Less Loader (modifyVars)\n\n${err.message}`));
}
}
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 you don't want to include this addition in your PR it's also fine to leave as is :)
It's just a suggestion for support of modifyVars: path.resolve(__dirname, 'path/to/vars.json')
{String}
in webpack.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.
good suggestion😀
test/index.test.js
Outdated
let res = false; | ||
const loaderOptions = { | ||
modifyVars: (loader) => { | ||
res = typeof loader === '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.
Could this actually test a small .less
file with some vars ? Can you confirm it works correctly in a real world project ?
{Function}
(options.modifyVars
)
@michael-ciniawsky I updated the test method and made an example repo. |
}, | ||
}; | ||
|
||
let inspect; |
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.
\n
test('should support receiving a function for options.modifyVars', async () => { | ||
const loaderOptions = { | ||
modifyVars: (loader) => { | ||
expect(loader).toBeInstanceOf(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.
\n
}); | ||
|
||
await compile('options-modifyvars-function', rules) | ||
.catch(e => e); |
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.
e
=> err
&& \n
|
||
await compile('options-modifyvars-function', rules) | ||
.catch(e => e); | ||
const [css] = inspect.arguments; |
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.
\n
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.
Review comments aside, features for both Sass & Less loaders have to be approved by @jhnns per our agreement prior to him starting work on his dissertation
Does this PR need more modifications? I'm very keen on trying out this feature |
Is this likely to land soon? |
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.
@stackjie Thanks for your PR 🎉
Can you elaborate a use case where this feature is useful? Why is the current option not enough?
Sorry for the delay btw 😞
@@ -34,6 +34,10 @@ function getOptions(loaderContext) { | |||
} | |||
} | |||
|
|||
if (typeof options.modifyVars === 'function') { | |||
options.modifyVars = options.modifyVars(loaderContext); |
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.
What's the idea behind passing the loaderContext
to modifyVars
? I'm trying to avoid these kind of extra features that make it hard to migrate/change anything later since we already exposed it to the outside.
@jhnns issues #229 |
So you basically want to hot reload upon config changes which is really hard/almost impossible to do in a clean way. This is also not possible with other parts of the "scripts": {
"build-watch": "nodemon npx webpack --config path/to/config"
} This restarts the process as soon as you change the config. Not exactly hot reloading, but still some kind of watch mechanism. Does that help you? |
@jhnns Thank you so much for your suggestion, my problem has been solved, this PR can be closed.😁 |
I'm working on a plugin to set up Ant Design for create-react-app, and I've been trying to see if I can solve this issue about auto-reloading the I'm loading the vars from a Less or JSON file, so it would be awesome if I could force I've just tried this PR, and I'm able to pass
Changing this
That's a good point, but it would be really nice if it was possible. It doesn't seem too difficult in theory, and this PR would make it a bit easier. I think I'm at a dead-end though, so I'll just add the |
No description provided.