Skip to content

Commit

Permalink
Merge pull request #50 from grafana/mckn/support-nested-plugins
Browse files Browse the repository at this point in the history
Build: Support for building nested plugins
  • Loading branch information
mckn authored Sep 12, 2022
2 parents 4888301 + 6fb3f8c commit 52f0946
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"@grafana/eslint-config": "^2.5.0",
"@swc/jest": "^0.2.20",
"@types/glob": "^7.0.0",
"@types/jest": "^27.4.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
Expand Down
19 changes: 12 additions & 7 deletions templates/common/.config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,23 @@ We are going to use [`webpack-merge`](https://github.com/survivejs/webpack-merge

```typescript
// webpack.config.ts
import type { Configuration } from 'webpack';
import { merge } from 'webpack-merge';
import grafanaConfig from './.config/webpack/webpack.config';

const config = (env) =>
merge(grafanaConfig(env), {
// Add custom config here...
output: {
asyncChunks: true,
},
});
const config = async (env): Promise<Configuration> => {
const baseConfig = await grafanaConfig(env);

return merge(baseConfig, {
// Add custom config here...
output: {
asyncChunks: true,
},
});
};

export default config;

```

#### 3. Update the `package.json` to use the new Webpack config
Expand Down
1 change: 0 additions & 1 deletion templates/common/.config/webpack/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const SOURCE_DIR = 'src';
export const DIST_DIR = 'dist';
export const ENTRY_FILE = `${SOURCE_DIR}/module.ts`;
24 changes: 24 additions & 0 deletions templates/common/.config/webpack/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fs from 'fs';
import path from 'path';
import util from 'util';
import glob from 'glob';
import { SOURCE_DIR } from './constants';

const globAsync = util.promisify(glob);

export function getPackageJson() {
return require(path.resolve(process.cwd(), 'package.json'));
}
Expand All @@ -15,3 +19,23 @@ export function getPluginId() {
export function hasReadme() {
return fs.existsSync(path.resolve(process.cwd(), SOURCE_DIR, 'README.md'));
}

export async function getEntries(): Promise<Record<string, string>> {
const parent = '..';
const pluginsJson = await globAsync('**/src/**/plugin.json');

const plugins = await Promise.all(pluginsJson.map(pluginJson => {
const folder = path.dirname(pluginJson);
return globAsync(`${folder}/module.{ts,tsx,js}`);
}));

return plugins.reduce((result, modules) => {
return modules.reduce((result, module) => {
const pluginPath = path.resolve(path.dirname(module), parent);
const pluginName = path.basename(pluginPath);

result[`${pluginName}/module`] = path.join(parent, module);
return result;
}, result);
}, {});
}
10 changes: 4 additions & 6 deletions templates/common/.config/webpack/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import path from 'path';
import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin';
import { Configuration } from 'webpack';

import { getPackageJson, getPluginId, hasReadme } from './utils';
import { SOURCE_DIR, DIST_DIR, ENTRY_FILE } from './constants';
import { getPackageJson, getPluginId, hasReadme, getEntries } from './utils';
import { SOURCE_DIR, DIST_DIR } from './constants';

const config = (env): Configuration => ({
const config = async (env): Promise<Configuration> => ({
cache: {
type: 'filesystem',
buildDependencies: {
Expand All @@ -27,9 +27,7 @@ const config = (env): Configuration => ({

devtool: env.production ? 'source-map' : 'eval-source-map',

entry: {
module: path.resolve(process.cwd(), ENTRY_FILE),
},
entry: await getEntries(),

externals: [
'lodash',
Expand Down
2 changes: 2 additions & 0 deletions templates/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@swc/jest": "^0.2.20",
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.3",
"@types/glob": "^8.0.0",
"@types/jest": "^27.4.1",{{#if_eq pluginType "app"}}
"@types/react-router-dom": "^5.3.3",{{/if_eq}}
"@types/node": "^17.0.19",
Expand All @@ -40,6 +41,7 @@
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-webpack-plugin": "^3.1.1",
"glob": "^8.0.3",
"jest": "27.5.0",
"fork-ts-checker-webpack-plugin": "^7.2.0",
"prettier": "^2.5.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@
resolved "https://registry.yarnpkg.com/@types/fined/-/fined-1.1.3.tgz#83f03e8f0a8d3673dfcafb18fce3571f6250e1bc"
integrity sha512-CWYnSRnun3CGbt6taXeVo2lCbuaj4mchVJ4UF/BdU5TSuIn3AmS13pGMwCsBUoehGbhZrBrpNJZSZI5EVilXww==

"@types/glob@^7.1.1":
"@types/glob@^7.0.0", "@types/glob@^7.1.1":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
integrity sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==
Expand Down

0 comments on commit 52f0946

Please sign in to comment.