From 8f4d924531adfdde40e5804ad572219fac118cda Mon Sep 17 00:00:00 2001 From: Esteban Beltran Date: Tue, 26 Jul 2022 10:36:18 +0200 Subject: [PATCH] Grafana/toolkit: Fix incorrect image and font generation for plugin builds (#52661) --- .../src/config/webpack/loaders.test.ts | 25 +------ .../src/config/webpack/loaders.ts | 66 +++---------------- 2 files changed, 10 insertions(+), 81 deletions(-) diff --git a/packages/grafana-toolkit/src/config/webpack/loaders.test.ts b/packages/grafana-toolkit/src/config/webpack/loaders.test.ts index 0c5e3f02804..4d63f47e1a2 100644 --- a/packages/grafana-toolkit/src/config/webpack/loaders.test.ts +++ b/packages/grafana-toolkit/src/config/webpack/loaders.test.ts @@ -1,4 +1,4 @@ -import { getStylesheetEntries, hasThemeStylesheets } from './loaders'; +import { getStylesheetEntries } from './loaders'; describe('Loaders', () => { describe('stylesheet helpers', () => { @@ -22,28 +22,5 @@ describe('Loaders', () => { expect(result).toThrow(); }); }); - - describe('hasThemeStylesheets', () => { - it('throws when only one theme file is defined', () => { - const errorSpy = jest.spyOn(console, 'error').mockImplementation(); - const result = () => { - hasThemeStylesheets(`${__dirname}/../mocks/stylesheetsSupport/missing-theme-file`); - }; - expect(result).toThrow(); - errorSpy.mockRestore(); - }); - - it('returns false when no theme files present', () => { - const result = hasThemeStylesheets(`${__dirname}/../mocks/stylesheetsSupport/no-theme-files`); - - expect(result).toBeFalsy(); - }); - - it('returns true when theme files present', () => { - const result = hasThemeStylesheets(`${__dirname}/../mocks/stylesheetsSupport/ok`); - - expect(result).toBeTruthy(); - }); - }); }); }); diff --git a/packages/grafana-toolkit/src/config/webpack/loaders.ts b/packages/grafana-toolkit/src/config/webpack/loaders.ts index 3bad34d293f..e558dba405e 100644 --- a/packages/grafana-toolkit/src/config/webpack/loaders.ts +++ b/packages/grafana-toolkit/src/config/webpack/loaders.ts @@ -1,8 +1,6 @@ import fs from 'fs'; import path from 'path'; -import { getPluginId } from '../utils/getPluginId'; - const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const supportedExtensions = ['css', 'scss', 'less', 'sass']; @@ -33,39 +31,6 @@ export const getStylesheetEntries = (root: string = process.cwd()) => { return entries; }; -export const hasThemeStylesheets = (root: string = process.cwd()) => { - const stylesheetsPaths = getStylesheetPaths(root); - const stylesheetsSummary: boolean[] = []; - - const result = stylesheetsPaths.reduce((acc, current) => { - if (fs.existsSync(`${current}.css`) || fs.existsSync(`${current}.scss`)) { - stylesheetsSummary.push(true); - return acc && true; - } else { - stylesheetsSummary.push(false); - return false; - } - }, true); - - const hasMissingStylesheets = stylesheetsSummary.filter((s) => s).length === 1; - - // seems like there is one theme file defined only - if (result === false && hasMissingStylesheets) { - console.error('\nWe think you want to specify theme stylesheet, but it seems like there is something missing...'); - stylesheetsSummary.forEach((s, i) => { - if (s) { - console.log(stylesheetsPaths[i], 'discovered'); - } else { - console.log(stylesheetsPaths[i], 'missing'); - } - }); - - throw new Error('Stylesheet missing!'); - } - - return result; -}; - export const getStyleLoaders = () => { const extractionLoader = { loader: MiniCssExtractPlugin.loader, @@ -139,34 +104,21 @@ export const getStyleLoaders = () => { }; export const getFileLoaders = () => { - const shouldExtractCss = hasThemeStylesheets(); - return [ { test: /\.(png|jpe?g|gif|svg)$/, - use: [ - shouldExtractCss - ? { - loader: require.resolve('file-loader'), - options: { - outputPath: '/', - name: '[path][name].[ext]', - }, - } - : // When using single css import images are inlined as base64 URIs in the result bundle - { - loader: 'url-loader', - }, - ], + type: 'asset/resource', + generator: { + publicPath: `img/`, + outputPath: 'img/', + }, }, { test: /\.(woff|woff2|eot|ttf|otf)(\?v=\d+\.\d+\.\d+)?$/, - loader: require.resolve('file-loader'), - options: { - // Keep publicPath relative for host.com/grafana/ deployments - publicPath: `public/plugins/${getPluginId()}/fonts`, - outputPath: 'fonts', - name: '[name].[ext]', + type: 'asset/resource', + generator: { + publicPath: `fonts/`, + outputPath: 'fonts/', }, }, ];