Skip to content

Commit

Permalink
fix: support svgo.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Sep 11, 2021
1 parent ad47317 commit c2bf66b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 63 deletions.
43 changes: 0 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,11 @@
"space": 2
},
"dependencies": {
"js-yaml": "^4.1.0",
"lodash.merge": "^4.6.2",
"svgo": "^2.5.0",
"vscode-set-text": "^1.0.0"
},
"devDependencies": {
"@types/js-yaml": "^4.0.3",
"@types/lodash.merge": "^4.6.6",
"@types/svgo": "^2.4.1",
"@types/vscode": "^1.60.0",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can enable/disable [plugins](https://github.com/svg/svgo/blob/master/docs/ho

### Project config

[To configure with `.svgo.yml`](https://github.com/svg/svgo/blob/master/docs/how-it-works/en.md#1-config), just put the config file in project root.
[To configure with `svgo.config.js`](https://github.com/svg/svgo#configuration), just put the config file in project root.

## License

Expand Down
26 changes: 9 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {ExtensionContext, TextDocument, commands, window, workspace} from 'vscode';
import {load} from 'js-yaml';
import setText from 'vscode-set-text';
import merge from 'lodash.merge';
import {OptimizeOptions, DefaultPlugins, Plugin, optimize} from 'svgo';
import {OptimizeOptions, DefaultPlugins, Plugin, loadConfig, optimize} from 'svgo';

const defaultPlugins: Array<DefaultPlugins['name']> = [
'removeDoctype',
Expand Down Expand Up @@ -58,10 +57,6 @@ function isSVG({languageId, fileName}: TextDocument): boolean {
return languageId === 'xml' && fileName.endsWith('.svg');
}

function isYAML({languageId}: TextDocument): boolean {
return languageId === 'yaml';
}

function getPluginConfig(): OptimizeOptions {
const svgoConfig = workspace.getConfiguration('svgo');

Expand All @@ -87,19 +82,16 @@ function getPluginConfig(): OptimizeOptions {
return pluginConfig;
}

function getProjectConfig(): OptimizeOptions {
const yaml = workspace.textDocuments.find(textDocument => isYAML(textDocument) && textDocument.fileName === '.svgo.yml');

if (yaml) {
return load(yaml.getText()) as OptimizeOptions;
}
async function getProjectConfig(): Promise<OptimizeOptions> {
const configFile = workspace.textDocuments.find(({fileName}) => fileName === 'svgo.config.js');
const projectConfig = await loadConfig(configFile?.fileName) ?? {};

return {};
return projectConfig;
}

function getConfig(config: OptimizeOptions): OptimizeOptions {
async function getConfig(config: OptimizeOptions): Promise<OptimizeOptions> {
const pluginConfig = getPluginConfig();
const projectConfig = getProjectConfig();
const projectConfig = await getProjectConfig();

return merge(pluginConfig, projectConfig, config);
}
Expand All @@ -109,7 +101,7 @@ const minifyTextDocument = async (textDocument: TextDocument) => {
return;
}

const config = getConfig({
const config = await getConfig({
js2svg: {
pretty: false,
},
Expand All @@ -124,7 +116,7 @@ const prettifyTextDocument = async (textDocument: TextDocument) => {
return;
}

const config = getConfig({
const config = await getConfig({
js2svg: {
pretty: true,
},
Expand Down

0 comments on commit c2bf66b

Please sign in to comment.