-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
grafana/toolkit: Add option to override webpack config #20872
grafana/toolkit: Add option to override webpack config #20872
Conversation
f329cd2
to
951833d
Compare
d0ac1a2
to
a98163a
Compare
@Sintifo thanks for this. I'm gonna take a look at this tomorrow morning and come back with some feedback |
@Sintifo my comment is awaiting an answer. This PR may not be the best approach. |
a98163a
to
bd1104f
Compare
@stevenvachon I updated the PR to your suggested approach. This allows the user to implement the Example: import { getWebpackConfig as originalGetter, WebpackConfigurationGetter } from '@grafana/toolkit/src/config'
import WorkerPlugin from 'worker-plugin';
export const getWebpackConfig: WebpackConfigurationGetter = (options) => {
const webpackConfig = originalGetter(options);
console.log('Custom config');
webpackConfig.plugins.push(new WorkerPlugin())
return webpackConfig;
} I am not sure how you generate the index.ts for top-level imports. When trying to build the toolkit I didn't get any in the dist directory. I thought about using dynamic |
Regarding this API:
I think this function should get a default toolkit's webpack config object as an argument so that you don't need |
bd1104f
to
de30f59
Compare
@dprokop import { CustomWebpackConfigurationGetter } from '@grafana/toolkit/src/config'
import WorkerPlugin from 'worker-plugin';
export const getWebpackConfig: CustomWebpackConfigurationGetter = (defaultConfig, options) => {
console.log('Custom config');
defaultConfig.plugins.push(new WorkerPlugin())
return defaultConfig;
} |
What do you mean, the path |
const customWebpackPath = path.resolve(process.cwd(), 'webpack.plugin.config.ts'); | ||
if (fs.existsSync(customWebpackPath)) { | ||
const customConfig = require(customWebpackPath) as { getWebpackConfig: CustomWebpackConfigurationGetter }; | ||
return customConfig.getWebpackConfig(baseConfig, 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.
Having a meaningful error message in the console would be wise if the exported thing is not a function.
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.
Added error if export is not a function.
de30f59
to
0bafec8
Compare
Added a example in the README to be easier to use |
I think that this approach too is overly complicated. Why don't we let plugin authors create their own 100% custom config? For example, they may prefer JavaScript over TypeScript. Is there an issue with allowing them to import defaultConfig from '@grafana/toolkit/webpack-config'; // or similar
import {mergeDeep} from 'lodash';
export default mergeDeep({}, defaultConfig, {...}); |
100% we do... you just don't have to use toolkit and put in your own system (webpack, grunt, rollup, whatever). The trick with toolkit is allowing people to extend only a little bit. Here is a project with a complete webpack implementation: or grunt: |
export const loadWebpackConfig: WebpackConfigurationGetter = options => { | ||
const baseConfig = getBaseWebpackConfig(options); | ||
|
||
const customWebpackPath = path.resolve(process.cwd(), 'webpack.plugin.config.ts'); |
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 not naming this file webpack.config.ts?
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.
I wanted to differ it from the default naming of webpack.
If we use the default webpack.config.ts everybody expects a custom config as proposed by steven.
@stevenvachon goal with toolkit is to have a sane common config that addresses most of the use cases. I think that with this change we introduce a "safe" way of extending the basic config, without the need of creating a complete custom one. It does not differ much from what you are suggesting with merging the configs, but it has advantage of the config being typed(with |
@stevenvachon This approach would require the config to be more static. Currently the config depends on the given options for production and watching. |
@sebimarkgraf this seems close to mergeable. I would love to see a test verifying that it actually works. You can take some inspiration from the tests and mocks that are implemented in toolkit's Also - did you have a chance to test it with the actual custom config? |
@dprokop I tested it to add another plugin for my own config. I will create a test and update the PR then. |
22debd8
to
9cee96b
Compare
@dprokop Updated with test cases. |
export type CustomWebpackConfigurationGetter = ( | ||
originalConfig: webpack.Configuration, | ||
options: WebpackConfigurationOptions | ||
) => webpack.Configuration; | ||
|
||
export const findModuleFiles = (base: string, files?: string[], result?: string[]) => { | ||
files = files || fs.readdirSync(base); |
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.
Is it possible to use the async version of this function instead?
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 would make sense if we have any tasks after the webpack task.
This would require to have the complete loading async and therefore add a lot of noise to this PR.
If you want I can still change everything to async/await.
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's probably not critical for this PR, but async functions are a Node.js best practice.
9cee96b
to
f3b7b63
Compare
Added async/await as additional commit |
175eb3f
to
736deeb
Compare
packages/grafana-toolkit/README.md
Outdated
@@ -127,6 +127,27 @@ Currently we support following Jest configuration properties: | |||
- [`snapshotSerializers`](https://jest-bot.github.io/jest/docs/configuration.html#snapshotserializers-array-string) | |||
- [`moduleNameMapper`](https://jestjs.io/docs/en/configuration#modulenamemapper-object-string-string) | |||
|
|||
### Can I add custom Webpack rules or 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.
### Can I add custom Webpack rules or plugins? | |
### How can I customize Webpack rules or plugins? |
@sebimarkgraf I've had a chance to test your implementation today and it works great, thanks for this! Some final remarks:
|
Your argument of aligning all configurations with the original name is quite compelling. Since stevenvachon does not seem to take a definite side, I will change the naming. |
* Toolkit: Add possibility to add custom webpack config * Toolkit: Refactor webpack to utilize async-await * Toolkit: Rename config file and allow named export
* Toolkit: Add possibility to add custom webpack config * Toolkit: Refactor webpack to utilize async-await * Toolkit: Rename config file and allow named export
* Toolkit: Add possibility to add custom webpack config * Toolkit: Refactor webpack to utilize async-await * Toolkit: Rename config file and allow named export
What this PR does / why we need it:
Implement possible option for #20833 to allow user to override webpack configuration.
This is a WIP and is intended to show a possible implementation
Fixes #20833