Skip to content

Commit

Permalink
fix(v2): don't remove viewBox from svg
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanDor authored Dec 14, 2020
1 parent 4d7ebcf commit 3570aa0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
33 changes: 32 additions & 1 deletion packages/docusaurus/src/webpack/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

import path from 'path';

import {excludeJS, clientDir, getDocusaurusAliases} from '../base';
import {
excludeJS,
clientDir,
getDocusaurusAliases,
createBaseConfig,
} from '../base';
import * as utils from '../utils';
import {mapValues} from 'lodash';

describe('babel transpilation exclude logic', () => {
Expand Down Expand Up @@ -69,3 +75,28 @@ describe('getDocusaurusAliases()', () => {
expect(relativeDocusaurusAliases).toMatchSnapshot();
});
});

describe('base webpack config', () => {
const props = {
outDir: '',
siteDir: '',
baseUrl: '',
generatedFilesDir: '',
routesPaths: '',
};

afterEach(() => {
jest.restoreAllMocks();
});

test('should use svg rule', () => {
const fileLoaderUtils = utils.getFileLoaderUtils();
const mockSvg = jest.spyOn(fileLoaderUtils.rules, 'svg');
jest
.spyOn(utils, 'getFileLoaderUtils')
.mockImplementation(() => fileLoaderUtils);

createBaseConfig(props, false, false);
expect(mockSvg).toBeCalled();
});
});
18 changes: 17 additions & 1 deletion packages/docusaurus/src/webpack/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'webpack';
import path from 'path';

import {applyConfigureWebpack} from '../utils';
import {applyConfigureWebpack, getFileLoaderUtils} from '../utils';
import {
ConfigureWebpackFn,
ConfigureWebpackFnMergeStrategy,
Expand Down Expand Up @@ -132,3 +132,19 @@ describe('extending generated webpack config', () => {
});
});
});

describe('getFileLoaderUtils()', () => {
test('plugin svgo/removeViewBox should be disabled', () => {
const use = getFileLoaderUtils().rules.svg().use;
expect(use).toContainEqual(
expect.objectContaining({
loader: '@svgr/webpack',
options: expect.objectContaining({
svgoConfig: {
plugins: [{removeViewBox: false}],
},
}),
}),
);
});
});
5 changes: 1 addition & 4 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export function createBaseConfig(
rules: [
fileLoaderUtils.rules.images(),
fileLoaderUtils.rules.media(),
fileLoaderUtils.rules.svg(),
fileLoaderUtils.rules.otherAssets(),
{
test: /\.(j|t)sx?$/,
Expand Down Expand Up @@ -183,10 +184,6 @@ export function createBaseConfig(
onlyLocals: isServer,
}),
},
{
test: /\.svg$/,
use: '@svgr/webpack?-prettier,+svgo,+titleProp,+ref![path]',
},
],
},
plugins: [
Expand Down
20 changes: 20 additions & 0 deletions packages/docusaurus/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ export function getFileLoaderUtils(): Record<string, any> {
};
},

svg: (): RuleSetRule => {
return {
use: [
{
loader: '@svgr/webpack',
options: {
prettier: false,
svgo: true,
svgoConfig: {
plugins: [{removeViewBox: false}],
},
titleProp: true,
ref: ![path],
},
},
],
test: /\.svg$/,
};
},

otherAssets: (): RuleSetRule => {
return {
use: [loaders.file({folder: 'files'})],
Expand Down

0 comments on commit 3570aa0

Please # to comment.