Skip to content

Fix: create-plugin common Webpack config types #1680

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

Merged
merged 13 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/create-plugin/templates/common/.config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ We are going to use [`webpack-merge`](https://github.com/survivejs/webpack-merge
// webpack.config.ts
import type { Configuration } from 'webpack';
import { merge } from 'webpack-merge';
import grafanaConfig from './.config/webpack/webpack.config';
import grafanaConfig, { type Env } from './.config/webpack/webpack.config';

const config = async (env): Promise<Configuration> => {
const config = async (env: Env): Promise<Configuration> => {
const baseConfig = await grafanaConfig(env);

return merge(baseConfig, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function hasReadme() {

// Support bundling nested plugins by finding all plugin.json files in src directory
// then checking for a sibling module.[jt]sx? file.
export async function getEntries(): Promise<Record<string, string>> {
export async function getEntries() {
const pluginsJson = await glob('**/src/**/plugin.json', { absolute: true });

const plugins = await Promise.all(
Expand All @@ -51,13 +51,13 @@ export async function getEntries(): Promise<Record<string, string>> {
);

return plugins.reduce((result, modules) => {
return modules.reduce((result, module) => {
return modules.reduce((innerResult, module) => {
const pluginPath = path.dirname(module);
const pluginName = path.relative(process.cwd(), pluginPath).replace(/src\/?/i, '');
const entryName = pluginName === '' ? 'module' : `${pluginName}/module`;

result[entryName] = module;
return result;
innerResult[entryName] = module;
return innerResult;
}, result);
}, {});
}, {} as Record<string, string>);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CopyWebpackPlugin from 'copy-webpack-plugin';
import ESLintPlugin from 'eslint-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import path from 'path';
// @ts-expect-error Plugin is not typed
import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import { SubresourceIntegrityPlugin } from "webpack-subresource-integrity";
Expand All @@ -34,7 +35,11 @@ __webpack_public_path__ =
`,
});

const config = async (env): Promise<Configuration> => {
export type Env = {
[key: string]: true | string | Env;
};

const config = async (env: Env): Promise<Configuration> => {
const baseConfig: Configuration = {
cache: {
type: 'filesystem',
Expand Down Expand Up @@ -80,10 +85,10 @@ const config = async (env): Promise<Configuration> => {
// Mark legacy SDK imports as external if their name starts with the "grafana/" prefix
({ request }, callback) => {
const prefix = 'grafana/';
const hasPrefix = (request) => request.indexOf(prefix) === 0;
const stripPrefix = (request) => request.substr(prefix.length);
const hasPrefix = (request: string) => request.indexOf(prefix) === 0;
const stripPrefix = (request: string) => request.substr(prefix.length);

if (hasPrefix(request)) {
if (request && hasPrefix(request)) {
return callback(undefined, stripPrefix(request));
}

Expand Down
1 change: 1 addition & 0 deletions packages/create-plugin/templates/common/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/node": "^20.8.7",{{#if isAppType}}{{#unless useReactRouterV6}}
"@types/react-router-dom": "^{{ reactRouterVersion }}",{{/unless}}{{/if}}
"@types/testing-library__jest-dom": "5.14.8",
"@types/webpack-livereload-plugin": "^2.3.6",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
"copy-webpack-plugin": "^11.0.0",
Expand Down
Loading